feat(grpc-data): A1 initial-alarm-state signal in DriverInstanceActor/DriverHostActor — ordered forward, MarkConditionsKnown routing, OpcUaClient backstop, F5 signal timeout + diagnostic

This commit is contained in:
Joseph Doherty
2026-11-24 09:30:32 -05:00
parent d64578b1c5
commit ffdaa73d0c
5 changed files with 341 additions and 5 deletions
@@ -0,0 +1,100 @@
using System.Globalization;
using Microsoft.Extensions.Configuration;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
/// <summary>
/// A1 (plan §5.1 F5 + the OpcUaClient backstop): how long <c>DriverHostActor</c> waits for a native driver's
/// <see cref="IAlarmInitialStateSignal.InitialAlarmStateDelivered"/>, and the per-driver-type backstop for a source
/// that reports itself unsignalable. Both are measured from the driver's quality going Good.
/// </summary>
/// <remarks>
/// <para><c>DataConnections:ConditionKnownSignalTimeout:&lt;DriverType&gt;</c> — default 120 s. On expiry the
/// conditions <b>stay unknown</b> (never known-inactive); the "initial alarm state not signalled" diagnostic is
/// raised on the <see cref="ConditionStateHub"/> and a later signal clears it.</para>
/// <para><c>DataConnections:ConditionKnownSettle:&lt;DriverType&gt;</c> — the backstop timer. Only
/// <see cref="DriverTypeNames.OpcUaClient"/> has one (default 60 s): an upstream with no ConditionRefresh is the
/// one source with no signal at all. A settle configured for any other driver type is ignored.</para>
/// <para>Values are seconds (<c>120</c>) or a <see cref="TimeSpan"/> (<c>00:02:00</c>).</para>
/// </remarks>
public sealed class ConditionSignalOptions
{
/// <summary>The configuration section of the per-DriverType signal timeouts.</summary>
public const string SignalTimeoutSection = "DataConnections:ConditionKnownSignalTimeout";
/// <summary>The configuration section of the per-DriverType backstop settle windows.</summary>
public const string SettleSection = "DataConnections:ConditionKnownSettle";
/// <summary>The default signal timeout (F5).</summary>
public static readonly TimeSpan DefaultSignalTimeout = TimeSpan.FromSeconds(120);
/// <summary>The default OpcUaClient backstop settle window.</summary>
public static readonly TimeSpan DefaultOpcUaClientSettle = TimeSpan.FromSeconds(60);
private readonly Dictionary<string, TimeSpan> _signalTimeouts = new(StringComparer.OrdinalIgnoreCase);
private TimeSpan _opcUaClientSettle = DefaultOpcUaClientSettle;
/// <summary>The hub the F5 diagnostic is raised on (null ⇒ log + counter only).</summary>
public ConditionStateHub? Hub { get; init; }
/// <summary>The signal timeout for <paramref name="driverType"/>.</summary>
/// <param name="driverType">The driver type.</param>
/// <returns>The timeout.</returns>
public TimeSpan SignalTimeoutFor(string driverType) =>
_signalTimeouts.TryGetValue(driverType, out var t) ? t : DefaultSignalTimeout;
/// <summary>The backstop settle window for <paramref name="driverType"/>, or null when it has none.</summary>
/// <param name="driverType">The driver type.</param>
/// <returns>The window.</returns>
public TimeSpan? BackstopSettleFor(string driverType) =>
string.Equals(driverType, DriverTypeNames.OpcUaClient, StringComparison.OrdinalIgnoreCase) ? _opcUaClientSettle : null;
/// <summary>Override one driver type's signal timeout (tests, configuration).</summary>
/// <param name="driverType">The driver type.</param>
/// <param name="timeout">The timeout (&gt; 0).</param>
/// <returns>This instance.</returns>
public ConditionSignalOptions WithSignalTimeout(string driverType, TimeSpan timeout)
{
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(timeout, TimeSpan.Zero);
_signalTimeouts[driverType] = timeout;
return this;
}
/// <summary>Override the OpcUaClient backstop window (tests, configuration).</summary>
/// <param name="settle">The window (≥ 0).</param>
/// <returns>This instance.</returns>
public ConditionSignalOptions WithOpcUaClientSettle(TimeSpan settle)
{
ArgumentOutOfRangeException.ThrowIfLessThan(settle, TimeSpan.Zero);
_opcUaClientSettle = settle;
return this;
}
/// <summary>Bind from configuration; malformed or non-positive values throw (fail host start).</summary>
/// <param name="configuration">The configuration, or null for the defaults.</param>
/// <param name="hub">The condition-state hub.</param>
/// <returns>The options.</returns>
public static ConditionSignalOptions FromConfiguration(IConfiguration? configuration, ConditionStateHub? hub)
{
var options = new ConditionSignalOptions { Hub = hub };
if (configuration is null) return options;
foreach (var child in configuration.GetSection(SignalTimeoutSection).GetChildren())
options.WithSignalTimeout(child.Key, Parse(child.Path, child.Value));
foreach (var child in configuration.GetSection(SettleSection).GetChildren())
{
if (string.Equals(child.Key, DriverTypeNames.OpcUaClient, StringComparison.OrdinalIgnoreCase))
options.WithOpcUaClientSettle(Parse(child.Path, child.Value));
}
return options;
}
private static TimeSpan Parse(string path, string? value)
{
if (double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var seconds) && seconds > 0)
return TimeSpan.FromSeconds(seconds);
if (TimeSpan.TryParse(value, CultureInfo.InvariantCulture, out var span) && span > TimeSpan.Zero) return span;
throw new InvalidOperationException($"{path} '{value}' is not a positive number of seconds or a TimeSpan.");
}
}