using Microsoft.Extensions.Logging;
using Opc.Ua;
using Opc.Ua.Server;
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer;
///
/// Production that writes the OPC UA Server object's
/// ServiceLevel Variable through the SDK. Clients reading
/// VariableIds.Server_ServiceLevel see the live value updated whenever the redundancy
/// state changes — that's the standard OPC UA non-transparent-redundancy signal callers use
/// to pick a primary.
///
/// Uses (a ) and
/// its child variable, which the SDK populates
/// automatically during initialization. Writes are
/// guarded by so concurrent diagnostics scans
/// from the SDK don't fight with our update.
///
/// SDK 2.0 (lmxopcua#801): the server now recomputes Server.ServiceLevel from session headroom on
/// every session create/close (255 when lightly loaded) and overwrites any value ≥ 200 — so a Primary's
/// 250, a Secondary's 240 and the Stale tier's 200 all read back as 255 after the next client connects,
/// and both halves of a pair look identical. The SDK stops doing that only once a provider claims the
/// value through , so this publisher
/// claims it at construction and writes through the returned writer. A server that does not offer the
/// claim (test doubles) keeps the direct diagnostics-guarded write.
///
///
public sealed class SdkServiceLevelPublisher : IServiceLevelPublisher
{
private readonly IServerInternal _serverInternal;
private readonly ILogger _logger;
private readonly Action? _claimedWriter;
/// Initializes a new instance of the SdkServiceLevelPublisher class.
/// The OPC UA server internal interface.
/// The logger instance.
public SdkServiceLevelPublisher(IServerInternal serverInternal, ILogger logger)
{
_serverInternal = serverInternal;
_logger = logger;
// Claim ownership so the SDK's session-headroom calculator stops overwriting the redundancy value
// (lmxopcua#801). Throws if the server has no ServiceLevel or another provider already claimed it —
// the hosted service's binding catch turns that into a failed endpoint rather than a silent 255.
if (serverInternal is IServerServiceLevelControl control)
_claimedWriter = control.ClaimServiceLevelControl();
}
///
public void Publish(byte serviceLevel)
{
if (_claimedWriter is not null)
{
try
{
_claimedWriter(serviceLevel);
return;
}
catch (Exception ex)
{
_logger.LogWarning(ex, "SdkServiceLevelPublisher: write to Server.ServiceLevel threw");
throw;
}
}
var node = _serverInternal.ServerObject?.ServiceLevel;
if (node is null)
{
_logger.LogWarning("SdkServiceLevelPublisher: ServerObject.ServiceLevel unavailable");
throw new InvalidOperationException("Server.ServiceLevel is unavailable.");
}
try
{
_serverInternal.UpdateServerDiagnostics(_ =>
{
node.Value = serviceLevel;
node.Timestamp = DateTime.UtcNow;
node.StatusCode = StatusCodes.Good;
node.ClearChangeMasks(_serverInternal.DefaultSystemContext, includeChildren: false);
});
}
catch (Exception ex)
{
_logger.LogWarning(ex, "SdkServiceLevelPublisher: write to Server.ServiceLevel threw");
throw;
}
}
}