-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMainWindow.IedSmvQuickStart.cs
More file actions
214 lines (188 loc) · 7.58 KB
/
Copy pathMainWindow.IedSmvQuickStart.cs
File metadata and controls
214 lines (188 loc) · 7.58 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
// Copyright 2026 Ari Sulistiono
// SPDX-License-Identifier: Apache-2.0
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using ArIED61850Tester.Models;
using ArIED61850Tester.Services;
namespace ArIED61850Tester;
public partial class MainWindow
{
private const string IedSmvQuickStartTag = "ARSAS_IED_SMV_QUICK_START";
private static readonly bool IedSmvQuickStartRegistered = RegisterIedSmvQuickStart();
private SmvViewerWindow? _activeSmvViewerWindow;
private static bool RegisterIedSmvQuickStart()
{
EventManager.RegisterClassHandler(
typeof(UniformGrid),
FrameworkElement.LoadedEvent,
new RoutedEventHandler(IedSmvActionGrid_Loaded),
handledEventsToo: true);
return true;
}
private static void IedSmvActionGrid_Loaded(object sender, RoutedEventArgs e)
{
if (sender is not UniformGrid grid ||
grid.DataContext is not Iec61850MonitorDevice device ||
Window.GetWindow(grid) is not MainWindow window)
{
return;
}
window.InstallIedSmvQuickStart(grid, device);
}
private void InstallIedSmvQuickStart(UniformGrid actionGrid, Iec61850MonitorDevice device)
{
if (actionGrid.Children
.OfType<Button>()
.Any(button => Equals(button.Tag, IedSmvQuickStartTag)))
{
return;
}
var deviceButtons = actionGrid.Children
.OfType<Button>()
.Count(button => ReferenceEquals(button.Tag, device));
if (deviceButtons < 4)
return;
actionGrid.Columns = Math.Max(7, actionGrid.Columns);
var icon = new System.Windows.Shapes.Path
{
Data = Geometry.Parse("M3,12 H6 L8,5 L11,19 L14,8 L16,12 H21"),
Style = TryFindResource("LucideIcon") as Style,
Stroke = new SolidColorBrush(Color.FromRgb(79, 70, 229)),
StrokeThickness = 2.1
};
var button = new Button
{
Tag = IedSmvQuickStartTag,
CommandParameter = device,
Style = TryFindResource("IedIconButton") as Style,
Width = 27,
Height = 27,
Margin = new Thickness(0),
Cursor = System.Windows.Input.Cursors.Hand,
ToolTip = "Open Sampled Values and capture the first discovered SMV stream on the Ethernet adapter routed to this IED",
Content = new Viewbox
{
Width = 14,
Height = 14,
Child = icon
}
};
button.Click += IedSmvQuickStart_Click;
actionGrid.Children.Add(button);
}
private void IedSmvQuickStart_Click(object sender, RoutedEventArgs e)
{
if (sender is not Button { CommandParameter: Iec61850MonitorDevice device })
return;
SelectedDevice = device;
if (_activeSmvViewerWindow is { IsLoaded: true } existing)
{
if (existing.DeviceId.Equals(device.DeviceId, StringComparison.OrdinalIgnoreCase))
{
if (existing.WindowState == WindowState.Minimized)
existing.WindowState = WindowState.Normal;
existing.Activate();
existing.Focus();
SetStatus($"SMV workspace active for {device.Name}; the loaded snapshot and waveform were preserved.");
return;
}
existing.Close();
}
IReadOnlyList<GooseAdapterOption> adapters;
try
{
adapters = new SmvSnapshotCaptureService().ListAdapters();
}
catch (Exception ex)
{
adapters = Array.Empty<GooseAdapterOption>();
AddLog("ERROR", "SMV", $"Npcap adapter discovery failed for {device.Name}: {ex.Message}");
MarkDiagnosticAlert();
}
var adapter = ResolveSmvAdapterForIed(device, adapters, out var routeDetail);
var window = new SmvViewerWindow(device)
{
Owner = this
};
window.EnableFastWorkflow(adapter, routeDetail);
window.Closed += ActiveSmvViewerWindow_Closed;
_activeSmvViewerWindow = window;
window.Show();
window.Activate();
if (adapter is null)
{
SetStatus($"SMV workspace opened for {device.Name}; select the process/station-bus adapter to capture.");
AddLog(
"WARN",
"SMV",
$"One-click SMV opened for {device.Name} ({device.IpAddress}), but automatic adapter selection was not unique. {routeDetail}");
return;
}
SetStatus($"SMV snapshot starting for {device.Name} on its routed Ethernet adapter.");
AddLog(
"INFO",
"SMV",
$"One-click SMV workspace opened for {device.Name} ({device.IpAddress}) on adapter {adapter.Index}: {adapter.Description}. Route selection: {routeDetail}");
}
private void ActiveSmvViewerWindow_Closed(object? sender, EventArgs e)
{
if (sender is SmvViewerWindow window)
window.Closed -= ActiveSmvViewerWindow_Closed;
if (ReferenceEquals(_activeSmvViewerWindow, sender))
_activeSmvViewerWindow = null;
}
private static GooseAdapterOption? ResolveSmvAdapterForIed(
Iec61850MonitorDevice device,
IReadOnlyCollection<GooseAdapterOption> adapters,
out string routeDetail)
{
routeDetail = "No route was resolved.";
if (!IPAddress.TryParse(device.IpAddress, out var target) ||
target.AddressFamily != AddressFamily.InterNetwork)
{
routeDetail = $"IED address '{device.IpAddress}' is not a valid IPv4 endpoint.";
return null;
}
var localAddress = ResolveLocalIpv4ForTarget(target);
if (localAddress is not null)
{
var networkInterface = NetworkInterface.GetAllNetworkInterfaces()
.FirstOrDefault(candidate => candidate.GetIPProperties().UnicastAddresses.Any(unicast =>
unicast.Address.AddressFamily == AddressFamily.InterNetwork &&
unicast.Address.Equals(localAddress)));
if (networkInterface is not null)
{
var matches = adapters
.Where(adapter => CaptureAdapterMatchesNetworkInterface(adapter, networkInterface))
.ToList();
if (matches.Count == 1)
{
routeDetail =
$"Windows route {localAddress} → {target} via {networkInterface.Name} ({networkInterface.Description})";
return matches[0];
}
routeDetail = matches.Count == 0
? $"Windows selected {networkInterface.Name} ({localAddress}), but no Npcap adapter matched its ID/MAC."
: $"Windows selected {networkInterface.Name} ({localAddress}), but {matches.Count} Npcap adapters matched.";
}
else
{
routeDetail = $"Windows selected local address {localAddress}, but its network interface was not found.";
}
}
var usable = adapters
.Where(adapter => !LooksLikeLoopback(adapter))
.ToList();
if (usable.Count == 1)
{
routeDetail += $" Falling back to the only non-loopback capture adapter: {usable[0].DisplayText}.";
return usable[0];
}
return null;
}
}