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
@@ -12,6 +12,53 @@ namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Adapters;
/// </summary>
public static class OpcUaAlarmMapper
{
/// <summary>
/// The OtOpcUa namespace holding conditions IMPORTED from another cluster over a gRPC data
/// connection (OtOpcUa <c>docs/AlarmTracking.md</c> → <i>Imported conditions</i>). Their
/// <c>ConditionId</c> is an opaque NodeId stable for the life of the data connection, while
/// their <c>SourceName</c> is a display path that moves with the server's mapping (Gitea #147).
/// </summary>
public const string ImportedConditionsNamespaceUri = "https://zb.com/otopcua/mapped/conditions";
/// <summary>
/// <see cref="BuildIdentity(string?, string?, string?)"/> plus the OtOpcUa imported-condition
/// rule (Gitea #147): when <paramref name="importedConditionId"/> is supplied (the event's
/// ConditionId is in <see cref="ImportedConditionsNamespaceUri"/>, as a durable
/// <c>nsu=…</c> string), it IS the per-condition key and the usual
/// <c>SourceName.ConditionName</c> becomes a display-only label.
/// </summary>
/// <remarks>
/// <para>
/// <b>Why only imported conditions.</b> Their SourceName is
/// <c>&lt;anchor&gt;/&lt;placement path&gt;</c> and moves whenever 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 it, one condition would read as one alarm clearing and a
/// new one raising. Every other OPC UA condition keeps the <c>SourceName.ConditionName</c> key:
/// OtOpcUa local conditions already have a unique, stable SourceName (the RawPath / scripted
/// alarm id), third-party servers make no stability promise about ConditionId (some mint a node
/// per occurrence, some omit it), and re-keying them would rename every alarm already persisted
/// in <c>native_alarm_state</c>, referenced by MES scripts or shown to operators.
/// </para>
/// <para>
/// The display label is exactly what the key would have been before #147, so an operator sees
/// the same text; it updates in place on every event while the key stays fixed.
/// </para>
/// </remarks>
/// <param name="subscriptionSourceReference">The binding string this alarm feed was subscribed under.</param>
/// <param name="sourceName">The event's <c>SourceName</c>, or null/empty when unset.</param>
/// <param name="conditionName">The event's <c>ConditionName</c>, or null/empty when unset.</param>
/// <param name="importedConditionId">The durable ConditionId of an imported condition, else null.</param>
/// <returns>The per-condition key, the routing identity, and the display label (null when the key is the label).</returns>
public static (string SourceReference, string SourceObjectReference, string? DisplayName) BuildIdentity(
string? subscriptionSourceReference, string? sourceName, string? conditionName, string? importedConditionId)
{
var (readableKey, routingRef) = BuildIdentity(subscriptionSourceReference, sourceName, conditionName);
if (string.IsNullOrEmpty(importedConditionId))
return (readableKey, routingRef, null);
return (importedConditionId, routingRef, string.IsNullOrEmpty(readableKey) ? null : readableKey);
}
/// <summary>
/// Derives the two identity strings a native OPC UA alarm transition carries:
/// the routing identity that <c>DataConnectionActor</c> matches against the stored
@@ -1128,6 +1128,18 @@ public class RealOpcUaClient : IOpcUaClient
// ConditionRefresh caveat as index 13: a replayed snapshot may re-stamp it.
selectClauses.Add(
SelectField(ObjectTypeIds.AcknowledgeableConditionType, "AckedState", "TransitionTime")); // 18
// 19: ConditionId — Part 9 §5.5.2's dedicated operand: TypeDefinitionId = ConditionType,
// EMPTY browse path, AttributeId = NodeId, which selects the condition node's own NodeId.
// Used as the per-condition key for OtOpcUa IMPORTED conditions (Gitea #147, see
// OpcUaAlarmMapper.BuildIdentity); null on non-condition events and servers that do
// not support the operand.
selectClauses.Add(new SimpleAttributeOperand
{
TypeDefinitionId = ObjectTypeIds.ConditionType,
BrowsePath = Array.Empty<QualifiedName>(),
AttributeId = Attributes.NodeId
}); // 19
filter.SelectClauses = selectClauses;
// UNAVAILABLE via standard OPC UA A&C event fields (documented here so future
@@ -1343,8 +1355,15 @@ public class RealOpcUaClient : IOpcUaClient
if (string.IsNullOrEmpty(sourceName))
sourceName = fields[1] is NodeId sourceNode ? sourceNode.ToString() ?? "" : "";
var conditionName = fields.Length > 11 ? fields[11] as string : null;
var (sourceRef, sourceObjectRef) =
OpcUaAlarmMapper.BuildIdentity(subscriptionSourceReference, sourceName, conditionName);
// Gitea #147: an OtOpcUa IMPORTED condition (mapped/conditions namespace) is keyed on its
// ConditionId, because its SourceName moves when the server's mapping changes. ConditionId
// is select clause 19; OtOpcUa also sets SourceNode = ConditionId for these, which covers a
// server that leaves the dedicated operand unanswered.
var importedConditionId = ImportedConditionKey(fields.Length > 19 ? fields[19] as NodeId? : null)
?? ImportedConditionKey(fields[1] as NodeId?);
var (sourceRef, sourceObjectRef, displayName) = OpcUaAlarmMapper.BuildIdentity(
subscriptionSourceReference, sourceName, conditionName, importedConditionId);
if (string.IsNullOrEmpty(sourceRef))
return; // not a condition event we can key
@@ -1430,7 +1449,38 @@ public class RealOpcUaClient : IOpcUaClient
// else the observed event time; null unless the condition is active AND acked.
AckTime: OpcUaAlarmMapper.DeriveAckTime(active, acked, sourceAckTime, time),
SnapshotSourceReference: inRefresh ? subscriptionSourceReference ?? string.Empty : null,
SnapshotId: inRefresh && _alarmSnapshotIds.TryGetValue(handle, out var epoch) ? epoch : null));
SnapshotId: inRefresh && _alarmSnapshotIds.TryGetValue(handle, out var epoch) ? epoch : null,
DisplayName: displayName));
}
/// <summary>
/// Namespace table used to recognise imported conditions when no session is attached (unit
/// tests drive <see cref="HandleAlarmEvent"/> directly). Production reads the live session's.
/// </summary>
internal NamespaceTable? NamespaceUrisOverride { get; set; }
/// <summary>
/// The durable <c>nsu=…</c> key of an OtOpcUa imported condition, or <c>null</c> when
/// <paramref name="nodeId"/> is not in <see cref="OpcUaAlarmMapper.ImportedConditionsNamespaceUri"/>
/// (Gitea #147). The durable form is used rather than <c>ns=&lt;index&gt;</c> because the
/// namespace index is per session and per server: the two members of an OtOpcUa pair, or one
/// server after an upgrade, may order their namespace arrays differently, and a key that
/// changed on failover would re-key every mirrored condition — the very defect being fixed.
/// The identifier is taken verbatim, never parsed (OtOpcUa: "never construct or parse it").
/// </summary>
/// <param name="nodeId">The ConditionId (or SourceNode) from the event, or null.</param>
/// <returns>The namespace-qualified key, or null when the node is not an imported condition.</returns>
private string? ImportedConditionKey(NodeId? nodeId)
{
if (nodeId is not { } id || id.IsNull || id.NamespaceIndex == 0)
return null;
var table = _session?.NamespaceUris ?? NamespaceUrisOverride;
if (table == null)
return null;
var uri = table.GetString(id.NamespaceIndex);
if (!string.Equals(uri, OpcUaAlarmMapper.ImportedConditionsNamespaceUri, StringComparison.Ordinal))
return null;
return OpcUaNodeReference.ToDurable(new ExpandedNodeId(id), table);
}
/// <summary>The text of a LocalizedText event field (a struct in SDK 2), or null.</summary>