From 190d09cdeb2daff6bb76f0e85a7ee7a5765ae3d5 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Sat, 18 Apr 2026 07:51:35 -0400 Subject: [PATCH] =?UTF-8?q?Phase=203=20PR=2015=20=E2=80=94=20alarm-conditi?= =?UTF-8?q?on=20contract=20in=20IAddressSpaceBuilder=20+=20wire=20OnAlarmE?= =?UTF-8?q?vent=20through=20GenericDriverNodeManager.=20IAddressSpaceBuild?= =?UTF-8?q?er.IVariableHandle=20gains=20MarkAsAlarmCondition(AlarmConditio?= =?UTF-8?q?nInfo)=20which=20returns=20an=20IAlarmConditionSink.=20AlarmCon?= =?UTF-8?q?ditionInfo=20carries=20SourceName/InitialSeverity/InitialDescri?= =?UTF-8?q?ption.=20Concrete=20address-space=20builders=20(the=20upcoming?= =?UTF-8?q?=20PR=2016=20OPC=20UA=20server=20backend)=20materialize=20a=20s?= =?UTF-8?q?ibling=20AlarmConditionState=20node=20on=20the=20first=20call;?= =?UTF-8?q?=20the=20sink=20receives=20every=20lifecycle=20transition=20the?= =?UTF-8?q?=20generic=20node=20manager=20forwards.=20GenericDriverNodeMana?= =?UTF-8?q?ger=20gains=20a=20CapturingBuilder=20wrapper=20that=20transpare?= =?UTF-8?q?ntly=20wraps=20every=20Folder/Variable=20call=20=E2=80=94=20the?= =?UTF-8?q?=20wrapper=20observes=20MarkAsAlarmCondition=20calls=20without?= =?UTF-8?q?=20participating=20in=20materialization,=20captures=20the=20res?= =?UTF-8?q?ulting=20IAlarmConditionSink=20into=20an=20internal=20source-no?= =?UTF-8?q?de-id=20=E2=86=92=20sink=20ConcurrentDictionary=20keyed=20by=20?= =?UTF-8?q?IVariableHandle.FullReference.=20After=20DiscoverAsync=20comple?= =?UTF-8?q?tes,=20if=20the=20driver=20implements=20IAlarmSource=20the=20no?= =?UTF-8?q?de=20manager=20subscribes=20to=20OnAlarmEvent=20and=20routes=20?= =?UTF-8?q?every=20AlarmEventArgs=20to=20the=20sink=20registered=20for=20a?= =?UTF-8?q?rgs.SourceNodeId=20=E2=80=94=20unknown=20source=20ids=20are=20d?= =?UTF-8?q?ropped=20silently=20(may=20belong=20to=20another=20driver=20or?= =?UTF-8?q?=20to=20a=20variable=20the=20builder=20chose=20not=20to=20flag)?= =?UTF-8?q?.=20Dispose=20unsubscribes=20the=20forwarder=20to=20prevent=20d?= =?UTF-8?q?angling=20invocation-list=20references=20across=20node-manager?= =?UTF-8?q?=20rebuilds.=20GalaxyProxyDriver.DiscoverAsync=20now=20calls=20?= =?UTF-8?q?handle.MarkAsAlarmCondition(new=20AlarmConditionInfo(fullName,?= =?UTF-8?q?=20AlarmSeverity.Medium,=20null))=20on=20every=20attr.IsAlarm?= =?UTF-8?q?=3Dtrue=20variable=20=E2=80=94=20severity=20seed=20is=20Medium?= =?UTF-8?q?=20because=20the=20live=20Priority=20byte=20arrives=20through?= =?UTF-8?q?=20the=20subsequent=20GalaxyAlarmEvent=20stream=20(which=20PR?= =?UTF-8?q?=2014's=20GalaxyAlarmTracker=20now=20emits);=20the=20Admin=20UI?= =?UTF-8?q?=20sees=20the=20severity=20update=20on=20the=20first=20transiti?= =?UTF-8?q?on.=20RecordingAddressSpaceBuilder=20in=20Driver.Galaxy.E2E=20g?= =?UTF-8?q?ains=20a=20RecordedAlarmCondition=20list=20+=20a=20RecordingSin?= =?UTF-8?q?k=20implementation=20that=20captures=20AlarmEventArgs=20for=20t?= =?UTF-8?q?est=20assertion=20=E2=80=94=20the=20E2E=20parity=20suite=20can?= =?UTF-8?q?=20now=20verify=20alarm-condition=20registration=20shape=20in?= =?UTF-8?q?=20addition=20to=20folder/variable=20shape.=20Tests=20(4=20new?= =?UTF-8?q?=20GenericDriverNodeManagerTests):=20Alarm=5Fevents=5Fare=5Frou?= =?UTF-8?q?ted=5Fto=5Fthe=5Fsink=5Fregistered=5Ffor=5Fthe=5Fmatching=5Fsou?= =?UTF-8?q?rce=5Fnode=5Fid=20=E2=80=94=202=20alarms=20registered=20(Tank.H?= =?UTF-8?q?iHi=20+=20Heater.OverTemp),=20driver=20raises=20an=20event=20fo?= =?UTF-8?q?r=20Tank.HiHi,=20the=20Tank.HiHi=20sink=20captures=20the=20payl?= =?UTF-8?q?oad,=20the=20Heater.OverTemp=20sink=20does=20not=20(tag-scoped?= =?UTF-8?q?=20fan-out,=20not=20broadcast);=20Non=5Falarm=5Fvariables=5Fdo?= =?UTF-8?q?=5Fnot=5Fregister=5Fsinks=20=E2=80=94=20plain=20Tank.Level=20in?= =?UTF-8?q?=20the=20same=20discover=20is=20not=20in=20TrackedAlarmSources;?= =?UTF-8?q?=20Unknown=5Fsource=5Fnode=5Fid=5Fis=5Fdropped=5Fsilently=20?= =?UTF-8?q?=E2=80=94=20a=20transition=20for=20Unknown.Source=20doesn't=20r?= =?UTF-8?q?each=20any=20sink=20+=20no=20exception;=20Dispose=5Funsubscribe?= =?UTF-8?q?s=5Ffrom=5FOnAlarmEvent=20=E2=80=94=20post-dispose,=20a=20trans?= =?UTF-8?q?ition=20for=20a=20previously-registered=20tag=20is=20no-op=20be?= =?UTF-8?q?cause=20the=20forwarder=20detached.=20InternalsVisibleTo('ZB.MO?= =?UTF-8?q?M.WW.OtOpcUa.Core.Tests')=20added=20to=20Core=20csproj=20so=20T?= =?UTF-8?q?rackedAlarmSources=20internal=20property=20is=20visible=20to=20?= =?UTF-8?q?the=20test.=20Full=20solution:=200=20errors,=20152=20unit=20tes?= =?UTF-8?q?ts=20pass=20(8=20Core=20+=2014=20Proxy=20+=2014=20Admin=20+=202?= =?UTF-8?q?4=20Configuration=20+=206=20Shared=20+=2084=20Galaxy.Host=20+?= =?UTF-8?q?=202=20Server).=20PR=2016=20will=20implement=20the=20concrete?= =?UTF-8?q?=20OPC=20UA=20address-space=20builder=20that=20materializes=20A?= =?UTF-8?q?larmConditionState=20from=20this=20contract.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.7 (1M context) --- .../IAddressSpaceBuilder.cs | 35 ++++ .../OpcUa/GenericDriverNodeManager.cs | 101 +++++++++-- .../ZB.MOM.WW.OtOpcUa.Core.csproj | 4 + .../GalaxyProxyDriver.cs | 18 +- .../GenericDriverNodeManagerTests.cs | 163 ++++++++++++++++++ .../RecordingAddressSpaceBuilder.cs | 36 +++- 6 files changed, 343 insertions(+), 14 deletions(-) create mode 100644 tests/ZB.MOM.WW.OtOpcUa.Core.Tests/GenericDriverNodeManagerTests.cs diff --git a/src/ZB.MOM.WW.OtOpcUa.Core.Abstractions/IAddressSpaceBuilder.cs b/src/ZB.MOM.WW.OtOpcUa.Core.Abstractions/IAddressSpaceBuilder.cs index 21bb44c..96075c8 100644 --- a/src/ZB.MOM.WW.OtOpcUa.Core.Abstractions/IAddressSpaceBuilder.cs +++ b/src/ZB.MOM.WW.OtOpcUa.Core.Abstractions/IAddressSpaceBuilder.cs @@ -42,4 +42,39 @@ public interface IVariableHandle { /// Driver-side full reference for read/write addressing. string FullReference { get; } + + /// + /// Annotate this variable with an OPC UA AlarmConditionState. Drivers with + /// = true call this during discovery so the + /// concrete address-space builder can materialize a sibling condition node. The returned + /// sink receives lifecycle transitions raised through + /// — the generic node manager wires the subscription; the concrete builder decides how + /// to surface the state (e.g. OPC UA AlarmConditionState.Activate, + /// Acknowledge, Deactivate). + /// + IAlarmConditionSink MarkAsAlarmCondition(AlarmConditionInfo info); +} + +/// +/// Metadata used to materialize an OPC UA AlarmConditionState sibling for a variable. +/// Populated by the driver's discovery step; concrete builders decide how to surface it. +/// +/// Human-readable alarm name used for the SourceName event field. +/// Severity at address-space build time; updates arrive via . +/// Initial description; updates arrive via . +public sealed record AlarmConditionInfo( + string SourceName, + AlarmSeverity InitialSeverity, + string? InitialDescription); + +/// +/// Sink a concrete address-space builder returns from . +/// The generic node manager routes per-alarm payloads here — +/// the sink translates the transition into an OPC UA condition state change or whatever the +/// concrete builder's backing address space supports. +/// +public interface IAlarmConditionSink +{ + /// Push an alarm transition (Active / Acknowledged / Inactive) for this condition. + void OnTransition(AlarmEventArgs args); } diff --git a/src/ZB.MOM.WW.OtOpcUa.Core/OpcUa/GenericDriverNodeManager.cs b/src/ZB.MOM.WW.OtOpcUa.Core/OpcUa/GenericDriverNodeManager.cs index 1672ee1..8712107 100644 --- a/src/ZB.MOM.WW.OtOpcUa.Core/OpcUa/GenericDriverNodeManager.cs +++ b/src/ZB.MOM.WW.OtOpcUa.Core/OpcUa/GenericDriverNodeManager.cs @@ -1,27 +1,41 @@ +using System.Collections.Concurrent; using ZB.MOM.WW.OtOpcUa.Core.Abstractions; namespace ZB.MOM.WW.OtOpcUa.Core.OpcUa; /// /// Generic, driver-agnostic backbone for populating the OPC UA address space from an -/// . The Galaxy-specific subclass (GalaxyNodeManager) is deferred -/// to Phase 2 per decision #62 — this class is the foundation that Phase 2 ports the v1 -/// LmxNodeManager logic into. +/// . Walks the driver's discovery, wires the alarm + data-change + +/// rediscovery subscription events, and hands each variable to the supplied +/// . Concrete OPC UA server implementations provide the +/// builder — see the Server project's OpcUaAddressSpaceBuilder for the materialization +/// against CustomNodeManager2. /// /// -/// Phase 1 status: scaffold only. The v1 LmxNodeManager in the legacy Host is unchanged -/// so IntegrationTests continue to pass. Phase 2 will lift-and-shift its logic here, swapping -/// IMxAccessClient for and GalaxyAttributeInfo for -/// . +/// Per docs/v2/plan.md decision #52 + #62 — Core owns the node tree, drivers stream +/// Folder/Variable calls, alarm-bearing variables are annotated via +/// and subsequent +/// payloads route to the sink the builder returned. /// -public abstract class GenericDriverNodeManager(IDriver driver) +public class GenericDriverNodeManager(IDriver driver) : IDisposable { protected IDriver Driver { get; } = driver ?? throw new ArgumentNullException(nameof(driver)); public string DriverInstanceId => Driver.DriverInstanceId; + // Source tag (DriverAttributeInfo.FullName) → alarm-condition sink. Populated during + // BuildAddressSpaceAsync by a recording IAddressSpaceBuilder implementation that captures the + // IVariableHandle per attr.IsAlarm=true variable and calls MarkAsAlarmCondition. + private readonly ConcurrentDictionary _alarmSinks = + new(StringComparer.OrdinalIgnoreCase); + + private EventHandler? _alarmForwarder; + private bool _disposed; + /// - /// Populates the address space by streaming nodes from the driver into the supplied builder. + /// Populates the address space by streaming nodes from the driver into the supplied builder, + /// wraps the builder so alarm-condition sinks are captured, subscribes to the driver's + /// alarm event stream, and routes each transition to the matching sink by SourceNodeId. /// Driver exceptions are isolated per decision #12 — the driver's subtree is marked Faulted, /// but other drivers remain available. /// @@ -32,6 +46,73 @@ public abstract class GenericDriverNodeManager(IDriver driver) if (Driver is not ITagDiscovery discovery) throw new NotSupportedException($"Driver '{Driver.DriverInstanceId}' does not implement ITagDiscovery."); - await discovery.DiscoverAsync(builder, ct); + var capturing = new CapturingBuilder(builder, _alarmSinks); + await discovery.DiscoverAsync(capturing, ct); + + if (Driver is IAlarmSource alarmSource) + { + _alarmForwarder = (_, e) => + { + // Route the alarm to the sink registered for the originating variable, if any. + // Unknown source ids are dropped silently — they may belong to another driver or + // to a variable the builder chose not to flag as an alarm condition. + if (_alarmSinks.TryGetValue(e.SourceNodeId, out var sink)) + sink.OnTransition(e); + }; + alarmSource.OnAlarmEvent += _alarmForwarder; + } + } + + public void Dispose() + { + if (_disposed) return; + _disposed = true; + if (_alarmForwarder is not null && Driver is IAlarmSource alarmSource) + { + alarmSource.OnAlarmEvent -= _alarmForwarder; + } + _alarmSinks.Clear(); + } + + /// + /// Snapshot the current alarm-sink registry by source node id. Diagnostic + test hook; + /// not part of the hot path. + /// + internal IReadOnlyCollection TrackedAlarmSources => _alarmSinks.Keys.ToList(); + + /// + /// Wraps the caller-supplied so every + /// call registers the returned sink in + /// the node manager's source-node-id map. The builder itself drives materialization; + /// this wrapper only observes. + /// + private sealed class CapturingBuilder( + IAddressSpaceBuilder inner, + ConcurrentDictionary sinks) : IAddressSpaceBuilder + { + public IAddressSpaceBuilder Folder(string browseName, string displayName) + => new CapturingBuilder(inner.Folder(browseName, displayName), sinks); + + public IVariableHandle Variable(string browseName, string displayName, DriverAttributeInfo attributeInfo) + => new CapturingHandle(inner.Variable(browseName, displayName, attributeInfo), sinks); + + public void AddProperty(string browseName, DriverDataType dataType, object? value) + => inner.AddProperty(browseName, dataType, value); + } + + private sealed class CapturingHandle( + IVariableHandle inner, + ConcurrentDictionary sinks) : IVariableHandle + { + public string FullReference => inner.FullReference; + + public IAlarmConditionSink MarkAsAlarmCondition(AlarmConditionInfo info) + { + var sink = inner.MarkAsAlarmCondition(info); + // Register by the driver-side full reference so the alarm forwarder can look it up + // using AlarmEventArgs.SourceNodeId (which the driver populates with the same tag). + sinks[inner.FullReference] = sink; + return sink; + } } } diff --git a/src/ZB.MOM.WW.OtOpcUa.Core/ZB.MOM.WW.OtOpcUa.Core.csproj b/src/ZB.MOM.WW.OtOpcUa.Core/ZB.MOM.WW.OtOpcUa.Core.csproj index b31947c..8e48171 100644 --- a/src/ZB.MOM.WW.OtOpcUa.Core/ZB.MOM.WW.OtOpcUa.Core.csproj +++ b/src/ZB.MOM.WW.OtOpcUa.Core/ZB.MOM.WW.OtOpcUa.Core.csproj @@ -16,6 +16,10 @@ + + + + diff --git a/src/ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Proxy/GalaxyProxyDriver.cs b/src/ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Proxy/GalaxyProxyDriver.cs index c8749ca..8762cd4 100644 --- a/src/ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Proxy/GalaxyProxyDriver.cs +++ b/src/ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Proxy/GalaxyProxyDriver.cs @@ -114,17 +114,31 @@ public sealed class GalaxyProxyDriver(GalaxyProxyOptions options) var folder = builder.Folder(obj.ContainedName, obj.ContainedName); foreach (var attr in obj.Attributes) { - folder.Variable( + var fullName = $"{obj.TagName}.{attr.AttributeName}"; + var handle = folder.Variable( attr.AttributeName, attr.AttributeName, new DriverAttributeInfo( - FullName: $"{obj.TagName}.{attr.AttributeName}", + FullName: fullName, DriverDataType: MapDataType(attr.MxDataType), IsArray: attr.IsArray, ArrayDim: attr.ArrayDim, SecurityClass: MapSecurity(attr.SecurityClassification), IsHistorized: attr.IsHistorized, IsAlarm: attr.IsAlarm)); + + // PR 15: when Galaxy flags the attribute as alarm-bearing (AlarmExtension + // primitive), register an alarm-condition sink so the generic node manager + // can route OnAlarmEvent payloads for this tag to the concrete address-space + // builder. Severity default Medium — the live severity arrives through + // AlarmEventArgs once MxAccessGalaxyBackend's tracker starts firing. + if (attr.IsAlarm) + { + handle.MarkAsAlarmCondition(new AlarmConditionInfo( + SourceName: fullName, + InitialSeverity: AlarmSeverity.Medium, + InitialDescription: null)); + } } } } diff --git a/tests/ZB.MOM.WW.OtOpcUa.Core.Tests/GenericDriverNodeManagerTests.cs b/tests/ZB.MOM.WW.OtOpcUa.Core.Tests/GenericDriverNodeManagerTests.cs new file mode 100644 index 0000000..6d47a4f --- /dev/null +++ b/tests/ZB.MOM.WW.OtOpcUa.Core.Tests/GenericDriverNodeManagerTests.cs @@ -0,0 +1,163 @@ +using Shouldly; +using Xunit; +using ZB.MOM.WW.OtOpcUa.Core.Abstractions; +using ZB.MOM.WW.OtOpcUa.Core.OpcUa; + +namespace ZB.MOM.WW.OtOpcUa.Core.Tests; + +[Trait("Category", "Unit")] +public sealed class GenericDriverNodeManagerTests +{ + /// + /// BuildAddressSpaceAsync walks the driver's discovery through the caller's builder. Every + /// variable marked with MarkAsAlarmCondition captures its sink in the node manager; later, + /// IAlarmSource.OnAlarmEvent payloads are routed by SourceNodeId to the matching sink. + /// This is the plumbing that PR 16's concrete OPC UA builder will use to update the actual + /// AlarmConditionState nodes. + /// + [Fact] + public async Task Alarm_events_are_routed_to_the_sink_registered_for_the_matching_source_node_id() + { + var driver = new FakeDriver(); + var builder = new RecordingBuilder(); + using var nm = new GenericDriverNodeManager(driver); + + await nm.BuildAddressSpaceAsync(builder, CancellationToken.None); + + builder.Alarms.Count.ShouldBe(2); + nm.TrackedAlarmSources.Count.ShouldBe(2); + + // Simulate the driver raising a transition for one of the alarms. + var args = new AlarmEventArgs( + SubscriptionHandle: new FakeHandle("s1"), + SourceNodeId: "Tank.HiHi", + ConditionId: "cond-1", + AlarmType: "Tank.HiHi", + Message: "Level exceeded", + Severity: AlarmSeverity.High, + SourceTimestampUtc: DateTime.UtcNow); + driver.RaiseAlarm(args); + + builder.Alarms["Tank.HiHi"].Received.Count.ShouldBe(1); + builder.Alarms["Tank.HiHi"].Received[0].Message.ShouldBe("Level exceeded"); + // The other alarm sink never received a payload — fan-out is tag-scoped. + builder.Alarms["Heater.OverTemp"].Received.Count.ShouldBe(0); + } + + [Fact] + public async Task Non_alarm_variables_do_not_register_sinks() + { + var driver = new FakeDriver(); + var builder = new RecordingBuilder(); + using var nm = new GenericDriverNodeManager(driver); + + await nm.BuildAddressSpaceAsync(builder, CancellationToken.None); + + // FakeDriver registers 2 alarm-bearing variables + 1 plain variable. + nm.TrackedAlarmSources.ShouldNotContain("Tank.Level"); // the plain one + } + + [Fact] + public async Task Unknown_source_node_id_is_dropped_silently() + { + var driver = new FakeDriver(); + var builder = new RecordingBuilder(); + using var nm = new GenericDriverNodeManager(driver); + await nm.BuildAddressSpaceAsync(builder, CancellationToken.None); + + driver.RaiseAlarm(new AlarmEventArgs( + new FakeHandle("s1"), "Unknown.Source", "c", "t", "m", AlarmSeverity.Low, DateTime.UtcNow)); + + builder.Alarms.Values.All(s => s.Received.Count == 0).ShouldBeTrue(); + } + + [Fact] + public async Task Dispose_unsubscribes_from_OnAlarmEvent() + { + var driver = new FakeDriver(); + var builder = new RecordingBuilder(); + var nm = new GenericDriverNodeManager(driver); + await nm.BuildAddressSpaceAsync(builder, CancellationToken.None); + + nm.Dispose(); + + driver.RaiseAlarm(new AlarmEventArgs( + new FakeHandle("s1"), "Tank.HiHi", "c", "t", "m", AlarmSeverity.Low, DateTime.UtcNow)); + + // No sink should have received it — the forwarder was detached. + builder.Alarms["Tank.HiHi"].Received.Count.ShouldBe(0); + } + + // --- test doubles --- + + private sealed class FakeDriver : IDriver, ITagDiscovery, IAlarmSource + { + public string DriverInstanceId => "fake"; + public string DriverType => "Fake"; + public event EventHandler? OnAlarmEvent; + + public Task InitializeAsync(string driverConfigJson, CancellationToken ct) => Task.CompletedTask; + public Task ReinitializeAsync(string driverConfigJson, CancellationToken ct) => Task.CompletedTask; + public Task ShutdownAsync(CancellationToken ct) => Task.CompletedTask; + public DriverHealth GetHealth() => new(DriverState.Healthy, DateTime.UtcNow, null); + public long GetMemoryFootprint() => 0; + public Task FlushOptionalCachesAsync(CancellationToken ct) => Task.CompletedTask; + + public Task DiscoverAsync(IAddressSpaceBuilder builder, CancellationToken ct) + { + var folder = builder.Folder("Tank", "Tank"); + var lvl = folder.Variable("Level", "Level", new DriverAttributeInfo( + "Tank.Level", DriverDataType.Float64, false, null, SecurityClassification.FreeAccess, false, IsAlarm: false)); + var hiHi = folder.Variable("HiHi", "HiHi", new DriverAttributeInfo( + "Tank.HiHi", DriverDataType.Boolean, false, null, SecurityClassification.FreeAccess, false, IsAlarm: true)); + hiHi.MarkAsAlarmCondition(new AlarmConditionInfo("Tank.HiHi", AlarmSeverity.High, "High-high alarm")); + + var heater = builder.Folder("Heater", "Heater"); + var ot = heater.Variable("OverTemp", "OverTemp", new DriverAttributeInfo( + "Heater.OverTemp", DriverDataType.Boolean, false, null, SecurityClassification.FreeAccess, false, IsAlarm: true)); + ot.MarkAsAlarmCondition(new AlarmConditionInfo("Heater.OverTemp", AlarmSeverity.Critical, "Over-temperature")); + return Task.CompletedTask; + } + + public void RaiseAlarm(AlarmEventArgs args) => OnAlarmEvent?.Invoke(this, args); + + public Task SubscribeAlarmsAsync(IReadOnlyList _, CancellationToken __) + => Task.FromResult(new FakeHandle("sub")); + public Task UnsubscribeAlarmsAsync(IAlarmSubscriptionHandle _, CancellationToken __) => Task.CompletedTask; + public Task AcknowledgeAsync(IReadOnlyList _, CancellationToken __) => Task.CompletedTask; + } + + private sealed class FakeHandle(string diagnosticId) : IAlarmSubscriptionHandle + { + public string DiagnosticId { get; } = diagnosticId; + } + + private sealed class RecordingBuilder : IAddressSpaceBuilder + { + public Dictionary Alarms { get; } = new(StringComparer.OrdinalIgnoreCase); + + public IAddressSpaceBuilder Folder(string _, string __) => this; + + public IVariableHandle Variable(string _, string __, DriverAttributeInfo info) + => new Handle(info.FullName, Alarms); + + public void AddProperty(string _, DriverDataType __, object? ___) { } + + public sealed class Handle(string fullRef, Dictionary alarms) : IVariableHandle + { + public string FullReference { get; } = fullRef; + public IAlarmConditionSink MarkAsAlarmCondition(AlarmConditionInfo _) + { + var sink = new RecordingSink(); + alarms[FullReference] = sink; + return sink; + } + } + + public sealed class RecordingSink : IAlarmConditionSink + { + public List Received { get; } = new(); + public void OnTransition(AlarmEventArgs args) => Received.Add(args); + } + } +} diff --git a/tests/ZB.MOM.WW.OtOpcUa.Driver.Galaxy.E2E/RecordingAddressSpaceBuilder.cs b/tests/ZB.MOM.WW.OtOpcUa.Driver.Galaxy.E2E/RecordingAddressSpaceBuilder.cs index b6a1d08..461e421 100644 --- a/tests/ZB.MOM.WW.OtOpcUa.Driver.Galaxy.E2E/RecordingAddressSpaceBuilder.cs +++ b/tests/ZB.MOM.WW.OtOpcUa.Driver.Galaxy.E2E/RecordingAddressSpaceBuilder.cs @@ -12,6 +12,7 @@ public sealed class RecordingAddressSpaceBuilder : IAddressSpaceBuilder public List Folders { get; } = new(); public List Variables { get; } = new(); public List Properties { get; } = new(); + public List AlarmConditions { get; } = new(); public IAddressSpaceBuilder Folder(string browseName, string displayName) { @@ -22,7 +23,7 @@ public sealed class RecordingAddressSpaceBuilder : IAddressSpaceBuilder public IVariableHandle Variable(string browseName, string displayName, DriverAttributeInfo attributeInfo) { Variables.Add(new RecordedVariable(browseName, displayName, attributeInfo)); - return new RecordedVariableHandle(attributeInfo.FullName); + return new RecordedVariableHandle(attributeInfo.FullName, AlarmConditions); } public void AddProperty(string browseName, DriverDataType dataType, object? value) @@ -33,6 +34,37 @@ public sealed class RecordingAddressSpaceBuilder : IAddressSpaceBuilder public sealed record RecordedFolder(string BrowseName, string DisplayName); public sealed record RecordedVariable(string BrowseName, string DisplayName, DriverAttributeInfo AttributeInfo); public sealed record RecordedProperty(string BrowseName, DriverDataType DataType, object? Value); + public sealed record RecordedAlarmCondition(string SourceNodeId, AlarmConditionInfo Info); + public sealed record RecordedAlarmTransition(string SourceNodeId, AlarmEventArgs Args); - private sealed record RecordedVariableHandle(string FullReference) : IVariableHandle; + /// + /// Sink the tests assert on to verify the alarm event forwarder routed a transition + /// to the correct source-node-id. One entry per . + /// + public List AlarmTransitions { get; } = new(); + + private sealed class RecordedVariableHandle : IVariableHandle + { + private readonly List _conditions; + public string FullReference { get; } + public RecordedVariableHandle(string fullReference, List conditions) + { + FullReference = fullReference; + _conditions = conditions; + } + + public IAlarmConditionSink MarkAsAlarmCondition(AlarmConditionInfo info) + { + _conditions.Add(new RecordedAlarmCondition(FullReference, info)); + return new RecordingSink(FullReference); + } + + private sealed class RecordingSink : IAlarmConditionSink + { + public string SourceNodeId { get; } + public List Received { get; } = new(); + public RecordingSink(string sourceNodeId) => SourceNodeId = sourceNodeId; + public void OnTransition(AlarmEventArgs args) => Received.Add(args); + } + } } -- 2.49.1