-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMainWindow.ClockSync.cs
More file actions
200 lines (170 loc) · 7.14 KB
/
Copy pathMainWindow.ClockSync.cs
File metadata and controls
200 lines (170 loc) · 7.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
using System.Collections.Specialized;
using System.ComponentModel;
using System.Net;
using ArIED61850Tester.Models;
using ArIED61850Tester.Services;
namespace ArIED61850Tester;
public partial class MainWindow
{
private readonly SntpClockService _sntpClockService = new();
private readonly SemaphoreSlim _clockSyncIntegrationGate = new(1, 1);
private readonly HashSet<string> _clockSyncObservedClients = new(StringComparer.OrdinalIgnoreCase);
private readonly HashSet<string> _clockSyncRepliedClients = new(StringComparer.OrdinalIgnoreCase);
private string _lastClockSyncStatus = string.Empty;
private bool _clockSyncLifecycleAttached;
internal event Action<SntpClockServiceSnapshot>? ClockSyncSnapshotChanged;
internal SntpClockServiceSnapshot ClockSyncSnapshot => _sntpClockService.Snapshot;
private void InitializeClockSyncLifecycle()
{
if (_clockSyncLifecycleAttached)
return;
_clockSyncLifecycleAttached = true;
Devices.CollectionChanged += ClockSyncDevices_CollectionChanged;
foreach (var device in Devices)
AttachClockSyncDevice(device);
_sntpClockService.StatusChanged += ClockSyncService_StatusChanged;
_sntpClockService.ClientRequestObserved += ClockSyncService_ClientRequestObserved;
_sntpClockService.ReplySent += ClockSyncService_ReplySent;
Closed += ClockSyncMainWindow_Closed;
}
private void ClockSyncDevices_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.OldItems != null)
{
foreach (var item in e.OldItems.OfType<Iec61850MonitorDevice>())
item.PropertyChanged -= ClockSyncDevice_PropertyChanged;
}
if (e.NewItems != null)
{
foreach (var item in e.NewItems.OfType<Iec61850MonitorDevice>())
AttachClockSyncDevice(item);
}
}
private void AttachClockSyncDevice(Iec61850MonitorDevice device)
{
device.PropertyChanged -= ClockSyncDevice_PropertyChanged;
device.PropertyChanged += ClockSyncDevice_PropertyChanged;
if (device.IsConnected)
ScheduleClockSyncReconcile(device);
}
private void ClockSyncDevice_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName != nameof(Iec61850MonitorDevice.IsConnected) ||
sender is not Iec61850MonitorDevice device ||
!device.IsConnected)
return;
ScheduleClockSyncReconcile(device);
}
private void ScheduleClockSyncReconcile(Iec61850MonitorDevice device)
{
if (!Dispatcher.CheckAccess())
{
Dispatcher.BeginInvoke(new Action(() => ScheduleClockSyncReconcile(device)));
return;
}
_ = EnsureClockSyncForDeviceAsync(device);
}
private async Task EnsureClockSyncForDeviceAsync(Iec61850MonitorDevice device)
{
if (!IPAddress.TryParse(device.IpAddress, out var iedAddress) ||
iedAddress.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
return;
await _clockSyncIntegrationGate.WaitAsync();
try
{
await _sntpClockService.EnsureStartedAsync(iedAddress, _applicationCancellation.Token);
_sntpClockService.RequestImmediateBroadcast();
}
catch (OperationCanceledException) when (_applicationCancellation.IsCancellationRequested)
{
}
catch (Exception ex)
{
AddLog("WARN", "Clock Sync",
$"{device.Name}: IEC 61850 remains connected, but ARSAS SNTP could not start: {ex.Message}");
}
finally
{
_clockSyncIntegrationGate.Release();
}
}
private void ClockSyncService_StatusChanged(SntpClockServiceSnapshot snapshot)
{
void Publish()
{
// FAT telemetry must receive every evidence-counter change even when the textual
// service detail did not change. The live log remains deduplicated separately.
ClockSyncSnapshotChanged?.Invoke(snapshot);
var status = $"{snapshot.State}|{snapshot.TransportMode}|{snapshot.Detail}";
if (status.Equals(_lastClockSyncStatus, StringComparison.Ordinal))
return;
_lastClockSyncStatus = status;
var level = snapshot.State switch
{
SntpClockServiceState.Serving => "INFO",
SntpClockServiceState.Starting => "INFO",
SntpClockServiceState.Stopped => "INFO",
_ => "WARN"
};
AddLog(level, "Clock Sync", snapshot.Detail);
}
if (Dispatcher.CheckAccess())
Publish();
else
Dispatcher.BeginInvoke(new Action(Publish));
}
private void ClockSyncService_ClientRequestObserved(SntpClientObservation observation)
{
var key = observation.Address.ToString();
void Publish()
{
if (!_clockSyncObservedClients.Add(key))
return;
var device = Devices.FirstOrDefault(item =>
item.IpAddress.Equals(key, StringComparison.OrdinalIgnoreCase));
var name = device?.Name ?? key;
AddLog("INFO", "Clock Sync",
$"{name} ({key}) sent an SNTPv{observation.Version} client request to ARSAS. Request observed; synchronization is not yet proven.");
}
if (Dispatcher.CheckAccess())
Publish();
else
Dispatcher.BeginInvoke(new Action(Publish));
}
private void ClockSyncService_ReplySent(SntpReplyObservation observation)
{
var key = observation.Address.ToString();
void Publish()
{
if (!_clockSyncRepliedClients.Add(key))
return;
var device = Devices.FirstOrDefault(item =>
item.IpAddress.Equals(key, StringComparison.OrdinalIgnoreCase));
var name = device?.Name ?? key;
var transport = observation.TransportMode == SntpClockTransportMode.NpcapRaw ? "Npcap RAW" : "UDP";
AddLog("INFO", "Clock Sync",
$"{name} ({key}) received an ARSAS SNTP Mode 4 reply via {transport}. Reply sent; relay clock synchronization remains unproven until device evidence confirms it.");
}
if (Dispatcher.CheckAccess())
Publish();
else
Dispatcher.BeginInvoke(new Action(Publish));
}
private async void ClockSyncMainWindow_Closed(object? sender, EventArgs e)
{
try
{
Devices.CollectionChanged -= ClockSyncDevices_CollectionChanged;
foreach (var device in Devices)
device.PropertyChanged -= ClockSyncDevice_PropertyChanged;
_sntpClockService.StatusChanged -= ClockSyncService_StatusChanged;
_sntpClockService.ClientRequestObserved -= ClockSyncService_ClientRequestObserved;
_sntpClockService.ReplySent -= ClockSyncService_ReplySent;
await _sntpClockService.DisposeAsync();
}
catch
{
// Application shutdown must never be blocked by a commissioning helper service.
}
}
}