-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathIoListTestingWindow.P0RuntimeActions.cs
More file actions
316 lines (283 loc) · 11.7 KB
/
Copy pathIoListTestingWindow.P0RuntimeActions.cs
File metadata and controls
316 lines (283 loc) · 11.7 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
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Threading;
using ArIED61850Tester.Models.IoTesting;
using ArIED61850Tester.Services.IoTesting;
namespace ArIED61850Tester;
/// <summary>
/// P0 operator-action responsiveness for the FAT bench.
///
/// Start/Continue paints a local busy state before the existing safe preparation workflow
/// begins. Resume can emit one evidence record per active point; production journal writes
/// remain ordered and hash-chained, but their visible StreamWriter flush is coalesced into
/// one flush for the complete rebaseline transaction. Stop detaches the evidence journal on
/// the Dispatcher and performs the expensive durable disk barrier + full read-back on a
/// worker. Only the pressed button is muted while work is in flight; the DataGrid, search,
/// IED explorer and window chrome remain interactive.
/// </summary>
public partial class IoListTestingWindow
{
private static readonly bool P0RuntimeActionsRegistered = RegisterP0RuntimeActions();
private bool _p0RuntimeActionsInstalled;
private bool _p0StartInProgress;
private bool _p0ResumeInProgress;
private bool _p0StopInProgress;
private Button? _p0StartButton;
private Button? _p0ResumeButton;
private Button? _p0StopButton;
private static bool RegisterP0RuntimeActions()
{
EventManager.RegisterClassHandler(
typeof(IoListTestingWindow),
FrameworkElement.LoadedEvent,
new RoutedEventHandler(P0RuntimeActions_Loaded));
return true;
}
private static void P0RuntimeActions_Loaded(object sender, RoutedEventArgs e)
{
if (sender is not IoListTestingWindow window || window._p0RuntimeActionsInstalled)
return;
window._p0RuntimeActionsInstalled = true;
window.Dispatcher.BeginInvoke(
new Action(window.InstallP0RuntimeActionHandlers),
DispatcherPriority.Loaded);
}
private void InstallP0RuntimeActionHandlers()
{
_p0StartButton = FindButtonByContentBindingPath(this, nameof(SelectedStartWorkflowText));
if (_p0StartButton != null)
{
// Preserve the existing safe Start/Continue workflow, but insert one rendered
// frame before it begins. This avoids the Windows "not responding" impression
// even if the first preparation phase has synchronous setup before its await.
_p0StartButton.Click -= StartSelectedIedSafely_Click;
_p0StartButton.Click += P0StartSelectedIedSafely_Click;
}
_p0ResumeButton = FindButtonByContent(this, "Resume");
if (_p0ResumeButton != null)
{
// XAML attached the legacy synchronous handler during InitializeComponent.
// Replace only this edge and leave Session/controller ownership unchanged.
_p0ResumeButton.Click -= ResumeSession_Click;
_p0ResumeButton.Click += P0ResumeSession_Click;
}
_p0StopButton = FindButtonByContent(this, "Stop");
if (_p0StopButton != null)
{
_p0StopButton.Click -= StopSession_Click;
_p0StopButton.Click += P0StopSession_Click;
}
Closed += P0RuntimeActions_Closed;
}
private void P0RuntimeActions_Closed(object? sender, EventArgs e)
{
Closed -= P0RuntimeActions_Closed;
if (_p0StartButton != null)
{
_p0StartButton.Click -= P0StartSelectedIedSafely_Click;
_p0StartButton = null;
}
if (_p0ResumeButton != null)
{
_p0ResumeButton.Click -= P0ResumeSession_Click;
_p0ResumeButton = null;
}
if (_p0StopButton != null)
{
_p0StopButton.Click -= P0StopSession_Click;
_p0StopButton = null;
}
}
private async void P0StartSelectedIedSafely_Click(object sender, RoutedEventArgs e)
{
if (_p0StartInProgress || sender is not Button button)
return;
var targetIed = SelectedIed;
_p0StartInProgress = true;
var originalOpacity = button.Opacity;
button.IsHitTestVisible = false;
button.Opacity = 0.68;
// Content stays bound to SelectedStartWorkflowText. SetPreparingIed in the existing
// workflow therefore changes the same button naturally to "Connecting …" without
// replacing/breaking its binding.
await Dispatcher.Yield(DispatcherPriority.Render);
var stopwatch = Stopwatch.StartNew();
try
{
StartSelectedIedSafely_Click(sender, e);
await WaitForP0StartWorkflowCompletionAsync(targetIed);
Trace.WriteLine(
$"[IO FAT P0] Start/Continue workflow completed in {stopwatch.ElapsedMilliseconds} ms; " +
$"ied={targetIed?.IedName ?? "<none>"}; state={Session.State}; active={Session.IsSessionActive}.");
}
finally
{
button.Opacity = originalOpacity;
button.IsHitTestVisible = true;
_p0StartInProgress = false;
RaiseSelectedIedContextProperties();
}
}
private async Task WaitForP0StartWorkflowCompletionAsync(IoTestIedPlan? targetIed)
{
// The legacy handler is async void. Yield once so it can enter SetPreparingIed and
// reach its first asynchronous acquisition await. If preflight returned early there
// is nothing to wait for.
await Dispatcher.Yield(DispatcherPriority.Background);
if (targetIed == null || !targetIed.IsPreparing)
return;
var completion = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
PropertyChangedEventHandler? handler = null;
handler = (_, args) =>
{
if (args.PropertyName == nameof(IoTestIedPlan.IsPreparing) && !targetIed.IsPreparing)
completion.TrySetResult(true);
};
targetIed.PropertyChanged += handler;
try
{
if (!targetIed.IsPreparing)
return;
await completion.Task;
}
finally
{
targetIed.PropertyChanged -= handler;
}
}
private async void P0ResumeSession_Click(object sender, RoutedEventArgs e)
{
if (_p0ResumeInProgress || sender is not Button button)
return;
_p0ResumeInProgress = true;
var originalContent = button.Content;
var originalOpacity = button.Opacity;
button.Content = "Continuing…";
button.IsHitTestVisible = false;
button.Opacity = 0.68;
// Paint the busy state before any controller/project PropertyChanged burst starts.
await Dispatcher.Yield(DispatcherPriority.Render);
var stopwatch = Stopwatch.StartNew();
try
{
IoTestSessionActionResult result;
using (IoTestEvidenceJournal.BeginCoalescedVisibleFlushScope())
result = Session.Resume();
ShowActionResult(result, "FAT session could not resume");
if (result.Succeeded)
Storage?.ScheduleSave();
RaiseStatusProperties();
RaiseSelectedIedContextProperties();
Trace.WriteLine($"[IO FAT P0] Resume completed in {stopwatch.ElapsedMilliseconds} ms; succeeded={result.Succeeded}.");
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidOperationException)
{
Trace.WriteLine($"[IO FAT P0] Resume failed after {stopwatch.ElapsedMilliseconds} ms: {ex}");
MessageBox.Show(
this,
ex.Message,
"FAT session could not resume",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
finally
{
button.Content = originalContent;
button.Opacity = originalOpacity;
button.IsHitTestVisible = true;
_p0ResumeInProgress = false;
RaiseSelectedIedContextProperties();
}
}
private async void P0StopSession_Click(object sender, RoutedEventArgs e)
{
if (_p0StopInProgress || sender is not Button button)
return;
_p0StopInProgress = true;
var originalContent = button.Content;
var originalOpacity = button.Opacity;
button.Content = "Stopping…";
button.IsHitTestVisible = false;
button.Opacity = 0.68;
// Paint immediately. Do not disable the Window: scrolling and inspection remain
// available while the detached evidence file is being durably sealed/read back.
await Dispatcher.Yield(DispatcherPriority.Render);
var stopwatch = Stopwatch.StartNew();
try
{
IoTestSessionActionResult result;
using (IoTestEvidenceJournal.BeginDeferredSealScope())
result = Session.Stop();
ShowActionResult(result, "FAT session could not stop");
if (result.Succeeded)
{
// Stop() has already made the controller/session state immutable. Await the
// physical disk barrier and complete hash-chain read-back off Dispatcher.
await IoTestEvidenceJournal.AwaitDeferredSealsAsync();
if (Storage != null)
await Task.Run(Storage.SaveNow);
}
RaiseStatusProperties();
RaiseSelectedIedContextProperties();
Trace.WriteLine($"[IO FAT P0] Stop completed in {stopwatch.ElapsedMilliseconds} ms; succeeded={result.Succeeded}.");
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidOperationException)
{
Trace.WriteLine($"[IO FAT P0] Stop failed after {stopwatch.ElapsedMilliseconds} ms: {ex}");
MessageBox.Show(
this,
ex.Message,
"FAT evidence could not be sealed",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
finally
{
button.Content = originalContent;
button.Opacity = originalOpacity;
button.IsHitTestVisible = true;
_p0StopInProgress = false;
RaiseSelectedIedContextProperties();
}
}
private static Button? FindButtonByContentBindingPath(DependencyObject root, string path)
{
var childCount = VisualTreeHelper.GetChildrenCount(root);
for (var index = 0; index < childCount; index++)
{
var child = VisualTreeHelper.GetChild(root, index);
if (child is Button button)
{
var binding = BindingOperations.GetBinding(button, ContentControl.ContentProperty);
if (string.Equals(binding?.Path?.Path, path, StringComparison.Ordinal))
return button;
}
var nested = FindButtonByContentBindingPath(child, path);
if (nested != null)
return nested;
}
return null;
}
private static Button? FindButtonByContent(DependencyObject root, string content)
{
var childCount = VisualTreeHelper.GetChildrenCount(root);
for (var index = 0; index < childCount; index++)
{
var child = VisualTreeHelper.GetChild(root, index);
if (child is Button button &&
string.Equals(button.Content?.ToString(), content, StringComparison.Ordinal))
{
return button;
}
var nested = FindButtonByContent(child, content);
if (nested != null)
return nested;
}
return null;
}
}