-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFaultRecordUxBehavior.cs
More file actions
355 lines (318 loc) · 14 KB
/
Copy pathFaultRecordUxBehavior.cs
File metadata and controls
355 lines (318 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
using System.ComponentModel;
using System.Net;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using ArIED61850Tester.Models;
namespace ArIED61850Tester;
/// <summary>
/// Adds compact protocol-capability actions to each IED card. Capability actions are
/// intentionally scoped to the card's IED instead of being presented as global tools.
/// </summary>
internal static class FaultRecordUxBehavior
{
private const string CapabilityPanelMarker = "ARSAS_IED_CAPABILITIES";
private const string PrimaryRcbToolTipMarker = "RCB Export Filter";
private static readonly ConditionalWeakTable<ListBoxItem, CardRegistration> Registrations = new();
private static int _installed;
public static void Install()
{
if (Interlocked.Exchange(ref _installed, 1) != 0)
return;
EventManager.RegisterClassHandler(
typeof(ListBoxItem),
FrameworkElement.LoadedEvent,
new RoutedEventHandler(OnListBoxItemLoaded),
handledEventsToo: true);
}
private static void OnListBoxItemLoaded(object sender, RoutedEventArgs e)
{
if (sender is not ListBoxItem item || item.DataContext is not Iec61850MonitorDevice)
return;
if (Window.GetWindow(item) is not MainWindow)
return;
if (Registrations.TryGetValue(item, out _))
return;
item.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() =>
{
if (item.DataContext is not Iec61850MonitorDevice currentDevice ||
Window.GetWindow(item) is not MainWindow currentWindow ||
Registrations.TryGetValue(item, out _))
{
return;
}
var actionPanel = FindDescendants<System.Windows.Controls.Primitives.UniformGrid>(item)
.FirstOrDefault(panel => panel.Children
.OfType<Button>()
.Any(button => (button.ToolTip?.ToString() ?? string.Empty)
.Contains(PrimaryRcbToolTipMarker, StringComparison.OrdinalIgnoreCase)));
// The UniformGrid lives inside the lifecycle-action Grid. The IED card content is
// one level higher. Adding the capability row to the nested action Grid causes both
// rows to overlap and hides Play / Stop / Edit / Save.
if (actionPanel?.Parent is not Grid primaryActionGrid ||
primaryActionGrid.Parent is not Grid cardGrid)
{
return;
}
var legacyRcbButton = actionPanel.Children
.OfType<Button>()
.FirstOrDefault(button => (button.ToolTip?.ToString() ?? string.Empty)
.Contains(PrimaryRcbToolTipMarker, StringComparison.OrdinalIgnoreCase));
if (legacyRcbButton != null)
actionPanel.Children.Remove(legacyRcbButton);
actionPanel.Columns = 4;
primaryActionGrid.RowDefinitions.Clear();
cardGrid.MinHeight = Math.Max(cardGrid.MinHeight, 108);
var existing = cardGrid.Children
.OfType<Grid>()
.FirstOrDefault(panel => Equals(panel.Tag, CapabilityPanelMarker));
if (existing != null)
cardGrid.Children.Remove(existing);
var gooseButton = CreateCapabilityButton(
"GOOSE",
(_, _) => OpenGooseWorkspace(currentWindow, currentDevice));
var smvButton = CreateCapabilityButton(
"SMV",
(_, _) => OpenSmvViewer(currentWindow, currentDevice));
var fileButton = CreateCapabilityButton(
"FILE",
(_, _) => OpenFaultRecordWindow(currentWindow, currentDevice));
var rcbButton = CreateCapabilityButton(
"RCB",
(_, _) => currentWindow.OpenRcbExportFilter(currentDevice));
var capabilityPanel = new Grid
{
Tag = CapabilityPanelMarker,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(0, 7, 0, 1)
};
for (var column = 0; column < 4; column++)
capabilityPanel.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
AddCapabilityButton(capabilityPanel, gooseButton, 0);
AddCapabilityButton(capabilityPanel, smvButton, 1);
AddCapabilityButton(capabilityPanel, fileButton, 2);
AddCapabilityButton(capabilityPanel, rcbButton, 3);
cardGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
Grid.SetRow(capabilityPanel, cardGrid.RowDefinitions.Count - 1);
Grid.SetColumn(capabilityPanel, 0);
Grid.SetColumnSpan(capabilityPanel, 2);
Panel.SetZIndex(capabilityPanel, 10);
cardGrid.Children.Add(capabilityPanel);
var registration = new CardRegistration(
currentDevice,
gooseButton,
smvButton,
fileButton,
rcbButton,
(_, _) => QueueCapabilityRefresh(item, currentDevice, gooseButton, smvButton, fileButton, rcbButton));
Registrations.Add(item, registration);
currentDevice.PropertyChanged += registration.PropertyChangedHandler;
RefreshCapabilityState(currentDevice, gooseButton, smvButton, fileButton, rcbButton);
}));
}
private static void QueueCapabilityRefresh(
ListBoxItem item,
Iec61850MonitorDevice device,
Button gooseButton,
Button smvButton,
Button fileButton,
Button rcbButton)
{
void RefreshOnUiThread()
{
if (!item.IsLoaded || !ReferenceEquals(item.DataContext, device))
return;
RefreshCapabilityState(device, gooseButton, smvButton, fileButton, rcbButton);
}
if (item.Dispatcher.CheckAccess())
{
RefreshOnUiThread();
return;
}
item.Dispatcher.BeginInvoke(DispatcherPriority.DataBind, new Action(RefreshOnUiThread));
}
private static Button CreateCapabilityButton(string capability, RoutedEventHandler click)
{
var button = new Button
{
Width = double.NaN,
Height = 26,
MinHeight = 0,
MinWidth = 44,
Padding = new Thickness(5, 0, 5, 0),
Margin = new Thickness(2, 0, 2, 0),
Focusable = false,
HorizontalAlignment = HorizontalAlignment.Stretch,
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
FontSize = 9.6,
FontWeight = FontWeights.SemiBold,
Content = capability,
Tag = capability
};
// IedIconButton uses a restrained rounded-rectangle template (CornerRadius 9).
// Stretching it across a star-sized column produces a readable pill instead of the
// tiny ellipse caused by the large SoftButton template and narrow content width.
if (Application.Current.TryFindResource("IedIconButton") is Style style)
button.Style = style;
button.Click += click;
return button;
}
private static void AddCapabilityButton(Grid panel, Button button, int column)
{
Grid.SetColumn(button, column);
panel.Children.Add(button);
}
private static void RefreshCapabilityState(
Iec61850MonitorDevice device,
Button gooseButton,
Button smvButton,
Button fileButton,
Button rcbButton)
{
var hasGoose = device.SclWorkspace?.GooseStreams.Count > 0 ||
device.LiveDiscoveryModel?.GooseControlBlocks.Count > 0;
var hasSmv = device.SclWorkspace?.SampledValuesStreams.Count > 0 ||
device.LiveDiscoveryModel?.SampledValueControlBlocks.Count > 0;
var fileServiceVerified = device.LiveDiscoveryModel?.FileDirectory.IsSuccess == true;
var endpointReady = !string.IsNullOrWhiteSpace(device.IpAddress) &&
IPAddress.TryParse(device.IpAddress, out _) &&
device.Port is > 0 and <= 65535;
var hasRcbInventory = device.LiveDiscoveryModel?.ReportControls.Count > 0 ||
!string.IsNullOrWhiteSpace(device.SclSourcePath);
ApplyState(
gooseButton,
hasGoose ? CapabilityState.Available : CapabilityState.Unavailable,
hasGoose
? $"Open GOOSE Subscriber for {device.Name}"
: $"{device.Name} has no configured or discovered GOOSE control block");
ApplyState(
smvButton,
hasSmv ? CapabilityState.Available : CapabilityState.Unavailable,
hasSmv
? $"Open SMV Viewer for {device.Name}"
: $"{device.Name} has no configured or discovered Sampled Value control block");
ApplyState(
fileButton,
fileServiceVerified
? CapabilityState.Available
: endpointReady ? CapabilityState.ProbeReady : CapabilityState.Unavailable,
fileServiceVerified
? $"Browse and download fault records from {device.Name}"
: endpointReady
? $"Probe the MMS file service and browse fault records from {device.Name}"
: $"Bind a valid MMS endpoint before using File Transfer for {device.Name}");
ApplyState(
rcbButton,
hasRcbInventory ? CapabilityState.Available : CapabilityState.Unavailable,
hasRcbInventory
? $"Select and export one RCB for legacy SAS import from {device.Name}"
: $"Open SCL or complete live discovery before using RCB Export for {device.Name}");
}
private static void ApplyState(Button button, CapabilityState state, string toolTip)
{
button.IsEnabled = state != CapabilityState.Unavailable;
button.Opacity = state switch
{
CapabilityState.Available => 1.0,
CapabilityState.ProbeReady => 0.78,
_ => 0.42
};
button.ToolTip = toolTip;
button.Background = state switch
{
CapabilityState.Available => new SolidColorBrush(Color.FromRgb(234, 241, 255)),
CapabilityState.ProbeReady => new SolidColorBrush(Color.FromRgb(255, 247, 230)),
_ => new SolidColorBrush(Color.FromRgb(243, 246, 250))
};
button.BorderBrush = state switch
{
CapabilityState.Available => new SolidColorBrush(Color.FromRgb(177, 198, 237)),
CapabilityState.ProbeReady => new SolidColorBrush(Color.FromRgb(232, 194, 121)),
_ => new SolidColorBrush(Color.FromRgb(215, 223, 234))
};
button.Foreground = state == CapabilityState.ProbeReady
? new SolidColorBrush(Color.FromRgb(138, 91, 10))
: new SolidColorBrush(Color.FromRgb(35, 67, 119));
}
private static void OpenGooseWorkspace(MainWindow mainWindow, Iec61850MonitorDevice device)
{
mainWindow.SelectedDevice = device;
if (mainWindow.FindName("MainTabs") is TabControl tabs)
tabs.SelectedIndex = 3;
mainWindow.Activate();
}
private static void OpenSmvViewer(MainWindow mainWindow, Iec61850MonitorDevice device)
{
mainWindow.SelectedDevice = device;
var existing = Application.Current.Windows
.OfType<SmvViewerWindow>()
.FirstOrDefault(window => window.DeviceId.Equals(device.DeviceId, StringComparison.OrdinalIgnoreCase));
if (existing != null)
{
if (existing.WindowState == WindowState.Minimized)
existing.WindowState = WindowState.Normal;
existing.Activate();
return;
}
new SmvViewerWindow(device) { Owner = mainWindow }.Show();
}
private static void OpenFaultRecordWindow(MainWindow mainWindow, Iec61850MonitorDevice device)
{
mainWindow.SelectedDevice = device;
if (string.IsNullOrWhiteSpace(device.IpAddress) || !IPAddress.TryParse(device.IpAddress, out _))
{
MessageBox.Show(
mainWindow,
"Bind a valid MMS endpoint to this IED before using File Transfer.",
"File Transfer",
MessageBoxButton.OK,
MessageBoxImage.Information);
return;
}
var endpoint = $"{device.IpAddress}:{device.Port}";
var existing = Application.Current.Windows
.OfType<FaultRecordWindow>()
.FirstOrDefault(window => window.EndpointText.Equals(endpoint, StringComparison.OrdinalIgnoreCase));
if (existing != null)
{
if (existing.WindowState == WindowState.Minimized)
existing.WindowState = WindowState.Normal;
existing.Activate();
return;
}
new FaultRecordWindow(device.Name, device.IpAddress, device.Port)
{
Owner = mainWindow
}.Show();
}
private static IEnumerable<T> FindDescendants<T>(DependencyObject root)
where T : DependencyObject
{
var count = VisualTreeHelper.GetChildrenCount(root);
for (var index = 0; index < count; index++)
{
var child = VisualTreeHelper.GetChild(root, index);
if (child is T match)
yield return match;
foreach (var nested in FindDescendants<T>(child))
yield return nested;
}
}
private enum CapabilityState
{
Unavailable,
ProbeReady,
Available
}
private sealed record CardRegistration(
Iec61850MonitorDevice Device,
Button GooseButton,
Button SmvButton,
Button FileButton,
Button RcbButton,
PropertyChangedEventHandler PropertyChangedHandler);
}