fix(dcl): key OPC UA alarms on ConditionId (#147)

OtOpcUa now exposes conditions imported over gRPC data connections in the
namespace https://zb.com/otopcua/mapped/conditions. Their ConditionId is
an opaque c1. NodeId stable for the life of the connection, but their
SourceName (<anchor>/<placement path>) moves when the primary placement
mapping changes. ScadaBridge keyed every condition on
SourceName.ConditionName, so a mapping change read as one alarm clearing
and a new one raising.

- Event filter selects ConditionId (select clause 19: ConditionType,
  empty browse path, AttributeId = NodeId).
- When the ConditionId (or SourceNode, which OtOpcUa sets to it) is in the
  mapped/conditions namespace, the per-condition key is its durable
  nsu=... string (index-proof across an OtOpcUa pair / upgrade). Every
  other OPC UA condition keeps the SourceName.ConditionName key, so no
  existing persisted row, MES script or operator view is re-keyed.
- The old SourceName.ConditionName text becomes an additive, display-only
  DisplayName: NativeAlarmTransition -> AlarmStateChanged ->
  AlarmStateUpdate.display_name (26) / DebugAlarmStateDto.display_name
  (22) -> ScriptAlarm.DisplayName. Persisted in native_alarm_state
  metadata_json (no column, no CDC trigger change); updates in place on
  every transition under the fixed key. Protos regenerated.
- Alarm Summary and Debug View show DisplayName (key as tooltip) and
  filter on it; site event log writes "label [key]".
- Upgrade: a pre-#147 row for an imported condition is retired by the
  first completed refresh snapshot (one return-to-normal + drop), then the
  condition continues under its ConditionId.

Tests: RealOpcUaImportedConditionKeyTests, OpcUaAlarmMapperTests,
NativeAlarmActorTests (DisplayName, rehydration, legacy-row retirement),
StreamRelayActorTests, SiteCommand golden sample, AlarmsAccessorTests,
DebugTreeBuilderTests; filter select-clause count 19 -> 20.
Docs: Component-DataConnectionLayer/Commons/Communication/SiteRuntime.
This commit is contained in:
Joseph Doherty
2026-11-28 09:14:49 -05:00
parent a117f79ad1
commit d3067ca036
29 changed files with 992 additions and 310 deletions
@@ -380,7 +380,14 @@ Other/custom protocols do not implement the capability; a subscribe request agai
The `DataConnectionActor` opens **one alarm feed per source** (not per subscriber) and routes incoming transitions to instance subscribers by **source-object reference** — a prefix match of the transition's `SourceObjectReference` (falling back to `SourceReference`) against each subscriber's registered `SourceReference`. Subscribers (the Site Runtime's `NativeAlarmActor` instances) are **ref-counted per source**, so the underlying feed is opened once and torn down only when the last subscriber for that source unsubscribes.
The registered `SourceReference` is a **NodeId** for OPC UA (the picker, CSV and config all store NodeIds), so the transition's routing identity must live in the same space or nothing matches. **Gitea #17**: the OPC UA adapter previously set `SourceObjectReference` from the event's `SourceName` (a plain name — the RawPath for OtOpcUa), which never prefix-matches a NodeId binding, so every native OPC UA alarm transition was silently dropped. Because each OPC UA feed is opened for exactly one binding, the adapter now tags every transition on that feed with the **binding string verbatim** (via the pure `OpcUaAlarmMapper.BuildIdentity`), making the routing key an exact match independent of whether the binding is stored as `ns=<index>` or the durable `nsu=<uri>` form. The Server-object aggregate feed (empty binding) keeps an empty routing identity, so it reaches only "mirror everything" subscribers and never leaks into a specific-node binding. The per-condition `SourceReference` key stays the human-readable `SourceName.ConditionName`, so persistence and display are unchanged. MxGateway is unaffected — its bindings are object names and its mapper already emits matching names.
The registered `SourceReference` is a **NodeId** for OPC UA (the picker, CSV and config all store NodeIds), so the transition's routing identity must live in the same space or nothing matches. **Gitea #17**: the OPC UA adapter previously set `SourceObjectReference` from the event's `SourceName` (a plain name — the RawPath for OtOpcUa), which never prefix-matches a NodeId binding, so every native OPC UA alarm transition was silently dropped. Because each OPC UA feed is opened for exactly one binding, the adapter now tags every transition on that feed with the **binding string verbatim** (via the pure `OpcUaAlarmMapper.BuildIdentity`), making the routing key an exact match independent of whether the binding is stored as `ns=<index>` or the durable `nsu=<uri>` form. The Server-object aggregate feed (empty binding) keeps an empty routing identity, so it reaches only "mirror everything" subscribers and never leaks into a specific-node binding. The per-condition `SourceReference` key stays the human-readable `SourceName.ConditionName`, so persistence and display are unchanged — with one exception, below. MxGateway is unaffected — its bindings are object names and its mapper already emits matching names.
**Imported OtOpcUa conditions key on `ConditionId` (Gitea #147).** OtOpcUa can import another cluster's conditions over a gRPC data connection; they live in the namespace `https://zb.com/otopcua/mapped/conditions` with an opaque `ConditionId` (`s=c1.<base64url>`, never constructed or parsed) that is stable for the life of the data connection, while their `SourceName` is `<anchor>/<placement path>` and **moves** when the primary placement mapping changes (mapping disabled/undeployed or no longer listing the condition, a higher-priority mapping starts listing it, or a condition first seen while the Raw mapping was unavailable moving once Raw lists it). Keyed on `SourceName.ConditionName`, a mapping change looked like one alarm clearing and a new one raising. The event filter therefore selects `ConditionId` (select clause **19**: `ConditionType`, empty browse path, `AttributeId = NodeId`, per Part 9), and when the ConditionId — or, if a server leaves that operand unanswered, the `SourceNode`, which OtOpcUa sets to the ConditionId for these — is in that namespace, `OpcUaAlarmMapper.BuildIdentity` makes the durable `nsu=https://zb.com/otopcua/mapped/conditions;s=c1.…` string the per-condition key. The `nsu=` form is deliberate: namespace indices can differ between the two members of an OtOpcUa pair or across a server upgrade, and a key that changed on failover would re-introduce the defect. The old `SourceName.ConditionName` text becomes the additive, display-only **`DisplayName`** (`NativeAlarmTransition.DisplayName` → `AlarmStateChanged.DisplayName` → `AlarmStateUpdate.display_name` = 26 / `DebugAlarmStateDto.display_name` = 22 → `ScriptAlarm.DisplayName`; persisted in `native_alarm_state.metadata_json`, so no column or replication-trigger change), carried on every transition so it updates in place while the key holds. The Alarm Summary and Debug View show `DisplayName` when present (the key is the tooltip) and the site event log writes `label [key]`; an older central or site simply sees an empty `DisplayName` and shows the key.
- **Scope — imported conditions only.** Every other OPC UA condition keeps `SourceName.ConditionName`: OtOpcUa local conditions already have a unique, stable SourceName (RawPath / scripted alarm id), third-party servers make no stability promise for ConditionId (some mint a node per occurrence, some omit it), and re-keying them would rename every condition already in `native_alarm_state`, in MES scripts and on operators' screens.
- **Upgrade of persisted state.** A row written for an imported condition by a pre-#147 build is keyed the old way. It is rehydrated as before, and the first refresh after (re)subscribe lists the condition under its new key, so the snapshot swap retires the old key with one synthesised return-to-normal + `NativeAlarmDropped` and the condition continues under the ConditionId — no ghost row survives a completed refresh. If no refresh completes (the subscribe-time ConditionRefresh fails), the old row stays until the next successful one; a redeploy clears native alarm state outright.
- **Routing is unchanged** — still the binding string (`SourceObjectReference`); template native-alarm-source bindings and condition-type filters do not look at the per-condition key. Transition-kind derivation (`_alarmLastState`) uses the same key, so an ack after a SourceName move is an Acknowledge, not a fresh Raise. A ConditionId also changes when the origin rotates its `ConditionKeySecret` or the connection is re-created — the server then removes and re-adds the condition with RefreshRequired (#148), which this mirror handles as a clear + raise.
- Covered by `RealOpcUaImportedConditionKeyTests`, `OpcUaAlarmMapperTests`, `NativeAlarmActorTests` (`DisplayName_*`, `Rehydration_RestoresDisplayName`, `LegacyKeyedRow_IsRetiredByTheFirstSnapshot`), `StreamRelayActorTests` and `DebugTreeBuilderTests`.
- **State gating**: `SubscribeAlarmsRequest` is handled only in the **Connected** state; requests arriving while **Connecting**/**Reconnecting** are coalesced to the latest request per subscriber actor/source and processed on entering Connected. Unsubscribes and subscriber termination are handled in every state.
- **Capability check**: if `_adapter is not IAlarmSubscribableConnection`, the actor replies `SubscribeAlarmsResponse(Success = false, ...)`.
@@ -397,7 +404,7 @@ All defined in Commons so the feed is identical across protocols:
| Type | Shape |
|------|-------|
| `NativeAlarmTransition` | `SourceReference`, `SourceObjectReference`, `AlarmTypeName`, `Kind`, `Condition`, `Category`, `Description`, `Message`, `OperatorUser`, `OperatorComment`, `OriginalRaiseTime?`, `TransitionTime`, `CurrentValue`, `LimitValue`, `AckTime?` |
| `NativeAlarmTransition` | `SourceReference`, `SourceObjectReference`, `AlarmTypeName`, `Kind`, `Condition`, `Category`, `Description`, `Message`, `OperatorUser`, `OperatorComment`, `OriginalRaiseTime?`, `TransitionTime`, `CurrentValue`, `LimitValue`, `AckTime?`, `SnapshotSourceReference?`, `SnapshotId?`, `DisplayName?` (#147) |
| `AlarmConditionState` | `Active`, `Acknowledged`, `Confirmed?` (null when not confirmable), `Shelve`, `Suppressed`, `Severity` (0–1000, higher = more urgent), `Priority?` (raw source number) |
**Severity direction (Gitea #129).** `Severity` is OPC UA Part 9 direction for both adapters. `OpcUaAlarmMapper` clamps the server's Severity to 0–1000 and repeats it as `Priority`. `MxGatewayAlarmMapper` reads the gateway's `severity` field — which mxaccessgw fills from the AVEVA alarm **priority** (1 = most urgent) — bands it onto `Severity` (AVEVA priority 1–250 → 1000, 251–500 → 750, 501–750 → 500, 751–999 → 250 (`AlarmPriorityBands`, the same table as OtOpcUa `GalaxyAlarmPriority`, lmxopcua#887); out-of-range clamps: ≤ 0 → 1000, > 999 → 250) and keeps it on `Priority` clamped to 0–1000, i.e. the value `Severity` carried before #129. A Galaxy alarm therefore reads the same `Severity` through OtOpcUa and straight from the gateway.