All commands gain --failover-urls (-F) to specify alternate endpoints. Short-lived commands try each URL in order on initial connect. The subscribe command monitors KeepAlive and automatically reconnects to the next available server, re-creating the subscription on failover. Verified with live service start/stop: primary down triggers failover to secondary, primary restart allows failback. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
72 lines
2.8 KiB
C#
72 lines
2.8 KiB
C#
using CliFx;
|
|
using CliFx.Attributes;
|
|
using CliFx.Infrastructure;
|
|
using Opc.Ua;
|
|
using Opc.Ua.Client;
|
|
|
|
namespace OpcUaCli.Commands;
|
|
|
|
[Command("write", Description = "Write a value to a node")]
|
|
public class WriteCommand : ICommand
|
|
{
|
|
/// <summary>
|
|
/// Gets the OPC UA endpoint URL to connect to before issuing the write.
|
|
/// </summary>
|
|
[CommandOption("url", 'u', Description = "OPC UA server endpoint URL", IsRequired = true)]
|
|
public string Url { get; init; } = default!;
|
|
|
|
[CommandOption("username", 'U', Description = "Username for authentication")]
|
|
public string? Username { get; init; }
|
|
|
|
[CommandOption("password", 'P', Description = "Password for authentication")]
|
|
public string? Password { get; init; }
|
|
|
|
[CommandOption("security", 'S', Description = "Transport security: none, sign, encrypt (default: none)")]
|
|
public string Security { get; init; } = "none";
|
|
|
|
[CommandOption("failover-urls", 'F', Description = "Comma-separated failover endpoint URLs for redundancy")]
|
|
public string? FailoverUrls { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets the node identifier that should receive the write.
|
|
/// </summary>
|
|
[CommandOption("node", 'n', Description = "Node ID (e.g. ns=2;s=MyNode)", IsRequired = true)]
|
|
public string NodeId { get; init; } = default!;
|
|
|
|
/// <summary>
|
|
/// Gets the textual value supplied on the command line before type conversion.
|
|
/// </summary>
|
|
[CommandOption("value", 'v', Description = "Value to write", IsRequired = true)]
|
|
public string Value { get; init; } = default!;
|
|
|
|
/// <summary>
|
|
/// Connects to the OPC UA endpoint, converts the supplied value, and writes it to the target node.
|
|
/// </summary>
|
|
/// <param name="console">The console used to report the write result.</param>
|
|
public async ValueTask ExecuteAsync(IConsole console)
|
|
{
|
|
var urls = FailoverUrlParser.Parse(Url, FailoverUrls);
|
|
using var failover = new OpcUaFailoverHelper(urls, Username, Password, Security);
|
|
using var session = await failover.ConnectAsync();
|
|
|
|
var node = new NodeId(NodeId);
|
|
var current = await session.ReadValueAsync(node);
|
|
var typedValue = OpcUaHelper.ConvertValue(Value, current.Value);
|
|
|
|
var writeValue = new WriteValue
|
|
{
|
|
NodeId = node,
|
|
AttributeId = Attributes.Value,
|
|
Value = new DataValue(new Variant(typedValue))
|
|
};
|
|
|
|
var request = new WriteValueCollection { writeValue };
|
|
var response = await session.WriteAsync(null, request, CancellationToken.None);
|
|
|
|
if (StatusCode.IsGood(response.Results[0]))
|
|
await console.Output.WriteLineAsync($"Write successful: {NodeId} = {typedValue}");
|
|
else
|
|
await console.Output.WriteLineAsync($"Write failed: {response.Results[0]}");
|
|
}
|
|
}
|