fix(dcl): resolve OPC UA alarm type NodeId to friendly name so conditionFilter works (#8)

HandleAlarmEvent set AlarmTypeName to the event-type NodeId string ("i=9341"),
but the client-side conditionFilter gate (and the OPC UA WhereClause) use friendly
type names — so a friendly-name filter built a correct server WhereClause yet the
client gate dropped every event (zero alarms delivered). Resolve the event-type
NodeId to its friendly name via an inverse of KnownConditionTypeIds (NodeId-string
fallback for custom types) so both sides agree. Also fix a dead-code ternary in
the SourceName derivation.
This commit is contained in:
Joseph Doherty
2026-06-15 14:25:35 -04:00
parent 8825df56be
commit 00304a26e6
3 changed files with 125 additions and 3 deletions
@@ -96,4 +96,18 @@ public class AlarmConditionFilterTests
Assert.Equal(new[] { "AnalogLimit.Hi", "DiscreteAlarm" }, f.Names.OrderBy(n => n).ToArray());
Assert.Empty(AlarmConditionFilter.Parse(null).Names);
}
[Fact]
public void IsAllowed_OpcUaResolvedFriendlyName_MatchesFriendlyNameFilter()
{
// M2.4 (#8) regression: OPC UA delivers events whose AlarmTypeName, after
// RealOpcUaClient.ResolveAlarmTypeName, is a standard friendly type name
// (e.g. "ExclusiveLevelAlarmType"). A friendly-name filter on that source
// built a correct server WhereClause; the client gate must agree and deliver,
// not drop every event (which the prior NodeId-string AlarmTypeName caused).
var f = AlarmConditionFilter.Parse("ExclusiveLevelAlarmType,DiscreteAlarmType");
Assert.True(f.IsAllowed(Tx("ExclusiveLevelAlarmType")));
Assert.True(f.IsAllowed(Tx("DiscreteAlarmType")));
Assert.False(f.IsAllowed(Tx("OffNormalAlarmType")));
}
}
@@ -0,0 +1,63 @@
using Opc.Ua;
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Adapters;
namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests;
/// <summary>
/// M2.4 (#8) regression: standard OPC UA A&amp;C events carry an event-type
/// <see cref="NodeId"/> (e.g. <c>i=9341</c> for ExclusiveLevelAlarmType), but the
/// client-side conditionFilter gate — and the server-side WhereClause — both key off
/// the friendly type names in <see cref="RealOpcUaClient.KnownConditionTypeIds"/>.
/// <see cref="RealOpcUaClient.ResolveAlarmTypeName"/> bridges the two by resolving the
/// event-type NodeId back to its friendly name (NodeId-string fallback for custom
/// types), so a friendly-name filter actually matches the events the server delivers.
/// </summary>
public class RealOpcUaClientAlarmFilterTests
{
[Fact]
public void ResolveAlarmTypeName_KnownStandardNodeId_ReturnsFriendlyName()
{
// The well-known NodeId for ExclusiveLevelAlarmType (i=9341) must resolve to
// the friendly name the conditionFilter/WhereClause use.
var resolved = RealOpcUaClient.ResolveAlarmTypeName(ObjectTypeIds.ExclusiveLevelAlarmType);
Assert.Equal("ExclusiveLevelAlarmType", resolved);
}
[Fact]
public void ResolveAlarmTypeName_DiscreteAlarmNodeId_ReturnsFriendlyName()
{
var resolved = RealOpcUaClient.ResolveAlarmTypeName(ObjectTypeIds.DiscreteAlarmType);
Assert.Equal("DiscreteAlarmType", resolved);
}
[Fact]
public void ResolveAlarmTypeName_UnknownCustomNodeId_ReturnsNodeIdString()
{
// A vendor/custom subtype not in KnownConditionTypeIds: we cannot map it to a
// friendly name, so we fall back to its NodeId string. This is consistent —
// the WhereClause is also omitted for unknown names, so the client gate matches
// the NodeId string, which is the only thing such a filter could carry.
var custom = new NodeId(987654u, 7);
var resolved = RealOpcUaClient.ResolveAlarmTypeName(custom);
Assert.Equal(custom.ToString(), resolved);
}
[Fact]
public void ResolveAlarmTypeName_Null_ReturnsEmptyString()
{
Assert.Equal("", RealOpcUaClient.ResolveAlarmTypeName(null));
}
[Fact]
public void InverseMap_RoundTrips_EveryKnownConditionType()
{
// The friendly→NodeId map (KnownConditionTypeIds) and the NodeId→friendly map
// are derived from a single source of truth, so they must round-trip for every
// entry — guards against the two maps drifting apart.
foreach (var (friendlyName, nodeId) in RealOpcUaClient.KnownConditionTypeIds)
{
var resolved = RealOpcUaClient.ResolveAlarmTypeName(nodeId);
Assert.Equal(friendlyName, resolved);
}
}
}