using System.Collections.Concurrent; using System.Reflection; using Opc.Ua; using ZB.MOM.WW.ScadaBridge.Commons.Types.Alarms; using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums; using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Adapters; namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests.Adapters; /// /// ScadaBridge#138 / OtOpcUa Warm design §4.5 + §5 "Alarms are compatible": when a Warm standby /// is promoted, OtOpcUa runs a ConditionRefresh on every live event item ON ITS OWN — the client /// never asked. The existing items therefore receive an unsolicited RefreshStart, the retained /// conditions, RefreshEnd, then new events. ScadaBridge must treat that exactly like the refresh it /// triggers itself on subscribe: one framed snapshot with its own epoch, and live derivation /// before and after it. /// public class RealOpcUaPromotionRefreshTests { private const string Handle = "h1"; private const string Source = "ns=2;s=Tank01"; private static readonly MethodInfo Handler = typeof(RealOpcUaClient).GetMethod("HandleAlarmEvent", BindingFlags.Instance | BindingFlags.NonPublic)!; /// What CreateAlarmSubscriptionAsync sets up for a live handle: its per-condition state map. private static void SeedSubscribedHandle(RealOpcUaClient client) { var field = typeof(RealOpcUaClient).GetField("_alarmLastState", BindingFlags.Instance | BindingFlags.NonPublic)!; var map = (ConcurrentDictionary>)field.GetValue(client)!; map[Handle] = new Dictionary(StringComparer.Ordinal); } private static void Boundary(RealOpcUaClient client, NodeId type, List sink) => Handler.Invoke(client, [Handle, Source, new EventFieldList { EventFields = new Variant[] { Variant.From(type), Variant.Null, Variant.Null, Variant.Null, Variant.Null, Variant.Null } }, (Action)sink.Add]); private static void Condition(RealOpcUaClient client, string condition, bool active, bool acked, List sink) { var fields = new Variant[] { Variant.From(ObjectTypeIds.AlarmConditionType), // 0 EventType Variant.From(new NodeId("Tank01", 2)), // 1 SourceNode Variant.From("Tank01"), // 2 SourceName Variant.From(DateTime.UtcNow), // 3 Time Variant.From(new LocalizedText("msg")), // 4 Message Variant.From((ushort)500), // 5 Severity Variant.From(active), // 6 ActiveState/Id Variant.From(acked), // 7 AckedState/Id Variant.Null, // 8 ConfirmedState/Id Variant.From(false), // 9 SuppressedOrShelved Variant.Null, // 10 ShelvingState Variant.From(condition), // 11 ConditionName }; Handler.Invoke(client, [Handle, Source, new EventFieldList { EventFields = fields }, (Action)sink.Add]); } /// /// Subscribe-time refresh, live traffic, then the UNSOLICITED promotion refresh on the same /// item, then live traffic again. The promotion replay is framed SnapshotStart → Snapshot… → /// SnapshotComplete under a fresh epoch (distinct from the subscribe-time one), and live events /// after it are derived live again — the replay does not leave the item stuck in Snapshot mode. /// [Fact] public void UnsolicitedPromotionRefresh_IsFramedAsItsOwnSnapshot_AndLiveDerivationResumes() { var client = new RealOpcUaClient(); SeedSubscribedHandle(client); var sink = new List(); // Subscribe-time refresh (the one ScadaBridge triggers): Hi is active. Boundary(client, ObjectTypeIds.RefreshStartEventType, sink); Condition(client, "Hi", active: true, acked: false, sink); Boundary(client, ObjectTypeIds.RefreshEndEventType, sink); // Live while the old Active serves: Lo raises. Condition(client, "Lo", active: true, acked: false, sink); var liveBefore = sink[^1]; // Promotion on the other node: an unsolicited refresh on the existing item. Boundary(client, ObjectTypeIds.RefreshStartEventType, sink); Condition(client, "Hi", active: true, acked: false, sink); Condition(client, "Lo", active: true, acked: false, sink); Boundary(client, ObjectTypeIds.RefreshEndEventType, sink); // Live after promotion: Lo is acknowledged. Condition(client, "Lo", active: true, acked: true, sink); var liveAfter = sink[^1]; Assert.Equal( ["SnapshotStart", "Snapshot", "SnapshotComplete", liveBefore.Kind.ToString(), "SnapshotStart", "Snapshot", "Snapshot", "SnapshotComplete", liveAfter.Kind.ToString()], sink.Select(t => t.Kind.ToString())); // Live kinds really are live (not Snapshot) on both sides of the promotion refresh. Assert.Equal(AlarmTransitionKind.Raise, liveBefore.Kind); Assert.Equal(AlarmTransitionKind.Acknowledge, liveAfter.Kind); Assert.Null(liveBefore.SnapshotId); Assert.Null(liveAfter.SnapshotId); // The promotion replay has its own epoch, shared by its boundaries and its conditions. var subscribeEpoch = sink[0].SnapshotId; var promotionEpoch = sink[4].SnapshotId; Assert.NotNull(subscribeEpoch); Assert.NotNull(promotionEpoch); Assert.NotEqual(subscribeEpoch, promotionEpoch); Assert.All(sink.Skip(4).Take(4), t => Assert.Equal(promotionEpoch, t.SnapshotId)); Assert.All(sink.Skip(4).Take(4), t => Assert.Equal(Source, t.SnapshotSourceReference)); } }