feat(cli): add enable/disable condition commands (H4 client path)

This commit is contained in:
Joseph Doherty
2026-06-19 01:48:21 -04:00
parent eb328e5761
commit 88d5ba45bf
10 changed files with 488 additions and 0 deletions
@@ -0,0 +1,55 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using Opc.Ua;
using ZB.MOM.WW.OtOpcUa.Client.Shared;
namespace ZB.MOM.WW.OtOpcUa.Client.CLI.Commands;
[Command("disable", Description = "Disable an alarm condition (OPC UA Part 9 ConditionType Disable)")]
public class DisableCommand : CommandBase
{
/// <summary>
/// Creates the disable command used to disable an OPC UA alarm condition from the terminal.
/// </summary>
/// <param name="factory">The factory that creates the shared client service for the command run.</param>
public DisableCommand(IOpcUaClientServiceFactory factory) : base(factory)
{
}
/// <summary>
/// Gets the condition node ID of the alarm to disable.
/// </summary>
[CommandOption("node", 'n', Description = "Condition node ID of the alarm to disable", IsRequired = true)]
public string NodeId { get; init; } = default!;
/// <summary>
/// Connects to the server and disables the specified alarm condition.
/// </summary>
/// <param name="console">The CLI console used for output and cancellation handling.</param>
public override async ValueTask ExecuteAsync(IConsole console)
{
ConfigureLogging();
IOpcUaClientService? service = null;
try
{
var ct = console.RegisterCancellationHandler();
(service, _) = await CreateServiceAndConnectAsync(ct);
var statusCode = await service.DisableAsync(NodeId, ct);
if (StatusCode.IsGood(statusCode))
await console.Output.WriteLineAsync($"Disable successful: {NodeId}");
else
await console.Output.WriteLineAsync($"Disable failed: {statusCode}");
}
finally
{
if (service != null)
{
await service.DisconnectAsync();
service.Dispose();
}
}
}
}
@@ -0,0 +1,55 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using Opc.Ua;
using ZB.MOM.WW.OtOpcUa.Client.Shared;
namespace ZB.MOM.WW.OtOpcUa.Client.CLI.Commands;
[Command("enable", Description = "Enable an alarm condition (OPC UA Part 9 ConditionType Enable)")]
public class EnableCommand : CommandBase
{
/// <summary>
/// Creates the enable command used to enable an OPC UA alarm condition from the terminal.
/// </summary>
/// <param name="factory">The factory that creates the shared client service for the command run.</param>
public EnableCommand(IOpcUaClientServiceFactory factory) : base(factory)
{
}
/// <summary>
/// Gets the condition node ID of the alarm to enable.
/// </summary>
[CommandOption("node", 'n', Description = "Condition node ID of the alarm to enable", IsRequired = true)]
public string NodeId { get; init; } = default!;
/// <summary>
/// Connects to the server and enables the specified alarm condition.
/// </summary>
/// <param name="console">The CLI console used for output and cancellation handling.</param>
public override async ValueTask ExecuteAsync(IConsole console)
{
ConfigureLogging();
IOpcUaClientService? service = null;
try
{
var ct = console.RegisterCancellationHandler();
(service, _) = await CreateServiceAndConnectAsync(ct);
var statusCode = await service.EnableAsync(NodeId, ct);
if (StatusCode.IsGood(statusCode))
await console.Output.WriteLineAsync($"Enable successful: {NodeId}");
else
await console.Output.WriteLineAsync($"Enable failed: {statusCode}");
}
finally
{
if (service != null)
{
await service.DisconnectAsync();
service.Dispose();
}
}
}
}
@@ -135,6 +135,32 @@ public interface IOpcUaClientService : IDisposable
/// </returns>
Task<StatusCode> ShelveAlarmAsync(string conditionNodeId, ShelveKind kind, double shelvingTimeSeconds = 0, CancellationToken ct = default);
/// <summary>
/// Enables an alarm condition by invoking the OPC UA Part 9 ConditionType <c>Enable</c> method
/// (the H4 client-driven path). The method takes no input arguments.
/// </summary>
/// <param name="conditionNodeId">The condition node associated with the alarm being enabled.</param>
/// <param name="ct">The cancellation token that aborts the enable request.</param>
/// <returns>
/// <see cref="StatusCodes.Good"/> on success, or the server's bad <see cref="StatusCode"/>
/// (from the underlying <see cref="ServiceResultException"/>) when the enable call
/// returns a bad result. Other transport-level failures still surface as exceptions.
/// </returns>
Task<StatusCode> EnableAsync(string conditionNodeId, CancellationToken ct = default);
/// <summary>
/// Disables an alarm condition by invoking the OPC UA Part 9 ConditionType <c>Disable</c> method
/// (the H4 client-driven path). The method takes no input arguments.
/// </summary>
/// <param name="conditionNodeId">The condition node associated with the alarm being disabled.</param>
/// <param name="ct">The cancellation token that aborts the disable request.</param>
/// <returns>
/// <see cref="StatusCodes.Good"/> on success, or the server's bad <see cref="StatusCode"/>
/// (from the underlying <see cref="ServiceResultException"/>) when the disable call
/// returns a bad result. Other transport-level failures still surface as exceptions.
/// </returns>
Task<StatusCode> DisableAsync(string conditionNodeId, CancellationToken ct = default);
/// <summary>
/// Reads raw historical samples for a historized node.
/// </summary>
@@ -461,6 +461,50 @@ public sealed class OpcUaClientService : IOpcUaClientService
return StatusCodes.Good;
}
/// <inheritdoc />
public Task<StatusCode> EnableAsync(string conditionNodeId, CancellationToken ct = default) =>
CallEnableDisableAsync(conditionNodeId, enabling: true, ct);
/// <inheritdoc />
public Task<StatusCode> DisableAsync(string conditionNodeId, CancellationToken ct = default) =>
CallEnableDisableAsync(conditionNodeId, enabling: false, ct);
/// <summary>
/// Shared body for the H4 Enable/Disable client path. Invokes the OPC UA Part 9 ConditionType
/// <c>Enable</c>/<c>Disable</c> method (no input arguments) on the condition object node, mirroring
/// the <see cref="AcknowledgeAlarmAsync"/> convention: the methods live on the <c>.Condition</c>
/// child node, and a bad call result surfaces as the returned <see cref="StatusCode"/> rather than
/// escaping as an uncaught <see cref="ServiceResultException"/>.
/// </summary>
/// <param name="conditionNodeId">The condition node associated with the alarm.</param>
/// <param name="enabling"><c>true</c> to invoke Enable; <c>false</c> to invoke Disable.</param>
/// <param name="ct">The cancellation token that aborts the request.</param>
private async Task<StatusCode> CallEnableDisableAsync(string conditionNodeId, bool enabling, CancellationToken ct)
{
ThrowIfDisposed();
ThrowIfNotConnected();
// Enable/Disable live on the same .Condition child node as Acknowledge/Confirm.
var conditionObjId = conditionNodeId.EndsWith(".Condition")
? NodeId.Parse(conditionNodeId)
: NodeId.Parse(conditionNodeId + ".Condition");
var methodId = enabling ? MethodIds.ConditionType_Enable : MethodIds.ConditionType_Disable;
try
{
await _session!.CallMethodAsync(conditionObjId, methodId, [], ct);
}
catch (ServiceResultException ex)
{
Logger.Warning(ex, "Failed to {Operation} alarm on {ConditionId} (status {Status})",
enabling ? "enable" : "disable", conditionNodeId, ex.StatusCode);
return ex.StatusCode;
}
Logger.Debug("{Operation} alarm on {ConditionId}", enabling ? "Enabled" : "Disabled", conditionNodeId);
return StatusCodes.Good;
}
/// <inheritdoc />
public async Task<IReadOnlyList<DataValue>> HistoryReadRawAsync(
NodeId nodeId, DateTime startTime, DateTime endTime, int maxValues = 1000, CancellationToken ct = default)