-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFaultRecordWindow.RedownloadUx.cs
More file actions
604 lines (527 loc) · 21.9 KB
/
Copy pathFaultRecordWindow.RedownloadUx.cs
File metadata and controls
604 lines (527 loc) · 21.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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using AR.Iec61850.FaultRecords;
namespace ArIED61850Tester;
/// <summary>
/// Adds an explicit re-download workflow without weakening the persistent local-state
/// indication. A green row remains selectable, the fresh package is downloaded into a
/// separate complete directory first, and only then replaces the known-good local copy.
/// </summary>
public partial class FaultRecordWindow
{
private readonly HashSet<string> _redownloadSelections = new(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<FaultRecordRow, PropertyChangedEventHandler> _redownloadRowHandlers = new();
private Button? _smartDownloadButton;
private TextBlock? _smartSelectionSummary;
private bool _redownloadUxInstalled;
private bool _smartDownloadRunning;
private void InstallRedownloadUx()
{
if (_redownloadUxInstalled)
return;
_redownloadUxInstalled = true;
// Toasts are feedback for the whole workflow, not a header control. Keeping them
// centered prevents them from covering Scan fault records in the top-right corner.
ToastHost.HorizontalAlignment = HorizontalAlignment.Center;
ToastHost.VerticalAlignment = VerticalAlignment.Center;
ToastHost.Margin = new Thickness(0);
FaultRecordsGrid.LoadingRow += RedownloadUx_LoadingRow;
Records.CollectionChanged += RedownloadUx_RecordsChanged;
PropertyChanged += RedownloadUx_WindowPropertyChanged;
Closed += RedownloadUx_Closed;
foreach (var row in Records)
AttachRedownloadRow(row);
Dispatcher.BeginInvoke(
DispatcherPriority.Loaded,
new Action(() =>
{
ResolveSmartDownloadControls();
ConfigureVisibleRecordRows();
UpdateSmartSelectionUi();
}));
}
private void RedownloadUx_Closed(object? sender, EventArgs e)
{
FaultRecordsGrid.LoadingRow -= RedownloadUx_LoadingRow;
Records.CollectionChanged -= RedownloadUx_RecordsChanged;
PropertyChanged -= RedownloadUx_WindowPropertyChanged;
if (_smartDownloadButton != null)
{
_smartDownloadButton.PreviewMouseLeftButtonDown -= SmartDownloadButton_PreviewMouseLeftButtonDown;
_smartDownloadButton.PreviewKeyDown -= SmartDownloadButton_PreviewKeyDown;
}
foreach (var pair in _redownloadRowHandlers)
pair.Key.PropertyChanged -= pair.Value;
_redownloadRowHandlers.Clear();
}
private void RedownloadUx_WindowPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(IsBusy) or nameof(CanDownload) or nameof(SelectionSummary))
UpdateSmartSelectionUi();
}
private void RedownloadUx_RecordsChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.OldItems != null)
{
foreach (var row in e.OldItems.OfType<FaultRecordRow>())
DetachRedownloadRow(row);
}
if (e.NewItems != null)
{
foreach (var row in e.NewItems.OfType<FaultRecordRow>())
AttachRedownloadRow(row);
}
if (e.Action == NotifyCollectionChangedAction.Reset)
{
foreach (var pair in _redownloadRowHandlers.ToArray())
DetachRedownloadRow(pair.Key);
foreach (var row in Records)
AttachRedownloadRow(row);
}
var validIds = Records
.Select(row => row.Record.RecordId)
.ToHashSet(StringComparer.OrdinalIgnoreCase);
_redownloadSelections.RemoveWhere(id => !validIds.Contains(id));
Dispatcher.BeginInvoke(
DispatcherPriority.Loaded,
new Action(() =>
{
ResolveSmartDownloadControls();
ConfigureVisibleRecordRows();
UpdateSmartSelectionUi();
}));
}
private void AttachRedownloadRow(FaultRecordRow row)
{
if (_redownloadRowHandlers.ContainsKey(row))
return;
PropertyChangedEventHandler handler = (_, e) =>
{
if (e.PropertyName is nameof(FaultRecordRow.IsSelected) or
nameof(FaultRecordRow.LocalState) or
nameof(FaultRecordRow.Status) or
nameof(FaultRecordRow.CanSelectForDownload))
{
Dispatcher.BeginInvoke(
DispatcherPriority.DataBind,
new Action(() =>
{
ConfigureRecordRow(row);
UpdateSmartSelectionUi();
}));
}
};
_redownloadRowHandlers[row] = handler;
row.PropertyChanged += handler;
}
private void DetachRedownloadRow(FaultRecordRow row)
{
if (!_redownloadRowHandlers.Remove(row, out var handler))
return;
row.PropertyChanged -= handler;
_redownloadSelections.Remove(row.Record.RecordId);
}
private void RedownloadUx_LoadingRow(object? sender, DataGridRowEventArgs e)
{
Dispatcher.BeginInvoke(
DispatcherPriority.Loaded,
new Action(() => ConfigureDataGridRow(e.Row)));
}
private void ConfigureVisibleRecordRows()
{
FaultRecordsGrid.UpdateLayout();
foreach (var row in Records)
ConfigureRecordRow(row);
}
private void ConfigureRecordRow(FaultRecordRow row)
{
if (FaultRecordsGrid.ItemContainerGenerator.ContainerFromItem(row) is DataGridRow gridRow)
ConfigureDataGridRow(gridRow);
}
private void ConfigureDataGridRow(DataGridRow gridRow)
{
if (gridRow.DataContext is not FaultRecordRow row)
return;
var checkBox = FindVisualDescendants<CheckBox>(gridRow).FirstOrDefault();
if (checkBox == null)
return;
checkBox.Click -= DownloadedRecordCheckBox_Click;
if (row.LocalState == FaultRecordLocalState.Downloaded)
{
BindingOperations.ClearBinding(checkBox, ToggleButton.IsCheckedProperty);
BindingOperations.ClearBinding(checkBox, UIElement.IsEnabledProperty);
checkBox.IsEnabled = row.Record.Files.Count > 0;
checkBox.IsChecked = _redownloadSelections.Contains(row.Record.RecordId);
checkBox.ToolTip = "Already downloaded. Select to download again and overwrite the local copy.";
checkBox.Click += DownloadedRecordCheckBox_Click;
return;
}
BindingOperations.SetBinding(
checkBox,
ToggleButton.IsCheckedProperty,
new Binding(nameof(FaultRecordRow.IsSelected))
{
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
BindingOperations.SetBinding(
checkBox,
UIElement.IsEnabledProperty,
new Binding(nameof(FaultRecordRow.CanSelectForDownload))
{
Mode = BindingMode.OneWay
});
checkBox.ToolTip = null;
}
private void DownloadedRecordCheckBox_Click(object sender, RoutedEventArgs e)
{
if (sender is not CheckBox checkBox || checkBox.DataContext is not FaultRecordRow row)
return;
if (checkBox.IsChecked == true)
_redownloadSelections.Add(row.Record.RecordId);
else
_redownloadSelections.Remove(row.Record.RecordId);
UpdateSmartSelectionUi();
}
private void ResolveSmartDownloadControls()
{
if (_smartDownloadButton == null)
{
_smartDownloadButton = FindVisualDescendants<Button>(this)
.FirstOrDefault(button =>
string.Equals(button.Content?.ToString(), "Download selected", StringComparison.Ordinal));
if (_smartDownloadButton != null)
{
BindingOperations.ClearBinding(_smartDownloadButton, UIElement.IsEnabledProperty);
_smartDownloadButton.PreviewMouseLeftButtonDown += SmartDownloadButton_PreviewMouseLeftButtonDown;
_smartDownloadButton.PreviewKeyDown += SmartDownloadButton_PreviewKeyDown;
}
}
if (_smartSelectionSummary == null)
{
_smartSelectionSummary = FindVisualDescendants<TextBlock>(this)
.FirstOrDefault(textBlock =>
{
var expression = BindingOperations.GetBindingExpression(textBlock, TextBlock.TextProperty);
return string.Equals(
expression?.ParentBinding.Path?.Path,
nameof(SelectionSummary),
StringComparison.Ordinal);
});
if (_smartSelectionSummary != null)
BindingOperations.ClearBinding(_smartSelectionSummary, TextBlock.TextProperty);
}
}
private void SmartDownloadButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton != MouseButton.Left)
return;
e.Handled = true;
StartSmartDownload();
}
private void SmartDownloadButton_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key is not (Key.Enter or Key.Space))
return;
e.Handled = true;
StartSmartDownload();
}
private async void StartSmartDownload()
{
if (_smartDownloadRunning || IsBusy)
return;
_smartDownloadRunning = true;
try
{
await RunSmartDownloadAsync().ConfigureAwait(true);
}
finally
{
_smartDownloadRunning = false;
UpdateSmartSelectionUi();
}
}
private async Task RunSmartDownloadAsync()
{
var selected = Records
.Where(row =>
row.Record.Files.Count > 0 &&
(row.IsSelected || _redownloadSelections.Contains(row.Record.RecordId)))
.ToArray();
if (selected.Length == 0)
{
StatusText = "Select at least one available fault record.";
ShowToast("Select a record first.", ToastKind.Information);
return;
}
if (string.IsNullOrWhiteSpace(DestinationDirectory))
{
StatusText = "Choose a local destination directory before downloading.";
ShowToast("Choose a download folder first.", ToastKind.Information);
return;
}
ResetOperationCancellation();
IsBusy = true;
IsIndeterminate = false;
ProgressValue = 0;
UpdateSmartSelectionUi();
var completedRecords = 0;
var overwrittenRecords = 0;
var failedRecords = 0;
long downloadedBytes = 0;
try
{
Directory.CreateDirectory(DestinationDirectory);
await _client.ConnectAsync(_host, _port, _operationCancellation!.Token);
for (var index = 0; index < selected.Length; index++)
{
_operationCancellation.Token.ThrowIfCancellationRequested();
var row = selected[index];
var previousDirectory = row.LocalDirectory;
var overwriteExisting = row.LocalState == FaultRecordLocalState.Downloaded &&
!string.IsNullOrWhiteSpace(previousDirectory) &&
Directory.Exists(previousDirectory);
row.Status = "Downloading";
row.Detail = string.Empty;
StatusText = overwriteExisting
? $"Downloading a fresh copy of {row.RecordName} ({index + 1}/{selected.Length})…"
: $"Downloading {row.RecordName} ({index + 1}/{selected.Length})…";
var recordIndex = index;
var progress = new Progress<Iec61850FaultRecordDownloadProgress>(item =>
{
var withinRecord = item.ExpectedBytes is > 0
? Math.Clamp(item.BytesTransferred / (double)item.ExpectedBytes.Value, 0d, 1d)
: item.TotalFiles > 0
? Math.Clamp(item.CompletedFiles / (double)item.TotalFiles, 0d, 1d)
: 0d;
ProgressValue = ((recordIndex + withinRecord) / selected.Length) * 100d;
StatusText = $"{row.RecordName}: {FormatBytes(item.BytesTransferred)} transferred, file {item.CompletedFiles}/{item.TotalFiles}.";
});
var result = await _client.DownloadAsync(
row.Record,
DestinationDirectory,
progress,
_operationCancellation.Token);
if (!result.IsSuccess)
{
failedRecords++;
row.Status = "Failed";
row.Detail = result.Message;
ProgressValue = ((index + 1d) / selected.Length) * 100d;
continue;
}
try
{
var committedDirectory = CommitFreshRecordDirectory(
previousDirectory,
result.DestinationDirectory,
DestinationDirectory,
row.RecordName);
completedRecords++;
if (overwriteExisting)
overwrittenRecords++;
downloadedBytes += result.BytesTransferred;
row.MarkDownloaded(committedDirectory);
row.IsSelected = false;
_redownloadSelections.Remove(row.Record.RecordId);
ConfigureRecordRow(row);
}
catch (Exception ex) when (
ex is IOException or
UnauthorizedAccessException or
ArgumentException or
InvalidOperationException)
{
failedRecords++;
row.Status = "Failed";
row.Detail =
"The new relay copy downloaded successfully, but replacing the previous local record failed: " +
$"{ex.GetType().Name}: {ex.Message}. The fresh copy remains at '{result.DestinationDirectory}'.";
}
ProgressValue = ((index + 1d) / selected.Length) * 100d;
}
RefreshLocalDownloadStates(preserveFailureStatus: true);
StatusText = failedRecords == 0
? overwrittenRecords > 0
? $"Downloaded {completedRecords:N0} record(s); automatically overwrote {overwrittenRecords:N0} existing local record(s)."
: $"Downloaded {completedRecords:N0} record(s), {FormatBytes(downloadedBytes)}, to '{DestinationDirectory}'."
: $"Downloaded {completedRecords:N0} record(s); {failedRecords:N0} failed. Select a failed row to review diagnostics.";
if (failedRecords == 0)
{
var toast = completedRecords == 1
? overwrittenRecords == 1
? $"File downloaded and overwritten — {selected[0].RecordName}"
: $"File downloaded — {selected[0].RecordName}"
: overwrittenRecords > 0
? $"{completedRecords:N0} downloaded; {overwrittenRecords:N0} overwritten."
: $"{completedRecords:N0} fault records downloaded successfully.";
ShowToast(toast, ToastKind.Success);
}
else if (completedRecords > 0)
{
ShowToast($"{completedRecords:N0} downloaded, {failedRecords:N0} failed.", ToastKind.Warning);
}
else
{
ShowToast("Download failed. Transfer diagnostics opened automatically.", ToastKind.Error);
}
}
catch (OperationCanceledException)
{
StatusText = "Fault-record download cancelled. Partial temporary files were cleaned up.";
ShowToast("Download cancelled safely.", ToastKind.Information);
}
catch (Exception ex)
{
StatusText = $"Fault-record download failed: {ex.Message}";
ShowToast("Download failed. Review transfer diagnostics.", ToastKind.Error);
}
finally
{
IsIndeterminate = false;
IsBusy = false;
RaiseSelectionState();
ConfigureVisibleRecordRows();
UpdateSmartSelectionUi();
}
}
private static string CommitFreshRecordDirectory(
string previousDirectory,
string freshDirectory,
string destinationRoot,
string recordName)
{
if (string.IsNullOrWhiteSpace(previousDirectory) ||
!Directory.Exists(previousDirectory) ||
PathsMatch(previousDirectory, freshDirectory))
{
return freshDirectory;
}
if (!IsUnderDownloadRoot(previousDirectory, destinationRoot) ||
!IsUnderDownloadRoot(freshDirectory, destinationRoot))
{
throw new InvalidOperationException(
"The previous or fresh record directory is outside the selected download root.");
}
var backupDirectory = Path.Combine(
Path.GetFullPath(destinationRoot),
$".arsas-{SanitizeRedownloadName(recordName)}-{Guid.NewGuid():N}.previous");
Directory.Move(previousDirectory, backupDirectory);
try
{
Directory.Move(freshDirectory, previousDirectory);
}
catch
{
if (!Directory.Exists(previousDirectory) && Directory.Exists(backupDirectory))
{
try
{
Directory.Move(backupDirectory, previousDirectory);
}
catch
{
// The hidden backup remains recoverable if rollback cannot complete.
}
}
throw;
}
TryRemoveDirectory(backupDirectory);
return previousDirectory;
}
private static bool IsUnderDownloadRoot(string candidate, string root)
{
var rootFullPath = Path.GetFullPath(root);
var candidateFullPath = Path.GetFullPath(candidate);
var rootPrefix = rootFullPath.EndsWith(Path.DirectorySeparatorChar)
? rootFullPath
: rootFullPath + Path.DirectorySeparatorChar;
var comparison = OperatingSystem.IsWindows()
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;
return candidateFullPath.StartsWith(rootPrefix, comparison);
}
private static bool PathsMatch(string left, string right)
{
var comparison = OperatingSystem.IsWindows()
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;
var leftFullPath = Path.GetFullPath(left)
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
var rightFullPath = Path.GetFullPath(right)
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
return leftFullPath.Equals(rightFullPath, comparison);
}
private static string SanitizeRedownloadName(string value)
{
var source = string.IsNullOrWhiteSpace(value) ? "fault-record" : value.Trim();
var invalid = Path.GetInvalidFileNameChars();
var characters = source
.Select(character => character < ' ' || invalid.Contains(character) ? '_' : character)
.ToArray();
var result = new string(characters).Trim().TrimEnd('.');
return string.IsNullOrWhiteSpace(result) ? "fault-record" : result;
}
private static void TryRemoveDirectory(string path)
{
try
{
if (Directory.Exists(path))
Directory.Delete(path, recursive: true);
}
catch (IOException)
{
}
catch (UnauthorizedAccessException)
{
}
}
private void UpdateSmartSelectionUi()
{
ResolveSmartDownloadControls();
var normalSelected = Records.Count(row => row.IsSelected && row.Record.Files.Count > 0);
var redownloadSelected = Records.Count(row =>
_redownloadSelections.Contains(row.Record.RecordId) &&
row.Record.Files.Count > 0);
var selected = normalSelected + redownloadSelected;
var downloaded = Records.Count(row => row.LocalState == FaultRecordLocalState.Downloaded);
if (_smartSelectionSummary != null)
{
_smartSelectionSummary.Text =
$"{Records.Count:N0} record(s) • {selected:N0} selected • {downloaded:N0} downloaded";
}
if (_smartDownloadButton != null)
_smartDownloadButton.IsEnabled = !IsBusy && !_smartDownloadRunning && selected > 0;
}
private void HideStartupScanToast()
{
_toastTimer.Stop();
ToastHost.BeginAnimation(OpacityProperty, null);
ToastTranslate.BeginAnimation(TranslateTransform.YProperty, null);
ToastHost.Visibility = Visibility.Collapsed;
ToastHost.Opacity = 0;
}
private static IEnumerable<T> FindVisualDescendants<T>(DependencyObject root)
where T : DependencyObject
{
if (root == null)
yield break;
var count = VisualTreeHelper.GetChildrenCount(root);
for (var index = 0; index < count; index++)
{
var child = VisualTreeHelper.GetChild(root, index);
if (child is T match)
yield return match;
foreach (var nested in FindVisualDescendants<T>(child))
yield return nested;
}
}
}