-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIoListTestingWindow.ContextUx.cs
More file actions
445 lines (383 loc) · 17 KB
/
Copy pathIoListTestingWindow.ContextUx.cs
File metadata and controls
445 lines (383 loc) · 17 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
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Threading;
using ArIED61850Tester.Models.IoTesting;
using ArIED61850Tester.Services.IoTesting;
namespace ArIED61850Tester;
public partial class IoListTestingWindow
{
private bool _selectedIedContextInstalled;
public bool SelectedCanStartWorkflow =>
SelectedIed != null && !SelectedIed.IsPreparing && Session.CanStart;
public bool SelectedCanPause =>
IsSelectedSessionIed && Session.CanPause;
public bool SelectedCanResume =>
IsSelectedSessionIed && Session.CanResume;
public bool SelectedCanStop =>
IsSelectedSessionIed && Session.CanStop;
public string SelectedStartWorkflowText
{
get
{
if (SelectedIed == null)
return "Select IED";
if (SelectedIed.IsPreparing)
return $"Connecting {SelectedIed.IedName}…";
if (Session.IsSessionActive)
{
return IsSelectedSessionIed
? "IED session active"
: $"{Session.ActiveIed?.IedName ?? "Another IED"} FAT active";
}
var enabled = EnabledPoints(SelectedIed);
var allPassed = enabled.Count > 0 && enabled.All(point => point.Runtime.State == IoTestPointState.Passed);
var hasCompleted = enabled.Any(point => point.Runtime.IsComplete);
if (SelectedIed.IsLiveMonitoring)
{
if (allPassed)
return "Retest FAT";
return hasCompleted ? "Continue FAT" : "Start FAT";
}
if (allPassed)
return "Reconnect / Retest";
return hasCompleted ? "Connect & Continue IED" : "Connect & Start IED";
}
}
public string SelectedFooterStatusText
{
get
{
if (SelectedIed == null)
return "Select an imported IED.";
if (SelectedIed.IsPreparing)
return SelectedIed.PreparationStatusText;
if (IsSelectedSessionIed && Session.State != IoTestSessionState.Idle)
return Session.StatusText;
var enabled = EnabledPoints(SelectedIed);
var complete = enabled.Count(point => point.Runtime.IsComplete);
var passed = enabled.Count(point => point.Runtime.State == IoTestPointState.Passed);
if (enabled.Count > 0 && passed == enabled.Count)
return $"{SelectedIed.IedName} complete · all {enabled.Count} enabled points PASS · evidence protected.";
if (complete > 0)
return $"{SelectedIed.IedName} selected · {complete}/{enabled.Count} complete · completed evidence will be preserved.";
return $"{SelectedIed.IedName} selected · {SelectedIed.LiveStatusText}";
}
}
public string SelectedProgressText
{
get
{
if (SelectedIed == null)
return "0 / 0 complete";
var enabled = EnabledPoints(SelectedIed);
var complete = enabled.Count(point => point.Runtime.IsComplete);
var passed = enabled.Count(point => point.Runtime.State == IoTestPointState.Passed);
var review = enabled.Count(point => point.Runtime.State == IoTestPointState.Review);
var failed = enabled.Count(point => point.Runtime.State == IoTestPointState.Failed);
return $"{complete} / {enabled.Count} complete · {passed} PASS · {review} review · {failed} fail";
}
}
public int SelectedEvidenceCount =>
(SelectedIed?.TestPoints.Sum(point =>
(point.Runtime.OnEvidence == null ? 0 : 1) +
(point.Runtime.OffEvidence == null ? 0 : 1)) ?? 0) +
SelectedSupplementalEvidenceCount;
private bool IsSelectedSessionIed =>
SelectedIed != null && ReferenceEquals(Session.ActiveIed, SelectedIed);
private void IoListTestingWindow_ContentRendered(object? sender, EventArgs e)
{
Dispatcher.BeginInvoke(
new Action(() =>
{
InstallSelectedIedContext();
InstallSupplementalEvidenceControls();
RefreshSupplementalEvidenceControls();
}),
DispatcherPriority.ContextIdle);
}
private void InstallSelectedIedContext()
{
AdoptWorkspacePreviewToggle();
if (_selectedIedContextInstalled)
{
RaiseSelectedIedContextProperties();
return;
}
foreach (var ied in Project.Ieds)
ied.PropertyChanged += ContextIed_PropertyChanged;
PropertyChanged += ContextWindow_PropertyChanged;
Session.PropertyChanged += ContextSession_PropertyChanged;
Closed += ContextWindow_Closed;
_selectedIedContextInstalled = true;
RaiseSelectedIedContextProperties();
}
private void AdoptWorkspacePreviewToggle()
{
if (_printPreviewToggle != null && !ReferenceEquals(_printPreviewToggle, WorkspacePreviewToggle))
{
if (LogicalTreeHelper.GetParent(_printPreviewToggle) is Panel parent)
parent.Children.Remove(_printPreviewToggle);
}
_printPreviewToggle = WorkspacePreviewToggle;
_printPreviewToggle.Content = _printPreviewActive ? "Signals View" : "Print Preview";
_printPreviewToggle.ToolTip = "Toggle the selected IED between signal evidence and native print preview";
}
/// <summary>
/// Card-local connection action. Several different IED cards can run this method at
/// once; each device owns its own MainWindow connection workflow and model progress.
/// Evidence capture remains a separate, single-active session selected by Start FAT.
/// </summary>
private async void ConnectIed_Click(object sender, RoutedEventArgs e)
{
var targetIed = (sender as FrameworkElement)?.DataContext as IoTestIedPlan;
if (targetIed == null || targetIed.IsPreparing)
return;
var enabledReady = targetIed.TestPoints
.Where(point => point.TestEnabled && point.ImportReady)
.ToList();
if (enabledReady.Count == 0)
{
ShowActionResult(
IoTestSessionActionResult.Failure("No import-ready IO-list signal is enabled for this IED."),
"IED connection scope is not ready");
return;
}
// For a continuation, connect only what still needs evidence. Completed rows keep
// their sealed evidence and do not have to exist in a replacement relay model.
// If every row is already complete, refresh the complete enabled scope instead.
var pendingScope = enabledReady.Where(point => !point.Runtime.IsComplete).ToList();
IReadOnlyCollection<IoTestPointPlan> connectionScope = pendingScope.Count > 0
? pendingScope
: enabledReady;
if (ReferenceEquals(SelectedIed, targetIed))
PreparationStatusText = $"Connecting {targetIed.IedName} · {targetIed.IpAddress}:102";
RaisePreparationProperties();
RaiseSelectedIedContextProperties();
try
{
if (Owner is not MainWindow engineeringWindow)
return;
var progress = new Progress<string>(message =>
{
if (ReferenceEquals(SelectedIed, targetIed))
PreparationStatusText = message;
RaiseStatusProperties();
RaiseSelectedIedContextProperties();
});
var preparation = await PrepareIndependentIedConnectionAsync(
engineeringWindow,
targetIed,
progress,
connectionScope);
RaiseStatusProperties();
RaiseSelectedIedContextProperties();
if (!preparation.Succeeded)
{
if (ReferenceEquals(SelectedIed, targetIed))
PreparationStatusText = preparation.Message;
ShowActionResult(preparation, $"{targetIed.IedName} acquisition could not start");
return;
}
await CaptureTimeSyncEvidenceAfterPreparationAsync(engineeringWindow, targetIed);
if (ReferenceEquals(SelectedIed, targetIed))
PreparationStatusText = $"{targetIed.IedName} live · ready for FAT evidence";
Storage?.ScheduleSave();
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidOperationException or ArgumentException)
{
if (ReferenceEquals(SelectedIed, targetIed))
PreparationStatusText = ex.Message;
MessageBox.Show(
this,
ex.Message,
$"Connect {targetIed.IedName} failed",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
finally
{
targetIed.SetPreparationState(false, targetIed.LiveStatusText);
RaisePreparationProperties();
RaiseSelectedIedContextProperties();
}
}
private async void StartSelectedIedSafely_Click(object sender, RoutedEventArgs e)
{
var selectedIed = SelectedIed;
if (selectedIed?.IsPreparing == true)
return;
if (selectedIed == null)
{
var missingSelection = IoTestSessionPreflight.Validate(null);
ShowActionResult(missingSelection, "FAT session scope is not ready");
return;
}
var enabledReady = selectedIed.TestPoints
.Where(point => point.TestEnabled && point.ImportReady)
.ToList();
var completedPoints = enabledReady
.Where(point => point.Runtime.IsComplete)
.ToList();
var incompletePoints = enabledReady
.Where(point => !point.Runtime.IsComplete)
.ToList();
var explicitRetest = false;
if (completedPoints.Count > 0 && incompletePoints.Count == 0)
{
var answer = MessageBox.Show(
this,
$"All {completedPoints.Count} enabled rows for {selectedIed.IedName} already contain completed evidence.\n\nA normal Start FAT click will not erase them. Choose Yes only when you intentionally want to retest every completed row and replace its ON/OFF evidence.",
"Retest completed evidence?",
MessageBoxButton.YesNo,
MessageBoxImage.Warning,
MessageBoxResult.No);
if (answer != MessageBoxResult.Yes)
{
PreparationStatusText = $"{selectedIed.IedName} evidence preserved · no retest started.";
RaiseSelectedIedContextProperties();
return;
}
explicitRetest = true;
}
var protectedPoints = explicitRetest
? Array.Empty<IoTestPointPlan>()
: completedPoints.ToArray();
// Completed evidence is outside the continuation scope from the first preflight
// through live-model preparation and Session.Start. This prevents a completed row
// that has since disappeared from the relay model from blocking otherwise-valid
// pending rows. The original TestEnabled flags are restored in the outer finally.
foreach (var point in protectedPoints)
point.TestEnabled = false;
try
{
var preflight = IoTestSessionPreflight.Validate(selectedIed);
if (!preflight.Succeeded)
{
ShowActionResult(preflight, "FAT session scope is not ready");
return;
}
SetPreparingIed(selectedIed, $"Connecting {selectedIed.IedName} · {selectedIed.IpAddress}:102");
if (Owner is MainWindow engineeringWindow)
{
var progress = new Progress<string>(message =>
{
PreparationStatusText = message;
RaiseStatusProperties();
RaiseSelectedIedContextProperties();
});
var preparation = await engineeringWindow.PrepareIoTestIedForFatAsync(
Project,
selectedIed,
progress);
RaiseStatusProperties();
RaiseSelectedIedContextProperties();
if (!preparation.Succeeded)
{
PreparationStatusText = preparation.Message;
ShowActionResult(preparation, "IED acquisition could not start");
return;
}
await CaptureTimeSyncEvidenceAfterPreparationAsync(engineeringWindow, selectedIed);
}
var result = Session.Start(selectedIed);
ShowActionResult(result, "FAT evidence session could not start");
RaiseStatusProperties();
RaiseSelectedIedContextProperties();
if (result.Succeeded)
{
PreparationStatusText = protectedPoints.Length > 0
? $"{selectedIed.IedName} live · {protectedPoints.Length} completed row(s) preserved · waiting for pending OFF → ON → OFF tests"
: $"{selectedIed.IedName} live · waiting for OFF → ON → OFF";
Storage?.ScheduleSave();
}
else
{
PreparationStatusText = result.Message;
}
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidOperationException or ArgumentException)
{
PreparationStatusText = ex.Message;
MessageBox.Show(
this,
ex.Message,
"Connect and start IED failed",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
finally
{
foreach (var point in protectedPoints)
point.TestEnabled = true;
selectedIed.SetPreparationState(false, selectedIed.LiveStatusText);
SetPreparingIed(null, string.Empty);
RaiseSelectedIedContextProperties();
}
}
private Task<IoTestSessionActionResult> PrepareIndependentIedConnectionAsync(
MainWindow engineeringWindow,
IoTestIedPlan targetIed,
IProgress<string> progress,
IReadOnlyCollection<IoTestPointPlan> connectionScope)
=> engineeringWindow.PrepareIoTestIedForFatAsync(
Project,
targetIed,
progress,
connectionScope);
private void ContextWindow_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(SelectedIed) or nameof(IsPreparingIed) or nameof(PreparationStatusText))
RaiseSelectedIedContextProperties();
}
private void ContextSession_PropertyChanged(object? sender, PropertyChangedEventArgs e)
=> RaiseSelectedIedContextProperties();
private void ContextIed_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(IoTestIedPlan.IsPreparing))
RaisePreparationProperties();
if (!ReferenceEquals(sender, SelectedIed))
return;
Raise(nameof(SelectedIedSummary));
RaiseSelectedIedContextProperties();
}
private void RaiseSelectedIedContextProperties()
{
Raise(nameof(SelectedCanStartWorkflow));
Raise(nameof(SelectedCanPause));
Raise(nameof(SelectedCanResume));
Raise(nameof(SelectedCanStop));
Raise(nameof(SelectedStartWorkflowText));
Raise(nameof(SelectedFooterStatusText));
Raise(nameof(SelectedProgressText));
Raise(nameof(SelectedSupplementalEvidenceCount));
Raise(nameof(SelectedEvidenceCount));
RefreshSupplementalEvidenceControls();
}
private void ContextWindow_Closed(object? sender, EventArgs e)
{
foreach (var ied in Project.Ieds)
ied.PropertyChanged -= ContextIed_PropertyChanged;
PropertyChanged -= ContextWindow_PropertyChanged;
Session.PropertyChanged -= ContextSession_PropertyChanged;
Closed -= ContextWindow_Closed;
}
private static List<IoTestPointPlan> EnabledPoints(IoTestIedPlan ied)
=> ied.TestPoints.Where(point => point.TestEnabled).ToList();
}
public sealed class IoFatAllPassedVisibilityConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var selectedCount = values.Length > 0 && values[0] is int count ? count : 0;
var passed = values.Length > 1 && values[1] is int passedCount ? passedCount : 0;
var allPassed = selectedCount > 0 && passed >= selectedCount;
if (string.Equals(parameter?.ToString(), "Inverse", StringComparison.OrdinalIgnoreCase))
allPassed = !allPassed;
return allPassed ? Visibility.Visible : Visibility.Collapsed;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
=> targetTypes.Select(_ => Binding.DoNothing).ToArray();
}