using System.Globalization; using CliFx.Attributes; using CliFx.Infrastructure; using ZB.MOM.WW.OtOpcUa.Core.Abstractions; using ZB.MOM.WW.OtOpcUa.Driver.Cli.Common; namespace ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Cli.Commands; /// /// Write one value to a FOCAS address. PMC G/R writes are real — be careful /// which file you hit on a running machine. Parameter writes may require the /// CNC to be in MDI mode + the parameter-write switch enabled. /// [Command("write", Description = "Write a single FOCAS address.")] public sealed class WriteCommand : FocasCommandBase { /// Gets the FOCAS address to write to. [CommandOption("address", 'a', Description = "FOCAS address — same format as `read`.", IsRequired = true)] public string Address { get; init; } = default!; /// Gets the data type of the value to write. [CommandOption("type", 't', Description = "Bit / Byte / Int16 / Int32 / Float32 / Float64 / String (default Int16).")] public FocasDataType DataType { get; init; } = FocasDataType.Int16; /// Gets the value to write. [CommandOption("value", 'v', Description = "Value to write. Parsed per --type (booleans accept true/false/1/0).", IsRequired = true)] public string Value { get; init; } = default!; /// public override async ValueTask ExecuteAsync(IConsole console) { ConfigureLogging(); // Driver.FOCAS.Cli-003: validate numeric option ranges before any driver work so // a zero/negative port/timeout surfaces as a clean CommandException rather than an // opaque downstream exception. ValidateOptions(); var ct = console.RegisterCancellationHandler(); var tagName = ReadCommand.SynthesiseTagName(Address, DataType); var tag = new FocasTagDefinition( Name: tagName, DeviceHostAddress: HostAddress, Address: Address, DataType: DataType, Writable: true); var options = BuildOptions([tag]); var parsed = ParseValue(Value, DataType); // Driver.FOCAS.Cli-004: `await using` is the sole disposal mechanism — FocasDriver.DisposeAsync // already invokes ShutdownAsync, so a redundant explicit ShutdownAsync(CancellationToken.None) // in a finally block ran shutdown twice. The await-using on the next line is enough. await using var driver = new FocasDriver(options, DriverInstanceId); await driver.InitializeAsync("{}", ct); var results = await driver.WriteAsync([new WriteRequest(tagName, parsed)], ct); await console.Output.WriteLineAsync(SnapshotFormatter.FormatWrite(Address, results[0])); } /// Parse --value per , invariant culture throughout. /// /// Driver.FOCAS.Cli-001: numeric parses are wrapped so that malformed input /// ( / ) surfaces /// as a clean rather than a raw /// .NET stack trace — matching the friendly message the Bit path already produces. /// /// The raw string value to parse. /// The data type to parse the value as. /// The parsed value as an object. internal static object ParseValue(string raw, FocasDataType type) { if (type == FocasDataType.Bit) return ParseBool(raw); if (type == FocasDataType.String) return raw; try { return type switch { FocasDataType.Byte => (object)sbyte.Parse(raw, CultureInfo.InvariantCulture), FocasDataType.Int16 => (object)short.Parse(raw, CultureInfo.InvariantCulture), FocasDataType.Int32 => (object)int.Parse(raw, CultureInfo.InvariantCulture), FocasDataType.Float32 => (object)float.Parse(raw, CultureInfo.InvariantCulture), FocasDataType.Float64 => (object)double.Parse(raw, CultureInfo.InvariantCulture), _ => throw new CliFx.Exceptions.CommandException($"Unsupported DataType '{type}' for write."), }; } catch (FormatException ex) { throw new CliFx.Exceptions.CommandException( $"Value '{raw}' is not a valid {type}: {ex.Message}"); } catch (OverflowException ex) { throw new CliFx.Exceptions.CommandException( $"Value '{raw}' is out of range for {type}: {ex.Message}"); } } private static bool ParseBool(string raw) => raw.Trim().ToLowerInvariant() switch { "1" or "true" or "on" or "yes" => true, "0" or "false" or "off" or "no" => false, _ => throw new CliFx.Exceptions.CommandException( $"Boolean value '{raw}' is not recognised. Use true/false, 1/0, on/off, or yes/no."), }; }