d3067ca036
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.
291 lines
13 KiB
C#
291 lines
13 KiB
C#
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
|
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Adapters;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests;
|
|
|
|
/// <summary>Pure OPC UA A&C field → AlarmConditionState/transition mapping.</summary>
|
|
public class OpcUaAlarmMapperTests
|
|
{
|
|
// ── Routing identity must be the binding, not the event name ──
|
|
|
|
// ── Gitea #147: OtOpcUa imported conditions key on ConditionId ──
|
|
|
|
private const string ImportedId = "nsu=https://zb.com/otopcua/mapped/conditions;s=c1.eyJDb25uZWN0aW9uSWQiOiJ4In0";
|
|
|
|
/// <summary>An imported condition is keyed on its ConditionId; SourceName.ConditionName becomes the label.</summary>
|
|
[Fact]
|
|
public void BuildIdentity_ImportedCondition_KeysOnConditionId_WithReadableLabel()
|
|
{
|
|
var (sourceRef, routing, display) = OpcUaAlarmMapper.BuildIdentity(
|
|
"nsu=https://zb.com/otopcua/mapped/raw;s=Plant/remote-a", "Plant/remote-a/Modbus/dev1/HR200", "HR200", ImportedId);
|
|
|
|
Assert.Equal(ImportedId, sourceRef);
|
|
Assert.Equal("nsu=https://zb.com/otopcua/mapped/raw;s=Plant/remote-a", routing); // routing unchanged
|
|
Assert.Equal("Plant/remote-a/Modbus/dev1/HR200.HR200", display);
|
|
}
|
|
|
|
/// <summary>A SourceName move changes only the label, never the key.</summary>
|
|
[Fact]
|
|
public void BuildIdentity_ImportedCondition_SourceNameMove_KeepsKey()
|
|
{
|
|
var before = OpcUaAlarmMapper.BuildIdentity("", "Plant/remote-a/Modbus/dev1/HR200", "HR200", ImportedId);
|
|
var after = OpcUaAlarmMapper.BuildIdentity("", "assembly/remote-line-2/filler/HR200", "HR200", ImportedId);
|
|
|
|
Assert.Equal(before.SourceReference, after.SourceReference);
|
|
Assert.NotEqual(before.DisplayName, after.DisplayName);
|
|
}
|
|
|
|
/// <summary>Every other condition keeps the pre-#147 readable key and carries no separate label.</summary>
|
|
[Theory]
|
|
[InlineData(null)]
|
|
[InlineData("")]
|
|
public void BuildIdentity_NotImported_KeepsSourceNameKey_NoLabel(string? importedId)
|
|
{
|
|
var (sourceRef, _, display) = OpcUaAlarmMapper.BuildIdentity("", "pymodbus/plc/HR200", "HR200", importedId);
|
|
|
|
Assert.Equal("pymodbus/plc/HR200.HR200", sourceRef);
|
|
Assert.Null(display);
|
|
}
|
|
|
|
/// <summary>Verifies that a NodeId binding routes on the binding rather than the event name.</summary>
|
|
[Fact]
|
|
public void BuildIdentity_NodeIdBinding_RoutesOnBindingNotName()
|
|
{
|
|
// OtOpcUa v3 post-migration: SourceName is the RawPath, ConditionName the leaf. The
|
|
// binding is a durable node reference. The bug was SourceObjectReference = SourceName,
|
|
// which never prefix-matches a NodeId binding, so every transition was dropped.
|
|
var (sourceRef, sourceObjectRef) = OpcUaAlarmMapper.BuildIdentity(
|
|
subscriptionSourceReference: "nsu=https://zb.com/otopcua/raw;s=pymodbus/plc/HR200",
|
|
sourceName: "pymodbus/plc/HR200",
|
|
conditionName: "HR200");
|
|
|
|
// Routing identity == the binding, verbatim → DataConnectionActor exact-matches.
|
|
Assert.Equal("nsu=https://zb.com/otopcua/raw;s=pymodbus/plc/HR200", sourceObjectRef);
|
|
// Per-condition key stays the readable RawPath + condition, kept distinct.
|
|
Assert.Equal("pymodbus/plc/HR200.HR200", sourceRef);
|
|
}
|
|
|
|
/// <summary>Verifies that a legacy index binding routes verbatim.</summary>
|
|
[Fact]
|
|
public void BuildIdentity_LegacyIndexBinding_RoutesVerbatim()
|
|
{
|
|
// A binding stored in the old ns= form routes just as well — the point of using
|
|
// the binding verbatim is that it is form-agnostic.
|
|
var (_, sourceObjectRef) = OpcUaAlarmMapper.BuildIdentity(
|
|
subscriptionSourceReference: "ns=2;s=pymodbus/plc/HR200",
|
|
sourceName: "pymodbus/plc/HR200",
|
|
conditionName: "HR200");
|
|
|
|
Assert.Equal("ns=2;s=pymodbus/plc/HR200", sourceObjectRef);
|
|
}
|
|
|
|
/// <summary>Verifies that an aggregate feed keeps an empty identity.</summary>
|
|
[Fact]
|
|
public void BuildIdentity_AggregateFeed_StaysEmpty()
|
|
{
|
|
// Empty binding = the Server-object aggregate feed: it must reach only "mirror
|
|
// everything" subscribers (StartsWith("") matches all) and never a specific-node
|
|
// binding, so the routing identity stays empty while the per-condition key is real.
|
|
var (sourceRef, sourceObjectRef) = OpcUaAlarmMapper.BuildIdentity(
|
|
subscriptionSourceReference: "",
|
|
sourceName: "pymodbus/plc/HR200",
|
|
conditionName: "HR200");
|
|
|
|
Assert.Equal("", sourceObjectRef);
|
|
Assert.Equal("pymodbus/plc/HR200.HR200", sourceRef);
|
|
}
|
|
|
|
/// <summary>Verifies that a missing condition name uses the source name alone.</summary>
|
|
[Fact]
|
|
public void BuildIdentity_NoConditionName_UsesSourceNameAlone()
|
|
{
|
|
var (sourceRef, _) = OpcUaAlarmMapper.BuildIdentity(
|
|
subscriptionSourceReference: "nsu=urn:x;s=Tank01",
|
|
sourceName: "Tank01",
|
|
conditionName: null);
|
|
|
|
Assert.Equal("Tank01", sourceRef);
|
|
}
|
|
|
|
/// <summary>Verifies that a missing source name falls back to the binding for the key.</summary>
|
|
[Fact]
|
|
public void BuildIdentity_NoSourceName_FallsBackToBindingForKey()
|
|
{
|
|
// A server that leaves SourceName unset still yields a stable, non-empty key.
|
|
var (sourceRef, sourceObjectRef) = OpcUaAlarmMapper.BuildIdentity(
|
|
subscriptionSourceReference: "ns=2;s=X",
|
|
sourceName: null,
|
|
conditionName: "Hi");
|
|
|
|
Assert.Equal("ns=2;s=X.Hi", sourceRef);
|
|
Assert.Equal("ns=2;s=X", sourceObjectRef);
|
|
}
|
|
|
|
/// <summary>Verifies that severity normalization clamps to the range 0 to 1000.</summary>
|
|
[Fact]
|
|
public void NormalizeSeverity_ClampsTo0_1000()
|
|
{
|
|
Assert.Equal(1000, OpcUaAlarmMapper.NormalizeSeverity(5000));
|
|
Assert.Equal(0, OpcUaAlarmMapper.NormalizeSeverity(-1));
|
|
Assert.Equal(500, OpcUaAlarmMapper.NormalizeSeverity(500));
|
|
}
|
|
|
|
/// <summary>Verifies that an active, unacknowledged event builds the expected condition state.</summary>
|
|
[Fact]
|
|
public void BuildCondition_ActiveUnacked()
|
|
{
|
|
var c = OpcUaAlarmMapper.BuildCondition(active: true, acked: false, confirmed: null,
|
|
shelve: AlarmShelveState.Unshelved, suppressed: false, severity: 700);
|
|
Assert.True(c.Active);
|
|
Assert.False(c.Acknowledged);
|
|
Assert.Equal(700, c.Severity);
|
|
Assert.Equal(700, c.Priority); // OPC UA has no separate priority (#129)
|
|
Assert.Equal(AlarmShelveState.Unshelved, c.Shelve);
|
|
}
|
|
|
|
/// <summary>Verifies that an acknowledge edge yields an Acknowledge transition.</summary>
|
|
[Fact]
|
|
public void DeriveKind_AckEdge_YieldsAcknowledge()
|
|
{
|
|
Assert.Equal(AlarmTransitionKind.Acknowledge,
|
|
OpcUaAlarmMapper.DeriveKind(prevAcked: false, nowAcked: true, prevActive: true, nowActive: true));
|
|
}
|
|
|
|
/// <summary>Verifies that an active edge yields a Raise transition.</summary>
|
|
[Fact]
|
|
public void DeriveKind_ActiveEdge_YieldsRaise()
|
|
{
|
|
Assert.Equal(AlarmTransitionKind.Raise,
|
|
OpcUaAlarmMapper.DeriveKind(prevAcked: true, nowAcked: true, prevActive: false, nowActive: true));
|
|
}
|
|
|
|
/// <summary>Verifies that an inactive edge yields a Clear transition.</summary>
|
|
[Fact]
|
|
public void DeriveKind_InactiveEdge_YieldsClear()
|
|
{
|
|
Assert.Equal(AlarmTransitionKind.Clear,
|
|
OpcUaAlarmMapper.DeriveKind(prevAcked: true, nowAcked: true, prevActive: true, nowActive: false));
|
|
}
|
|
|
|
/// <summary>Verifies that the current shelving state name maps to the expected shelve state.</summary>
|
|
/// <param name="name">The shelving state name reported by the source, or null.</param>
|
|
/// <param name="expected">The expected shelve state.</param>
|
|
[Theory]
|
|
[InlineData("OneShotShelved", AlarmShelveState.OneShotShelved)]
|
|
[InlineData("TimedShelved", AlarmShelveState.TimedShelved)]
|
|
[InlineData("Unshelved", AlarmShelveState.Unshelved)]
|
|
[InlineData(null, AlarmShelveState.Unshelved)]
|
|
public void MapShelve_MapsCurrentStateName(string? name, AlarmShelveState expected)
|
|
{
|
|
Assert.Equal(expected, OpcUaAlarmMapper.MapShelve(name));
|
|
}
|
|
|
|
// ── PickLimitValue ─────────────────────────────────────────
|
|
|
|
/// <summary>Verifies that a set of all-null limits returns an empty value.</summary>
|
|
[Fact]
|
|
public void PickLimitValue_AllNull_ReturnsEmpty()
|
|
{
|
|
// All four limit fields absent (non-limit alarm type) → empty string.
|
|
Assert.Equal("", OpcUaAlarmMapper.PickLimitValue(null, null, null, null));
|
|
}
|
|
|
|
/// <summary>Verifies that a present high-high limit is returned.</summary>
|
|
[Fact]
|
|
public void PickLimitValue_HighHighLimitPresent_ReturnsIt()
|
|
{
|
|
// HighHighLimit takes top priority; other fields are null (absent).
|
|
var result = OpcUaAlarmMapper.PickLimitValue(100.5, null, null, null);
|
|
Assert.Equal("100.5", result);
|
|
}
|
|
|
|
/// <summary>Verifies that when only the high limit is present it is returned.</summary>
|
|
[Fact]
|
|
public void PickLimitValue_OnlyHighLimit_ReturnsHighLimit()
|
|
{
|
|
// Only HighLimit present (HighHighLimit absent on this alarm type).
|
|
var result = OpcUaAlarmMapper.PickLimitValue(null, 80.0, null, null);
|
|
Assert.Equal("80", result);
|
|
}
|
|
|
|
/// <summary>Verifies that the high-high limit wins over the high limit when both are present.</summary>
|
|
[Fact]
|
|
public void PickLimitValue_PriorityOrder_HighHighWinsOverHigh()
|
|
{
|
|
// When multiple limits are present, HighHighLimit takes precedence.
|
|
var result = OpcUaAlarmMapper.PickLimitValue(95.0, 80.0, 20.0, 5.0);
|
|
Assert.Equal("95", result);
|
|
}
|
|
|
|
/// <summary>Verifies that when only the low-low limit is present it is returned.</summary>
|
|
[Fact]
|
|
public void PickLimitValue_OnlyLowLow_ReturnsLowLow()
|
|
{
|
|
// LowLowLimit only — last in priority, but should still be returned.
|
|
var result = OpcUaAlarmMapper.PickLimitValue(null, null, null, -10.5);
|
|
Assert.Equal("-10.5", result);
|
|
}
|
|
|
|
/// <summary>Verifies that limit values are formatted using the invariant culture.</summary>
|
|
[Fact]
|
|
public void PickLimitValue_UsesInvariantCulture()
|
|
{
|
|
// Decimal separator must always be '.' regardless of thread culture.
|
|
var result = OpcUaAlarmMapper.PickLimitValue(1.5, null, null, null);
|
|
Assert.Contains('.', result); // invariant culture: '.' not ','
|
|
Assert.Equal("1.5", result);
|
|
}
|
|
|
|
// ── MES alarm-status API §6.4: AckTime stamping ──
|
|
|
|
private static readonly DateTimeOffset SourceAck =
|
|
new(2026, 8, 1, 10, 0, 0, TimeSpan.Zero);
|
|
private static readonly DateTimeOffset Observed =
|
|
new(2026, 8, 1, 10, 5, 0, TimeSpan.Zero);
|
|
|
|
/// <summary>Verifies that an active, acknowledged alarm prefers the source acknowledgement time.</summary>
|
|
[Fact]
|
|
public void DeriveAckTime_ActiveAcked_PrefersTheSourcesOwnAckInstant()
|
|
{
|
|
// OPC UA A&C DOES supply a true ack time (AckedState/TransitionTime). When the
|
|
// server sends it, it must win over the coarser observation time — that is the
|
|
// whole point of selecting field 18.
|
|
Assert.Equal(
|
|
SourceAck,
|
|
OpcUaAlarmMapper.DeriveAckTime(active: true, acked: true, SourceAck, Observed));
|
|
}
|
|
|
|
/// <summary>Verifies that an active, acknowledged alarm without a source time falls back to the observation time.</summary>
|
|
[Fact]
|
|
public void DeriveAckTime_ActiveAcked_WithoutSourceTime_FallsBackToObservationTime()
|
|
{
|
|
// Servers may omit AckedState/TransitionTime (base ConditionType events, or a
|
|
// server that does not expose it). The fallback is the event's own time — real,
|
|
// just coarser. Never fabricated, never null-when-acked.
|
|
Assert.Equal(
|
|
Observed,
|
|
OpcUaAlarmMapper.DeriveAckTime(active: true, acked: true, sourceAckTime: null, Observed));
|
|
}
|
|
|
|
/// <summary>Verifies that an unacknowledged alarm has no acknowledgement time even when the source reports an acknowledgement transition.</summary>
|
|
[Fact]
|
|
public void DeriveAckTime_Unacked_IsNull_EvenWhenSourceReportsAnAckTransition()
|
|
{
|
|
// AckedState/TransitionTime also stamps the flip BACK to unacked on a re-raise,
|
|
// so it is present-but-meaningless there. The acked check is what makes "null
|
|
// while unacked" and "cleared on re-raise" hold.
|
|
Assert.Null(OpcUaAlarmMapper.DeriveAckTime(
|
|
active: true, acked: false, sourceAckTime: SourceAck, Observed));
|
|
}
|
|
|
|
/// <summary>Verifies that an inactive alarm has no acknowledgement time.</summary>
|
|
[Fact]
|
|
public void DeriveAckTime_Inactive_IsNull()
|
|
{
|
|
// A cleared condition is on its way out of the mirror and reports no ack time,
|
|
// matching the MxGateway mapper (where INACTIVE maps to acked = true).
|
|
Assert.Null(OpcUaAlarmMapper.DeriveAckTime(
|
|
active: false, acked: true, sourceAckTime: SourceAck, Observed));
|
|
}
|
|
}
|