-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIoListTestingWindow.xaml.cs
More file actions
462 lines (412 loc) · 16.1 KB
/
Copy pathIoListTestingWindow.xaml.cs
File metadata and controls
462 lines (412 loc) · 16.1 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
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using ArIED61850Tester.Models.IoTesting;
using ArIED61850Tester.Services.IoTesting;
using Microsoft.Win32;
namespace ArIED61850Tester;
public partial class IoListTestingWindow : Window, INotifyPropertyChanged
{
private IoTestIedPlan? _selectedIed;
private IoTestIedPlan? _preparingIed;
private string _preparationStatusText = string.Empty;
public IoListTestingWindow()
: this(CreateEmptyProject(), CreateEmptyController(), null)
{
}
public IoListTestingWindow(
IoTestProject project,
IoTestSessionController session,
IoTestWorkspacePersistence? persistence)
{
Project = project ?? throw new ArgumentNullException(nameof(project));
Session = session ?? throw new ArgumentNullException(nameof(session));
Storage = persistence;
Project.InitializeRuntimeNotifications();
Session.PropertyChanged += Session_PropertyChanged;
InitializeComponent();
DataContext = this;
SelectedIed = Project.Ieds.FirstOrDefault();
}
public IoTestProject Project { get; }
public IoTestSessionController Session { get; }
public IoTestWorkspacePersistence? Storage { get; }
public IoTestIedPlan? SelectedIed
{
get => _selectedIed;
set
{
if (ReferenceEquals(_selectedIed, value))
return;
_selectedIed = value;
Raise();
Raise(nameof(SelectedIedSummary));
Raise(nameof(CanStartWorkflow));
}
}
public bool IsPreparingIed => _preparingIed != null;
public string PreparationStatusText
{
get => _preparationStatusText;
private set
{
var normalized = value?.Trim() ?? string.Empty;
if (_preparationStatusText == normalized)
return;
_preparationStatusText = normalized;
Raise();
Raise(nameof(FooterStatusText));
}
}
public Visibility PreparationVisibility => IsPreparingIed ? Visibility.Visible : Visibility.Collapsed;
public string PreparationIedText => _preparingIed == null ? string.Empty : $"Preparing {_preparingIed.IedName}";
public bool CanStartWorkflow =>
SelectedIed != null && !IsPreparingIed && Session.CanStart;
// Explorer navigation stays available while one IED is connecting or another FAT
// session is running. This is inspection-only; the active evidence scope remains
// pinned to Session.ActiveIed.
public bool CanSelectIed => true;
public bool CanEditPlan =>
!IsPreparingIed && Session.CanEditPlan;
public string StartWorkflowText =>
IsPreparingIed ? $"Connecting {_preparingIed!.IedName}…" : "Connect & Start IED";
public string ProjectSummary =>
$"{Project.Ieds.Count} IED · {Project.SignalCount} points · {Project.LiveBoundSignalCount} live";
public string SelectedIedSummary => SelectedIed == null
? "Select an imported IED"
: $"{SelectedIed.IpAddress} · {SelectedIed.EnabledCount} test points · {SelectedIed.LiveStatusText}";
public string FooterStatusText => IsPreparingIed
? PreparationStatusText
: Session.StatusText;
public event PropertyChangedEventHandler? PropertyChanged;
private async void StartSession_Click(object sender, RoutedEventArgs e)
{
if (IsPreparingIed)
return;
var selectedIed = SelectedIed;
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");
try
{
if (Owner is MainWindow engineeringWindow)
{
var progress = new Progress<string>(message =>
{
selectedIed.SetPreparationState(true, message);
PreparationStatusText = message;
RaiseStatusProperties();
});
var preparation = await engineeringWindow.PrepareIoTestIedForFatAsync(
Project,
selectedIed,
progress);
RaiseStatusProperties();
if (!preparation.Succeeded)
{
PreparationStatusText = preparation.Message;
ShowActionResult(preparation, "IED acquisition could not start");
return;
}
}
var result = Session.Start(selectedIed);
ShowActionResult(result, "FAT evidence session could not start");
RaiseStatusProperties();
if (result.Succeeded)
{
PreparationStatusText = $"{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
{
selectedIed.SetPreparationState(false, selectedIed.LiveStatusText);
SetPreparingIed(null, string.Empty);
}
}
private void PauseSession_Click(object sender, RoutedEventArgs e)
{
var result = Session.Pause();
ShowActionResult(result, "FAT session could not pause");
if (result.Succeeded)
Storage?.SaveNow();
}
private void ResumeSession_Click(object sender, RoutedEventArgs e)
{
var result = Session.Resume();
ShowActionResult(result, "FAT session could not resume");
if (result.Succeeded)
Storage?.ScheduleSave();
}
private void StopSession_Click(object sender, RoutedEventArgs e)
{
var result = Session.Stop();
ShowActionResult(result, "FAT session could not stop");
if (result.Succeeded)
Storage?.SaveNow();
}
private void SaveProgress_Click(object sender, RoutedEventArgs e)
{
try
{
Storage?.SaveNow();
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidOperationException)
{
MessageBox.Show(this, ex.Message, "Progress save failed", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private async void ExportExcel_Click(object sender, RoutedEventArgs e)
{
if (Storage == null)
return;
if (!EnsureSessionSealedForExport("Excel evidence workbook"))
return;
var dialog = new SaveFileDialog
{
Title = "Export ARSAS IO FAT result workbook",
Filter = "Excel workbook (*.xlsx)|*.xlsx",
FileName = $"{SafeFileName(Project.ProjectId)}_IO-FAT-Results_{DateTime.Now:yyyyMMdd_HHmm}.xlsx",
AddExtension = true,
DefaultExt = ".xlsx",
OverwritePrompt = true
};
if (dialog.ShowDialog(this) != true)
return;
try
{
IsEnabled = false;
Storage.SaveNow();
await IoFatExcelResultExportService.ExportAsync(
Storage.SourceWorkbookPath,
dialog.FileName,
Project);
MessageBox.Show(
this,
$"FAT result workbook created successfully.\n\n{dialog.FileName}\n\nThe approved source workbook was not modified.",
"Excel evidence exported",
MessageBoxButton.OK,
MessageBoxImage.Information);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or InvalidOperationException or ArgumentException)
{
MessageBox.Show(this, ex.Message, "Excel evidence export failed", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
IsEnabled = true;
}
}
private void ExportPdf_Click(object sender, RoutedEventArgs e)
{
if (!EnsureSessionSealedForExport("PDF evidence report"))
return;
var dialog = new SaveFileDialog
{
Title = "Export native ARSAS IO FAT PDF report",
Filter = "PDF evidence report (*.pdf)|*.pdf",
FileName = $"{SafeFileName(Project.ProjectId)}_IO-FAT_{DateTime.Now:yyyyMMdd_HHmm}.pdf",
AddExtension = true,
DefaultExt = ".pdf",
OverwritePrompt = true
};
if (dialog.ShowDialog(this) != true)
return;
try
{
IsEnabled = false;
Storage?.SaveNow();
IoFatPdfReportService.Save(dialog.FileName, Project);
MessageBox.Show(
this,
$"Native PDF evidence report created successfully.\n\n{dialog.FileName}",
"PDF report exported",
MessageBoxButton.OK,
MessageBoxImage.Information);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or InvalidOperationException or ArgumentException)
{
MessageBox.Show(this, ex.Message, "PDF report export failed", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
IsEnabled = true;
}
}
private async void ExportHandover_Click(object sender, RoutedEventArgs e)
{
if (Storage == null)
return;
if (!EnsureSessionSealedForExport("ARSAS project"))
return;
var dialog = new SaveFileDialog
{
Title = "Export portable ARSAS IO FAT project",
Filter = $"ARSAS project (*{IoFatProjectPackageService.PackageExtension})|*{IoFatProjectPackageService.PackageExtension}",
FileName = $"{SafeFileName(Project.ProjectId)}_{DateTime.Now:yyyyMMdd_HHmm}{IoFatProjectPackageService.PackageExtension}",
AddExtension = true,
DefaultExt = IoFatProjectPackageService.PackageExtension,
OverwritePrompt = true
};
if (dialog.ShowDialog(this) != true)
return;
try
{
IsEnabled = false;
var exportedPath = await IoFatProjectPackageService.ExportAsync(
Storage,
Session,
dialog.FileName);
MessageBox.Show(
this,
$"Portable ARSAS project created successfully.\n\n{exportedPath}\n\nOpen this .arsas file on another laptop to continue the remaining FAT scope. The package also contains the native PDF report and the completed Excel result workbook.",
"ARSAS project exported",
MessageBoxButton.OK,
MessageBoxImage.Information);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or InvalidOperationException or ArgumentException)
{
MessageBox.Show(this, ex.Message, "ARSAS project export failed", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
IsEnabled = true;
}
}
private bool EnsureSessionSealedForExport(string outputName)
{
if (!Session.IsSessionActive)
return true;
MessageBox.Show(
this,
$"Stop the active IED session before exporting the {outputName}. This seals and verifies the current evidence journal first.",
"Stop session before export",
MessageBoxButton.OK,
MessageBoxImage.Information);
return false;
}
private void ReturnToEngineering_Click(object sender, RoutedEventArgs e)
=> Close();
private void Window_Closing(object? sender, CancelEventArgs e)
{
if (IsPreparingIed)
{
MessageBox.Show(
this,
$"ARSAS is still preparing {_preparingIed!.IedName}. You can inspect other IEDs while it runs, but wait for acquisition setup to finish before closing this workspace.",
"IED preparation in progress",
MessageBoxButton.OK,
MessageBoxImage.Information);
e.Cancel = true;
return;
}
if (Session.IsSessionActive)
{
var answer = MessageBox.Show(
this,
"A FAT session is active. Returning to Engineering will stop the session, seal the evidence journal, and save the current project progress.\n\nStop the session and return?",
"Stop active FAT session",
MessageBoxButton.YesNo,
MessageBoxImage.Warning,
MessageBoxResult.No);
if (answer != MessageBoxResult.Yes)
{
e.Cancel = true;
return;
}
Session.Stop("Workspace closed by operator; evidence journal sealed.");
}
try
{
Storage?.SaveNow();
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidOperationException)
{
var answer = MessageBox.Show(
this,
$"ARSAS could not save the latest IO FAT progress.\n\n{ex.Message}\n\nClose the workspace anyway?",
"Progress save failed",
MessageBoxButton.YesNo,
MessageBoxImage.Error,
MessageBoxResult.No);
if (answer != MessageBoxResult.Yes)
e.Cancel = true;
}
}
protected override void OnClosed(EventArgs e)
{
Session.PropertyChanged -= Session_PropertyChanged;
base.OnClosed(e);
}
private void Session_PropertyChanged(object? sender, PropertyChangedEventArgs e)
=> RaiseStatusProperties();
private void SetPreparingIed(IoTestIedPlan? ied, string status)
{
_preparingIed = ied;
PreparationStatusText = status;
Raise(nameof(IsPreparingIed));
Raise(nameof(PreparationVisibility));
Raise(nameof(PreparationIedText));
Raise(nameof(CanStartWorkflow));
Raise(nameof(CanSelectIed));
Raise(nameof(CanEditPlan));
Raise(nameof(StartWorkflowText));
Raise(nameof(FooterStatusText));
}
private void RaiseStatusProperties()
{
Raise(nameof(CanStartWorkflow));
Raise(nameof(CanSelectIed));
Raise(nameof(CanEditPlan));
Raise(nameof(ProjectSummary));
Raise(nameof(SelectedIedSummary));
Raise(nameof(FooterStatusText));
}
private void ShowActionResult(IoTestSessionActionResult result, string title)
{
if (result.Succeeded)
return;
MessageBox.Show(this, result.Message, title, MessageBoxButton.OK, MessageBoxImage.Warning);
}
private static string SafeFileName(string value)
{
var invalid = Path.GetInvalidFileNameChars().ToHashSet();
var result = new string((value ?? "IO-FAT").Select(ch => invalid.Contains(ch) ? '_' : ch).ToArray()).Trim();
return result.Length == 0 ? "IO-FAT" : result;
}
private void Raise([CallerMemberName] string? propertyName = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
private static IoTestProject CreateEmptyProject() => new()
{
ProjectId = "EMPTY",
SchemaVersion = "ARSAS-FAT-IO-1.0",
ProjectName = "No IO List project loaded"
};
private static IoTestSessionController CreateEmptyController()
{
var project = CreateEmptyProject();
return new IoTestSessionController(
project,
_ => null,
action => action(),
Path.Combine(Path.GetTempPath(), "ARSAS", "IO Testing Preview"));
}
}