-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.GooseTimeline.cs
More file actions
361 lines (314 loc) · 12.9 KB
/
Copy pathMainWindow.GooseTimeline.cs
File metadata and controls
361 lines (314 loc) · 12.9 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
356
357
358
359
360
361
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using ArIED61850Tester.Models;
using ArIED61850Tester.Services;
using ArIED61850Tester.Views;
namespace ArIED61850Tester;
public partial class MainWindow
{
private const int MaxGooseTimelineEvents = 300;
private const int MaxPendingGooseTimelineEvents = 512;
private readonly ConcurrentQueue<GooseSubscriberFrameSnapshot> _pendingGooseTimeline = new();
private readonly Dictionary<string, DateTimeOffset> _lastGooseTimelineTimestamp = new(StringComparer.OrdinalIgnoreCase);
private GooseEventRow? _selectedGooseEvent;
private bool _goosePresentationInstalled;
private DateTimeOffset _nextGooseHighlightExpiryCheckUtc = DateTimeOffset.MinValue;
public BulkObservableCollection<GooseEventRow> GooseEvents { get; } = new();
public GooseEventRow? SelectedGooseEvent
{
get => _selectedGooseEvent;
set
{
if (!Set(ref _selectedGooseEvent, value))
return;
if (value is not null && _gooseStreamIndex.TryGetValue(value.StreamKey, out var stream))
SelectedGooseStream = stream;
}
}
public string GoosePublisherCountText => $"{GooseStreams.Count:N0}";
public string GooseEventCountText => $"{GooseEvents.Count:N0}";
public string GooseSelectedLeafCountText => $"{SelectedGooseStream?.Leaves.Count ?? 0:N0} values";
public Visibility GooseNoEventsVisibility => GooseEvents.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
[ModuleInitializer]
internal static void RegisterGoosePresentationIntegration()
{
EventManager.RegisterClassHandler(
typeof(MainWindow),
FrameworkElement.LoadedEvent,
new RoutedEventHandler(OnMainWindowLoadedForGoosePresentation));
EventManager.RegisterClassHandler(
typeof(MainWindow),
GooseSubscriberLiteView.RefreshAdaptersRequestedEvent,
new RoutedEventHandler(OnRefreshGooseAdaptersRequested));
EventManager.RegisterClassHandler(
typeof(MainWindow),
GooseSubscriberLiteView.RefreshModelsRequestedEvent,
new RoutedEventHandler(OnRefreshGooseModelsRequested));
EventManager.RegisterClassHandler(
typeof(MainWindow),
GooseSubscriberLiteView.StartRequestedEvent,
new RoutedEventHandler(OnStartGooseRequested));
EventManager.RegisterClassHandler(
typeof(MainWindow),
GooseSubscriberLiteView.StopRequestedEvent,
new RoutedEventHandler(OnStopGooseRequested));
EventManager.RegisterClassHandler(
typeof(MainWindow),
GooseSubscriberLiteView.ClearRequestedEvent,
new RoutedEventHandler(OnClearGooseRequested));
}
private static void OnMainWindowLoadedForGoosePresentation(object sender, RoutedEventArgs args)
{
if (sender is MainWindow window && ReferenceEquals(args.OriginalSource, window))
window.InstallGoosePresentationWorkspace();
}
private void InstallGoosePresentationWorkspace()
{
if (_goosePresentationInstalled)
return;
var gooseTab = MainTabs.Items
.OfType<TabItem>()
.FirstOrDefault(item => string.Equals(item.Header?.ToString(), "GOOSE Subscriber", StringComparison.Ordinal));
if (gooseTab is null)
return;
gooseTab.Content = new GooseSubscriberLiteView { DataContext = this };
_gooseSubscriberRuntime.FrameReceived += GooseTimeline_FrameReceived;
_uiFlushTimer.Tick += GooseTimelineUiFlushTimer_Tick;
PropertyChanged += GoosePresentation_PropertyChanged;
_goosePresentationInstalled = true;
RaiseGoosePresentationState();
}
private void GoosePresentation_PropertyChanged(object? sender, PropertyChangedEventArgs args)
{
if (args.PropertyName == nameof(SelectedGooseStream))
Raise(nameof(GooseSelectedLeafCountText));
}
private static void OnRefreshGooseAdaptersRequested(object sender, RoutedEventArgs args)
{
if (sender is not MainWindow window)
return;
window.RefreshGooseAdapters_Click(window, args);
args.Handled = true;
}
private static void OnRefreshGooseModelsRequested(object sender, RoutedEventArgs args)
{
if (sender is not MainWindow window)
return;
window.RefreshGooseModels_Click(window, args);
args.Handled = true;
}
private static void OnStartGooseRequested(object sender, RoutedEventArgs args)
{
if (sender is not MainWindow window)
return;
window.ResetGooseTimelineUi();
window.StartGooseSubscriber_Click(window, args);
args.Handled = true;
}
private static void OnStopGooseRequested(object sender, RoutedEventArgs args)
{
if (sender is not MainWindow window)
return;
window.StopGooseSubscriber_Click(window, args);
args.Handled = true;
}
private static void OnClearGooseRequested(object sender, RoutedEventArgs args)
{
if (sender is not MainWindow window)
return;
window.ResetGooseTimelineUi();
window.ClearGooseSubscriber_Click(window, args);
args.Handled = true;
}
private void GooseTimeline_FrameReceived(GooseSubscriberFrameSnapshot snapshot)
{
if (!IsMeaningfulGooseTimelineEvent(snapshot))
return;
_pendingGooseTimeline.Enqueue(snapshot);
while (_pendingGooseTimeline.Count > MaxPendingGooseTimelineEvents && _pendingGooseTimeline.TryDequeue(out _))
{
}
}
private static bool IsMeaningfulGooseTimelineEvent(GooseSubscriberFrameSnapshot snapshot)
{
if (snapshot.PacketCount <= 1 || snapshot.StreamEvent.ChangedValueCount > 0 || snapshot.StreamEvent.Diagnostics.Count > 0)
return true;
var status = snapshot.StreamEvent.GooseSequenceStatus.ToString();
return status.Contains("State", StringComparison.OrdinalIgnoreCase) ||
(!status.Contains("Retransmission", StringComparison.OrdinalIgnoreCase) &&
!status.Contains("Normal", StringComparison.OrdinalIgnoreCase));
}
private void GooseTimelineUiFlushTimer_Tick(object? sender, EventArgs args)
{
var nowUtc = DateTimeOffset.UtcNow;
if (nowUtc >= _nextGooseHighlightExpiryCheckUtc)
{
ExpireGooseHighlights(nowUtc);
_nextGooseHighlightExpiryCheckUtc = nowUtc.AddSeconds(1);
}
if (_pendingGooseTimeline.IsEmpty)
return;
var processed = 0;
while (processed < 48 && _pendingGooseTimeline.TryDequeue(out var captured))
{
var stream = BuildGooseStreamSnapshot(captured, _gooseBindingCatalog);
var eventRow = BuildGooseEventRow(captured, stream);
// Append in capture order. Tail insertion avoids shifting every realized row.
GooseEvents.Add(eventRow);
while (GooseEvents.Count > MaxGooseTimelineEvents)
GooseEvents.RemoveAt(0);
SelectedGooseEvent ??= eventRow;
processed++;
}
RaiseGoosePresentationState();
}
private void ExpireGooseHighlights(DateTimeOffset nowUtc)
{
foreach (var eventRow in GooseEvents)
eventRow.ExpireHighlight(nowUtc);
foreach (var stream in GooseStreams)
{
foreach (var leaf in stream.Leaves)
leaf.ExpireHighlight(nowUtc);
}
}
private GooseEventRow BuildGooseEventRow(
GooseSubscriberFrameSnapshot captured,
GooseStreamSnapshot stream)
{
var status = stream.SequenceStatus;
var hasDiagnostics = !string.IsNullOrWhiteSpace(stream.DiagnosticsSummary);
var isNew = captured.PacketCount <= 1;
var isStateChange = stream.ChangedValueCount > 0 || status.Contains("State", StringComparison.OrdinalIgnoreCase);
var eventText = isNew
? "New"
: hasDiagnostics
? "Warning"
: isStateChange
? "State change"
: FriendlySequenceStatus(status);
var eventTone = hasDiagnostics
? "Warning"
: isStateChange
? "Change"
: "Info";
var deltaText = "-";
if (_lastGooseTimelineTimestamp.TryGetValue(captured.StreamKey, out var previousTimestamp))
{
var delta = captured.CaptureTimestamp - previousTimestamp;
deltaText = FormatGooseDelta(delta);
}
_lastGooseTimelineTimestamp[captured.StreamKey] = captured.CaptureTimestamp;
return new GooseEventRow
{
StreamKey = captured.StreamKey,
Timestamp = captured.CaptureTimestamp,
DeltaText = deltaText,
EventText = eventText,
EventTone = eventTone,
Publisher = BuildGoosePublisherName(stream),
StateSequenceText = $"{stream.StateNumberText} / {stream.SequenceNumberText}",
Summary = BuildGooseEventSummary(stream, isNew, hasDiagnostics)
};
}
private static string BuildGoosePublisherName(GooseStreamSnapshot stream)
{
foreach (var candidate in new[] { stream.ModelIedName, stream.GoId, ShortGooseReference(stream.GoCbRef), stream.AppIdText })
{
if (!string.IsNullOrWhiteSpace(candidate))
return candidate;
}
return "GOOSE publisher";
}
private static string BuildGooseEventSummary(GooseStreamSnapshot stream, bool isNew, bool hasDiagnostics)
{
if (hasDiagnostics)
return ShortenGooseText(stream.DiagnosticsSummary, 150);
var changed = stream.Leaves
.Where(leaf => leaf.IsChanged)
.Take(2)
.Select(leaf =>
{
var current = ShortenGooseText(GooseEngineeringValueFormatter.Format(leaf.Value), 30);
return IsGenericGooseLeafName(leaf.SignalName)
? current
: $"{ShortenGooseText(leaf.SignalName, 22)}: {current}";
})
.ToArray();
if (changed.Length > 0)
{
var suffix = stream.ChangedValueCount > changed.Length
? $" • +{stream.ChangedValueCount - changed.Length:N0}"
: string.Empty;
return string.Join(" • ", changed) + suffix;
}
if (isNew)
return $"Publisher detected • {stream.Leaves.Count:N0} DataSet value(s)";
return FriendlySequenceStatus(stream.SequenceStatus);
}
private static bool IsGenericGooseLeafName(string? value)
{
var text = value?.Trim() ?? string.Empty;
return string.IsNullOrWhiteSpace(text) ||
text.StartsWith("Leaf ", StringComparison.OrdinalIgnoreCase);
}
private static string FriendlySequenceStatus(string value)
{
if (string.IsNullOrWhiteSpace(value))
return "GOOSE update";
var text = value.Replace("_", " ");
var result = new System.Text.StringBuilder(text.Length + 8);
for (var index = 0; index < text.Length; index++)
{
if (index > 0 && char.IsUpper(text[index]) && char.IsLower(text[index - 1]))
result.Append(' ');
result.Append(text[index]);
}
return result.ToString();
}
private static string FormatGooseDelta(TimeSpan delta)
{
if (delta.TotalMilliseconds < 1000)
return $"{Math.Max(0, delta.TotalMilliseconds):0.0} ms";
if (delta.TotalSeconds < 60)
return $"{delta.TotalSeconds:0.000} s";
return delta.ToString(@"mm\:ss\.fff", CultureInfo.InvariantCulture);
}
private static string ShortGooseReference(string? value)
{
var text = value?.Trim() ?? string.Empty;
var slash = text.LastIndexOf('/');
if (slash >= 0 && slash < text.Length - 1)
text = text[(slash + 1)..];
return text.Replace('$', '.');
}
private static string ShortenGooseText(string? value, int maximumLength)
{
var text = value?.Trim() ?? string.Empty;
if (text.Length <= maximumLength)
return text;
return text[..Math.Max(1, maximumLength - 1)] + "…";
}
private void ResetGooseTimelineUi()
{
while (_pendingGooseTimeline.TryDequeue(out _))
{
}
_lastGooseTimelineTimestamp.Clear();
_nextGooseHighlightExpiryCheckUtc = DateTimeOffset.MinValue;
GooseEvents.Clear();
SelectedGooseEvent = null;
RaiseGoosePresentationState();
}
private void RaiseGoosePresentationState()
{
Raise(nameof(GoosePublisherCountText));
Raise(nameof(GooseEventCountText));
Raise(nameof(GooseSelectedLeafCountText));
Raise(nameof(GooseNoEventsVisibility));
}
}