Compare commits
4 Commits
phase-7-fu
...
phase-7-fu
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e11350cf80 | ||
| a5bd60768d | |||
|
|
d6a8bb1064 | ||
| f3053580a0 |
146
src/ZB.MOM.WW.OtOpcUa.Server/Phase7/DriverSubscriptionBridge.cs
Normal file
146
src/ZB.MOM.WW.OtOpcUa.Server/Phase7/DriverSubscriptionBridge.cs
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
||||||
|
|
||||||
|
namespace ZB.MOM.WW.OtOpcUa.Server.Phase7;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Phase 7 follow-up (task #244). Subscribes to live driver <see cref="ISubscribable"/>
|
||||||
|
/// surfaces for every input path the Phase 7 engines care about + pushes incoming
|
||||||
|
/// <see cref="DataChangeEventArgs.Snapshot"/>s into <see cref="CachedTagUpstreamSource"/>
|
||||||
|
/// so <c>ctx.GetTag</c> reads see the freshest driver value.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>
|
||||||
|
/// Each <see cref="DriverFeed"/> declares a driver + the path-to-fullRef map for the
|
||||||
|
/// attributes that driver provides. The bridge groups by driver so each <see cref="ISubscribable"/>
|
||||||
|
/// gets one <c>SubscribeAsync</c> call with a batched fullRef list — drivers that
|
||||||
|
/// poll under the hood (Modbus, AB CIP, S7) consolidate the polls; drivers with
|
||||||
|
/// native subscriptions (Galaxy, OPC UA Client, TwinCAT) get a single watch list.
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// Because driver fullRefs are opaque + driver-specific (Galaxy
|
||||||
|
/// <c>"DelmiaReceiver_001.Temp"</c>, Modbus <c>"40001"</c>, AB CIP
|
||||||
|
/// <c>"Temperature[0]"</c>), the bridge keeps a per-feed reverse map from fullRef
|
||||||
|
/// back to UNS path. <c>OnDataChange</c> fires keyed by fullRef; the bridge
|
||||||
|
/// translates to the script-side path before calling <see cref="CachedTagUpstreamSource.Push"/>.
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// Lifecycle: construct → <see cref="StartAsync"/> with the feeds → keep alive
|
||||||
|
/// alongside the engines → <see cref="DisposeAsync"/> unsubscribes from every
|
||||||
|
/// driver + unhooks the OnDataChange handlers. Driver subscriptions don't leak
|
||||||
|
/// even on abnormal shutdown because the disposal awaits each
|
||||||
|
/// <c>UnsubscribeAsync</c>.
|
||||||
|
/// </para>
|
||||||
|
/// </remarks>
|
||||||
|
public sealed class DriverSubscriptionBridge : IAsyncDisposable
|
||||||
|
{
|
||||||
|
private readonly CachedTagUpstreamSource _sink;
|
||||||
|
private readonly ILogger<DriverSubscriptionBridge> _logger;
|
||||||
|
private readonly List<ActiveSubscription> _active = [];
|
||||||
|
private bool _started;
|
||||||
|
private bool _disposed;
|
||||||
|
|
||||||
|
public DriverSubscriptionBridge(
|
||||||
|
CachedTagUpstreamSource sink,
|
||||||
|
ILogger<DriverSubscriptionBridge> logger)
|
||||||
|
{
|
||||||
|
_sink = sink ?? throw new ArgumentNullException(nameof(sink));
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Subscribe each feed's driver to its declared fullRefs + wire push-to-cache.
|
||||||
|
/// Idempotent guard rejects double-start. Throws on the first subscribe failure
|
||||||
|
/// so misconfiguration surfaces fast — partial-subscribe state doesn't linger.
|
||||||
|
/// </summary>
|
||||||
|
public async Task StartAsync(IEnumerable<DriverFeed> feeds, CancellationToken ct)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(feeds);
|
||||||
|
if (_disposed) throw new ObjectDisposedException(nameof(DriverSubscriptionBridge));
|
||||||
|
if (_started) throw new InvalidOperationException("DriverSubscriptionBridge already started");
|
||||||
|
_started = true;
|
||||||
|
|
||||||
|
foreach (var feed in feeds)
|
||||||
|
{
|
||||||
|
if (feed.PathToFullRef.Count == 0) continue;
|
||||||
|
|
||||||
|
// Reverse map for OnDataChange dispatch — driver fires keyed by FullReference,
|
||||||
|
// we push keyed by the script-side path.
|
||||||
|
var fullRefToPath = feed.PathToFullRef
|
||||||
|
.ToDictionary(kv => kv.Value, kv => kv.Key, StringComparer.Ordinal);
|
||||||
|
var fullRefs = feed.PathToFullRef.Values.Distinct(StringComparer.Ordinal).ToList();
|
||||||
|
|
||||||
|
EventHandler<DataChangeEventArgs> handler = (_, e) =>
|
||||||
|
{
|
||||||
|
if (fullRefToPath.TryGetValue(e.FullReference, out var unsPath))
|
||||||
|
_sink.Push(unsPath, e.Snapshot);
|
||||||
|
};
|
||||||
|
feed.Driver.OnDataChange += handler;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// OTOPCUA0001 suppression — the analyzer flags ISubscribable calls outside
|
||||||
|
// CapabilityInvoker. This bridge IS the lifecycle-coordinator for Phase 7
|
||||||
|
// subscriptions: it runs once at engine compose, doesn't hot-path per
|
||||||
|
// script evaluation (the engines read from the cache instead), and surfaces
|
||||||
|
// any subscribe failure by aborting bridge start. Wrapping in the per-call
|
||||||
|
// resilience pipeline would add nothing — there's no caller to retry on
|
||||||
|
// behalf of, and the breaker/bulkhead semantics belong to actual driver Read
|
||||||
|
// dispatch, which still goes through CapabilityInvoker via DriverNodeManager.
|
||||||
|
#pragma warning disable OTOPCUA0001
|
||||||
|
var handle = await feed.Driver.SubscribeAsync(fullRefs, feed.PublishingInterval, ct).ConfigureAwait(false);
|
||||||
|
#pragma warning restore OTOPCUA0001
|
||||||
|
_active.Add(new ActiveSubscription(feed.Driver, handle, handler));
|
||||||
|
_logger.LogInformation(
|
||||||
|
"Phase 7 bridge subscribed {Count} attribute(s) from driver {Driver} (handle {Handle})",
|
||||||
|
fullRefs.Count, feed.Driver.GetType().Name, handle.DiagnosticId);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
feed.Driver.OnDataChange -= handler;
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask DisposeAsync()
|
||||||
|
{
|
||||||
|
if (_disposed) return;
|
||||||
|
_disposed = true;
|
||||||
|
foreach (var sub in _active)
|
||||||
|
{
|
||||||
|
sub.Driver.OnDataChange -= sub.Handler;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
#pragma warning disable OTOPCUA0001 // bridge lifecycle — see StartAsync suppression rationale
|
||||||
|
await sub.Driver.UnsubscribeAsync(sub.Handle, CancellationToken.None).ConfigureAwait(false);
|
||||||
|
#pragma warning restore OTOPCUA0001
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Driver {Driver} UnsubscribeAsync threw on bridge dispose (handle {Handle})",
|
||||||
|
sub.Driver.GetType().Name, sub.Handle.DiagnosticId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_active.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
private sealed record ActiveSubscription(
|
||||||
|
ISubscribable Driver,
|
||||||
|
ISubscriptionHandle Handle,
|
||||||
|
EventHandler<DataChangeEventArgs> Handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// One driver's contribution to the Phase 7 bridge — the driver's <see cref="ISubscribable"/>
|
||||||
|
/// surface plus the path-to-fullRef map the bridge uses to translate driver-side
|
||||||
|
/// <see cref="DataChangeEventArgs.FullReference"/> back to script-side paths.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Driver">The driver's subscribable surface (every shipped driver implements <see cref="ISubscribable"/>).</param>
|
||||||
|
/// <param name="PathToFullRef">UNS path the script uses → driver-opaque fullRef. Empty map = nothing to subscribe (skipped).</param>
|
||||||
|
/// <param name="PublishingInterval">Forwarded to the driver's <see cref="ISubscribable.SubscribeAsync"/>.</param>
|
||||||
|
public sealed record DriverFeed(
|
||||||
|
ISubscribable Driver,
|
||||||
|
IReadOnlyDictionary<string, string> PathToFullRef,
|
||||||
|
TimeSpan PublishingInterval);
|
||||||
@@ -73,7 +73,7 @@ public static class Phase7EngineComposer
|
|||||||
disposables.Add(vtEngine);
|
disposables.Add(vtEngine);
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptedAlarmSource? alarmSource = null;
|
IReadable? alarmReadable = null;
|
||||||
if (scriptedAlarms.Count > 0)
|
if (scriptedAlarms.Count > 0)
|
||||||
{
|
{
|
||||||
var alarmDefs = ProjectScriptedAlarms(scriptedAlarms, scriptById).ToList();
|
var alarmDefs = ProjectScriptedAlarms(scriptedAlarms, scriptById).ToList();
|
||||||
@@ -83,17 +83,17 @@ public static class Phase7EngineComposer
|
|||||||
var engineLogger = loggerFactory.CreateLogger("Phase7HistorianRouter");
|
var engineLogger = loggerFactory.CreateLogger("Phase7HistorianRouter");
|
||||||
alarmEngine.OnEvent += (_, e) => _ = RouteToHistorianAsync(e, historianSink, engineLogger);
|
alarmEngine.OnEvent += (_, e) => _ = RouteToHistorianAsync(e, historianSink, engineLogger);
|
||||||
alarmEngine.LoadAsync(alarmDefs, CancellationToken.None).GetAwaiter().GetResult();
|
alarmEngine.LoadAsync(alarmDefs, CancellationToken.None).GetAwaiter().GetResult();
|
||||||
alarmSource = new ScriptedAlarmSource(alarmEngine);
|
var alarmSource = new ScriptedAlarmSource(alarmEngine);
|
||||||
|
// Task #245 — expose each alarm's current Active state as IReadable so OPC UA
|
||||||
|
// variable reads on Source=ScriptedAlarm nodes return the live predicate truth
|
||||||
|
// instead of BadNotFound. ScriptedAlarmSource stays registered as IAlarmSource
|
||||||
|
// for the event stream; the IReadable is a separate adapter over the same engine.
|
||||||
|
alarmReadable = new ScriptedAlarmReadable(alarmEngine);
|
||||||
disposables.Add(alarmEngine);
|
disposables.Add(alarmEngine);
|
||||||
disposables.Add(alarmSource);
|
disposables.Add(alarmSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScriptedAlarmSource is an IAlarmSource, not an IReadable — scripted-alarm
|
return new Phase7ComposedSources(vtSource, alarmReadable, disposables);
|
||||||
// variable-read dispatch (task #245) needs a dedicated engine-state adapter. Until
|
|
||||||
// that ships, reads against Source=ScriptedAlarm nodes return BadNotFound per the
|
|
||||||
// DriverNodeManager null-check path (the ADR-002 "misconfiguration not silent
|
|
||||||
// fallback" signal). The alarm event stream still fires via IAlarmSource.
|
|
||||||
return new Phase7ComposedSources(vtSource, ScriptedAlarmReadable: null, disposables);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static IEnumerable<VirtualTagDefinition> ProjectVirtualTags(
|
internal static IEnumerable<VirtualTagDefinition> ProjectVirtualTags(
|
||||||
|
|||||||
58
src/ZB.MOM.WW.OtOpcUa.Server/Phase7/ScriptedAlarmReadable.cs
Normal file
58
src/ZB.MOM.WW.OtOpcUa.Server/Phase7/ScriptedAlarmReadable.cs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
||||||
|
using ZB.MOM.WW.OtOpcUa.Core.ScriptedAlarms;
|
||||||
|
|
||||||
|
namespace ZB.MOM.WW.OtOpcUa.Server.Phase7;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// <see cref="IReadable"/> adapter exposing each scripted alarm's current
|
||||||
|
/// <see cref="AlarmActiveState"/> as an OPC UA boolean. Phase 7 follow-up (task #245).
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>
|
||||||
|
/// Paired with the <see cref="NodeSourceKind.ScriptedAlarm"/> dispatch in
|
||||||
|
/// <c>DriverNodeManager.OnReadValue</c>. Full-reference lookup is the
|
||||||
|
/// <c>ScriptedAlarmId</c> the walker wrote into <c>DriverAttributeInfo.FullName</c>
|
||||||
|
/// when emitting the alarm variable node.
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// Unknown alarm ids return <c>BadNodeIdUnknown</c> so misconfiguration surfaces
|
||||||
|
/// instead of silently reading <c>false</c>. Alarms whose predicate has never
|
||||||
|
/// been evaluated (brand new, before the engine's first cascade tick) report
|
||||||
|
/// <see cref="AlarmActiveState.Inactive"/> via <see cref="AlarmConditionState.Fresh"/>,
|
||||||
|
/// which matches the Part 9 initial-state semantics.
|
||||||
|
/// </para>
|
||||||
|
/// </remarks>
|
||||||
|
public sealed class ScriptedAlarmReadable : IReadable
|
||||||
|
{
|
||||||
|
/// <summary>OPC UA <c>StatusCodes.BadNodeIdUnknown</c> — kept local so we don't pull the OPC stack.</summary>
|
||||||
|
private const uint BadNodeIdUnknown = 0x80340000;
|
||||||
|
|
||||||
|
private readonly ScriptedAlarmEngine _engine;
|
||||||
|
|
||||||
|
public ScriptedAlarmReadable(ScriptedAlarmEngine engine)
|
||||||
|
{
|
||||||
|
_engine = engine ?? throw new ArgumentNullException(nameof(engine));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<IReadOnlyList<DataValueSnapshot>> ReadAsync(
|
||||||
|
IReadOnlyList<string> fullReferences, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(fullReferences);
|
||||||
|
|
||||||
|
var now = DateTime.UtcNow;
|
||||||
|
var results = new DataValueSnapshot[fullReferences.Count];
|
||||||
|
for (var i = 0; i < fullReferences.Count; i++)
|
||||||
|
{
|
||||||
|
var alarmId = fullReferences[i];
|
||||||
|
var state = _engine.GetState(alarmId);
|
||||||
|
if (state is null)
|
||||||
|
{
|
||||||
|
results[i] = new DataValueSnapshot(null, BadNodeIdUnknown, null, now);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
var active = state.Active == AlarmActiveState.Active;
|
||||||
|
results[i] = new DataValueSnapshot(active, 0u, now, now);
|
||||||
|
}
|
||||||
|
return Task.FromResult<IReadOnlyList<DataValueSnapshot>>(results);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,226 @@
|
|||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
using Shouldly;
|
||||||
|
using Xunit;
|
||||||
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
||||||
|
using ZB.MOM.WW.OtOpcUa.Server.Phase7;
|
||||||
|
|
||||||
|
namespace ZB.MOM.WW.OtOpcUa.Server.Tests.Phase7;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Task #244 — covers the bridge that pumps live driver <c>OnDataChange</c>
|
||||||
|
/// notifications into the Phase 7 <see cref="CachedTagUpstreamSource"/>.
|
||||||
|
/// </summary>
|
||||||
|
[Trait("Category", "Unit")]
|
||||||
|
public sealed class DriverSubscriptionBridgeTests
|
||||||
|
{
|
||||||
|
private sealed class FakeDriver : ISubscribable
|
||||||
|
{
|
||||||
|
public List<IReadOnlyList<string>> SubscribeCalls { get; } = [];
|
||||||
|
public List<ISubscriptionHandle> Unsubscribed { get; } = [];
|
||||||
|
public ISubscriptionHandle? LastHandle { get; private set; }
|
||||||
|
public event EventHandler<DataChangeEventArgs>? OnDataChange;
|
||||||
|
|
||||||
|
public Task<ISubscriptionHandle> SubscribeAsync(
|
||||||
|
IReadOnlyList<string> fullReferences, TimeSpan publishingInterval, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
SubscribeCalls.Add(fullReferences);
|
||||||
|
LastHandle = new Handle($"sub-{SubscribeCalls.Count}");
|
||||||
|
return Task.FromResult(LastHandle);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task UnsubscribeAsync(ISubscriptionHandle handle, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
Unsubscribed.Add(handle);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Fire(string fullRef, object value)
|
||||||
|
{
|
||||||
|
OnDataChange?.Invoke(this, new DataChangeEventArgs(
|
||||||
|
LastHandle!, fullRef,
|
||||||
|
new DataValueSnapshot(value, 0u, DateTime.UtcNow, DateTime.UtcNow)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private sealed record Handle(string DiagnosticId) : ISubscriptionHandle;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task StartAsync_calls_SubscribeAsync_with_distinct_fullRefs()
|
||||||
|
{
|
||||||
|
var sink = new CachedTagUpstreamSource();
|
||||||
|
var driver = new FakeDriver();
|
||||||
|
await using var bridge = new DriverSubscriptionBridge(sink, NullLogger<DriverSubscriptionBridge>.Instance);
|
||||||
|
|
||||||
|
await bridge.StartAsync(new[]
|
||||||
|
{
|
||||||
|
new DriverFeed(driver,
|
||||||
|
new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["/Site/L1/A/Temp"] = "DR.Temp",
|
||||||
|
["/Site/L1/A/Pressure"] = "DR.Pressure",
|
||||||
|
},
|
||||||
|
TimeSpan.FromSeconds(1)),
|
||||||
|
}, CancellationToken.None);
|
||||||
|
|
||||||
|
driver.SubscribeCalls.Count.ShouldBe(1);
|
||||||
|
driver.SubscribeCalls[0].ShouldContain("DR.Temp");
|
||||||
|
driver.SubscribeCalls[0].ShouldContain("DR.Pressure");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task OnDataChange_pushes_to_cache_keyed_by_UNS_path()
|
||||||
|
{
|
||||||
|
var sink = new CachedTagUpstreamSource();
|
||||||
|
var driver = new FakeDriver();
|
||||||
|
await using var bridge = new DriverSubscriptionBridge(sink, NullLogger<DriverSubscriptionBridge>.Instance);
|
||||||
|
|
||||||
|
await bridge.StartAsync(new[]
|
||||||
|
{
|
||||||
|
new DriverFeed(driver,
|
||||||
|
new Dictionary<string, string> { ["/Site/L1/A/Temp"] = "DR.Temp" },
|
||||||
|
TimeSpan.FromSeconds(1)),
|
||||||
|
}, CancellationToken.None);
|
||||||
|
|
||||||
|
driver.Fire("DR.Temp", 42.5);
|
||||||
|
|
||||||
|
sink.ReadTag("/Site/L1/A/Temp").Value.ShouldBe(42.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task OnDataChange_with_unmapped_fullRef_is_ignored()
|
||||||
|
{
|
||||||
|
var sink = new CachedTagUpstreamSource();
|
||||||
|
var driver = new FakeDriver();
|
||||||
|
await using var bridge = new DriverSubscriptionBridge(sink, NullLogger<DriverSubscriptionBridge>.Instance);
|
||||||
|
|
||||||
|
await bridge.StartAsync(new[]
|
||||||
|
{
|
||||||
|
new DriverFeed(driver,
|
||||||
|
new Dictionary<string, string> { ["/p"] = "DR.A" },
|
||||||
|
TimeSpan.FromSeconds(1)),
|
||||||
|
}, CancellationToken.None);
|
||||||
|
|
||||||
|
driver.Fire("DR.B", 99); // not in map
|
||||||
|
|
||||||
|
sink.ReadTag("/p").StatusCode.ShouldBe(CachedTagUpstreamSource.UpstreamNotConfigured,
|
||||||
|
"unmapped fullRef shouldn't pollute the cache");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Empty_PathToFullRef_skips_SubscribeAsync_call()
|
||||||
|
{
|
||||||
|
var sink = new CachedTagUpstreamSource();
|
||||||
|
var driver = new FakeDriver();
|
||||||
|
await using var bridge = new DriverSubscriptionBridge(sink, NullLogger<DriverSubscriptionBridge>.Instance);
|
||||||
|
|
||||||
|
await bridge.StartAsync(new[]
|
||||||
|
{
|
||||||
|
new DriverFeed(driver, new Dictionary<string, string>(), TimeSpan.FromSeconds(1)),
|
||||||
|
}, CancellationToken.None);
|
||||||
|
|
||||||
|
driver.SubscribeCalls.ShouldBeEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DisposeAsync_unsubscribes_each_active_subscription()
|
||||||
|
{
|
||||||
|
var sink = new CachedTagUpstreamSource();
|
||||||
|
var driver = new FakeDriver();
|
||||||
|
var bridge = new DriverSubscriptionBridge(sink, NullLogger<DriverSubscriptionBridge>.Instance);
|
||||||
|
|
||||||
|
await bridge.StartAsync(new[]
|
||||||
|
{
|
||||||
|
new DriverFeed(driver,
|
||||||
|
new Dictionary<string, string> { ["/p"] = "DR.A" },
|
||||||
|
TimeSpan.FromSeconds(1)),
|
||||||
|
}, CancellationToken.None);
|
||||||
|
|
||||||
|
await bridge.DisposeAsync();
|
||||||
|
|
||||||
|
driver.Unsubscribed.Count.ShouldBe(1);
|
||||||
|
driver.Unsubscribed[0].ShouldBeSameAs(driver.LastHandle);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DisposeAsync_unhooks_OnDataChange_so_post_dispose_events_dont_push()
|
||||||
|
{
|
||||||
|
var sink = new CachedTagUpstreamSource();
|
||||||
|
var driver = new FakeDriver();
|
||||||
|
var bridge = new DriverSubscriptionBridge(sink, NullLogger<DriverSubscriptionBridge>.Instance);
|
||||||
|
|
||||||
|
await bridge.StartAsync(new[]
|
||||||
|
{
|
||||||
|
new DriverFeed(driver,
|
||||||
|
new Dictionary<string, string> { ["/p"] = "DR.A" },
|
||||||
|
TimeSpan.FromSeconds(1)),
|
||||||
|
}, CancellationToken.None);
|
||||||
|
|
||||||
|
await bridge.DisposeAsync();
|
||||||
|
driver.Fire("DR.A", 999); // post-dispose event
|
||||||
|
|
||||||
|
sink.ReadTag("/p").StatusCode.ShouldBe(CachedTagUpstreamSource.UpstreamNotConfigured);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task StartAsync_called_twice_throws()
|
||||||
|
{
|
||||||
|
var sink = new CachedTagUpstreamSource();
|
||||||
|
await using var bridge = new DriverSubscriptionBridge(sink, NullLogger<DriverSubscriptionBridge>.Instance);
|
||||||
|
await bridge.StartAsync(Array.Empty<DriverFeed>(), CancellationToken.None);
|
||||||
|
|
||||||
|
await Should.ThrowAsync<InvalidOperationException>(
|
||||||
|
() => bridge.StartAsync(Array.Empty<DriverFeed>(), CancellationToken.None));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DisposeAsync_is_idempotent()
|
||||||
|
{
|
||||||
|
var sink = new CachedTagUpstreamSource();
|
||||||
|
var bridge = new DriverSubscriptionBridge(sink, NullLogger<DriverSubscriptionBridge>.Instance);
|
||||||
|
await bridge.DisposeAsync();
|
||||||
|
await bridge.DisposeAsync(); // must not throw
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Subscribe_failure_unhooks_handler_and_propagates()
|
||||||
|
{
|
||||||
|
var sink = new CachedTagUpstreamSource();
|
||||||
|
var failingDriver = new ThrowingDriver();
|
||||||
|
await using var bridge = new DriverSubscriptionBridge(sink, NullLogger<DriverSubscriptionBridge>.Instance);
|
||||||
|
|
||||||
|
var feeds = new[]
|
||||||
|
{
|
||||||
|
new DriverFeed(failingDriver,
|
||||||
|
new Dictionary<string, string> { ["/p"] = "DR.A" },
|
||||||
|
TimeSpan.FromSeconds(1)),
|
||||||
|
};
|
||||||
|
|
||||||
|
await Should.ThrowAsync<InvalidOperationException>(
|
||||||
|
() => bridge.StartAsync(feeds, CancellationToken.None));
|
||||||
|
|
||||||
|
// Handler should be unhooked — firing now would NPE if it wasn't (event has 0 subs).
|
||||||
|
failingDriver.HasAnyHandlers.ShouldBeFalse(
|
||||||
|
"handler must be removed when SubscribeAsync throws so it doesn't leak");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Null_sink_or_logger_rejected()
|
||||||
|
{
|
||||||
|
Should.Throw<ArgumentNullException>(() => new DriverSubscriptionBridge(null!, NullLogger<DriverSubscriptionBridge>.Instance));
|
||||||
|
Should.Throw<ArgumentNullException>(() => new DriverSubscriptionBridge(new CachedTagUpstreamSource(), null!));
|
||||||
|
}
|
||||||
|
|
||||||
|
private sealed class ThrowingDriver : ISubscribable
|
||||||
|
{
|
||||||
|
private EventHandler<DataChangeEventArgs>? _handler;
|
||||||
|
public bool HasAnyHandlers => _handler is not null;
|
||||||
|
public event EventHandler<DataChangeEventArgs>? OnDataChange
|
||||||
|
{
|
||||||
|
add => _handler = (EventHandler<DataChangeEventArgs>?)Delegate.Combine(_handler, value);
|
||||||
|
remove => _handler = (EventHandler<DataChangeEventArgs>?)Delegate.Remove(_handler, value);
|
||||||
|
}
|
||||||
|
public Task<ISubscriptionHandle> SubscribeAsync(IReadOnlyList<string> _, TimeSpan __, CancellationToken ___) =>
|
||||||
|
throw new InvalidOperationException("driver offline");
|
||||||
|
public Task UnsubscribeAsync(ISubscriptionHandle _, CancellationToken __) => Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -72,9 +72,28 @@ public sealed class Phase7EngineComposerTests
|
|||||||
loggerFactory: NullLoggerFactory.Instance);
|
loggerFactory: NullLoggerFactory.Instance);
|
||||||
|
|
||||||
result.VirtualReadable.ShouldNotBeNull();
|
result.VirtualReadable.ShouldNotBeNull();
|
||||||
|
result.ScriptedAlarmReadable.ShouldBeNull("no alarms configured");
|
||||||
result.Disposables.Count.ShouldBeGreaterThan(0);
|
result.Disposables.Count.ShouldBeGreaterThan(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Compose_ScriptedAlarm_rows_returns_non_null_ScriptedAlarmReadable()
|
||||||
|
{
|
||||||
|
var scripts = new[] { ScriptRow("scr-1", "return false;") };
|
||||||
|
var alarms = new[] { AlarmRow("al-1", "scr-1") };
|
||||||
|
|
||||||
|
var result = Phase7EngineComposer.Compose(
|
||||||
|
scripts, [], alarms,
|
||||||
|
upstream: new CachedTagUpstreamSource(),
|
||||||
|
alarmStateStore: new InMemoryAlarmStateStore(),
|
||||||
|
historianSink: NullAlarmHistorianSink.Instance,
|
||||||
|
rootScriptLogger: new LoggerConfiguration().CreateLogger(),
|
||||||
|
loggerFactory: NullLoggerFactory.Instance);
|
||||||
|
|
||||||
|
result.ScriptedAlarmReadable.ShouldNotBeNull("task #245 — alarm Active state readable");
|
||||||
|
result.VirtualReadable.ShouldBeNull();
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Compose_missing_script_reference_throws_with_actionable_message()
|
public void Compose_missing_script_reference_throws_with_actionable_message()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,120 @@
|
|||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
using Serilog;
|
||||||
|
using Shouldly;
|
||||||
|
using Xunit;
|
||||||
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
||||||
|
using ZB.MOM.WW.OtOpcUa.Core.ScriptedAlarms;
|
||||||
|
using ZB.MOM.WW.OtOpcUa.Core.Scripting;
|
||||||
|
using ZB.MOM.WW.OtOpcUa.Server.Phase7;
|
||||||
|
|
||||||
|
namespace ZB.MOM.WW.OtOpcUa.Server.Tests.Phase7;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Task #245 — covers the IReadable adapter that surfaces each scripted alarm's
|
||||||
|
/// live <c>ActiveState</c> so OPC UA variable reads on Source=ScriptedAlarm nodes
|
||||||
|
/// return the predicate truth instead of BadNotFound.
|
||||||
|
/// </summary>
|
||||||
|
[Trait("Category", "Unit")]
|
||||||
|
public sealed class ScriptedAlarmReadableTests
|
||||||
|
{
|
||||||
|
private static (ScriptedAlarmEngine engine, CachedTagUpstreamSource upstream) BuildEngineWith(
|
||||||
|
params (string alarmId, string predicateSource)[] alarms)
|
||||||
|
{
|
||||||
|
var upstream = new CachedTagUpstreamSource();
|
||||||
|
var logger = new LoggerConfiguration().CreateLogger();
|
||||||
|
var factory = new ScriptLoggerFactory(logger);
|
||||||
|
var engine = new ScriptedAlarmEngine(upstream, new InMemoryAlarmStateStore(), factory, logger);
|
||||||
|
var defs = alarms.Select(a => new ScriptedAlarmDefinition(
|
||||||
|
AlarmId: a.alarmId,
|
||||||
|
EquipmentPath: "/eq",
|
||||||
|
AlarmName: a.alarmId,
|
||||||
|
Kind: AlarmKind.LimitAlarm,
|
||||||
|
Severity: AlarmSeverity.Medium,
|
||||||
|
MessageTemplate: "x",
|
||||||
|
PredicateScriptSource: a.predicateSource)).ToList();
|
||||||
|
engine.LoadAsync(defs, CancellationToken.None).GetAwaiter().GetResult();
|
||||||
|
return (engine, upstream);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Reads_return_false_for_newly_loaded_alarm_with_inactive_predicate()
|
||||||
|
{
|
||||||
|
var (engine, _) = BuildEngineWith(("a1", "return false;"));
|
||||||
|
using var _e = engine;
|
||||||
|
var readable = new ScriptedAlarmReadable(engine);
|
||||||
|
|
||||||
|
var result = await readable.ReadAsync(["a1"], CancellationToken.None);
|
||||||
|
|
||||||
|
result.Count.ShouldBe(1);
|
||||||
|
result[0].Value.ShouldBe(false);
|
||||||
|
result[0].StatusCode.ShouldBe(0u, "Good quality when the engine has state");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Reads_return_true_when_predicate_evaluates_to_active()
|
||||||
|
{
|
||||||
|
var (engine, upstream) = BuildEngineWith(
|
||||||
|
("tempAlarm", "return ctx.GetTag(\"/Site/Line/Cell/Temp\").Value is double d && d > 100;"));
|
||||||
|
using var _e = engine;
|
||||||
|
|
||||||
|
// Seed the upstream value + nudge the engine so the alarm transitions to Active.
|
||||||
|
upstream.Push("/Site/Line/Cell/Temp",
|
||||||
|
new DataValueSnapshot(150.0, 0u, DateTime.UtcNow, DateTime.UtcNow));
|
||||||
|
|
||||||
|
// Allow the engine's change-driven cascade to run.
|
||||||
|
await Task.Delay(50);
|
||||||
|
|
||||||
|
var readable = new ScriptedAlarmReadable(engine);
|
||||||
|
var result = await readable.ReadAsync(["tempAlarm"], CancellationToken.None);
|
||||||
|
|
||||||
|
result[0].Value.ShouldBe(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Reads_return_BadNodeIdUnknown_for_missing_alarm()
|
||||||
|
{
|
||||||
|
var (engine, _) = BuildEngineWith(("a1", "return false;"));
|
||||||
|
using var _e = engine;
|
||||||
|
var readable = new ScriptedAlarmReadable(engine);
|
||||||
|
|
||||||
|
var result = await readable.ReadAsync(["a-not-loaded"], CancellationToken.None);
|
||||||
|
|
||||||
|
result[0].Value.ShouldBeNull();
|
||||||
|
result[0].StatusCode.ShouldBe(0x80340000u,
|
||||||
|
"BadNodeIdUnknown surfaces a misconfiguration, not a silent false");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Reads_batch_round_trip_preserves_order()
|
||||||
|
{
|
||||||
|
var (engine, _) = BuildEngineWith(
|
||||||
|
("a1", "return false;"),
|
||||||
|
("a2", "return false;"));
|
||||||
|
using var _e = engine;
|
||||||
|
var readable = new ScriptedAlarmReadable(engine);
|
||||||
|
|
||||||
|
var result = await readable.ReadAsync(["a2", "missing", "a1"], CancellationToken.None);
|
||||||
|
|
||||||
|
result.Count.ShouldBe(3);
|
||||||
|
result[0].Value.ShouldBe(false); // a2
|
||||||
|
result[1].StatusCode.ShouldBe(0x80340000u); // missing
|
||||||
|
result[2].Value.ShouldBe(false); // a1
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Null_engine_rejected()
|
||||||
|
{
|
||||||
|
Should.Throw<ArgumentNullException>(() => new ScriptedAlarmReadable(null!));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Null_fullReferences_rejected()
|
||||||
|
{
|
||||||
|
var (engine, _) = BuildEngineWith(("a1", "return false;"));
|
||||||
|
using var _e = engine;
|
||||||
|
var readable = new ScriptedAlarmReadable(engine);
|
||||||
|
|
||||||
|
await Should.ThrowAsync<ArgumentNullException>(
|
||||||
|
() => readable.ReadAsync(null!, CancellationToken.None));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user