Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions IoListTestingWindow.ClockSyncUx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ArIED61850Tester;

public partial class IoListTestingWindow
{
private CheckBox? _clockSyncCheckBox;
private bool _clockSyncCheckBoxRefreshing;

protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
Loaded += ClockSyncUx_Loaded;
}

private void ClockSyncUx_Loaded(object sender, RoutedEventArgs e)
{
if (_clockSyncCheckBox != null)
{
RefreshClockSyncCheckBox();
return;
}

if (WorkspacePreviewToggle.Parent is not Panel actionPanel)
return;

var previewIndex = actionPanel.Children.IndexOf(WorkspacePreviewToggle);
if (previewIndex < 0)
return;

var checkBox = new CheckBox
{
Name = "ClockSyncEnabledCheckBox",
Content = "Clock Sync",
VerticalAlignment = VerticalAlignment.Center,
Margin = new Thickness(0, 0, 12, 0),
Padding = new Thickness(2, 0, 2, 0),
FontSize = 11.2,
FontWeight = FontWeights.SemiBold,
Foreground = TryFindResource("Ink") as Brush ?? Brushes.DimGray,
Focusable = false,
ToolTip = "SNTP laptop → IED. Checked: ARSAS serves and broadcasts laptop time using the SIPROTEC compatibility profile (stratum 2). Unchecked: ARSAS stops its SNTP service. IEC 61850 monitoring is unaffected."
};

_clockSyncCheckBox = checkBox;
RefreshClockSyncCheckBox();
checkBox.Checked += ClockSyncCheckBox_Changed;
checkBox.Unchecked += ClockSyncCheckBox_Changed;
actionPanel.Children.Insert(previewIndex + 1, checkBox);
}

private void RefreshClockSyncCheckBox()
{
if (_clockSyncCheckBox == null || Owner is not MainWindow mainWindow)
return;

_clockSyncCheckBoxRefreshing = true;
try
{
_clockSyncCheckBox.IsChecked = mainWindow.IsClockSyncEnabled;
}
finally
{
_clockSyncCheckBoxRefreshing = false;
}
}

private async void ClockSyncCheckBox_Changed(object sender, RoutedEventArgs e)
{
if (_clockSyncCheckBoxRefreshing ||
_clockSyncCheckBox == null ||
Owner is not MainWindow mainWindow)
return;

var requested = _clockSyncCheckBox.IsChecked == true;
_clockSyncCheckBox.IsEnabled = false;
try
{
await mainWindow.SetClockSyncEnabledAsync(requested);
RefreshClockSyncCheckBox();
}
finally
{
_clockSyncCheckBox.IsEnabled = true;
}
}
}
53 changes: 53 additions & 0 deletions MainWindow.ClockSyncToggle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using ArIED61850Tester.Models;

namespace ArIED61850Tester;

