feat(grpc-data): A1 — MTConnect signals after each full condition emission over a /current baseline
This commit is contained in:
@@ -107,6 +107,7 @@ internal sealed class MTConnectConditionTracker
|
||||
public IReadOnlySet<string> Apply(MTConnectStreamsResult document, bool baseline)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(document);
|
||||
if (baseline) HasBaseline = true;
|
||||
|
||||
var touched = new HashSet<string>(StringComparer.Ordinal);
|
||||
if (document.Observations is not { Count: > 0 }) return touched;
|
||||
@@ -135,6 +136,12 @@ internal sealed class MTConnectConditionTracker
|
||||
return touched;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether this tracker holds a <c>/current</c> baseline (grpc-data A1): only then is an absent or Normal
|
||||
/// condition an established state rather than "never read". Reset by <see cref="Clear"/>.
|
||||
/// </summary>
|
||||
public bool HasBaseline { get; private set; }
|
||||
|
||||
/// <summary>The derived state of one DataItem, or <c>null</c> when it has never been observed as a condition.</summary>
|
||||
/// <param name="dataItemId">The DataItem id.</param>
|
||||
/// <returns>The derived state, or null when the item was never observed.</returns>
|
||||
@@ -173,6 +180,7 @@ internal sealed class MTConnectConditionTracker
|
||||
public void Clear()
|
||||
{
|
||||
lock (_lock) _items.Clear();
|
||||
HasBaseline = false;
|
||||
}
|
||||
|
||||
/// <summary>Maps a parsed condition state word onto a level (unknown vendor words ⇒ Warning).</summary>
|
||||
|
||||
@@ -44,7 +44,7 @@ namespace ZB.MOM.WW.OtOpcUa.Driver.MTConnect;
|
||||
/// quality lifted (a resumed <c>/sample</c> carries only what changed).
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public sealed partial class MTConnectDriver : IAlarmSource
|
||||
public sealed partial class MTConnectDriver : IAlarmSource, IAlarmInitialStateSignal
|
||||
{
|
||||
/// <summary>Serializes condition-alarm diffing and delivery. See the class remarks.</summary>
|
||||
private readonly Lock _alarmLock = new();
|
||||
@@ -73,6 +73,15 @@ public sealed partial class MTConnectDriver : IAlarmSource
|
||||
/// <inheritdoc/>
|
||||
public event EventHandler<AlarmEventArgs>? OnAlarmEvent;
|
||||
|
||||
/// <inheritdoc/>
|
||||
/// <remarks>
|
||||
/// grpc-data A1 (§5.1 MTConnect row): raised after a FULL emission (the resync in
|
||||
/// <see cref="SubscribeAlarmsAsync"/>, the restarted pump's first act, comms-loss recovery) over a tracker
|
||||
/// built from a <c>/current</c> baseline, covering every subscribed condition whose level is known and not
|
||||
/// <c>Unavailable</c> (an Unavailable item is the device not reporting — it stays unknown).
|
||||
/// </remarks>
|
||||
public event EventHandler<AlarmInitialStateEventArgs>? InitialAlarmStateDelivered;
|
||||
|
||||
/// <summary>The condition tracker the pump folds CONDITION observations into. Exposed for tests.</summary>
|
||||
internal MTConnectConditionTracker ConditionTracker => Volatile.Read(ref _conditions);
|
||||
|
||||
@@ -171,9 +180,13 @@ public sealed partial class MTConnectDriver : IAlarmSource
|
||||
/// Only conditions bound to these DataItems are evaluated; <c>null</c> evaluates every subscribed one.
|
||||
/// </param>
|
||||
/// <param name="resync">Whether this is a (re)subscribe — see <see cref="MTConnectConditionAlarmMapper"/>.</param>
|
||||
private void EmitConditionTransitions(IReadOnlySet<string>? touchedDataItemIds, bool resync)
|
||||
/// <param name="baselineRead">
|
||||
/// grpc-data A1: the touched set came from a <c>/current</c> re-baseline — every subscribed condition's state is
|
||||
/// then established, so the signal covers all of them, not just the touched ones.
|
||||
/// </param>
|
||||
private void EmitConditionTransitions(IReadOnlySet<string>? touchedDataItemIds, bool resync, bool baselineRead = false)
|
||||
{
|
||||
if (touchedDataItemIds is { Count: 0 }) return;
|
||||
if (touchedDataItemIds is { Count: 0 } && !baselineRead) return;
|
||||
|
||||
var subscription = Volatile.Read(ref _alarmSubscription);
|
||||
if (subscription is not { References.Count: > 0 }) return;
|
||||
@@ -205,6 +218,42 @@ public sealed partial class MTConnectDriver : IAlarmSource
|
||||
|
||||
RaiseAlarmEvent(handlers, evt, conditionId);
|
||||
}
|
||||
|
||||
// grpc-data A1: a full emission (or a re-baseline) over a /current baseline establishes every subscribed
|
||||
// condition it can judge — known and not Unavailable. After every event of this emission, under the same
|
||||
// lock and handler gate (F2, G5).
|
||||
if ((touchedDataItemIds is null || baselineRead) && tracker.HasBaseline)
|
||||
{
|
||||
var established = new List<string>();
|
||||
foreach (var conditionId in subscription.References)
|
||||
{
|
||||
if (tracker.Get(binding.ToDataItemId(conditionId)) is { } state
|
||||
&& state.Level != MTConnectConditionLevel.Unavailable)
|
||||
established.Add(conditionId);
|
||||
}
|
||||
|
||||
if (established.Count > 0) RaiseInitialAlarmState(established);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Invokes every initial-state handler; a faulting one is logged. Caller holds <see cref="_alarmLock"/>.</summary>
|
||||
private void RaiseInitialAlarmState(IReadOnlyList<string> conditionIds)
|
||||
{
|
||||
if (InitialAlarmStateDelivered is not { } signal) return;
|
||||
var args = new AlarmInitialStateEventArgs(conditionIds);
|
||||
foreach (var registration in signal.GetInvocationList())
|
||||
{
|
||||
try
|
||||
{
|
||||
((EventHandler<AlarmInitialStateEventArgs>)registration).Invoke(this, args);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex,
|
||||
"MTConnect driver {DriverInstanceId} caught a fault from an initial-alarm-state subscriber; delivery continues.",
|
||||
_driverInstanceId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2758,7 +2758,7 @@ public sealed partial class MTConnectDriver
|
||||
// CONDITION observations also feed the alarm path, which keeps its own multi-condition state
|
||||
// (a /sample Normal for one nativeCode is not a return to normal while another code is active).
|
||||
var touchedConditions = tracker.Apply(document, baseline);
|
||||
EmitConditionTransitions(touchedConditions, resync: false);
|
||||
EmitConditionTransitions(touchedConditions, resync: false, baselineRead: baseline);
|
||||
|
||||
if (document.Observations is not { Count: > 0 })
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user