a25593a9c6
Group all 69 projects into category subfolders under src/ and tests/ so the Rider Solution Explorer mirrors the module structure. Folders: Core, Server, Drivers (with a nested Driver CLIs subfolder), Client, Tooling. - Move every project folder on disk with git mv (history preserved as renames). - Recompute relative paths in 57 .csproj files: cross-category ProjectReferences, the lib/ HintPath+None refs in Driver.Historian.Wonderware, and the external mxaccessgw refs in Driver.Galaxy and its test project. - Rebuild ZB.MOM.WW.OtOpcUa.slnx with nested solution folders. - Re-prefix project paths in functional scripts (e2e, compliance, smoke SQL, integration, install). Build green (0 errors); unit tests pass. Docs left for a separate pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
125 lines
4.6 KiB
C#
125 lines
4.6 KiB
C#
using CliFx;
|
|
using CliFx.Attributes;
|
|
using CliFx.Infrastructure;
|
|
using Serilog;
|
|
using ZB.MOM.WW.OtOpcUa.Client.Shared;
|
|
using ZB.MOM.WW.OtOpcUa.Client.Shared.Helpers;
|
|
using ZB.MOM.WW.OtOpcUa.Client.Shared.Models;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Client.CLI;
|
|
|
|
/// <summary>
|
|
/// Abstract base class for all CLI commands providing common connection options and helpers.
|
|
/// </summary>
|
|
public abstract class CommandBase : ICommand
|
|
{
|
|
internal static readonly IOpcUaClientServiceFactory DefaultFactory = new OpcUaClientServiceFactory();
|
|
|
|
private readonly IOpcUaClientServiceFactory _factory;
|
|
|
|
/// <summary>
|
|
/// Initializes a CLI command with the shared OPC UA client factory used to create per-command sessions.
|
|
/// </summary>
|
|
/// <param name="factory">The factory that creates the shared client service for the command execution.</param>
|
|
protected CommandBase(IOpcUaClientServiceFactory factory)
|
|
{
|
|
_factory = factory;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the primary OPC UA endpoint URL the command should connect to.
|
|
/// </summary>
|
|
[CommandOption("url", 'u', Description = "OPC UA server endpoint URL", IsRequired = true)]
|
|
public string Url { get; init; } = default!;
|
|
|
|
/// <summary>
|
|
/// Gets the optional username used for authenticated OPC UA sessions.
|
|
/// </summary>
|
|
[CommandOption("username", 'U', Description = "Username for authentication")]
|
|
public string? Username { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets the optional password paired with <see cref="Username" /> for authenticated OPC UA sessions.
|
|
/// </summary>
|
|
[CommandOption("password", 'P', Description = "Password for authentication")]
|
|
public string? Password { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets the transport security mode requested by the operator for this CLI session.
|
|
/// </summary>
|
|
[CommandOption("security", 'S',
|
|
Description = "Transport security: none, sign, encrypt, signandencrypt (default: none)")]
|
|
public string Security { get; init; } = "none";
|
|
|
|
/// <summary>
|
|
/// Gets the optional comma-separated failover endpoints that should be tried if the primary endpoint is unavailable.
|
|
/// </summary>
|
|
[CommandOption("failover-urls", 'F', Description = "Comma-separated failover endpoint URLs for redundancy")]
|
|
public string? FailoverUrls { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets a value indicating whether verbose Serilog output should be enabled for troubleshooting.
|
|
/// </summary>
|
|
[CommandOption("verbose", Description = "Enable verbose/debug logging")]
|
|
public bool Verbose { get; init; }
|
|
|
|
/// <summary>
|
|
/// Executes the command-specific workflow against the configured OPC UA endpoint.
|
|
/// </summary>
|
|
/// <param name="console">The CLI console used for output and cancellation handling.</param>
|
|
public abstract ValueTask ExecuteAsync(IConsole console);
|
|
|
|
/// <summary>
|
|
/// Creates a <see cref="ConnectionSettings" /> from the common command options.
|
|
/// </summary>
|
|
protected ConnectionSettings CreateConnectionSettings()
|
|
{
|
|
var securityMode = SecurityModeMapper.FromString(Security);
|
|
var failoverUrls = !string.IsNullOrWhiteSpace(FailoverUrls)
|
|
? FailoverUrlParser.Parse(Url, FailoverUrls)
|
|
: null;
|
|
|
|
var settings = new ConnectionSettings
|
|
{
|
|
EndpointUrl = Url,
|
|
FailoverUrls = failoverUrls,
|
|
Username = Username,
|
|
Password = Password,
|
|
SecurityMode = securityMode,
|
|
AutoAcceptCertificates = true
|
|
};
|
|
|
|
return settings;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new <see cref="IOpcUaClientService" />, connects it using the common options,
|
|
/// and returns both the service and the connection info.
|
|
/// </summary>
|
|
/// <param name="ct">The cancellation token that aborts connection setup for the command.</param>
|
|
protected async Task<(IOpcUaClientService Service, ConnectionInfo Info)> CreateServiceAndConnectAsync(
|
|
CancellationToken ct)
|
|
{
|
|
var service = _factory.Create();
|
|
var settings = CreateConnectionSettings();
|
|
var info = await service.ConnectAsync(settings, ct);
|
|
return (service, info);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configures Serilog based on the verbose flag.
|
|
/// </summary>
|
|
protected void ConfigureLogging()
|
|
{
|
|
var config = new LoggerConfiguration();
|
|
if (Verbose)
|
|
config.MinimumLevel.Debug()
|
|
.WriteTo.Console();
|
|
else
|
|
config.MinimumLevel.Warning()
|
|
.WriteTo.Console();
|
|
|
|
Log.Logger = config.CreateLogger();
|
|
}
|
|
}
|