diff --git a/docs/AlarmClientDiscovery.md b/docs/AlarmClientDiscovery.md
index c61e1dca..4bdfedd4 100644
--- a/docs/AlarmClientDiscovery.md
+++ b/docs/AlarmClientDiscovery.md
@@ -979,6 +979,24 @@ MXAccess does not guarantee the `Acked = false` update arrives before the
`InAlarm = false` update. The latch ensures a clear always emits `ACK_RTN`
when the alarm was acknowledged at any point during the active episode.
+**Initial sync.** The first `InAlarm` value after subscribe is the alarm's
+current state, not an observed raise. An alarm that is already active and
+acknowledged can deliver `Acked = true` before `InAlarm = true`, and no later
+`Acked` change arrives to correct it. So an `Acked = true` buffered before the
+first `InAlarm` value is kept when that value is `true`: the raise is reported
+as `ACK_ALM` (a Raise of an acknowledged alarm) and the latch is set. Any raise
+after the first `InAlarm` value is a new episode and starts unacknowledged.
+
+**Quality.** An `InAlarm` or `Acked` update whose MXAccess quality is not good
+(OPC quality bits `0xC0` not both set — bad or uncertain), or whose value is
+empty, is ignored and the alarm keeps its last known state. MXAccess reports an
+engine going off scan or a reference becoming unresolved this way. Coercing the
+empty value to `false` would report every active alarm on that object as
+returned to normal, and the gateway's reconcile would confirm it from the
+standby snapshot. The subtag provider already reports `degraded = true` on every
+record, so holding state adds no new signal. Priority updates fall back to the
+last known priority as before.
+
`SnapshotActive()` returns one `MxAlarmSnapshotRecord` per currently-active
alarm. State mapping:
diff --git a/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/LmxSubtagAlarmSourceTests.cs b/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/LmxSubtagAlarmSourceTests.cs
index ab74bd9b..61ad581f 100644
--- a/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/LmxSubtagAlarmSourceTests.cs
+++ b/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/LmxSubtagAlarmSourceTests.cs
@@ -75,6 +75,28 @@ public sealed class LmxSubtagAlarmSourceTests
Assert.Equal(DateTimeKind.Utc, received.TimestampUtc.Kind);
}
+ ///
+ /// Verifies the item quality reaches the normalized change, so the state
+ /// machine can hold state across a bad-quality update (MXA-005).
+ ///
+ [Fact]
+ public void HandleDataChange_CarriesQuality()
+ {
+ var server = new RecordingMxAccessServer();
+ using var source = new LmxSubtagAlarmSource(server, FakeServerHandle);
+
+ source.Advise(new[] { "Tank1.Alarm.InAlarm" });
+ int itemHandle = server.LastItemHandleFor("Tank1.Alarm.InAlarm");
+
+ SubtagValueChange? received = null;
+ source.ValueChanged += (_, change) => received = change;
+
+ source.HandleDataChange(itemHandle, pvItemValue: null, pftItemTimeStamp: null, quality: 0x08);
+
+ Assert.NotNull(received);
+ Assert.Equal(0x08, received!.Quality);
+ }
+
///
/// Verifies OnDataChange for an unknown item handle is ignored
/// (no raised).
diff --git a/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/SubtagAlarmStateMachineTests.cs b/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/SubtagAlarmStateMachineTests.cs
index c6773401..ef1d02af 100644
--- a/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/SubtagAlarmStateMachineTests.cs
+++ b/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/SubtagAlarmStateMachineTests.cs
@@ -203,7 +203,11 @@ public sealed class SubtagAlarmStateMachineTests
/// active must emit nothing and must NOT latch
/// AckedDuringEpisode — otherwise a stale ack from a prior episode
/// would mis-latch the next raise into a spurious ACK_RTN on clear. The
- /// subsequent raise/clear must therefore still emit UNACK_RTN.
+ /// subsequent raise/clear must therefore still emit UNACK_RTN. The
+ /// initial-sync .active = false is what marks the stale ack as a
+ /// prior episode's (an ack buffered before the FIRST .active value
+ /// is the initial state of an already-acked alarm — see
+ /// ).
///
[Fact]
public void AckedTrueWhileInactive_EmitsNothingAndDoesNotLatch()
@@ -215,6 +219,9 @@ public sealed class SubtagAlarmStateMachineTests
var ackEvents = sm.Apply("Tank01.Level.HiHi.acked", true, ts);
Assert.Empty(ackEvents);
+ // Initial sync: the alarm is not active.
+ Assert.Empty(sm.Apply("Tank01.Level.HiHi.active", false, ts.AddSeconds(1)));
+
// A fresh episode: raise then clear. Because the earlier ack must not
// have latched AckedDuringEpisode, the clear must be UNACK_RTN.
sm.Apply("Tank01.Level.HiHi.active", true, ts.AddSeconds(5));
@@ -257,4 +264,88 @@ public sealed class SubtagAlarmStateMachineTests
var snap = Assert.Single(sm.SnapshotActive());
Assert.Equal(750, snap.Priority);
}
+
+ ///
+ /// MXA-007: an alarm that is already active and acknowledged at subscribe
+ /// time may deliver .acked = true before .active = true. The
+ /// buffered ack is the alarm's real state and must survive the initial
+ /// raise; no later .acked change would restore it.
+ ///
+ [Fact]
+ public void AckedBeforeActive_AtInitialSync_KeepsTheAck()
+ {
+ var sm = new SubtagAlarmStateMachine(new[] { Target() });
+ var ts = new DateTime(2026, 6, 13, 9, 0, 0, DateTimeKind.Utc);
+
+ Assert.Empty(sm.Apply("Tank01.Level.HiHi.acked", true, ts));
+ var raise = Assert.Single(sm.Apply("Tank01.Level.HiHi.active", true, ts));
+
+ Assert.Equal(MxAlarmStateKind.AckAlm, raise.Record.State);
+ Assert.Equal(MxAlarmStateKind.AckAlm, Assert.Single(sm.SnapshotActive()).State);
+
+ // Its return is an acknowledged return.
+ var clear = Assert.Single(sm.Apply("Tank01.Level.HiHi.active", false, ts.AddSeconds(10)));
+ Assert.Equal(MxAlarmStateKind.AckRtn, clear.Record.State);
+ }
+
+ ///
+ /// MXA-007 boundary: once the initial sync is over, a raise is a new
+ /// episode and starts unacknowledged even if the acked subtag still reads
+ /// true from the previous episode.
+ ///
+ [Fact]
+ public void RaiseAfterInitialSync_StartsUnacknowledged()
+ {
+ var sm = new SubtagAlarmStateMachine(new[] { Target() });
+ var ts = new DateTime(2026, 6, 13, 9, 0, 0, DateTimeKind.Utc);
+
+ sm.Apply("Tank01.Level.HiHi.active", true, ts);
+ sm.Apply("Tank01.Level.HiHi.acked", true, ts.AddSeconds(1));
+ sm.Apply("Tank01.Level.HiHi.active", false, ts.AddSeconds(2));
+
+ var raise = Assert.Single(sm.Apply("Tank01.Level.HiHi.active", true, ts.AddSeconds(3)));
+ Assert.Equal(MxAlarmStateKind.UnackAlm, raise.Record.State);
+ }
+
+ ///
+ /// MXA-005: an .active update with an empty value (what MXAccess
+ /// delivers for an off-scan engine or an unresolved reference) must not
+ /// read as a return to normal. The alarm holds its last known state.
+ ///
+ [Fact]
+ public void ActiveUpdateWithNullValue_HoldsState()
+ {
+ var sm = new SubtagAlarmStateMachine(new[] { Target() });
+ var ts = new DateTime(2026, 6, 13, 9, 0, 0, DateTimeKind.Utc);
+ sm.Apply("Tank01.Level.HiHi.active", true, ts);
+
+ var events = sm.Apply("Tank01.Level.HiHi.active", null, ts.AddSeconds(5), quality: 0);
+
+ Assert.Empty(events);
+ Assert.Equal(MxAlarmStateKind.UnackAlm, Assert.Single(sm.SnapshotActive()).State);
+ }
+
+ ///
+ /// MXA-005: a bad- or uncertain-quality update is ignored even when it
+ /// carries a value; a later good update applies normally.
+ ///
+ /// The non-good MXAccess quality.
+ [Theory]
+ [InlineData(0x00)] // Bad
+ [InlineData(0x08)] // Bad: not connected
+ [InlineData(0x40)] // Uncertain
+ public void ActiveOrAckedUpdateWithNonGoodQuality_IsIgnored(int quality)
+ {
+ var sm = new SubtagAlarmStateMachine(new[] { Target() });
+ var ts = new DateTime(2026, 6, 13, 9, 0, 0, DateTimeKind.Utc);
+ sm.Apply("Tank01.Level.HiHi.active", true, ts, quality: 0xC0);
+
+ Assert.Empty(sm.Apply("Tank01.Level.HiHi.active", false, ts.AddSeconds(1), quality));
+ Assert.Empty(sm.Apply("Tank01.Level.HiHi.acked", true, ts.AddSeconds(2), quality));
+ Assert.Equal(MxAlarmStateKind.UnackAlm, Assert.Single(sm.SnapshotActive()).State);
+
+ // Good quality again: the clear is real.
+ var clear = Assert.Single(sm.Apply("Tank01.Level.HiHi.active", false, ts.AddSeconds(3), quality: 0xC0));
+ Assert.Equal(MxAlarmStateKind.UnackRtn, clear.Record.State);
+ }
}
diff --git a/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/LmxSubtagAlarmSource.cs b/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/LmxSubtagAlarmSource.cs
index 64a3fa8a..7db080fd 100644
--- a/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/LmxSubtagAlarmSource.cs
+++ b/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/LmxSubtagAlarmSource.cs
@@ -85,9 +85,9 @@ public sealed class LmxSubtagAlarmSource : ISubtagAlarmSource
/// bypassing the live COM factory. Exposed via
/// InternalsVisibleTo("ZB.MOM.WW.MxGateway.Worker.Tests") so tests can
/// exercise and
- /// without a live LMXProxyServerClass.
+ /// without a live LMXProxyServerClass.
/// No COM event source is wired in this mode — tests drive
- /// directly to simulate an
+ /// directly to simulate an
/// OnDataChange callback.
///
/// The server abstraction to drive.
@@ -154,6 +154,21 @@ public sealed class LmxSubtagAlarmSource : ISubtagAlarmSource
int phItemHandle,
object? pvItemValue,
object? pftItemTimeStamp)
+ => HandleDataChange(phItemHandle, pvItemValue, pftItemTimeStamp, quality: null);
+
+ ///
+ /// Normalizes an MXAccess OnDataChange callback, carrying the
+ /// item quality, into a .
+ ///
+ /// The MXAccess item handle that changed.
+ /// The new item value (left as a boxed COM variant; the state machine coerces it).
+ /// The item timestamp as delivered by MXAccess (a culture-formatted string).
+ /// The MXAccess item quality, or when not reported.
+ internal void HandleDataChange(
+ int phItemHandle,
+ object? pvItemValue,
+ object? pftItemTimeStamp,
+ int? quality)
{
if (!addressesByItemHandle.TryGetValue(phItemHandle, out string? itemAddress))
{
@@ -174,6 +189,7 @@ public sealed class LmxSubtagAlarmSource : ISubtagAlarmSource
ItemAddress = itemAddress,
Value = pvItemValue,
TimestampUtc = timestampUtc,
+ Quality = quality,
});
}
@@ -279,8 +295,10 @@ public sealed class LmxSubtagAlarmSource : ISubtagAlarmSource
///
/// COM OnDataChange delegate target. Forwards to
- /// ; the quality, status array, and server
- /// handle carry no information the subtag fallback needs.
+ /// with the
+ /// item quality, which the state machine uses to hold an alarm's state
+ /// across a bad-quality update; the status array and server handle carry
+ /// no information the subtag fallback needs.
///
private void OnDataChange(
int hLMXServerHandle,
@@ -290,7 +308,7 @@ public sealed class LmxSubtagAlarmSource : ISubtagAlarmSource
object pftItemTimeStamp,
ref MXSTATUS_PROXY[] pVars)
{
- HandleDataChange(phItemHandle, pvItemValue, pftItemTimeStamp);
+ HandleDataChange(phItemHandle, pvItemValue, pftItemTimeStamp, pwItemQuality);
}
private static DateTime ConvertTimestampUtc(object? pftItemTimeStamp)
diff --git a/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/SubtagAlarmConsumer.cs b/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/SubtagAlarmConsumer.cs
index 4d9257bc..9418ead4 100644
--- a/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/SubtagAlarmConsumer.cs
+++ b/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/SubtagAlarmConsumer.cs
@@ -197,7 +197,7 @@ public sealed class SubtagAlarmConsumer : IMxAccessAlarmConsumer
}
IReadOnlyList transitions =
- stateMachine.Apply(change.ItemAddress, change.Value, change.TimestampUtc);
+ stateMachine.Apply(change.ItemAddress, change.Value, change.TimestampUtc, change.Quality);
if (transitions.Count == 0)
{
return;
diff --git a/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/SubtagAlarmStateMachine.cs b/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/SubtagAlarmStateMachine.cs
index 145c01fb..84f1454f 100644
--- a/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/SubtagAlarmStateMachine.cs
+++ b/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/SubtagAlarmStateMachine.cs
@@ -66,14 +66,45 @@ public sealed class SubtagAlarmStateMachine
}
///
- /// Applies a single subtag value change and returns any synthesized
- /// transitions. Unknown addresses and no-op changes return an empty list.
+ /// Applies a single subtag value change whose quality is not reported
+ /// and returns any synthesized transitions. Unknown addresses and no-op
+ /// changes return an empty list.
///
/// The subtag address whose value changed.
/// The new value (bool, numeric, or string).
/// The UTC timestamp of the change.
/// The synthesized alarm transitions, if any.
public IReadOnlyList Apply(string itemAddress, object? value, DateTime timestampUtc)
+ => Apply(itemAddress, value, timestampUtc, quality: null);
+
+ ///
+ /// Applies a single subtag value change and returns any synthesized
+ /// transitions. Unknown addresses, no-op changes, and state updates that
+ /// carry no trustworthy value return an empty list.
+ ///
+ ///
+ /// An .active or .acked update whose quality is not good,
+ /// or whose value is empty, is ignored and the alarm keeps its last known
+ /// state. MXAccess reports an off-scan engine or an unresolved reference
+ /// with a bad quality and (typically) an empty value; coercing that to
+ /// would report every active alarm on the object
+ /// as returned to normal when nothing is known about it (MXA-005). The
+ /// subtag provider is already reported as degraded whenever it serves, so
+ /// holding state needs no further signal.
+ ///
+ /// The subtag address whose value changed.
+ /// The new value (bool, numeric, or string).
+ /// The UTC timestamp of the change.
+ ///
+ /// The MXAccess (OPC-style) quality of the update, or
+ /// when the source does not report one.
+ ///
+ /// The synthesized alarm transitions, if any.
+ public IReadOnlyList Apply(
+ string itemAddress,
+ object? value,
+ DateTime timestampUtc,
+ int? quality)
{
if (itemAddress is null || !_bindingsByAddress.TryGetValue(itemAddress, out SubtagBinding binding))
{
@@ -81,6 +112,12 @@ public sealed class SubtagAlarmStateMachine
}
AlarmState state = binding.State;
+ if ((binding.Role is SubtagRole.Active or SubtagRole.Acked)
+ && (value is null || (quality.HasValue && !IsGoodQuality(quality.Value))))
+ {
+ return NoEvents;
+ }
+
switch (binding.Role)
{
case SubtagRole.Active:
@@ -117,8 +154,16 @@ public sealed class SubtagAlarmStateMachine
return records;
}
+ // OPC-style quality: the top two bits of the low byte are 11 for Good.
+ private static bool IsGoodQuality(int quality) => (quality & 0xC0) == 0xC0;
+
private IReadOnlyList ApplyActive(AlarmState state, bool newActive, DateTime timestampUtc)
{
+ // The first usable .active value of this subscription is the initial
+ // sync, not an observed raise.
+ bool initialSync = !state.ActiveObserved;
+ state.ActiveObserved = true;
+
if (newActive == state.Active)
{
return NoEvents;
@@ -126,13 +171,24 @@ public sealed class SubtagAlarmStateMachine
if (newActive)
{
- // false -> true: alarm raised, initially unacknowledged.
+ // false -> true: alarm raised, initially unacknowledged — except at
+ // initial sync, where an alarm that is already active and
+ // acknowledged may deliver its .acked = true before its
+ // .active = true. That buffered ack is the alarm's real state, and
+ // no further .acked change will arrive to restore it, so it is kept
+ // (MXA-007). A raise seen after initial sync is a new episode and
+ // starts unacknowledged.
+ bool alreadyAcked = initialSync && state.Acked;
MxAlarmStateKind previous = state.DerivedState();
state.Active = true;
- state.Acked = false;
- state.AckedDuringEpisode = false;
+ state.Acked = alreadyAcked;
+ state.AckedDuringEpisode = alreadyAcked;
state.FirstRaiseUtc = timestampUtc;
- return Single(state, MxAlarmStateKind.UnackAlm, previous, timestampUtc);
+ return Single(
+ state,
+ alreadyAcked ? MxAlarmStateKind.AckAlm : MxAlarmStateKind.UnackAlm,
+ previous,
+ timestampUtc);
}
else
{
@@ -309,6 +365,13 @@ public sealed class SubtagAlarmStateMachine
/// Gets or sets whether the alarm is currently acknowledged.
public bool Acked { get; set; }
+ ///
+ /// Gets or sets whether a usable .active value has been applied
+ /// since subscribe. Until then an .acked value is buffered state
+ /// from the initial sync rather than an acknowledgement of a raise.
+ ///
+ public bool ActiveObserved { get; set; }
+
///
/// Latches that the alarm was acknowledged at some point during the
/// current active episode, so a clear emits ACK_RTN even when the
diff --git a/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/SubtagValueChange.cs b/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/SubtagValueChange.cs
index 18113570..1bfad3f5 100644
--- a/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/SubtagValueChange.cs
+++ b/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/SubtagValueChange.cs
@@ -13,4 +13,12 @@ public sealed class SubtagValueChange
/// Gets or sets the UTC timestamp the change was observed.
public DateTime TimestampUtc { get; set; }
+
+ ///
+ /// Gets or sets the MXAccess (OPC-style) quality of the update, or
+ /// when the source does not report one. A
+ /// non-good quality makes the state machine hold the alarm's last
+ /// known state instead of coercing the value.
+ ///
+ public int? Quality { get; set; }
}