-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMainWindow.IedGooseQuickStart.cs
More file actions
272 lines (239 loc) · 10.1 KB
/
Copy pathMainWindow.IedGooseQuickStart.cs
File metadata and controls
272 lines (239 loc) · 10.1 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
// Copyright 2026 Ari Sulistiono
// SPDX-License-Identifier: Apache-2.0
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Threading;
using ArIED61850Tester.Models;
namespace ArIED61850Tester;
public partial class MainWindow
{
private const string IedGooseQuickStartTag = "ARSAS_IED_GOOSE_QUICK_START";
private static readonly bool IedGooseQuickStartRegistered = RegisterIedGooseQuickStart();
private static bool RegisterIedGooseQuickStart()
{
EventManager.RegisterClassHandler(
typeof(UniformGrid),
FrameworkElement.LoadedEvent,
new RoutedEventHandler(IedActionGrid_Loaded),
handledEventsToo: true);
return true;
}
private static void IedActionGrid_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.InstallIedGooseQuickStart(grid, device);
}
private void InstallIedGooseQuickStart(UniformGrid actionGrid, Iec61850MonitorDevice device)
{
if (actionGrid.Children
.OfType<Button>()
.Any(button => Equals(button.Tag, IedGooseQuickStartTag)))
{
return;
}
var deviceButtons = actionGrid.Children
.OfType<Button>()
.Count(button => ReferenceEquals(button.Tag, device));
if (deviceButtons < 4)
return;
actionGrid.Columns = Math.Max(6, actionGrid.Columns);
var icon = new System.Windows.Shapes.Path
{
Data = Geometry.Parse(
"M12,10 A2,2 0 1 1 11.99,10 " +
"M12,14 V22 " +
"M7.8,16.2 A6,6 0 0 1 7.8,7.8 " +
"M4.9,19.1 A10,10 0 0 1 4.9,4.9"),
Style = TryFindResource("LucideIcon") as Style,
Stroke = new SolidColorBrush(Color.FromRgb(22, 163, 74))
};
var button = new Button
{
Tag = IedGooseQuickStartTag,
CommandParameter = device,
Style = TryFindResource("IedIconButton") as Style,
Width = 27,
Height = 27,
Margin = new Thickness(0),
Cursor = System.Windows.Input.Cursors.Hand,
ToolTip = "Open GOOSE Subscriber and start capture on the Ethernet adapter routed to this IED",
Content = new Viewbox
{
Width = 14,
Height = 14,
Child = icon
}
};
button.Click += IedGooseQuickStart_Click;
actionGrid.Children.Add(button);
}
private async void IedGooseQuickStart_Click(object sender, RoutedEventArgs e)
{
if (sender is not Button { CommandParameter: Iec61850MonitorDevice device })
return;
SelectedDevice = device;
MainTabs.SelectedIndex = 3;
UpdateNavigationVisuals(3, animate: true);
ActivateGooseSubscriberWorkspace();
await Dispatcher.Yield(DispatcherPriority.Loaded);
if (GooseAdapters.Count == 0)
RefreshGooseAdapters();
var adapter = ResolveGooseAdapterForIed(device, out var routeDetail);
if (adapter is null)
{
GooseStatusText =
$"GOOSE workspace opened for {device.Name}, but ARSAS could not identify one unique capture adapter for route {device.IpAddress}. Select the station-LAN adapter and press Start.";
SetStatus($"GOOSE: adapter selection required for {device.Name}.");
AddLog("WARN", "GOOSE", $"Automatic adapter selection failed for {device.Name} ({device.IpAddress}). {routeDetail}");
return;
}
await StartGooseForIedAsync(device, adapter, routeDetail);
}
private async Task StartGooseForIedAsync(
Iec61850MonitorDevice device,
GooseAdapterOption adapter,
string routeDetail)
{
if (GooseActionBusy)
return;
if (IsGooseCapturing &&
SelectedGooseAdapter is not null &&
SelectedGooseAdapter.Name.Equals(adapter.Name, StringComparison.OrdinalIgnoreCase))
{
SelectedGooseAdapter = adapter;
GooseStatusText =
$"LIVE on {adapter.DisplayText} · selected from {device.Name} route {device.IpAddress} · capture already running.";
SetStatus($"GOOSE Subscriber already live for {device.Name} on the routed Ethernet adapter.");
return;
}
if (IsGooseCapturing)
await StopGooseSubscriberAsync();
SelectedGooseAdapter = adapter;
GooseActionBusy = true;
try
{
RefreshGooseBindingPreview();
ResetGooseView(resetCounters: true);
await _gooseSubscriberRuntime.StartAsync(
adapter.Selector,
_gooseBindingCatalog.SclDocument,
GooseCaptureFilter,
_applicationCancellation.Token);
IsGooseCapturing = true;
GooseStatusText =
$"Listening on {adapter.DisplayText} for {device.Name} ({device.IpAddress}). Waiting for GOOSE frames…";
SetStatus($"GOOSE Subscriber started for {device.Name} on its routed Ethernet adapter.");
AddLog(
"INFO",
"GOOSE",
$"One-click subscriber started for {device.Name} ({device.IpAddress}) on adapter {adapter.Index}: {adapter.Description}. Route selection: {routeDetail}. Binding: {_gooseBindingCatalog.Summary}");
}
catch (Exception ex)
{
IsGooseCapturing = false;
GooseStatusText = $"Could not start GOOSE subscriber for {device.Name}: {DescribeGooseFailure(ex)}";
AddLog("ERROR", "GOOSE", GooseStatusText);
MarkDiagnosticAlert();
}
finally
{
GooseActionBusy = false;
}
}
private GooseAdapterOption? ResolveGooseAdapterForIed(
Iec61850MonitorDevice device,
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(adapter => adapter.GetIPProperties().UnicastAddresses.Any(unicast =>
unicast.Address.AddressFamily == AddressFamily.InterNetwork &&
unicast.Address.Equals(localAddress)));
if (networkInterface is not null)
{
var matches = GooseAdapters
.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 = GooseAdapters
.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;
}
private static IPAddress? ResolveLocalIpv4ForTarget(IPAddress target)
{
try
{
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Connect(new IPEndPoint(target, 102));
return (socket.LocalEndPoint as IPEndPoint)?.Address;
}
catch (SocketException)
{
return null;
}
}
private static bool CaptureAdapterMatchesNetworkInterface(
GooseAdapterOption captureAdapter,
NetworkInterface networkInterface)
{
var captureMac = NormalizeGooseAdapterMac(captureAdapter.MacAddress);
var windowsMac = NormalizeGooseAdapterMac(networkInterface.GetPhysicalAddress().ToString());
if (captureMac.Length > 0 && captureMac.Equals(windowsMac, StringComparison.OrdinalIgnoreCase))
return true;
if (!string.IsNullOrWhiteSpace(networkInterface.Id) &&
captureAdapter.Name.Contains(networkInterface.Id, StringComparison.OrdinalIgnoreCase))
{
return true;
}
return captureAdapter.FriendlyName.Equals(networkInterface.Name, StringComparison.OrdinalIgnoreCase) ||
captureAdapter.FriendlyName.Equals(networkInterface.Description, StringComparison.OrdinalIgnoreCase);
}
private static string NormalizeGooseAdapterMac(string? value)
=> Regex.Replace(value ?? string.Empty, "[^0-9A-Fa-f]", string.Empty).ToUpperInvariant();
private static bool LooksLikeLoopback(GooseAdapterOption adapter)
{
var text = $"{adapter.Name} {adapter.Description} {adapter.FriendlyName}";
return text.Contains("loopback", StringComparison.OrdinalIgnoreCase) ||
text.Contains("npcap loopback", StringComparison.OrdinalIgnoreCase);
}
}