feat(grpc-data): A1 — AbCip signals per node at the end of each ALMD/ALMA poll tick
This commit is contained in:
@@ -196,12 +196,16 @@ internal sealed class AbCipAlarmProjection : IAsyncDisposable
|
||||
{
|
||||
// results index layout: per sourceNode, ALMD = [InAlarm, Severity]; ALMA = the plan's member list.
|
||||
var cursor = 0;
|
||||
// grpc-data A1 (§5.1 AbCip row, G1): the nodes whose state this tick established — ALMD: InAlarm read Good;
|
||||
// ALMA: every plan ref read Good. Signalled per node at the end of the tick, after every transition (F2).
|
||||
var established = new List<string>(sub.SourceNodeIds.Count);
|
||||
for (var i = 0; i < sub.SourceNodeIds.Count; i++)
|
||||
{
|
||||
var nodeId = sub.SourceNodeIds[i];
|
||||
var plan = sub.Plans[i];
|
||||
if (plan.Alma is not null)
|
||||
{
|
||||
if (AllGood(results, cursor, plan.Alma.RefCount)) established.Add(nodeId);
|
||||
TickAlma(sub, nodeId, plan.Alma, results, cursor);
|
||||
cursor += plan.Alma.RefCount;
|
||||
continue;
|
||||
@@ -210,6 +214,7 @@ internal sealed class AbCipAlarmProjection : IAsyncDisposable
|
||||
var severityDv = results[cursor + 1];
|
||||
cursor += 2;
|
||||
if (inAlarmDv.StatusCode != AbCipStatusMapper.Good) continue;
|
||||
established.Add(nodeId);
|
||||
|
||||
var nowFaulted = ToBool(inAlarmDv.Value);
|
||||
// severityDv.StatusCode is not checked here. When the Severity read is Bad (value null),
|
||||
@@ -246,6 +251,18 @@ internal sealed class AbCipAlarmProjection : IAsyncDisposable
|
||||
Kind: AlarmTransitionKind.Clear));
|
||||
}
|
||||
}
|
||||
|
||||
// G5: only a subscription that is still live signals (an unsubscribe / supersede raises nothing).
|
||||
bool live;
|
||||
lock (_subsLock) live = _subs.TryGetValue(sub.Handle.Id, out var current) && ReferenceEquals(current, sub);
|
||||
if (live && established.Count > 0) _driver.InvokeInitialAlarmState(new AlarmInitialStateEventArgs(established));
|
||||
}
|
||||
|
||||
private static bool AllGood(IReadOnlyList<DataValueSnapshot> results, int at, int count)
|
||||
{
|
||||
for (var k = 0; k < count; k++)
|
||||
if (results[at + k].StatusCode != AbCipStatusMapper.Good) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private async Task RunPollLoopAsync(Subscription sub, CancellationToken ct)
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip;
|
||||
/// handles GC-collected tags.</para>
|
||||
/// </remarks>
|
||||
public sealed class AbCipDriver : IDriver, IReadable, IWritable, ITagDiscovery, IBatchSubscribable,
|
||||
IHostConnectivityProbe, IPerCallHostResolver, IAlarmSource, ISkippedTagReporter, IAsyncDisposable, IDisposable
|
||||
IHostConnectivityProbe, IPerCallHostResolver, IAlarmSource, IAlarmInitialStateSignal, ISkippedTagReporter, IAsyncDisposable, IDisposable
|
||||
{
|
||||
/// <summary>Authored tags this driver could not build into its poll table.</summary>
|
||||
private readonly SkippedTagLedger _skippedTags = new();
|
||||
@@ -183,6 +183,13 @@ public sealed class AbCipDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
||||
/// <param name="args">The alarm event arguments.</param>
|
||||
internal void InvokeAlarmEvent(AlarmEventArgs args) => OnAlarmEvent?.Invoke(this, args);
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <remarks>grpc-data A1 (§5.1 AbCip row): raised at the end of each alarm poll tick for the nodes whose
|
||||
/// <c>InAlarm</c> (ALMA: every plan ref) read Good, after that tick's transitions.</remarks>
|
||||
public event EventHandler<AlarmInitialStateEventArgs>? InitialAlarmStateDelivered;
|
||||
|
||||
internal void InvokeInitialAlarmState(AlarmInitialStateEventArgs args) => InitialAlarmStateDelivered?.Invoke(this, args);
|
||||
|
||||
/// <summary>Initializes a new instance of the <see cref="AbCipDriver"/> class.</summary>
|
||||
/// <param name="options">The driver configuration options.</param>
|
||||
/// <param name="driverInstanceId">A unique identifier for this driver instance.</param>
|
||||
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// grpc-data A1 (§5.1 AbCip row): the real ALMD poll loop raises
|
||||
/// <see cref="IAlarmInitialStateSignal.InitialAlarmStateDelivered"/> at the end of each tick for the nodes whose
|
||||
/// <c>InAlarm</c> read Good, after that tick's transitions (ii); a node on an unreachable controller is excluded
|
||||
/// and holds back nothing (G1, viii); an unsubscribed projection signals nothing (G5).
|
||||
/// </summary>
|
||||
[Trait("Category", "Unit")]
|
||||
[Trait("Feature", "GrpcData")]
|
||||
[Trait("Phase", "A1")]
|
||||
public sealed class AbCipInitialAlarmStateSignalTests
|
||||
{
|
||||
private const string DeviceA = "ab://10.0.0.5/1,0";
|
||||
private const string DeviceB = "ab://10.0.0.6/1,0";
|
||||
private const string TankA = "TankA_Alm";
|
||||
private const string TankB = "TankB_Alm";
|
||||
|
||||
private static CancellationToken Ct => TestContext.Current.CancellationToken;
|
||||
|
||||
private static AbCipTagDefinition Almd(string name, string device) => new(
|
||||
name, device, name, AbCipDataType.Structure,
|
||||
Members: [new AbCipStructureMember("InAlarm", AbCipDataType.Bool), new AbCipStructureMember("Acked", AbCipDataType.Bool),
|
||||
new AbCipStructureMember("Severity", AbCipDataType.DInt)]);
|
||||
|
||||
private static AbCipDriver NewDriver(FakeAbCipTagFactory factory, params AbCipTagDefinition[] tags) => new(new AbCipDriverOptions
|
||||
{
|
||||
Devices = [new AbCipDeviceOptions(DeviceA), new AbCipDeviceOptions(DeviceB)],
|
||||
RawTags = AbCipRawTags.From(tags),
|
||||
EnableAlarmProjection = true,
|
||||
AlarmPollInterval = TimeSpan.FromMilliseconds(20),
|
||||
}, "drv-1", factory);
|
||||
|
||||
/// <summary>(ii) a standing alarm's Raise precedes the signal that covers it.</summary>
|
||||
[Fact]
|
||||
public async Task Signal_follows_the_ticks_transitions_and_covers_the_Good_nodes()
|
||||
{
|
||||
var factory = new FakeAbCipTagFactory
|
||||
{
|
||||
Customise = p => new FakeAbCipTag(p) { Value = p.TagName.EndsWith(".InAlarm", StringComparison.Ordinal) ? true : 500 },
|
||||
};
|
||||
var drv = NewDriver(factory, Almd(TankA, DeviceA));
|
||||
await drv.InitializeAsync("{}", Ct);
|
||||
var trace = Record(drv);
|
||||
var handle = await drv.SubscribeAlarmsAsync([TankA], Ct);
|
||||
|
||||
await WaitFor(() => Snapshot(trace).Any(t => t.StartsWith("signal", StringComparison.Ordinal)));
|
||||
await drv.UnsubscribeAlarmsAsync(handle, Ct);
|
||||
await drv.ShutdownAsync(Ct);
|
||||
|
||||
var lines = Snapshot(trace);
|
||||
var first = Array.FindIndex(lines, t => t.StartsWith("signal", StringComparison.Ordinal));
|
||||
lines[..first].ShouldBe(["event:" + TankA + ":Raise"]);
|
||||
lines[first].ShouldBe("signal:" + TankA);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// (viii) G1: two ALMD nodes on two controllers, B's reads failing. Every signal names A only, and A keeps
|
||||
/// signalling every tick.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task Unreachable_controller_node_is_excluded_and_holds_back_nothing()
|
||||
{
|
||||
var factory = new FakeAbCipTagFactory
|
||||
{
|
||||
Customise = p => new FakeAbCipTag(p)
|
||||
{
|
||||
Value = p.TagName.EndsWith(".InAlarm", StringComparison.Ordinal) ? false : 500,
|
||||
ThrowOnRead = p.Gateway.Contains("10.0.0.6", StringComparison.Ordinal),
|
||||
},
|
||||
};
|
||||
var drv = NewDriver(factory, Almd(TankA, DeviceA), Almd(TankB, DeviceB));
|
||||
await drv.InitializeAsync("{}", Ct);
|
||||
var trace = Record(drv);
|
||||
var handle = await drv.SubscribeAlarmsAsync([TankA, TankB], Ct);
|
||||
|
||||
await WaitFor(() => Snapshot(trace).Count(t => t.StartsWith("signal", StringComparison.Ordinal)) >= 3);
|
||||
await drv.UnsubscribeAlarmsAsync(handle, Ct);
|
||||
await drv.ShutdownAsync(Ct);
|
||||
|
||||
Snapshot(trace).Where(t => t.StartsWith("signal", StringComparison.Ordinal))
|
||||
.ShouldAllBe(s => s == "signal:" + TankA, "B's InAlarm never read Good");
|
||||
}
|
||||
|
||||
/// <summary>(xi) G5: a tick for a subscription that is no longer live raises no signal.</summary>
|
||||
[Fact]
|
||||
public async Task Released_subscription_signals_nothing()
|
||||
{
|
||||
var factory = new FakeAbCipTagFactory();
|
||||
var drv = NewDriver(factory, Almd(TankA, DeviceA));
|
||||
await drv.InitializeAsync("{}", Ct);
|
||||
var signals = new List<AlarmInitialStateEventArgs>();
|
||||
drv.InitialAlarmStateDelivered += (_, e) => { lock (signals) signals.Add(e); };
|
||||
var plans = new AbCipAlarmProjection(drv, TimeSpan.FromHours(1));
|
||||
var stale = new AbCipAlarmProjection.Subscription(new AbCipAlarmSubscriptionHandle(99), [TankA], new CancellationTokenSource());
|
||||
stale.Plans = [new AbCipAlarmProjection.NodePlan(null)];
|
||||
|
||||
Should.NotThrow(() => plans.Tick(stale, [new DataValueSnapshot(false, AbCipStatusMapper.Good, null, DateTime.UtcNow),
|
||||
new DataValueSnapshot(500, AbCipStatusMapper.Good, null, DateTime.UtcNow)]));
|
||||
signals.ShouldBeEmpty("the subscription was never (or is no longer) registered");
|
||||
await drv.ShutdownAsync(Ct);
|
||||
}
|
||||
|
||||
/// <summary>(xi) G5: after the alarm subscription is released no further signal is raised.</summary>
|
||||
[Fact]
|
||||
public async Task No_signal_after_the_alarm_subscription_is_released()
|
||||
{
|
||||
var factory = new FakeAbCipTagFactory
|
||||
{
|
||||
Customise = p => new FakeAbCipTag(p) { Value = p.TagName.EndsWith(".InAlarm", StringComparison.Ordinal) ? false : 500 },
|
||||
};
|
||||
var drv = NewDriver(factory, Almd(TankA, DeviceA));
|
||||
await drv.InitializeAsync("{}", Ct);
|
||||
var trace = Record(drv);
|
||||
var handle = await drv.SubscribeAlarmsAsync([TankA], Ct);
|
||||
await WaitFor(() => Snapshot(trace).Length >= 2);
|
||||
|
||||
await drv.UnsubscribeAlarmsAsync(handle, Ct);
|
||||
var after = Snapshot(trace).Length;
|
||||
await Task.Delay(200, Ct); // ten poll intervals
|
||||
Snapshot(trace).Length.ShouldBe(after);
|
||||
await drv.ShutdownAsync(Ct);
|
||||
}
|
||||
|
||||
private static List<string> Record(AbCipDriver drv)
|
||||
{
|
||||
var trace = new List<string>();
|
||||
drv.OnAlarmEvent += (_, e) => { lock (trace) trace.Add($"event:{e.ConditionId}:{e.Kind}"); };
|
||||
drv.InitialAlarmStateDelivered += (_, e) =>
|
||||
{
|
||||
lock (trace) trace.Add("signal:" + string.Join(',', e.ConditionIds));
|
||||
};
|
||||
return trace;
|
||||
}
|
||||
|
||||
private static string[] Snapshot(List<string> trace)
|
||||
{
|
||||
lock (trace) return [.. trace];
|
||||
}
|
||||
|
||||
private static async Task WaitFor(Func<bool> cond)
|
||||
{
|
||||
var deadline = DateTime.UtcNow.AddSeconds(5);
|
||||
while (DateTime.UtcNow < deadline) { if (cond()) return; await Task.Delay(5, Ct); }
|
||||
throw new TimeoutException("condition never met");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user