Files
lmxopcua/src/ZB.MOM.WW.OtOpcUa.Driver.S7.Cli/S7CommandBase.cs
2026-04-26 00:49:10 -04:00

82 lines
3.3 KiB
C#

using CliFx.Attributes;
using ZB.MOM.WW.OtOpcUa.Driver.Cli.Common;
using S7NetCpuType = global::S7.Net.CpuType;
namespace ZB.MOM.WW.OtOpcUa.Driver.S7.Cli;
/// <summary>
/// Base for every S7 CLI command. Carries the ISO-on-TCP endpoint options
/// (host / port / CPU type / rack / slot) that S7.Net needs for its handshake +
/// exposes <see cref="BuildOptions"/> so each command can synthesise an
/// <see cref="S7DriverOptions"/> on demand.
/// </summary>
public abstract class S7CommandBase : DriverCommandBase
{
[CommandOption("host", 'h', Description = "PLC IP address or hostname.", IsRequired = true)]
public string Host { get; init; } = default!;
[CommandOption("port", 'p', Description = "ISO-on-TCP port (default 102).")]
public int Port { get; init; } = 102;
[CommandOption("cpu", 'c', Description =
"S7 CPU family: S7200 / S7200Smart / S7300 / S7400 / S71200 / S71500 " +
"(default S71500). Determines the ISO-TSAP slot byte.")]
public S7NetCpuType CpuType { get; init; } = S7NetCpuType.S71500;
[CommandOption("rack", Description = "Rack number (default 0 — single-rack).")]
public short Rack { get; init; } = 0;
[CommandOption("slot", Description =
"CPU slot. S7-300 = 2, S7-400 = 2 or 3, S7-1200 / S7-1500 = 0 (default 0).")]
public short Slot { get; init; } = 0;
[CommandOption("timeout-ms", Description = "Per-operation timeout in ms (default 5000).")]
public int TimeoutMs { get; init; } = 5000;
[CommandOption("tsap-mode", Description =
"ISO-on-TCP / S7comm connection class: Auto (default — S7netplus picks the TSAP " +
"from --cpu), Pg, Op, S7Basic, or Other (requires --local-tsap and --remote-tsap). " +
"Hardened S7-1500 / ET 200SP CPUs may refuse PG and require Op or S7Basic. See " +
"docs/v2/s7.md \"TSAP / Connection Type\" for the byte table and motivation.")]
public TsapMode TsapMode { get; init; } = TsapMode.Auto;
[CommandOption("local-tsap", Description =
"Optional 16-bit local TSAP override (e.g. 0x4D57). Required when --tsap-mode is Other; " +
"wins over the class default under Pg/Op/S7Basic.")]
public ushort? LocalTsap { get; init; }
[CommandOption("remote-tsap", Description =
"Optional 16-bit remote TSAP override. Required when --tsap-mode is Other; wins over " +
"the class default under Pg/Op/S7Basic.")]
public ushort? RemoteTsap { get; init; }
/// <inheritdoc />
public override TimeSpan Timeout
{
get => TimeSpan.FromMilliseconds(TimeoutMs);
init { /* driven by TimeoutMs */ }
}
/// <summary>
/// Build an <see cref="S7DriverOptions"/> with the endpoint fields this base
/// collected + whatever <paramref name="tags"/> the subclass declares. Probe
/// disabled — CLI runs are one-shot.
/// </summary>
protected S7DriverOptions BuildOptions(IReadOnlyList<S7TagDefinition> tags) => new()
{
Host = Host,
Port = Port,
CpuType = CpuType,
Rack = Rack,
Slot = Slot,
Timeout = Timeout,
Tags = tags,
Probe = new S7ProbeOptions { Enabled = false },
TsapMode = TsapMode,
LocalTsap = LocalTsap,
RemoteTsap = RemoteTsap,
};
protected string DriverInstanceId => $"s7-cli-{Host}:{Port}";
}