using CliFx.Attributes; using CliFx.Infrastructure; using ZB.MOM.WW.OtOpcUa.Driver.Cli.Common; namespace ZB.MOM.WW.OtOpcUa.Driver.S7.Cli.Commands; /// /// Read one S7 address (DB / M / I / Q area). Addresses use S7.Net grammar — the driver /// parses them via S7AddressParser so whatever the server accepts the CLI accepts /// too. /// [Command("read", Description = "Read a single S7 address.")] public sealed class ReadCommand : S7CommandBase { [CommandOption("address", 'a', Description = "S7 address. Examples: DB1.DBW0 (DB1, word 0); M0.0 (merker bit); IW4 (input word 4); " + "QD8 (output dword 8); DB2.DBD20 (DB2, dword 20); DB5.DBX4.3 (DB5, byte 4, bit 3); " + "DB10.STRING[0] (DB10 string). Bit addresses use dot notation.", IsRequired = true)] public string Address { get; init; } = default!; // Driver.S7.Cli-002: help text trimmed to the types the driver actually implements. // Int64 / UInt64 / Float64 / String / DateTime are defined in S7DataType but the driver // raises NotSupportedException (→ BadNotSupported) on reads of those types. [CommandOption("type", 't', Description = "Bool / Byte / Int16 / UInt16 / Int32 / UInt32 / Float32 (default Int16). " + "Int64, UInt64, Float64, String, and DateTime are not yet implemented and will return BadNotSupported.")] public S7DataType DataType { get; init; } = S7DataType.Int16; [CommandOption("string-length", Description = "For type=String: S7-string max length (default 254, S7 max).")] public int StringLength { get; init; } = 254; public override async ValueTask ExecuteAsync(IConsole console) { ConfigureLogging(); var ct = console.RegisterCancellationHandler(); var tagName = SynthesiseTagName(Address, DataType); var tag = new S7TagDefinition( Name: tagName, Address: Address, DataType: DataType, Writable: false, StringLength: StringLength); var options = BuildOptions([tag]); await using var driver = new S7Driver(options, DriverInstanceId); try { await driver.InitializeAsync("{}", ct); var snapshot = await driver.ReadAsync([tagName], ct); await console.Output.WriteLineAsync(SnapshotFormatter.Format(Address, snapshot[0])); } finally { await driver.ShutdownAsync(CancellationToken.None); } } /// Tag-name key used internally. Address + type is already unique. internal static string SynthesiseTagName(string address, S7DataType type) => $"{address}:{type}"; }