Files
lmxopcua/src/Drivers/Cli/ZB.MOM.WW.OtOpcUa.Driver.AbCip.Cli/Commands/ProbeCommand.cs
T
Joseph Doherty f9fbd4ee0d fix(cli): AbCip subscribe serialises and guards console writes; Ctrl-C exits cleanly; arrays render
R07 U7: Driver.Cli.Common gains RunCancellableAsync (moved up from AbLegacy) and
CreateChangeLineWriter (one lock-serialised line per change; a failing console
write is logged and swallowed so it cannot fault the poll loop). AbCip
probe/read/write print Cancelled. on Ctrl-C instead of a stack trace, and AbCip
subscribe uses the shared writer. SnapshotFormatter renders arrays/lists as
[a, b, c] instead of System.Int32[].

Refs #747
Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
2026-10-22 16:34:48 -04:00

72 lines
3.1 KiB
C#

using CliFx.Attributes;
using CliFx.Infrastructure;
using ZB.MOM.WW.OtOpcUa.Driver.Cli.Common;
namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip.Cli.Commands;
/// <summary>
/// Probes an AB CIP gateway: initialises the driver (connects via libplctag), reads a
/// single tag, and prints health + the read result. Fastest way to answer "is the PLC
/// up + reachable + speaking CIP via this path?".
/// </summary>
[Command("probe", Description = "Verify the AB CIP gateway is reachable and a sample tag reads.")]
public sealed class ProbeCommand : AbCipCommandBase
{
/// <summary>Gets the tag path to probe.</summary>
[CommandOption("tag", 't', Description =
"Tag path to probe — a real tag on the controller. Any name works to prove reachability: " +
"libplctag knows only @raw, @tags and @udt/ as special names, so anything else (including " +
"'@raw_cpu_type', which no controller defines) is sent as an ordinary symbolic name and the " +
"controller's 'tag does not exist' answer already proves the CIP session is live.",
IsRequired = true)]
public string TagPath { get; init; } = default!;
/// <summary>Gets the data type of the probe tag.</summary>
[CommandOption("type", Description =
"Logix atomic type of the probe tag (default DInt).")]
public AbCipDataType DataType { get; init; } = AbCipDataType.DInt;
/// <inheritdoc />
public override async ValueTask ExecuteAsync(IConsole console)
{
ConfigureLogging();
RejectStructure(DataType);
var ct = console.RegisterCancellationHandler();
var probeTag = new AbCipTagDefinition(
Name: "__probe",
DeviceHostAddress: Gateway,
TagPath: TagPath,
DataType: DataType,
Writable: false);
var options = BuildOptions([probeTag]);
await using var driver = new AbCipDriver(options, DriverInstanceId);
try
{
// Ctrl-C prints "Cancelled." instead of an OperationCanceledException stack trace (review 07 U7).
await RunCancellableAsync(console, ct, async () =>
{
await driver.InitializeAsync("{}", ct);
var snapshot = await driver.ReadAsync(["__probe"], ct);
var health = driver.GetHealth();
await console.Output.WriteLineAsync($"Gateway: {Gateway}");
await console.Output.WriteLineAsync($"Family: {Family}");
await console.Output.WriteLineAsync($"Health: {health.State}");
if (health.LastError is { } err)
await console.Output.WriteLineAsync($"Last error: {err}");
await console.Output.WriteLineAsync();
await console.Output.WriteLineAsync(SnapshotFormatter.Format(TagPath, snapshot[0]));
});
}
finally
{
await driver.ShutdownAsync(CancellationToken.None);
// Flush Serilog before process exit so buffered log output emitted during
// driver shutdown is not lost.
FlushLogging();
}
}
}