-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIoListTestingWindow.ContextUx.cs
More file actions
341 lines (291 loc) · 12.9 KB
/
Copy pathIoListTestingWindow.ContextUx.cs
File metadata and controls
341 lines (291 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
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 && !IsPreparingIed && 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 (_preparingIed != null)
{
return ReferenceEquals(_preparingIed, SelectedIed)
? $"Connecting {SelectedIed.IedName}…"
: $"{_preparingIed.IedName} connecting…";
}
if (Session.IsSessionActive)
{
return IsSelectedSessionIed
? "IED session active"
: $"{Session.ActiveIed?.IedName ?? "Another IED"} active";
}
var enabled = EnabledPoints(SelectedIed);
if (enabled.Count > 0 && enabled.All(point => point.Runtime.State == IoTestPointState.Passed))
return "Reconnect / Retest";
return enabled.Any(point => point.Runtime.IsComplete)
? "Connect & Continue IED"
: "Connect & Start IED";
}
}
public string SelectedFooterStatusText
{
get
{
if (SelectedIed == null)
return "Select an imported IED.";
if (ReferenceEquals(_preparingIed, SelectedIed))
return 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";
}
private async void StartSelectedIedSafely_Click(object sender, RoutedEventArgs e)
{
if (IsPreparingIed)
return;
var selectedIed = SelectedIed;
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 Connect 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 =>
{
selectedIed.SetPreparationState(true, 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 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 (!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();
}