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
@@ -0,0 +1,153 @@
using System.Text;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.Driver.MTConnect;
/// <summary>
/// The MTConnect CONDITION → OPC UA Part 9 mapping (#626), as one pure function: given what the server was
/// last told about a condition and what the Agent says now, the single <see cref="AlarmEventArgs"/> to
/// raise — or none.
/// </summary>
/// <remarks>
/// <list type="table">
/// <listheader><term>Agent level</term><description>Part 9</description></listheader>
/// <item><term>Normal</term><description>inactive (return to normal), quality Good</description></item>
/// <item><term>Warning</term><description>active, severity 500 (<see cref="AlarmSeverity.Medium"/>)</description></item>
/// <item><term>Fault</term><description>active, severity 800 (<see cref="AlarmSeverity.High"/> bucket)</description></item>
/// <item><term>Unavailable</term><description>inactive, quality <c>BadNoCommunication</c></description></item>
/// </list>
/// <para>
/// Inactive → active is a <see cref="AlarmTransitionKind.Raise"/>; active → a DIFFERENT active statement
/// (Warning → Fault, a new <c>nativeCode</c>, a changed message) is a
/// <see cref="AlarmTransitionKind.Retrigger"/> — active and unacknowledged again with the new severity
/// and message; active → Normal/Unavailable is a <see cref="AlarmTransitionKind.Clear"/>. A change that
/// moves only the quality (Normal ⇄ Unavailable) is a quality-only annotation
/// (<see cref="AlarmEventArgs.IsQualityOnly"/>). An unchanged statement
/// (<see cref="MTConnectConditionState.SameAlarmAs"/>) emits nothing — which is what keeps a reconnect's
/// replayed <c>/current</c> from re-raising, and so un-acknowledging, an alarm the operator acked.
/// </para>
/// <para>
/// The one deliberate exception is a <c>resync</c>: an unchanged <c>Unavailable</c> is
/// re-asserted on a (re)subscribe, because the host re-annotates every condition Good when the driver
/// reconnects (#477) and nothing else would put the per-condition Bad back.
/// </para>
/// </remarks>
internal static class MTConnectConditionAlarmMapper
{
/// <summary>OPC UA severity of an active <c>Warning</c>.</summary>
internal const int WarningSeverity = 500;
/// <summary>OPC UA severity of an active <c>Fault</c>.</summary>
internal const int FaultSeverity = 800;
/// <summary>Canonical <c>Opc.Ua.StatusCodes.Good</c>.</summary>
private const uint Good = 0x00000000u;
/// <summary>Canonical <c>Opc.Ua.StatusCodes.BadNoCommunication</c> — the Agent has no state for the item.</summary>
private const uint BadNoCommunication = 0x80310000u;
/// <summary>
/// The event moving the server from <paramref name="delivered"/> to <paramref name="current"/>, or
/// <c>null</c> when nothing needs to be said.
/// </summary>
/// <param name="delivered">What the server was last told for this condition; <c>null</c> when nothing yet.</param>
/// <param name="current">The Agent's current statement.</param>
/// <param name="conditionId">The condition's ConditionId — the alarm-bearing raw tag's RawPath.</param>
/// <param name="dataItemId">The Agent DataItem id (carried as <see cref="AlarmEventArgs.SourceNodeId"/>).</param>
/// <param name="handle">The alarm subscription the event belongs to.</param>
/// <param name="resync">True on a (re)subscribe: re-assert an unchanged Unavailable quality.</param>
/// <returns>The event to raise, or <c>null</c>.</returns>
public static AlarmEventArgs? Transition(
MTConnectConditionState? delivered,
MTConnectConditionState current,
string conditionId,
string dataItemId,
IAlarmSubscriptionHandle handle,
bool resync)
{
ArgumentNullException.ThrowIfNull(current);
var wasActive = delivered?.IsActive == true;
if (delivered is not null && delivered.SameAlarmAs(current))
{
return resync && current.Level == MTConnectConditionLevel.Unavailable
? Build(current, delivered, conditionId, dataItemId, handle, AlarmTransitionKind.Unspecified)
: null;
}
if (current.IsActive)
{
return Build(current, delivered, conditionId, dataItemId, handle,
wasActive ? AlarmTransitionKind.Retrigger : AlarmTransitionKind.Raise);
}
if (wasActive)
{
return Build(current, delivered, conditionId, dataItemId, handle, AlarmTransitionKind.Clear);
}
// Inactive → inactive with a different level: only the quality moved. Nothing to say for a first-seen
// Normal — the host's connectivity annotation already confirms a materialised condition Good.
if (delivered is null && current.Level == MTConnectConditionLevel.Normal)
{
return null;
}
return Build(current, delivered, conditionId, dataItemId, handle, AlarmTransitionKind.Unspecified);
}
/// <summary>The Part 9 severity of an active level.</summary>
/// <param name="level">An active level.</param>
internal static int SeverityOf(MTConnectConditionLevel level) =>
level == MTConnectConditionLevel.Fault ? FaultSeverity : WarningSeverity;
/// <summary>
/// The condition's message: its text content, else <c>"{type} {level}"</c>; followed by whichever of
/// <c>nativeCode</c> / <c>nativeSeverity</c> / <c>qualifier</c> the Agent supplied — the Part 9 event
/// model has no slot for them, and an operator needs the native code to look the alarm up.
/// </summary>
/// <param name="state">The state to describe.</param>
internal static string MessageOf(MTConnectConditionState state)
{
var detail = state.Detail;
var level = state.Level == MTConnectConditionLevel.Unavailable ? "UNAVAILABLE" : state.Level.ToString();
var builder = new StringBuilder(detail.Text ?? $"{detail.ConditionType ?? "CONDITION"} {level}");
var extras = new List<string>(3);
if (detail.NativeCode is not null) extras.Add($"nativeCode={detail.NativeCode}");
if (detail.NativeSeverity is not null) extras.Add($"nativeSeverity={detail.NativeSeverity}");
if (detail.Qualifier is not null) extras.Add($"qualifier={detail.Qualifier}");
if (extras.Count > 0) builder.Append(" [").AppendJoin(", ", extras).Append(']');
return builder.ToString();
}
private static AlarmEventArgs Build(
MTConnectConditionState current,
MTConnectConditionState? delivered,
string conditionId,
string dataItemId,
IAlarmSubscriptionHandle handle,
AlarmTransitionKind kind)
{
// An inactive statement keeps the severity of the alarm it clears (or the one last shown), so a clear
// does not visibly re-grade the condition; a never-active condition reports Warning's 500.
var severity = current.IsActive
? SeverityOf(current.Level)
: delivered?.IsActive == true ? SeverityOf(delivered.Level) : WarningSeverity;
return new AlarmEventArgs(
SubscriptionHandle: handle,
SourceNodeId: dataItemId,
ConditionId: conditionId,
AlarmType: current.Detail.ConditionType ?? "CONDITION",
Message: MessageOf(current),
Severity: severity >= FaultSeverity ? AlarmSeverity.High : AlarmSeverity.Medium,
SourceTimestampUtc: current.TimestampUtc,
Kind: kind,
OpcUaSeverity: severity,
QualityStatusCode: current.Level == MTConnectConditionLevel.Unavailable ? BadNoCommunication : Good);
}
}