-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMainWindow.AlarmAnnunciator.cs
More file actions
560 lines (491 loc) · 21.3 KB
/
Copy pathMainWindow.AlarmAnnunciator.cs
File metadata and controls
560 lines (491 loc) · 21.3 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
using System.Collections.Concurrent;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using ArIED61850Tester.Models;
namespace ArIED61850Tester;
/// <summary>
/// Alarm Annunciator controller.
///
/// SOE/Event Log remains the occurrence/history authority. Current live point snapshots
/// are reconciled separately so a restored project immediately shows the physical alarm state
/// after its initial read without fabricating an SOE entry. A momentary alarm edge remains
/// latched even after the process value has returned to normal.
///
/// Presentation is grouped by IED. Only the selected IED's alarm windows are rendered;
/// the IED rail retains independent alarm/unacknowledged indication for every configured
/// device. This keeps both layout and flash updates bounded for large SAS projects.
/// </summary>
public partial class MainWindow
{
private readonly ConcurrentQueue<Iec61850EventEntry> _pendingAnnunciatorEvents = new();
private readonly Dictionary<string, List<string>> _annunciatorConfiguredReferences = new(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, AlarmAnnunciatorItem> _annunciatorByPointKey = new(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, AlarmAnnunciatorDeviceGroup> _annunciatorDeviceById = new(StringComparer.OrdinalIgnoreCase);
private DispatcherTimer? _annunciatorUiTimer;
private AlarmAnnunciatorDeviceGroup? _selectedAnnunciatorDevice;
private bool _annunciatorInitialized;
private bool _annunciatorFlashPhase = true;
private int _annunciatorUiTicks;
public BulkObservableCollection<AlarmAnnunciatorItem> AnnunciatorAlarms { get; } = new();
public BulkObservableCollection<AlarmAnnunciatorDeviceGroup> AnnunciatorDevices { get; } = new();
public AlarmAnnunciatorDeviceGroup? SelectedAnnunciatorDevice
{
get => _selectedAnnunciatorDevice;
set
{
if (ReferenceEquals(_selectedAnnunciatorDevice, value))
return;
_selectedAnnunciatorDevice = value;
if (_selectedAnnunciatorDevice != null)
{
_selectedAnnunciatorDevice.SetFlashPhase(_annunciatorFlashPhase);
foreach (var alarm in _selectedAnnunciatorDevice.Alarms.Where(item => item.IsFlashing))
alarm.SetFlashPhase(_annunciatorFlashPhase);
}
Raise();
}
}
public int AnnunciatorConfiguredCount => AnnunciatorDevices.Sum(group => group.ConfiguredCount);
public int AnnunciatorActiveCount => AnnunciatorDevices.Sum(group => group.ActiveCount);
public int AnnunciatorUnacknowledgedCount => AnnunciatorDevices.Sum(group => group.UnacknowledgedCount);
public int AnnunciatorDeviceCount => AnnunciatorDevices.Count;
public bool AnnunciatorHasUnacknowledged => AnnunciatorUnacknowledgedCount > 0;
public string AnnunciatorSummaryText => $"{AnnunciatorActiveCount} ACTIVE • {AnnunciatorUnacknowledgedCount} UNACK • {AnnunciatorDeviceCount} IED • {AnnunciatorConfiguredCount} WINDOWS";
public Visibility AnnunciatorEmptyVisibility => AnnunciatorDevices.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
public Visibility AnnunciatorContentVisibility => AnnunciatorDevices.Count > 0 ? Visibility.Visible : Visibility.Collapsed;
public double AnnunciatorBeaconOpacity => AnnunciatorHasUnacknowledged
? (_annunciatorFlashPhase ? 1d : 0.18d)
: AnnunciatorActiveCount > 0 ? 1d : 0.32d;
[ModuleInitializer]
internal static void RegisterAlarmAnnunciator()
{
EventManager.RegisterClassHandler(
typeof(MainWindow),
FrameworkElement.LoadedEvent,
new RoutedEventHandler(OnAlarmAnnunciatorWindowLoaded));
}
private static void OnAlarmAnnunciatorWindowLoaded(object sender, RoutedEventArgs e)
{
if (sender is MainWindow window)
window.InitializeAlarmAnnunciator();
}
private void InitializeAlarmAnnunciator()
{
if (_annunciatorInitialized)
return;
_annunciatorInitialized = true;
_runtime.EventRaised += AlarmRuntime_EventRaised;
GlobalPoints.CollectionChanged += AnnunciatorGlobalPoints_CollectionChanged;
Devices.CollectionChanged += AnnunciatorDevices_CollectionChanged;
Closed += AlarmAnnunciatorWindow_Closed;
_annunciatorUiTimer = new DispatcherTimer(DispatcherPriority.Background)
{
Interval = TimeSpan.FromMilliseconds(100)
};
_annunciatorUiTimer.Tick += AnnunciatorUiTimer_Tick;
_annunciatorUiTimer.Start();
SynchronizeAnnunciatorPointSelection();
RaiseAnnunciatorSummary();
}
private void AlarmAnnunciatorWindow_Closed(object? sender, EventArgs e)
{
if (!_annunciatorInitialized)
return;
_annunciatorInitialized = false;
_runtime.EventRaised -= AlarmRuntime_EventRaised;
GlobalPoints.CollectionChanged -= AnnunciatorGlobalPoints_CollectionChanged;
Devices.CollectionChanged -= AnnunciatorDevices_CollectionChanged;
Closed -= AlarmAnnunciatorWindow_Closed;
if (_annunciatorUiTimer != null)
{
_annunciatorUiTimer.Stop();
_annunciatorUiTimer.Tick -= AnnunciatorUiTimer_Tick;
_annunciatorUiTimer = null;
}
}
private void AlarmRuntime_EventRaised(Iec61850EventEntry entry)
=> _pendingAnnunciatorEvents.Enqueue(entry);
/// <summary>
/// Reconciles the annunciator fascia from the same point snapshot already accepted by
/// the Live Monitor. This never creates an Event Log entry and never starts another
/// acquisition path; SOE edges remain responsible for occurrence history/latching.
/// </summary>
private void ReconcileAnnunciatorFromLivePoint(Iec61850MonitorPoint point)
{
if (!_annunciatorInitialized ||
!point.CanUseAsAnnunciator ||
!IsAnnunciatorConfigured(point.DeviceId, point.IecReference))
{
return;
}
var item = EnsureAnnunciatorItem(point);
item.InitializeFromPoint(point);
RefreshAnnunciatorDeviceGroup(point.DeviceId);
}
private void AnnunciatorUiTimer_Tick(object? sender, EventArgs e)
{
FlushPendingAnnunciatorEvents();
_annunciatorUiTicks++;
if (_annunciatorUiTicks < 5)
return;
_annunciatorUiTicks = 0;
_annunciatorFlashPhase = !_annunciatorFlashPhase;
// At scale, do not invalidate every alarm window in the project. Only visible
// flashing windows and the compact IED rail indicators need a 2 Hz update.
if (SelectedAnnunciatorDevice != null)
{
foreach (var alarm in SelectedAnnunciatorDevice.Alarms.Where(item => item.IsFlashing))
alarm.SetFlashPhase(_annunciatorFlashPhase);
}
foreach (var group in AnnunciatorDevices.Where(group => group.HasUnacknowledged))
group.SetFlashPhase(_annunciatorFlashPhase);
Raise(nameof(AnnunciatorBeaconOpacity));
}
private void FlushPendingAnnunciatorEvents()
{
var changed = false;
var processed = 0;
while (processed < 1000 && _pendingAnnunciatorEvents.TryDequeue(out var entry))
{
processed++;
if (!IsAnnunciatorConfigured(entry.DeviceId, entry.IecReference))
continue;
var item = EnsureAnnunciatorItem(entry);
if (item.ApplyEvent(entry))
changed = true;
}
if (changed)
RaiseAnnunciatorSummary();
}
private void AnnunciatorGlobalPoints_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
=> SynchronizeAnnunciatorPointSelection();
private void AnnunciatorDevices_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Reset)
{
ClearAnnunciatorConfiguration();
return;
}
if (e.OldItems == null)
return;
foreach (var removed in e.OldItems.OfType<Iec61850MonitorDevice>())
RemoveAnnunciatorDevice(removed.DeviceId);
}
private void SynchronizeAnnunciatorPointSelection()
{
foreach (var point in GlobalPoints)
{
var configured = point.CanUseAsAnnunciator && IsAnnunciatorConfigured(point.DeviceId, point.IecReference);
point.IsAnnunciatorSelected = configured;
if (configured)
EnsureAnnunciatorItem(point).InitializeFromPoint(point);
}
RaiseAnnunciatorSummary();
}
private void AnnunciatorSelection_Click(object sender, RoutedEventArgs e)
{
if (sender is not CheckBox checkBox || checkBox.Tag is not Iec61850MonitorPoint point)
return;
var requested = checkBox.IsChecked == true;
if (requested && !point.CanUseAsAnnunciator)
{
checkBox.IsChecked = false;
point.IsAnnunciatorSelected = false;
SetStatus($"{point.SignalName}: Alarm Annunciator accepts IEC 61850 ST status points only.");
return;
}
if (!requested && _annunciatorByPointKey.TryGetValue(point.PointKey, out var existing) && !existing.IsNormal)
{
var confirm = MessageBox.Show(
this,
$"{point.SignalName} still has an active or unacknowledged annunciator occurrence.\n\nRemove it from Alarm Annunciator anyway?",
"Remove annunciator signal",
MessageBoxButton.YesNo,
MessageBoxImage.Warning);
if (confirm != MessageBoxResult.Yes)
{
checkBox.IsChecked = true;
point.IsAnnunciatorSelected = true;
return;
}
}
SetAnnunciatorSelection(point, requested);
}
private void SetAnnunciatorSelection(Iec61850MonitorPoint point, bool selected)
{
var normalized = NormalizeReference(point.IecReference);
if (normalized.Length == 0)
return;
if (!_annunciatorConfiguredReferences.TryGetValue(point.DeviceId, out var references))
{
references = new List<string>();
_annunciatorConfiguredReferences[point.DeviceId] = references;
}
if (selected)
{
if (!references.Contains(normalized, StringComparer.OrdinalIgnoreCase))
references.Add(normalized);
point.IsAnnunciatorSelected = true;
EnsureAnnunciatorItem(point).InitializeFromPoint(point);
SetStatus($"{point.DeviceName} / {point.SignalName}: added to Alarm Annunciator.");
}
else
{
references.RemoveAll(reference => reference.Equals(normalized, StringComparison.OrdinalIgnoreCase));
if (references.Count == 0)
_annunciatorConfiguredReferences.Remove(point.DeviceId);
point.IsAnnunciatorSelected = false;
RemoveAnnunciatorItem(point.PointKey);
SetStatus($"{point.DeviceName} / {point.SignalName}: removed from Alarm Annunciator.");
}
RaiseAnnunciatorSummary();
}
private void AcknowledgeAlarm_Click(object sender, RoutedEventArgs e)
{
if (sender is not Button button || button.Tag is not AlarmAnnunciatorItem item)
return;
if (!item.Acknowledge(DateTimeOffset.Now))
return;
SetStatus($"Alarm acknowledged: {item.DeviceName} / {item.SignalName}.");
RefreshAnnunciatorDeviceGroup(item.DeviceId);
RaiseAnnunciatorSummary();
}
private void AcknowledgeAllAlarms_Click(object sender, RoutedEventArgs e)
{
var group = SelectedAnnunciatorDevice;
if (group == null)
{
SetStatus("Alarm Annunciator: select an IED first.");
return;
}
var acknowledgedAt = DateTimeOffset.Now;
var count = 0;
foreach (var item in group.Alarms.Where(item => item.CanAcknowledge).ToArray())
{
if (item.Acknowledge(acknowledgedAt))
count++;
}
RefreshAnnunciatorDeviceGroup(group.DeviceId);
SetStatus(count == 0
? $"{group.DeviceName}: no unacknowledged alarms."
: $"{group.DeviceName}: acknowledged {count} alarm(s).");
RaiseAnnunciatorSummary();
}
private AlarmAnnunciatorItem EnsureAnnunciatorItem(Iec61850MonitorPoint point)
{
if (_annunciatorByPointKey.TryGetValue(point.PointKey, out var existing))
return existing;
var item = new AlarmAnnunciatorItem
{
DeviceId = point.DeviceId,
PointKey = point.PointKey,
ConfiguredReference = NormalizeReference(point.IecReference),
DeviceName = point.DeviceName,
SignalName = point.SignalName,
IecReference = point.IecReference,
IecTelegram = point.IecTelegram,
IecDataType = point.IecDataType
};
AddAnnunciatorItem(item);
return item;
}
private AlarmAnnunciatorItem EnsureAnnunciatorItem(Iec61850EventEntry entry)
{
var pointKey = string.IsNullOrWhiteSpace(entry.PointKey)
? BuildAnnunciatorPointKey(entry.DeviceId, entry.IecReference)
: entry.PointKey;
if (_annunciatorByPointKey.TryGetValue(pointKey, out var existing))
return existing;
var item = new AlarmAnnunciatorItem
{
DeviceId = entry.DeviceId,
PointKey = pointKey,
ConfiguredReference = NormalizeReference(entry.IecReference),
DeviceName = entry.DeviceName,
SignalName = entry.SignalName,
IecReference = entry.IecReference,
IecTelegram = entry.IecTelegram,
IecDataType = entry.IecDataType
};
AddAnnunciatorItem(item);
return item;
}
private AlarmAnnunciatorItem EnsureAnnunciatorPlaceholder(
Iec61850MonitorDevice device,
string reference,
SignalDefinition? signal)
{
var pointKey = BuildAnnunciatorPointKey(device.DeviceId, reference);
if (_annunciatorByPointKey.TryGetValue(pointKey, out var existing))
return existing;
var effectiveReference = signal?.ObjectReference ?? reference;
var item = new AlarmAnnunciatorItem
{
DeviceId = device.DeviceId,
PointKey = pointKey,
ConfiguredReference = NormalizeReference(reference),
DeviceName = device.Name,
SignalName = signal?.Name ?? BuildAnnunciatorSignalCaption(reference),
IecReference = effectiveReference,
IecTelegram = Iec61850MonitorPoint.StripIedNamePrefix(effectiveReference, device.Name),
IecDataType = signal?.DataType ?? string.Empty
};
item.MarkUnavailable(device.IsConnected ? "Waiting for live value" : "Offline / saved configuration");
AddAnnunciatorItem(item);
return item;
}
private void AddAnnunciatorItem(AlarmAnnunciatorItem item)
{
_annunciatorByPointKey[item.PointKey] = item;
item.PropertyChanged += AnnunciatorItem_PropertyChanged;
AnnunciatorAlarms.Add(item);
var group = EnsureAnnunciatorDeviceGroup(item.DeviceId, item.DeviceName);
if (!group.Alarms.Contains(item))
group.Alarms.Add(item);
group.Recalculate(_annunciatorFlashPhase);
RaiseAnnunciatorSummary();
}
private AlarmAnnunciatorDeviceGroup EnsureAnnunciatorDeviceGroup(string deviceId, string deviceName)
{
if (_annunciatorDeviceById.TryGetValue(deviceId, out var existing))
{
existing.DeviceName = deviceName;
return existing;
}
var group = new AlarmAnnunciatorDeviceGroup
{
DeviceId = deviceId,
DeviceName = deviceName
};
_annunciatorDeviceById[deviceId] = group;
AnnunciatorDevices.Add(group);
SelectedAnnunciatorDevice ??= group;
return group;
}
private void AnnunciatorItem_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (sender is not AlarmAnnunciatorItem item)
return;
if (e.PropertyName == nameof(AlarmAnnunciatorItem.DeviceName) &&
_annunciatorDeviceById.TryGetValue(item.DeviceId, out var namedGroup))
{
namedGroup.DeviceName = item.DeviceName;
}
if (e.PropertyName == nameof(AlarmAnnunciatorItem.VisualState))
{
RefreshAnnunciatorDeviceGroup(item.DeviceId);
RaiseAnnunciatorSummary();
}
}
private void RefreshAnnunciatorDeviceGroup(string deviceId)
{
if (!_annunciatorDeviceById.TryGetValue(deviceId, out var group))
return;
group.Recalculate(_annunciatorFlashPhase);
}
private void RemoveAnnunciatorItem(string pointKey)
{
if (!_annunciatorByPointKey.Remove(pointKey, out var item))
return;
item.PropertyChanged -= AnnunciatorItem_PropertyChanged;
AnnunciatorAlarms.Remove(item);
if (_annunciatorDeviceById.TryGetValue(item.DeviceId, out var group))
{
group.Alarms.Remove(item);
if (group.Alarms.Count == 0)
{
_annunciatorDeviceById.Remove(group.DeviceId);
AnnunciatorDevices.Remove(group);
if (ReferenceEquals(SelectedAnnunciatorDevice, group))
SelectedAnnunciatorDevice = AnnunciatorDevices.FirstOrDefault();
}
else
{
group.Recalculate(_annunciatorFlashPhase);
}
}
RaiseAnnunciatorSummary();
}
private void RemoveAnnunciatorDevice(string deviceId)
{
_annunciatorConfiguredReferences.Remove(deviceId);
foreach (var item in AnnunciatorAlarms.Where(item => item.DeviceId.Equals(deviceId, StringComparison.OrdinalIgnoreCase)).ToArray())
RemoveAnnunciatorItem(item.PointKey);
RaiseAnnunciatorSummary();
}
private void ClearAnnunciatorConfiguration()
{
_annunciatorConfiguredReferences.Clear();
foreach (var item in AnnunciatorAlarms)
item.PropertyChanged -= AnnunciatorItem_PropertyChanged;
AnnunciatorAlarms.Clear();
_annunciatorByPointKey.Clear();
AnnunciatorDevices.Clear();
_annunciatorDeviceById.Clear();
SelectedAnnunciatorDevice = null;
while (_pendingAnnunciatorEvents.TryDequeue(out _)) { }
foreach (var point in GlobalPoints)
point.IsAnnunciatorSelected = false;
RaiseAnnunciatorSummary();
}
private bool IsAnnunciatorConfigured(string deviceId, string? reference)
{
if (!_annunciatorConfiguredReferences.TryGetValue(deviceId, out var references))
return false;
var normalized = NormalizeReference(reference);
return references.Contains(normalized, StringComparer.OrdinalIgnoreCase);
}
private List<string> GetAnnunciatorReferencesForDevice(Iec61850MonitorDevice device)
{
if (!_annunciatorConfiguredReferences.TryGetValue(device.DeviceId, out var references))
return new List<string>();
return references.Distinct(StringComparer.OrdinalIgnoreCase).ToList();
}
private void RestoreAnnunciatorReferences(Iec61850MonitorDevice device, IEnumerable<string>? references)
{
var normalized = (references ?? Array.Empty<string>())
.Select(NormalizeReference)
.Where(reference => reference.Length > 0)
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
if (normalized.Count == 0)
{
_annunciatorConfiguredReferences.Remove(device.DeviceId);
return;
}
_annunciatorConfiguredReferences[device.DeviceId] = normalized;
foreach (var reference in normalized)
{
var signal = device.Signals.FirstOrDefault(candidate =>
NormalizeReference(candidate.ObjectReference).Equals(reference, StringComparison.OrdinalIgnoreCase));
EnsureAnnunciatorPlaceholder(device, reference, signal);
}
SynchronizeAnnunciatorPointSelection();
RaiseAnnunciatorSummary();
}
private void RaiseAnnunciatorSummary()
{
Raise(nameof(AnnunciatorConfiguredCount));
Raise(nameof(AnnunciatorActiveCount));
Raise(nameof(AnnunciatorUnacknowledgedCount));
Raise(nameof(AnnunciatorDeviceCount));
Raise(nameof(AnnunciatorHasUnacknowledged));
Raise(nameof(AnnunciatorSummaryText));
Raise(nameof(AnnunciatorEmptyVisibility));
Raise(nameof(AnnunciatorContentVisibility));
Raise(nameof(AnnunciatorBeaconOpacity));
}
private static string BuildAnnunciatorPointKey(string deviceId, string? reference)
=> $"{deviceId}|{NormalizeReference(reference)}";
private static string BuildAnnunciatorSignalCaption(string reference)
{
var normalized = (reference ?? string.Empty).Replace('$', '.');
var slash = normalized.LastIndexOf('/');
var tail = slash >= 0 ? normalized[(slash + 1)..] : normalized;
return string.IsNullOrWhiteSpace(tail) ? "Alarm signal" : tail;
}
}