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:
@@ -0,0 +1,225 @@
|
||||
namespace ZB.MOM.WW.OtOpcUa.Driver.MTConnect;
|
||||
|
||||
/// <summary>The four MTConnect CONDITION levels the alarm path distinguishes (#626).</summary>
|
||||
internal enum MTConnectConditionLevel
|
||||
{
|
||||
/// <summary><c><Normal/></c> — nothing is active.</summary>
|
||||
Normal = 0,
|
||||
|
||||
/// <summary><c><Warning/></c> — active, Part 9 severity 500.</summary>
|
||||
Warning = 1,
|
||||
|
||||
/// <summary><c><Fault/></c> — active, Part 9 severity 800.</summary>
|
||||
Fault = 2,
|
||||
|
||||
/// <summary><c><Unavailable/></c> — the Agent has no state for the item (inactive, Bad quality).</summary>
|
||||
Unavailable = 3,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The derived state of ONE CONDITION DataItem as the alarm path sees it (#626): the most severe active
|
||||
/// condition when any is active, else Normal or Unavailable.
|
||||
/// </summary>
|
||||
/// <param name="Level">The derived level.</param>
|
||||
/// <param name="Detail">
|
||||
/// The detail of the observation the level comes from — the winning active condition, or the latest
|
||||
/// Normal/Unavailable observation. Never null.
|
||||
/// </param>
|
||||
/// <param name="TimestampUtc">That observation's Agent timestamp.</param>
|
||||
/// <param name="ActiveCount">How many distinct conditions (by <c>nativeCode</c>) are active at once.</param>
|
||||
internal sealed record MTConnectConditionState(
|
||||
MTConnectConditionLevel Level,
|
||||
MTConnectConditionDetail Detail,
|
||||
DateTime TimestampUtc,
|
||||
int ActiveCount)
|
||||
{
|
||||
/// <summary>Whether the derived condition is active (Warning or Fault).</summary>
|
||||
public bool IsActive => Level is MTConnectConditionLevel.Warning or MTConnectConditionLevel.Fault;
|
||||
|
||||
/// <summary>
|
||||
/// Whether two states are the SAME alarm statement — the dedup key the alarm path diffs on. Two active
|
||||
/// states match only when level, <c>nativeCode</c>, <c>nativeSeverity</c>, <c>qualifier</c> and message
|
||||
/// text all agree (a new code, an escalation, or a changed message is a new statement); two inactive
|
||||
/// states match on the level alone, so a re-reported Normal never re-clears. The timestamp and the
|
||||
/// active count are deliberately NOT part of it: a replayed <c>/current</c> after a reconnect carries the
|
||||
/// same condition under a new sequence, and re-raising it would un-acknowledge an operator's ack.
|
||||
/// </summary>
|
||||
/// <param name="other">The state to compare against.</param>
|
||||
/// <returns>True when the alarm path must emit nothing to move from one to the other.</returns>
|
||||
public bool SameAlarmAs(MTConnectConditionState other)
|
||||
{
|
||||
if (Level != other.Level) return false;
|
||||
if (!IsActive) return true;
|
||||
|
||||
return string.Equals(Detail.NativeCode, other.Detail.NativeCode, StringComparison.Ordinal)
|
||||
&& string.Equals(Detail.NativeSeverity, other.Detail.NativeSeverity, StringComparison.Ordinal)
|
||||
&& string.Equals(Detail.Qualifier, other.Detail.Qualifier, StringComparison.Ordinal)
|
||||
&& string.Equals(Detail.Text, other.Detail.Text, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Folds MTConnect CONDITION observations into per-DataItem alarm state with the protocol's own
|
||||
/// multi-condition semantics (#626) — the alarm path's analogue of <see cref="MTConnectObservationIndex"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// <b>Why the index is not enough.</b> The index reconciles several states reported for one DataItem
|
||||
/// only <i>within one document</i> and otherwise treats each document as the new statement of the
|
||||
/// value. That is right for a <c>/current</c> snapshot but wrong for a <c>/sample</c> delta: an Agent
|
||||
/// streams each <c>nativeCode</c>'s activation and clearing separately, so a chunk carrying
|
||||
/// <c><Normal nativeCode="A"/></c> clears code A only — code B, raised in an earlier chunk,
|
||||
/// is still active. The alarm path must not report "return to normal" there.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>Rules, per DataItem, applied in document order.</b> <c>Warning</c>/<c>Fault</c> activates (or
|
||||
/// replaces) the entry keyed by its <c>nativeCode</c> (blank = one unnamed entry). <c>Normal</c> with a
|
||||
/// <c>nativeCode</c> clears that code; <c>Normal</c> without one clears every code. <c>Unavailable</c>
|
||||
/// clears every code and marks the item unavailable until any other level arrives. A <b>baseline</b>
|
||||
/// (<c>/current</c>, which lists every currently-active condition) first resets each DataItem it
|
||||
/// mentions, so an entry cleared while the driver was not listening cannot survive a re-baseline. An
|
||||
/// unrecognised (vendor) state word is treated as an active <c>Warning</c> — surfacing an unknown
|
||||
/// condition beats hiding it.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>Thread safety.</b> One internal lock; every member is atomic. The pump is the only writer.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
internal sealed class MTConnectConditionTracker
|
||||
{
|
||||
/// <summary>The parser's normalised spelling of <c><Unavailable/></c>.</summary>
|
||||
private const string UnavailableSentinel = "UNAVAILABLE";
|
||||
|
||||
private readonly Lock _lock = new();
|
||||
private readonly Dictionary<string, ItemState> _items = new(StringComparer.Ordinal);
|
||||
private long _order;
|
||||
|
||||
/// <summary>
|
||||
/// Folds every CONDITION observation of <paramref name="document"/> into the tracked state.
|
||||
/// </summary>
|
||||
/// <param name="document">A <c>/current</c> snapshot or one <c>/sample</c> chunk.</param>
|
||||
/// <param name="baseline">
|
||||
/// True for a <c>/current</c> answer — each DataItem it mentions is reset before its observations are
|
||||
/// applied; false for a <c>/sample</c> delta.
|
||||
/// </param>
|
||||
/// <returns>The DataItem ids whose condition state the document touched (empty when none).</returns>
|
||||
public IReadOnlySet<string> Apply(MTConnectStreamsResult document, bool baseline)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(document);
|
||||
|
||||
var touched = new HashSet<string>(StringComparer.Ordinal);
|
||||
if (document.Observations is not { Count: > 0 }) return touched;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
foreach (var observation in document.Observations)
|
||||
{
|
||||
if (observation?.Condition is null || string.IsNullOrWhiteSpace(observation.DataItemId)) continue;
|
||||
|
||||
if (!_items.TryGetValue(observation.DataItemId, out var item))
|
||||
{
|
||||
_items[observation.DataItemId] = item = new ItemState();
|
||||
}
|
||||
|
||||
if (touched.Add(observation.DataItemId) && baseline)
|
||||
{
|
||||
item.Active.Clear();
|
||||
item.Unavailable = false;
|
||||
}
|
||||
|
||||
ApplyOne(item, observation);
|
||||
}
|
||||
}
|
||||
|
||||
return touched;
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public MTConnectConditionState? Get(string dataItemId)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (string.IsNullOrEmpty(dataItemId) || !_items.TryGetValue(dataItemId, out var item)) return null;
|
||||
|
||||
if (item.Active.Count > 0)
|
||||
{
|
||||
// Most severe wins; among equals, the most recently activated — the newest statement the
|
||||
// operator has not yet seen.
|
||||
var winner = item.Active.Values
|
||||
.OrderByDescending(e => e.Level)
|
||||
.ThenByDescending(e => e.Order)
|
||||
.First();
|
||||
|
||||
return new MTConnectConditionState(winner.Level, winner.Detail, winner.TimestampUtc, item.Active.Count);
|
||||
}
|
||||
|
||||
var last = item.LastInactive;
|
||||
return last is null
|
||||
? null
|
||||
: new MTConnectConditionState(
|
||||
item.Unavailable ? MTConnectConditionLevel.Unavailable : MTConnectConditionLevel.Normal,
|
||||
last.Detail,
|
||||
last.TimestampUtc,
|
||||
0);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Drops every tracked state — the Agent-restart re-baseline, mirroring <see cref="MTConnectObservationIndex.Clear"/>.</summary>
|
||||
public void Clear()
|
||||
{
|
||||
lock (_lock) _items.Clear();
|
||||
}
|
||||
|
||||
/// <summary>Maps a parsed condition state word onto a level (unknown vendor words ⇒ Warning).</summary>
|
||||
/// <param name="value">The observation's state word.</param>
|
||||
internal static MTConnectConditionLevel LevelOf(string? value) => value switch
|
||||
{
|
||||
UnavailableSentinel => MTConnectConditionLevel.Unavailable,
|
||||
_ when string.Equals(value, "Normal", StringComparison.OrdinalIgnoreCase) => MTConnectConditionLevel.Normal,
|
||||
_ when string.Equals(value, "Fault", StringComparison.OrdinalIgnoreCase) => MTConnectConditionLevel.Fault,
|
||||
_ when string.Equals(value, "Unavailable", StringComparison.OrdinalIgnoreCase) => MTConnectConditionLevel.Unavailable,
|
||||
_ => MTConnectConditionLevel.Warning,
|
||||
};
|
||||
|
||||
private void ApplyOne(ItemState item, MTConnectObservation observation)
|
||||
{
|
||||
var detail = observation.Condition!;
|
||||
var entry = new Entry(LevelOf(observation.Value), detail, observation.TimestampUtc, ++_order);
|
||||
|
||||
switch (entry.Level)
|
||||
{
|
||||
case MTConnectConditionLevel.Normal:
|
||||
if (detail.NativeCode is null) item.Active.Clear();
|
||||
else item.Active.Remove(detail.NativeCode);
|
||||
item.Unavailable = false;
|
||||
item.LastInactive = entry;
|
||||
break;
|
||||
|
||||
case MTConnectConditionLevel.Unavailable:
|
||||
item.Active.Clear();
|
||||
item.Unavailable = true;
|
||||
item.LastInactive = entry;
|
||||
break;
|
||||
|
||||
default:
|
||||
item.Active[detail.NativeCode ?? string.Empty] = entry;
|
||||
item.Unavailable = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed record Entry(MTConnectConditionLevel Level, MTConnectConditionDetail Detail, DateTime TimestampUtc, long Order);
|
||||
|
||||
private sealed class ItemState
|
||||
{
|
||||
/// <summary>Active conditions keyed by <c>nativeCode</c> ("" for an unnamed one).</summary>
|
||||
public Dictionary<string, Entry> Active { get; } = new(StringComparer.Ordinal);
|
||||
|
||||
/// <summary>Whether the latest inactive statement was Unavailable.</summary>
|
||||
public bool Unavailable { get; set; }
|
||||
|
||||
/// <summary>The latest Normal/Unavailable observation (its text and type feed the clear message).</summary>
|
||||
public Entry? LastInactive { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user