public partial class MainWindow
{
private bool _clockSyncEnabled = true;

internal bool IsClockSyncEnabled => _clockSyncEnabled;

internal async Task SetClockSyncEnabledAsync(bool enabled)
{
if (_clockSyncEnabled == enabled)
return;

_clockSyncEnabled = enabled;

if (!enabled)
{
Devices.CollectionChanged -= ClockSyncDevices_CollectionChanged;
foreach (var device in Devices)
device.PropertyChanged -= ClockSyncDevice_PropertyChanged;
Comment on lines +20 to +22

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Guard queued reconciles when Clock Sync is disabled

If IsConnected changes on a runtime thread just before these handlers are removed, ClockSyncDevice_PropertyChanged may already be enqueuing ScheduleClockSyncReconcile on the dispatcher. While this method awaits StopAsync, that queued reconcile can wait behind _clockSyncIntegrationGate and then restart the SNTP service after the stop completes, because neither scheduling nor EnsureClockSyncForDeviceAsync checks _clockSyncEnabled. In that race, the unchecked control continues serving and broadcasting time; recheck the enabled state after dispatching and after acquiring the gate, or cancel pending reconciles.

Useful? React with 👍 / 👎.


await _clockSyncIntegrationGate.WaitAsync();
try
{
await _sntpClockService.StopAsync();
_clockSyncObservedClients.Clear();
AddLog("INFO", "Clock Sync",
"Clock Sync disabled from the FAT workspace. IEC 61850 monitoring remains active.");
}
catch (Exception ex)
{
AddLog("WARN", "Clock Sync",
$"Clock Sync disable requested, but the SNTP service reported: {ex.Message}");
}
finally
{
_clockSyncIntegrationGate.Release();
}

return;
}

Devices.CollectionChanged -= ClockSyncDevices_CollectionChanged;
Devices.CollectionChanged += ClockSyncDevices_CollectionChanged;
foreach (var device in Devices)
AttachClockSyncDevice(device);

AddLog("INFO", "Clock Sync",
"Clock Sync enabled from the FAT workspace. ARSAS will advertise laptop time to connected IPv4 IEDs using the SIPROTEC-compatible SNTP profile.");
}
}
2 changes: 1 addition & 1 deletion MainWindow.IoTesting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ private IoTestSessionController CreateIoTestSession(IoTestProject project, strin
=> new(
project,
ResolveIoTestDevice,
action => Dispatcher.BeginInvoke(action, DispatcherPriority.Background),
action => Dispatcher.BeginInvoke(action, DispatcherPriority.DataBind),
evidenceRoot);

private static string IoTestingProjectsRoot() => Path.Combine(
Expand Down
64 changes: 64 additions & 0 deletions tests/ARSAS.Tests/IoFatImmediateEvidenceRegressionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.ComponentModel;
using ArIED61850Tester.Models.IoTesting;
using ArIED61850Tester.Services.IoTesting;

namespace ARSAS.Tests;

public sealed class IoFatImmediateEvidenceRegressionTests
{
[Fact]
public void TrueEdge_PublishesOnTimestampBeforeFalseEdgeArrives()
{
var evaluator = new IoTestTransitionEvaluator();
var point = CreatePoint();
var onTimestampNotificationRaised = false;

point.Runtime.PropertyChanged += (_, args) =>
{
if (args.PropertyName == nameof(IoTestPointRuntime.OnRelayTimestampText))
onTimestampNotificationRaised = true;
};

evaluator.StartAttempt(point, Observation(false, 1));
var on = evaluator.Observe(point, Observation(true, 2));

Assert.Equal(IoTestPointState.OnCaptured, on.State);
Assert.NotNull(point.Runtime.OnEvidence);
Assert.Null(point.Runtime.OffEvidence);
Assert.Equal(
point.Runtime.OnEvidence!.IedTimestamp?.ToString("yyyy-MM-dd HH:mm:ss.fff"),
point.Runtime.OnRelayTimestampText);
Assert.NotEqual("—", point.Runtime.OnRelayTimestampText);
Assert.True(onTimestampNotificationRaised);
}

private static IoTestPointPlan CreatePoint()
=> new()
{
TestPointId = "TRUE-EVIDENCE-REGRESSION",
IedName = "SIPROTEC",
IpAddress = "192.168.1.10",
SignalName = "Protection pickup",
ObjectReference = "SIPROTEC/PROT.Op.general",
FunctionalConstraint = "ST",
ExpectedOnText = "true",
ExpectedOffText = "false",
ImportReady = true,
BindingStatus = "CID_DATASET_EXACT"
};

private static IoTestObservation Observation(bool state, long sequence)
{
var captured = new DateTimeOffset(2026, 8, 13, 4, 55, 0, TimeSpan.Zero)
.AddMilliseconds(sequence * 100);
return new IoTestObservation(
state,
state ? "true" : "false",
captured,
captured.AddMilliseconds(-4),
"Good",
"BRCB",
sequence,
1);
}
}
Loading