fix(alarms): Severity runs one direction, higher = more urgent; raw priority kept (#129)

AlarmConditionState.Severity mixed two conventions: OPC UA rows carried Part 9
Severity (higher = more severe) while MxGateway native alarms and computed
alarms carried a raw AVEVA-style priority (1 = most urgent). Every consumer
ranks higher as worse, so Worst Severity, the severity filter/sort and the
site event-log Error threshold picked the LEAST urgent MxGateway/computed
alarms.

- AlarmPriorityBands: the OtOpcUa GalaxyAlarmPriority table (lmxopcua#887,
  b61f3e05), value for value: 1-250 -> 1000, 251-500 -> 750, 501-750 -> 500,
  751-999 -> 250; <= 0 clamps to Critical, > 999 to Low.
- MxGatewayAlarmMapper bands the gateway severity (= AVEVA priority);
  AlarmConditionStateFactory.ForComputed bands PriorityLevel. OPC UA unchanged.
- AlarmConditionState gains trailing Priority (int?): the raw source number,
  exactly the pre-#129 Severity. AlarmStateChanged.Priority and the wire
  AlarmStateUpdate.priority keep that meaning (no proto change); central derives
  Severity by kind (SeverityFromPriority), so an older site decodes correctly.
- ScriptAlarm gains trailing Priority. The native MES 900-999 band and the MES
  API Severity/MinSeverity/MaxSeverity key off Priority; rig seed scripts moved
  from a.Severity to a.Priority, so MES output is unchanged.
- AlarmActor raise logs Error when the banded severity >= 700 (same threshold
  as NativeAlarmActor): priority 1-500 Error, 501+ Warning (was raw >= 700).
- NativeAlarmActor upgrades pre-#129 native_alarm_state rows on rehydrate.
- Debug snapshot decode restores Condition.Priority from the row's priority.
- Docs: Commons, DCL, SiteRuntime, Communication, CentralUI, InboundAPI,
  HighLevelReqs, MES alarm-status plan, CLAUDE.md.

Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
This commit is contained in:
Joseph Doherty
2026-11-12 13:58:28 -05:00
parent e36cd6055e
commit dd4f1c2aa3
39 changed files with 686 additions and 120 deletions
@@ -5,6 +5,7 @@ using Microsoft.CodeAnalysis.Scripting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Streaming;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Alarms;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
using ZB.MOM.WW.ScadaBridge.HealthMonitoring;
@@ -92,11 +93,15 @@ public class AlarmActor : ReceiveActor
private readonly ISiteEventLogger? _siteEventLogger;
/// <summary>
/// Priority at or above which a computed-alarm raise is logged as
/// Severity at or above which a computed-alarm raise is logged as
/// <c>Error</c> to the site event log; below it, raises log as <c>Warning</c>.
/// Mirrors the 0–1000 alarm-severity scale.
/// Compared against the alarm's priority banded onto the unified severity
/// (<see cref="AlarmPriorityBands"/>, higher = more urgent; Gitea #129) — the same
/// threshold <c>NativeAlarmActor</c> applies — so priority 1–500 (Critical/High)
/// logs as Error and 501+ (Medium/Low) as Warning. Before #129 the raw priority was
/// compared, which ranked the least urgent alarms (700+) as errors.
/// </summary>
private const int ErrorPriorityThreshold = 700;
private const int ErrorSeverityThreshold = 700;
private AlarmState _currentState = AlarmState.Normal;
/// <summary>
@@ -471,13 +476,13 @@ public class AlarmActor : ReceiveActor
}
/// <summary>
/// Maps an alarm priority (0–1000) to a site-event severity for a
/// <i>raise</i> transition — <c>Error</c> at or above
/// <see cref="ErrorPriorityThreshold"/>, otherwise <c>Warning</c>. Clears and
/// Maps an alarm priority (0–1000, lower = more urgent) to a site-event severity
/// for a <i>raise</i> transition — <c>Error</c> when its banded severity is at or
/// above <see cref="ErrorSeverityThreshold"/>, otherwise <c>Warning</c>. Clears and
/// inter-band transitions always log as <c>Info</c>.
/// </summary>
private static string RaiseSeverity(int priority) =>
priority >= ErrorPriorityThreshold ? "Error" : "Warning";
internal static string RaiseSeverity(int priority) =>
AlarmPriorityBands.ToSeverity(priority) >= ErrorSeverityThreshold ? "Error" : "Warning";
/// <summary>
/// Fire-and-forget an <c>alarm</c> operational event to the optional
@@ -43,7 +43,9 @@ public class NativeAlarmActor : ReceiveActor
/// <summary>
/// Severity at or above which a native-alarm raise is logged as
/// <c>Error</c> to the site event log; below it, raises log as <c>Warning</c>.
/// Mirrors the 0–1000 condition-severity scale.
/// On the unified 0–1000 condition-severity scale, higher = more urgent for every
/// source (Gitea #129): an MxAccess alarm in AVEVA's Critical (priority 1–250 → 1000)
/// or High (251–500 → 750) band logs as Error, Medium/Low as Warning.
/// </summary>
private const int ErrorSeverityThreshold = 700;
@@ -217,6 +219,8 @@ public class NativeAlarmActor : ReceiveActor
continue;
}
condition = UpgradeLegacyCondition(condition, _nativeKind);
// Restore the persisted display metadata (UA4) so the rehydrated condition renders
// fully (type/category/message/values) until the first source snapshot replaces it.
// Null/malformed metadata falls back to empty strings (pre-UA4 rows behave as before).
@@ -481,7 +485,10 @@ public class NativeAlarmActor : ReceiveActor
_instanceName,
t.SourceReference,
condition.Active ? AlarmState.Active : AlarmState.Normal,
condition.Severity,
// Gitea #129: the record's Priority (and the stream's AlarmStateUpdate.priority) is the
// source's raw number — what Severity carried before #129 — so the MES 900-999 band and
// an older central keep reading the same value. Severity rides on Condition.
condition.Priority ?? condition.Severity,
t.TransitionTime)
{
Kind = _nativeKind,
@@ -497,7 +504,7 @@ public class NativeAlarmActor : ReceiveActor
LimitValue = t.LimitValue,
NativeSourceCanonicalName = _source.CanonicalName,
// Gitea #59: the binding-level flag ("every condition under this source is
// MES-reportable"). Additive to the 900-999 severity band the consumer applies.
// MES-reportable"). Additive to the 900-999 priority band the consumer applies.
IsMesReportable = _source.IsMesReportable,
// MES alarm-status API §6.4: carried verbatim from the transition — the DCL
// adapter already decided whether an ack instant applies (null while unacked,
@@ -515,6 +522,32 @@ public class NativeAlarmActor : ReceiveActor
}
}
/// <summary>
/// Upgrades a condition persisted in <c>native_alarm_state</c> by a pre-#129 build, whose
/// <see cref="AlarmConditionState.Severity"/> held the raw source number and which has no
/// <see cref="AlarmConditionState.Priority"/>. That number becomes the Priority; for an
/// MxAccess source it is an AVEVA priority, so Severity is re-derived through
/// <see cref="AlarmPriorityBands"/>. An OPC UA row's Severity was already Part 9 and is kept.
/// A current row (Priority present) is returned unchanged.
/// </summary>
/// <param name="condition">The deserialized persisted condition.</param>
/// <param name="kind">This actor's native alarm kind.</param>
/// <returns>The condition in the post-#129 shape.</returns>
internal static AlarmConditionState UpgradeLegacyCondition(AlarmConditionState condition, AlarmKind kind)
{
if (condition.Priority is not null)
{
return condition;
}
var raw = condition.Severity;
return condition with
{
Severity = AlarmConditionStateFactory.SeverityFromPriority(kind, raw),
Priority = raw,
};
}
/// <summary>
/// Fire-and-forget an <c>alarm</c> operational event mirroring a native
/// condition transition. An active condition is a raise (severity by the
@@ -1393,7 +1393,9 @@ public class ScriptRuntimeContext
CurrentValue: a.CurrentValue,
LimitValue: a.LimitValue,
IsConfiguredPlaceholder: a.IsConfiguredPlaceholder,
IsMesReportable: a.IsMesReportable);
IsMesReportable: a.IsMesReportable,
// Gitea #129: the raw source number (pre-#129 Severity) — the MES 900-999 band keys off it.
Priority: a.Priority);
/// <summary>
/// Site-local tracking-status API for cached operations.