fix(worker): hold subtag alarm state on bad quality; keep an initial-sync ack (MXA-005, MXA-007, #254)
ci / windows-x86 (push) Failing after 16m6s
ci / java (push) Failing after 16m33s
ci / portable (push) Failing after 19m4s
ci / windows-x86 (push) Failing after 16m6s
ci / java (push) Failing after 16m33s
ci / portable (push) Failing after 19m4s
LmxSubtagAlarmSource now passes the OnDataChange quality through. The subtag state machine ignores InAlarm/Acked updates that are not good quality or carry no value instead of coercing them to false (which read as a Clear). An Acked=true buffered before the first InAlarm value is the initial state of an already-acked alarm and survives the raise.
This commit is contained in:
@@ -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:
|
||||
|
||||
|
||||
@@ -75,6 +75,28 @@ public sealed class LmxSubtagAlarmSourceTests
|
||||
Assert.Equal(DateTimeKind.Utc, received.TimestampUtc.Kind);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies the item quality reaches the normalized change, so the state
|
||||
/// machine can hold state across a bad-quality update (MXA-005).
|
||||
/// </summary>
|
||||
[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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies <c>OnDataChange</c> for an unknown item handle is ignored
|
||||
/// (no <see cref="LmxSubtagAlarmSource.ValueChanged"/> raised).
|
||||
|
||||
@@ -203,7 +203,11 @@ public sealed class SubtagAlarmStateMachineTests
|
||||
/// active must emit nothing and must NOT latch
|
||||
/// <c>AckedDuringEpisode</c> — 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 <c>.active = false</c> is what marks the stale ack as a
|
||||
/// prior episode's (an ack buffered before the FIRST <c>.active</c> value
|
||||
/// is the initial state of an already-acked alarm — see
|
||||
/// <see cref="AckedBeforeActive_AtInitialSync_KeepsTheAck"/>).
|
||||
/// </summary>
|
||||
[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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MXA-007: an alarm that is already active and acknowledged at subscribe
|
||||
/// time may deliver <c>.acked = true</c> before <c>.active = true</c>. The
|
||||
/// buffered ack is the alarm's real state and must survive the initial
|
||||
/// raise; no later <c>.acked</c> change would restore it.
|
||||
/// </summary>
|
||||
[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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
[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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MXA-005: an <c>.active</c> 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.
|
||||
/// </summary>
|
||||
[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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MXA-005: a bad- or uncertain-quality update is ignored even when it
|
||||
/// carries a value; a later good update applies normally.
|
||||
/// </summary>
|
||||
/// <param name="quality">The non-good MXAccess quality.</param>
|
||||
[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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,9 +85,9 @@ public sealed class LmxSubtagAlarmSource : ISubtagAlarmSource
|
||||
/// bypassing the live COM factory. Exposed via
|
||||
/// <c>InternalsVisibleTo("ZB.MOM.WW.MxGateway.Worker.Tests")</c> so tests can
|
||||
/// exercise <see cref="Advise"/> and
|
||||
/// <see cref="HandleDataChange"/> without a live <c>LMXProxyServerClass</c>.
|
||||
/// <see cref="HandleDataChange(int, object, object, int?)"/> without a live <c>LMXProxyServerClass</c>.
|
||||
/// No COM event source is wired in this mode — tests drive
|
||||
/// <see cref="HandleDataChange"/> directly to simulate an
|
||||
/// <see cref="HandleDataChange(int, object, object, int?)"/> directly to simulate an
|
||||
/// <c>OnDataChange</c> callback.
|
||||
/// </summary>
|
||||
/// <param name="server">The server abstraction to drive.</param>
|
||||
@@ -154,6 +154,21 @@ public sealed class LmxSubtagAlarmSource : ISubtagAlarmSource
|
||||
int phItemHandle,
|
||||
object? pvItemValue,
|
||||
object? pftItemTimeStamp)
|
||||
=> HandleDataChange(phItemHandle, pvItemValue, pftItemTimeStamp, quality: null);
|
||||
|
||||
/// <summary>
|
||||
/// Normalizes an MXAccess <c>OnDataChange</c> callback, carrying the
|
||||
/// item quality, into a <see cref="SubtagValueChange"/>.
|
||||
/// </summary>
|
||||
/// <param name="phItemHandle">The MXAccess item handle that changed.</param>
|
||||
/// <param name="pvItemValue">The new item value (left as a boxed COM variant; the state machine coerces it).</param>
|
||||
/// <param name="pftItemTimeStamp">The item timestamp as delivered by MXAccess (a culture-formatted string).</param>
|
||||
/// <param name="quality">The MXAccess item quality, or <see langword="null"/> when not reported.</param>
|
||||
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
|
||||
|
||||
/// <summary>
|
||||
/// COM <c>OnDataChange</c> delegate target. Forwards to
|
||||
/// <see cref="HandleDataChange"/>; the quality, status array, and server
|
||||
/// handle carry no information the subtag fallback needs.
|
||||
/// <see cref="HandleDataChange(int, object, object, int?)"/> 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.
|
||||
/// </summary>
|
||||
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)
|
||||
|
||||
@@ -197,7 +197,7 @@ public sealed class SubtagAlarmConsumer : IMxAccessAlarmConsumer
|
||||
}
|
||||
|
||||
IReadOnlyList<MxAlarmTransitionEvent> transitions =
|
||||
stateMachine.Apply(change.ItemAddress, change.Value, change.TimestampUtc);
|
||||
stateMachine.Apply(change.ItemAddress, change.Value, change.TimestampUtc, change.Quality);
|
||||
if (transitions.Count == 0)
|
||||
{
|
||||
return;
|
||||
|
||||
@@ -66,14 +66,45 @@ public sealed class SubtagAlarmStateMachine
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="itemAddress">The subtag address whose value changed.</param>
|
||||
/// <param name="value">The new value (bool, numeric, or string).</param>
|
||||
/// <param name="timestampUtc">The UTC timestamp of the change.</param>
|
||||
/// <returns>The synthesized alarm transitions, if any.</returns>
|
||||
public IReadOnlyList<MxAlarmTransitionEvent> Apply(string itemAddress, object? value, DateTime timestampUtc)
|
||||
=> Apply(itemAddress, value, timestampUtc, quality: null);
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// An <c>.active</c> or <c>.acked</c> 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
|
||||
/// <see langword="false"/> 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.
|
||||
/// </remarks>
|
||||
/// <param name="itemAddress">The subtag address whose value changed.</param>
|
||||
/// <param name="value">The new value (bool, numeric, or string).</param>
|
||||
/// <param name="timestampUtc">The UTC timestamp of the change.</param>
|
||||
/// <param name="quality">
|
||||
/// The MXAccess (OPC-style) quality of the update, or <see langword="null"/>
|
||||
/// when the source does not report one.
|
||||
/// </param>
|
||||
/// <returns>The synthesized alarm transitions, if any.</returns>
|
||||
public IReadOnlyList<MxAlarmTransitionEvent> 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<MxAlarmTransitionEvent> 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
|
||||
/// <summary>Gets or sets whether the alarm is currently acknowledged.</summary>
|
||||
public bool Acked { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether a usable <c>.active</c> value has been applied
|
||||
/// since subscribe. Until then an <c>.acked</c> value is buffered state
|
||||
/// from the initial sync rather than an acknowledgement of a raise.
|
||||
/// </summary>
|
||||
public bool ActiveObserved { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Latches that the alarm was acknowledged at some point during the
|
||||
/// current active episode, so a clear emits ACK_RTN even when the
|
||||
|
||||
@@ -13,4 +13,12 @@ public sealed class SubtagValueChange
|
||||
|
||||
/// <summary>Gets or sets the UTC timestamp the change was observed.</summary>
|
||||
public DateTime TimestampUtc { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the MXAccess (OPC-style) quality of the update, or
|
||||
/// <see langword="null"/> 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.
|
||||
/// </summary>
|
||||
public int? Quality { get; set; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user