826312befe
Clears CommentChecker findings in src/ (3,280 -> 209, the remainder being false positives like PLC-5/IEEE-754/M0.0) by adding missing summaries/params/returns, normalising inheritdoc usage, and removing Gitea/review tracking IDs while keeping the rationale they annotated. Comment-only: comment-stripped sources are identical to the parent commit.
231 lines
11 KiB
C#
231 lines
11 KiB
C#
namespace ZB.MOM.WW.OtOpcUa.Driver.MTConnect;
|
|
|
|
/// <summary>The four MTConnect CONDITION levels the alarm path distinguishes.</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: 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 when level, <c>nativeCode</c>, <c>nativeSeverity</c> and <c>qualifier</c> agree (a new
|
|
/// code or an escalation is a new statement); two inactive states match on the level alone, so a
|
|
/// re-reported Normal never re-clears. NOT part of it: the timestamp and the active count (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) and the message TEXT (an adapter that embeds a live value in
|
|
/// the text, e.g. "Spindle temp 82C", would otherwise re-raise and un-ack on every tick — the message
|
|
/// shown stays the statement's first text).
|
|
/// </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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Folds MTConnect CONDITION observations into per-DataItem alarm state with the protocol's own
|
|
/// multi-condition semantics — 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 (the alarm path does
|
|
/// NOT treat that as a clear — see <see cref="MTConnectConditionAlarmMapper"/>). 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);
|
|
|
|
/// <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>
|
|
/// <returns>The derived state, or null when the item was never observed.</returns>
|
|
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, a DETERMINISTIC tie-break (ordinal nativeCode, then type) —
|
|
// never document order, which an Agent may present differently on a re-baseline and which would
|
|
// then re-raise (and un-ack) an unchanged condition on every reconnect.
|
|
var winner = item.Active.Values
|
|
.OrderByDescending(e => e.Level)
|
|
.ThenBy(e => e.Detail.NativeCode ?? string.Empty, StringComparer.Ordinal)
|
|
.ThenBy(e => e.Detail.ConditionType ?? string.Empty, StringComparer.Ordinal)
|
|
.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>
|
|
/// <returns>The mapped condition level.</returns>
|
|
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);
|
|
|
|
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);
|
|
|
|
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; }
|
|
}
|
|
}
|