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>
44 lines
1.9 KiB
C#
44 lines
1.9 KiB
C#
using CliFx;
|
|
using CliFx.Attributes;
|
|
using CliFx.Infrastructure;
|
|
|
|
namespace OpcUaCli.Commands;
|
|
|
|
[Command("connect", Description = "Test connection to an OPC UA server")]
|
|
public class ConnectCommand : ICommand
|
|
{
|
|
/// <summary>
|
|
/// Gets the OPC UA endpoint URL to test.
|
|
/// </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>
|
|
/// Connects to the OPC UA endpoint and prints the resolved server metadata.
|
|
/// </summary>
|
|
/// <param name="console">The console used to report connection results.</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();
|
|
await console.Output.WriteLineAsync($"Connected to: {session.Endpoint.EndpointUrl}");
|
|
await console.Output.WriteLineAsync($"Server: {session.Endpoint.Server!.ApplicationName}");
|
|
await console.Output.WriteLineAsync($"Security Mode: {session.Endpoint.SecurityMode}");
|
|
await console.Output.WriteLineAsync($"Security Policy: {session.Endpoint.SecurityPolicyUri}");
|
|
await console.Output.WriteLineAsync("Connection successful.");
|
|
}
|
|
}
|