-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIoListTestingWindow.SupplementalEvidence.cs
More file actions
291 lines (258 loc) · 12.3 KB
/
Copy pathIoListTestingWindow.SupplementalEvidence.cs
File metadata and controls
291 lines (258 loc) · 12.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
using System.Windows;
using System.Windows.Controls;
using ArIED61850Tester.Models;
using ArIED61850Tester.Models.IoTesting;
using ArIED61850Tester.Services.IoTesting;
namespace ArIED61850Tester;
public partial class IoListTestingWindow
{
private Button? _comtradeEvidenceButton;
private Button? _timeSyncEvidenceButton;
private Button? _cleanSessionButton;
public int SelectedSupplementalEvidenceCount
{
get
{
if (SelectedIed == null)
return 0;
var comtrade = IoFatSupplementalEvidenceService.Count(
Storage,
SelectedIed.IedName,
IoFatSupplementalEvidenceService.ComtradeKind);
var timeSync = IoFatSupplementalEvidenceService.ReadLatest(
Storage,
SelectedIed.IedName,
IoFatSupplementalEvidenceService.TimeSyncKind) == null ? 0 : 1;
return comtrade + timeSync;
}
}
private void InstallSupplementalEvidenceControls()
{
if (_comtradeEvidenceButton != null || WorkspacePreviewToggle.Parent is not Panel actionPanel)
return;
_timeSyncEvidenceButton = CreateEvidenceButton("Time Sync · —", RefreshTimeSyncEvidence_Click);
_timeSyncEvidenceButton.ToolTip = "Capture IEC 61850 time-synchronization evidence for the selected IED";
_comtradeEvidenceButton = CreateEvidenceButton("COMTRADE · —", OpenComtradeEvidence_Click);
_comtradeEvidenceButton.ToolTip = "Browse relay fault records. A remote COMTRADE listing is sufficient File Service FAT evidence; download is optional.";
_cleanSessionButton = CreateEvidenceButton("New Clean FAT", NewCleanFatSession_Click);
_cleanSessionButton.ToolTip = "Archive all current FAT evidence and reset the project to a zero-evidence retest session";
var insertionIndex = actionPanel.Children.IndexOf(WorkspacePreviewToggle) + 1;
actionPanel.Children.Insert(insertionIndex, _timeSyncEvidenceButton);
actionPanel.Children.Insert(insertionIndex + 1, _comtradeEvidenceButton);
actionPanel.Children.Insert(insertionIndex + 2, _cleanSessionButton);
RefreshSupplementalEvidenceControls();
}
private Button CreateEvidenceButton(string content, RoutedEventHandler handler)
{
var button = new Button
{
Content = content,
Padding = new Thickness(10, 8, 10, 8),
Margin = new Thickness(0, 0, 6, 0),
MinWidth = 92
};
if (TryFindResource("SoftButton") is Style style)
button.Style = style;
button.Click += handler;
return button;
}
private void RefreshSupplementalEvidenceControls()
{
if (_timeSyncEvidenceButton == null || _comtradeEvidenceButton == null)
return;
var ied = SelectedIed;
if (ied == null)
{
_timeSyncEvidenceButton.Content = "Time Sync · —";
_timeSyncEvidenceButton.ToolTip = "Select an IED first";
_comtradeEvidenceButton.Content = "COMTRADE · —";
_comtradeEvidenceButton.ToolTip = "Select an IED first";
return;
}
var timeSync = IoFatSupplementalEvidenceService.ReadLatest(
Storage,
ied.IedName,
IoFatSupplementalEvidenceService.TimeSyncKind);
_timeSyncEvidenceButton.Content = timeSync == null
? "Time Sync · —"
: $"Time Sync · {timeSync.Verdict}";
_timeSyncEvidenceButton.ToolTip = timeSync == null
? "No time-sync evidence captured yet. It will be captured automatically after FAT connection."
: $"{timeSync.DisplayText}\n{timeSync.Reason}\nCaptured {timeSync.RecordedAtUtc.ToLocalTime():yyyy-MM-dd HH:mm:ss}";
var comtradeCount = IoFatSupplementalEvidenceService.Count(
Storage,
ied.IedName,
IoFatSupplementalEvidenceService.ComtradeKind);
_comtradeEvidenceButton.Content = ied.HasRemoteComtradeEvidence
? $"COMTRADE · PASS · {comtradeCount}"
: $"COMTRADE · {comtradeCount}";
_comtradeEvidenceButton.ToolTip = ied.HasRemoteComtradeEvidence
? $"File Service PASS via IEC 61850 FileDirectory\nLatest remote COMTRADE: {ied.LatestComtradeFiles}\nRemote path: {ied.LatestComtradeRemotePath}\nDownload is optional additional verification."
: "Open relay fault records. A supported remote COMTRADE returned by IEC 61850 FileDirectory becomes IED-level FAT evidence immediately; download is optional.";
}
private async Task CaptureTimeSyncEvidenceAfterPreparationAsync(
MainWindow engineeringWindow,
IoTestIedPlan ied)
{
var device = FindFatDevice(engineeringWindow, ied);
if (device == null)
return;
// Give the extra read-only sync signal a short bounded window to receive its
// first report/poll value after the FAT monitor starts. Do not block the FAT
// workflow when a relay does not expose a recognized status object.
var syncSignal = IoFatSupplementalEvidenceService.FindTimeSyncSignal(device);
if (syncSignal != null)
{
for (var attempt = 0; attempt < 8 && !HasLiveValue(syncSignal.Value); attempt++)
await Task.Delay(175).ConfigureAwait(true);
}
IoFatSupplementalEvidenceService.CaptureTimeSync(Storage, Project, ied, device);
Storage?.ScheduleSave();
RefreshSupplementalEvidenceControls();
Raise(nameof(SelectedSupplementalEvidenceCount));
Raise(nameof(SelectedEvidenceCount));
}
private async void RefreshTimeSyncEvidence_Click(object sender, RoutedEventArgs e)
{
if (SelectedIed == null)
return;
if (Owner is not MainWindow engineeringWindow)
{
MessageBox.Show(
this,
"Open this FAT workspace from the ARSAS engineering window to refresh live time-sync evidence.",
"Time Sync Evidence",
MessageBoxButton.OK,
MessageBoxImage.Information);
return;
}
var device = FindFatDevice(engineeringWindow, SelectedIed);
if (device == null || !device.IsConnected)
{
MessageBox.Show(
this,
"Connect the selected IED first. ARSAS will then capture its explicit synchronization status or timestamp fallback.",
"Time Sync Evidence",
MessageBoxButton.OK,
MessageBoxImage.Information);
return;
}
await CaptureTimeSyncEvidenceAfterPreparationAsync(engineeringWindow, SelectedIed);
}
private void NewCleanFatSession_Click(object sender, RoutedEventArgs e)
{
var answer = MessageBox.Show(
this,
"Create a NEW CLEAN FAT session for this project?\n\n" +
"• All current ON/OFF evidence and relay timestamps will be cleared from the active workspace.\n" +
"• Time Sync and COMTRADE evidence will be cleared from the active report.\n" +
"• Existing hash-chained evidence files will NOT be deleted or rewritten; they will be verified and archived outside the new export scope.\n" +
"• All import-ready IO points will be enabled again for retest.\n\n" +
"Use this after correcting relay time synchronization or whenever the customer requires a completely fresh FAT run.",
"New Clean FAT Session",
MessageBoxButton.YesNo,
MessageBoxImage.Warning,
MessageBoxResult.No);
if (answer != MessageBoxResult.Yes)
return;
try
{
if (Session.IsSessionActive)
{
var stopped = Session.Stop("Sealed before New Clean FAT retest reset.");
if (!stopped.Succeeded)
{
ShowActionResult(stopped, "Could not seal the active FAT session");
return;
}
}
var storage = Storage ?? throw new InvalidOperationException("FAT workspace persistence is not available.");
var result = IoFatCleanSessionService.ResetForRetest(storage, Project);
var sessionReset = Session.ResetForCleanRetest();
if (!sessionReset.Succeeded)
{
ShowActionResult(sessionReset, "Clean FAT session state could not be reset");
return;
}
PreparationStatusText = result.ArchivedJournalCount > 0
? $"NEW CLEAN FAT ready · {result.ResetPointCount} point(s) reset · {result.ArchivedJournalCount} prior journal(s) archived"
: $"NEW CLEAN FAT ready · {result.ResetPointCount} point(s) reset · active evidence is empty";
RefreshSupplementalEvidenceControls();
Raise(nameof(SelectedSupplementalEvidenceCount));
Raise(nameof(SelectedEvidenceCount));
RaiseStatusProperties();
RaiseSelectedIedContextProperties();
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or InvalidOperationException)
{
MessageBox.Show(
this,
ex.Message,
"New Clean FAT Session failed",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
private void OpenComtradeEvidence_Click(object sender, RoutedEventArgs e)
{
var ied = SelectedIed;
if (ied == null)
return;
var window = new FaultRecordWindow(ied.IedName, ied.IpAddress, 102)
{
Owner = this
};
window.ShowDialog();
// A successful FileDirectory browse that returns a supported fault record is the
// primary FAT evidence. Download is intentionally not a prerequisite.
var remoteEvidence = IoFatRemoteComtradeEvidenceService.CaptureLatest(
Storage,
Project,
ied,
window.Records.Select(row => row.Record));
// Preserve stronger optional local-artifact evidence when the operator also
// downloaded a record. A failed or skipped download never removes remote PASS.
var downloadedCaptured = 0;
foreach (var row in window.Records.Where(row =>
row.LocalState == FaultRecordLocalState.Downloaded &&
!string.IsNullOrWhiteSpace(row.LocalDirectory)))
{
var evidence = IoFatSupplementalEvidenceService.CaptureComtrade(
Storage,
Project,
ied,
row.RecordName,
row.LocalDirectory);
if (evidence != null)
downloadedCaptured++;
}
if (remoteEvidence != null)
{
PreparationStatusText =
$"{ied.IedName} · File Service PASS · latest COMTRADE {ied.LatestComtradeFiles} · download optional.";
Storage?.ScheduleSave();
}
else if (downloadedCaptured > 0)
{
PreparationStatusText = $"{ied.IedName} · downloaded COMTRADE/fault-record evidence journaled.";
Storage?.ScheduleSave();
}
RefreshSupplementalEvidenceControls();
Raise(nameof(SelectedSupplementalEvidenceCount));
Raise(nameof(SelectedEvidenceCount));
}
private static Iec61850MonitorDevice? FindFatDevice(MainWindow engineeringWindow, IoTestIedPlan ied)
=> engineeringWindow.Devices.FirstOrDefault(device =>
(device.Name.Equals(ied.IedName, StringComparison.OrdinalIgnoreCase) ||
device.SclIedName.Equals(ied.IedName, StringComparison.OrdinalIgnoreCase)) &&
device.IpAddress.Equals(ied.IpAddress, StringComparison.OrdinalIgnoreCase))
?? engineeringWindow.Devices.FirstOrDefault(device =>
device.IpAddress.Equals(ied.IpAddress, StringComparison.OrdinalIgnoreCase));
private static bool HasLiveValue(string? value)
{
var text = (value ?? string.Empty).Trim();
return text.Length > 0 && text != "-" && text != "—" &&
!text.Equals("Unknown", StringComparison.OrdinalIgnoreCase) &&
!text.Contains("not probed", StringComparison.OrdinalIgnoreCase);
}
}