feat(mtconnect): CONDITION data items as native Part 9 alarms via IAlarmSource (#626)

The parser keeps a condition's text, nativeCode, nativeSeverity, qualifier and type. A new
MTConnectConditionTracker applies MTConnect's multi-condition semantics (per-nativeCode
activation/clearing across /sample deltas, /current baseline reset, Unavailable), and
MTConnectConditionAlarmMapper turns tracker state into Part 9 transitions: Warning=500,
Fault=800, Normal=clear, Unavailable=inactive + BadNoCommunication; escalation or a new
code retriggers. The driver implements IAlarmSource keyed by the alarm tag's RawPath,
diffs against what the server was actually told (only when a handler is attached), so a
reconnect/re-baseline never re-raises an unchanged condition and a missed transition is
delivered on the next subscribe. Ack is a no-op success. Existing CONDITION String tags
are unchanged; a tag becomes an alarm only via the standard TagConfig 'alarm' object.

Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
This commit is contained in:
Joseph Doherty
2026-10-08 15:01:39 -04:00
parent cb85a455c9
commit 653d138637
9 changed files with 1290 additions and 9 deletions
@@ -47,7 +47,8 @@ namespace ZB.MOM.WW.OtOpcUa.Driver.MTConnect;
/// <see cref="IWritable"/>, <see cref="ISubscribable"/>, <see cref="ITagDiscovery"/>,
/// <see cref="IHostConnectivityProbe"/> and <see cref="IRediscoverable"/> — the last two
/// built on state this class already caches (the Agent <c>instanceId</c> and the probe
/// model).
/// model) — plus <see cref="IAlarmSource"/> for CONDITION data items (#626, in
/// <c>MTConnectDriver.Alarms.cs</c>).
/// </para>
/// <para>
/// <b><see cref="IWritable"/> is <i>observation injection</i>, not machine command, and it is
@@ -71,7 +72,7 @@ namespace ZB.MOM.WW.OtOpcUa.Driver.MTConnect;
/// <see cref="AgentSession.ProbeModel"/> (see <see cref="RunProbeLoopAsync"/>).
/// </para>
/// </remarks>
public sealed class MTConnectDriver
public sealed partial class MTConnectDriver
: IDriver, IReadable, IWritable, ISubscribable, ITagDiscovery, IHostConnectivityProbe, IRediscoverable,
ISkippedTagReporter
{
@@ -1981,6 +1982,10 @@ public sealed class MTConnectDriver
if (republishOnStart)
{
FanOut(SubscribedReferences(), ObservationIndex);
// #626: and the conditions, diffed against what the server was last told — so a restart
// delivers what changed while the driver was down and re-raises nothing that did not.
EmitConditionTransitions(touchedDataItemIds: null, resync: false);
}
while (!ct.IsCancellationRequested)
@@ -2140,6 +2145,7 @@ public sealed class MTConnectDriver
// then fails, holding stale values from a dead device model would be worse than
// holding none.
ObservationIndex.Clear();
ConditionTracker.Clear();
FanOut(SubscribedReferences(), ObservationIndex);
// The driver's own state is consistent by this point, so it is safe to hand the
@@ -2245,7 +2251,7 @@ public sealed class MTConnectDriver
.ConfigureAwait(false);
PublishAgentCursor(current.InstanceId, current.NextSequence);
ApplyAndFanOut(current);
ApplyAndFanOut(current, baseline: true);
_streamDegraded = false;
PublishHealthy(DateTime.UtcNow);
@@ -2328,11 +2334,16 @@ public sealed class MTConnectDriver
/// Folds a <c>/current</c> snapshot or one <c>/sample</c> chunk into the shared index and
/// publishes every reference it touched to the handles that subscribe it.
/// </summary>
private void ApplyAndFanOut(MTConnectStreamsResult document)
private void ApplyAndFanOut(MTConnectStreamsResult document, bool baseline = false)
{
var index = ObservationIndex;
index.Apply(document);
// #626: 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 = ConditionTracker.Apply(document, baseline);
EmitConditionTransitions(touchedConditions, resync: false);
if (document.Observations is not { Count: > 0 })
{
return;
@@ -2509,6 +2520,13 @@ public sealed class MTConnectDriver
/// </summary>
private bool HasSubscribedReferencesCore()
{
// #626: a CONDITION alarm subscription needs the same stream — a driver whose only authored tags are
// alarms would otherwise never hear a condition change.
if (HasAlarmReferencesCore())
{
return true;
}
foreach (var state in _subscriptions.Values)
{
if (state.References.Count > 0)
@@ -2786,6 +2804,12 @@ public sealed class MTConnectDriver
index.Apply(current);
Volatile.Write(ref _index, index);
// #626: the CONDITION tracker is per-session state like the index. Nothing is RAISED here — the
// lifecycle semaphore is held; the restarted pump delivers the diff as its first act.
var conditions = new MTConnectConditionTracker();
conditions.Apply(current, baseline: true);
Volatile.Write(ref _conditions, conditions);
// A fresh session has no failures behind it — including the /sample verdict, because a
// re-initialize is precisely the event that means an operator changed something.
_streamDegraded = false;
@@ -2867,6 +2891,7 @@ public sealed class MTConnectDriver
// against a torn-down driver answers BadWaitingForInitialData ("authored, no value") rather
// than BadNodeIdUnknown ("no such tag").
Volatile.Write(ref _index, new MTConnectObservationIndex(Volatile.Read(ref _binding).IndexTags));
Volatile.Write(ref _conditions, new MTConnectConditionTracker());
return Task.CompletedTask;
}