diff --git a/IoListTestingWindow.P0ClosedIedLifecycle.cs b/IoListTestingWindow.P0ClosedIedLifecycle.cs new file mode 100644 index 00000000..9b65f951 --- /dev/null +++ b/IoListTestingWindow.P0ClosedIedLifecycle.cs @@ -0,0 +1,285 @@ +using System.ComponentModel; +using System.IO; +using System.Text.Json; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media; +using System.Windows.Threading; +using ArIED61850Tester.Models.IoTesting; +using ArIED61850Tester.Services.IoTesting; + +namespace ArIED61850Tester; + +/// +/// P0 Close IED / re-import continuity. +/// +/// Close is a deliberate transaction: persist the current project, copy the selected IED's +/// proven snapshot into an independent checkpoint, seal only that IED's evidence session, +/// stop/remove the shared Engineering runtime device, remove the IED from the active FAT +/// scope, then persist the reduced project. Re-imported IED objects may restore historical +/// evidence only through IoFatClosedIedCheckpointService identity/configuration matching. +/// Existing IEDs present when the FAT window opens are never auto-restored from a closed-IED +/// checkpoint, preventing an old checkpoint from overriding normal project snapshot restore. +/// +public partial class IoListTestingWindow +{ + private static readonly bool P0ClosedIedLifecycleRegistered = RegisterP0ClosedIedLifecycle(); + + private readonly HashSet _p0ClosedIedRestoreAttempted = new(); + private bool _p0ClosedIedLifecycleInstalled; + private bool _p0CloseIedInProgress; + private Button? _p0CloseIedButton; + + private static bool RegisterP0ClosedIedLifecycle() + { + EventManager.RegisterClassHandler( + typeof(IoListTestingWindow), + FrameworkElement.LoadedEvent, + new RoutedEventHandler(P0ClosedIedLifecycle_Loaded), + handledEventsToo: true); + return true; + } + + private static void P0ClosedIedLifecycle_Loaded(object sender, RoutedEventArgs e) + { + if (sender is not IoListTestingWindow window || window._p0ClosedIedLifecycleInstalled) + return; + + window._p0ClosedIedLifecycleInstalled = true; + foreach (var existing in window.Project.Ieds) + window._p0ClosedIedRestoreAttempted.Add(existing); + + window.PropertyChanged += window.P0ClosedIedLifecycle_PropertyChanged; + window.Closed += window.P0ClosedIedLifecycle_Closed; + window.Dispatcher.BeginInvoke( + new Action(window.InstallP0CloseIedButton), + DispatcherPriority.Loaded); + } + + private void InstallP0CloseIedButton() + { + if (_p0CloseIedButton != null) + return; + + var stopButton = FindP0ClosedIedVisualDescendants