Files
UA-.NETStandard/samples/Machinery/MachineryClient/Program.cs
Klaus Landsdorf 01d6b5d26c Machinery: the OPC 40001 companion SDK with Jobs, Result Transfer, Process Values and Energy (#4578)
> Tracking issue #4576. #4577 is merged, so this branch is rebased onto
> `master` and contains only the Machinery commits.

## Summary

Second of four stacked PRs. Adds the OPC UA for Machinery series as
model
packages plus a server and a client SDK, following the companion pattern
of
DI and Robotics, and the two models it depends on that the repository
does
not carry yet.

| Package | Content |
|---|---|
| `Opc.Ua.IA` | OPC 10000-200 IA 1.01.4, **moved out of
`Opc.Ua.Robotics`** (see below) |
| `Opc.Ua.ECM` | OPC 34100 Energy Consumption Management 1.0.1 |
| `Opc.Ua.PADIM` | OPC 30081 PA-DIM 1.02.0 with the IRDI dictionary |
| `Opc.Ua.Machinery` | OPC 40001-1 Basic Building Blocks 1.04.1 |
| `Opc.Ua.Machinery.ProcessValues` | OPC 40001-2 Process Values 1.00.0 |
| `Opc.Ua.Machinery.Jobs` | OPC 40001-3 Job Management 1.0.1 |
| `Opc.Ua.Machinery.Energy` | OPC 40001-4 Energy 1.00 |
| `Opc.Ua.Machinery.Result` | OPC 40001-101 Result Transfer 1.01.0 |
| `Opc.Ua.Machinery.Server` | `AddMachinery()` /
`ConfigureMachinery(...)`, fluent machine builder, server-driven state
machines, process values, job management over the shared ISA-95 V2
binder, energy, result management incl. `GenerateFileForRead` |
| `Opc.Ua.Machinery.Client` | machine discovery, identification, state
observation, process values, job and result access |

OPC 40001-101 `ResultTransferType` redeclares `GenerateFileForRead` with
its
own arguments; the generator fix for such overrides, merged with #4577,
is
what makes it appear on the `ResultTransfer` instances.

All NodeSets are byte-identical to `UA-Nodeset/latest`. Each model
carries a
pinned `Model/*.NodeIds.csv` the generator validates, next to the
unmodified
upstream table; `tools/nodesets/derive-identifier-table.py` derives the
former from the latter.

## Changes to existing code

- **IA moves out of Robotics.** Machinery needs IA as well (OPC 40001-1
types
`Stacklight` with `BasicStacklightType`), and two assemblies generating
the
same model under the same prefix collide at any consumer referencing
both.
The NodeSet moves verbatim, so no generated type changes; the prefix
stays
`Opc.Ua.IA`. Source-compatible, but IA types now live in `Opc.Ua.IA.dll`
rather than `Opc.Ua.Robotics.dll` — binary-breaking for consumers of the
  Robotics package.
- **`samples/OpenUsd/GeneratorServer`** drops its hand-reduced Machinery
  NodeSet copy and the script that produced it (a workaround for the
generator defect fixed by #4577) and references `Opc.Ua.Machinery`
instead.
- `README.md` and `docs/README.md` list the new models; the README also
  corrects the Industrial Automation entry (Part 200, not OPC 40001-1).

## Review follow-ups

Separate commits after the docs/samples commit, so the reviewed state
stays
comparable. Each fix comes with a test that fails without it.

- Copilot review: a transition is committed only while the state machine
is
still in its source state; optional Jobs/Result namespaces the server
does
not load are reported as absent, as documented; resources of registered
machines (result transfer, job-management change pumps) are released
with
  the node manager (`DisposeMachineryResourcesAsync` for a custom DI
  manager); `NodeId`/`LocalizedText` use their null sentinels instead of
  `Nullable<T>`.
- Own review with the rubric in `.github/copilot-instructions.md`: a
failed
browse call propagates from the client instead of reading as "no
children";
  the predefined result metadata compares `CreationTime` against the
  `DateTimeUtc` sentinels (the `DateTime` conversion shifted
  `DateTime.MaxValue` by the host's UTC offset); process values below
`Monitoring` are found; a second metering point named `Main` is
rejected;
  the zero-point event is reported when the handler throws; smaller
  state-sharing and documentation corrections.

## Testing

- `tests/Opc.Ua.Machinery.Tests`: 162 tests on net8 and net10 and with
the
netstandard2.1 libraries; the integration tests connect a real client
session over `opc.tcp` to a hosted
  server.
- With IA extracted: Robotics 636, OpenUsd 921, DI 433 and
SourceGeneration
  263 tests pass.
- `UA.slnx` builds for net472, net48, netstandard2.0/2.1 and net8/9/10
with
  SDK 10.0.401 and no warnings.
- Changed-line coverage of this layer (`check-coverage.ps1`): 91.68 %
(6059 of 6609 changed lines, against `master`).
- Fork CI, full PR matrix incl. macOS, on the current master: green, all
184
jobs ([run
36582685653](https://github.com/biancode/UA-.NETStandard/actions/runs/36582685653)).
CodeQL: green ([run
36582689589](https://github.com/biancode/UA-.NETStandard/actions/runs/36582689589)).
- Beyond the in-repo tests the implementation has been validated end to
end
  against a separate, non-public OPC UA client and server.

## Reviewing

Commits are ordered so each builds: IA extraction → identifier-table
tool →
ECM/PA-DIM → Machinery models → server → client → tests → docs/samples →
GeneratorServer sample → review follow-ups. The model commits are large
only because of the
NodeSets and identifier tables; the hand-written code starts at the
server
commit.

---------

Co-authored-by: Marc Schier <marcschier@users.noreply.github.com>
2026-09-29 19:59:34 +02:00

360 lines
12 KiB
C#

/* ========================================================================
* Copyright (c) 2005-2026 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MachinerySample.Client;
using Microsoft.Extensions.Logging;
using Opc.Ua;
using Opc.Ua.Client.StateMachines;
using Opc.Ua.Di;
using Opc.Ua.Machinery.Client;
using Opc.Ua.Machinery.Result;
MachineryClientOptions options = MachineryClientOptions.Parse(args);
ITelemetryContext telemetry = DefaultTelemetry.Create(
logging => logging.SetMinimumLevel(LogLevel.Warning));
using var cancellation = new CancellationTokenSource();
Console.CancelKeyPress += (_, eventArgs) =>
{
eventArgs.Cancel = true;
cancellation.Cancel();
};
SampleSession connection = await SampleSession
.ConnectAsync(options, telemetry, cancellation.Token)
.ConfigureAwait(false);
await using ConfiguredAsyncDisposable _ = connection.ConfigureAwait(false);
MachineryClient machinery = connection.Session.Machinery(telemetry);
if (machinery.MachinesFolderId.IsNull)
{
Console.Error.WriteLine(
"The server does not publish the OPC 40001-1 Machinery namespace.");
return 1;
}
NodeId firstMachine = NodeId.Null;
await foreach (MachineEntry machine in machinery
.EnumerateMachinesAsync(cancellation.Token)
.ConfigureAwait(false))
{
if (firstMachine.IsNull)
{
firstMachine = machine.NodeId;
}
Console.WriteLine($"Machine {machine.BrowseName} ({machine.NodeId})");
MachineIdentification? identification = await machinery
.ReadIdentificationAsync(machine.NodeId, cancellation.Token)
.ConfigureAwait(false);
if (identification != null)
{
Console.WriteLine(
$" {identification.Manufacturer.Text} {identification.Model.Text} " +
$"— serial {identification.SerialNumber}");
Console.WriteLine($" location: {identification.Location ?? "(not published)"}");
}
FiniteStateSnapshot? state = await machinery
.GetItemStateAsync(machine.NodeId, cancellation.Token)
.ConfigureAwait(false);
FiniteStateSnapshot? mode = await machinery
.GetOperationModeAsync(machine.NodeId, cancellation.Token)
.ConfigureAwait(false);
Console.WriteLine(
$" state: {state?.CurrentState.Text ?? "(not published)"} / " +
$"mode: {mode?.CurrentState.Text ?? "(not published)"}");
await foreach (MachineEntry component in machinery
.EnumerateComponentsAsync(machine.NodeId, cancellation.Token)
.ConfigureAwait(false))
{
Console.WriteLine($" component {component.BrowseName}");
}
// OPC 40001-1 §7.1: every building block a machinery item supports is
// reachable through this one folder, so a client can discover them
// instead of probing for each in turn.
await foreach (MachineEntry block in machinery
.EnumerateBuildingBlocksAsync(machine.NodeId, cancellation.Token)
.ConfigureAwait(false))
{
Console.WriteLine($" building block {block.BrowseName}");
}
DeviceHealthEnumeration? health = await machinery
.ReadDeviceHealthAsync(machine.NodeId, cancellation.Token)
.ConfigureAwait(false);
if (health.HasValue)
{
Console.WriteLine($" device health: {health.Value}");
}
await PrintCountersAsync(machinery, machine.NodeId, cancellation.Token)
.ConfigureAwait(false);
await PrintEquipmentAsync(machinery, machine.NodeId, cancellation.Token)
.ConfigureAwait(false);
await PrintProcessValuesAsync(machinery, machine.NodeId, cancellation.Token)
.ConfigureAwait(false);
await PrintEnergyAsync(machinery, machine.NodeId, cancellation.Token)
.ConfigureAwait(false);
}
if (firstMachine.IsNull)
{
Console.Error.WriteLine("The server publishes no machines.");
return 1;
}
if (options.Download)
{
await DownloadLatestResultAsync(machinery, firstMachine, cancellation.Token)
.ConfigureAwait(false);
}
if (options.ObserveSeconds > 0)
{
Console.WriteLine(
$"Observing MachineryItemState for {options.ObserveSeconds}s " +
"(OPC 40001-1 state changes always come from the server) …");
using var observation = CancellationTokenSource.CreateLinkedTokenSource(
cancellation.Token);
observation.CancelAfter(TimeSpan.FromSeconds(options.ObserveSeconds));
try
{
await foreach (FiniteStateSnapshot snapshot in machinery
.ObserveItemStateAsync(
firstMachine,
connection.Streaming,
cancellationToken: observation.Token)
.ConfigureAwait(false))
{
Console.WriteLine(
$" {snapshot.Timestamp:HH:mm:ss} -> {snapshot.CurrentState} " +
$"(via {snapshot.LastTransition})");
}
}
catch (OperationCanceledException)
{
// The observation window elapsed or the user pressed Ctrl+C.
}
}
return 0;
static string Invariant(double? value, string format = "F1")
{
return value.HasValue
? value.Value.ToString(format, CultureInfo.InvariantCulture)
: "(not published)";
}
static string InvariantValue(Variant value)
{
if (value.TryGetValue(out double number))
{
return number.ToString("G6", CultureInfo.InvariantCulture);
}
if (value.TryGetValue(out float single))
{
return single.ToString("G6", CultureInfo.InvariantCulture);
}
return FormattableString.Invariant($"{value}");
}
static async Task PrintCountersAsync(
MachineryClient machinery,
NodeId machine,
CancellationToken cancellationToken)
{
MachineryOperationCounters? counters = await machinery
.ReadOperationCountersAsync(machine, cancellationToken)
.ConfigureAwait(false);
if (counters != null)
{
string powerOn = Invariant(counters.PowerOnDuration);
string operating = Invariant(counters.OperationDuration);
Console.WriteLine(
$" operation counters: power-on {powerOn} h, operating {operating} h");
}
await foreach (MachineryLifetimeVariable life in machinery
.ReadLifetimeCountersAsync(machine, cancellationToken)
.ConfigureAwait(false))
{
Console.WriteLine(
$" lifetime {life.BrowseName.Name}: {Invariant(life.Remaining, "F0")} of " +
$"{Invariant(life.StartValue, "F0")} left");
}
}
static async Task PrintEquipmentAsync(
MachineryClient machinery,
NodeId machine,
CancellationToken cancellationToken)
{
await foreach (MachineryEquipmentItem equipment in machinery
.EnumerateEquipmentAsync(machine, cancellationToken)
.ConfigureAwait(false))
{
Console.WriteLine(
$" equipment {equipment.BrowseName.Name} " +
$"({equipment.MachineryEquipmentTypeId})");
if (equipment.EquipmentLife?.Remaining is double remaining)
{
Console.WriteLine(
$" life: {Invariant(remaining, "F0")} of " +
$"{Invariant(equipment.EquipmentLife.StartValue, "F0")} left");
}
}
}
static async Task PrintProcessValuesAsync(
MachineryClient machinery,
NodeId machine,
CancellationToken cancellationToken)
{
await foreach (MachineEntry entry in machinery
.EnumerateProcessValuesAsync(machine, cancellationToken)
.ConfigureAwait(false))
{
MachineryProcessValue? processValue = await machinery
.ReadProcessValueAsync(entry.NodeId, cancellationToken)
.ConfigureAwait(false);
if (processValue == null)
{
continue;
}
string unit = processValue.EngineeringUnits?.DisplayName.Text ?? string.Empty;
Console.WriteLine(
$" process value {entry.BrowseName.Name}: " +
$"{Invariant(processValue.Value)} {unit} " +
$"(setpoint {Invariant(processValue.Setpoint)}, " +
$"{Invariant(processValue.PercentageValue)} % of range)");
Console.WriteLine(
$" limits: {Invariant(processValue.LowLowLimit)} / " +
$"{Invariant(processValue.LowLimit)} / " +
$"{Invariant(processValue.HighLimit)} / " +
$"{Invariant(processValue.HighHighLimit)}");
}
}
static async Task PrintEnergyAsync(
MachineryClient machinery,
NodeId machine,
CancellationToken cancellationToken)
{
await foreach (MachineEntry resource in machinery
.EnumerateEnergyResourcesAsync(machine, cancellationToken)
.ConfigureAwait(false))
{
MachineryMeteringPoint? main = await machinery
.ReadMainMeteringPointAsync(resource.NodeId, cancellationToken)
.ConfigureAwait(false);
Console.WriteLine(
$" energy {resource.BrowseName.Name}: " +
$"Main = {main?.ApplicationTag ?? "(missing)"}");
if (main == null)
{
continue;
}
foreach (MachineryMeasurementValue measurement in main.Measurements)
{
// Variant.ToString() formats with the current culture, which would
// print a German decimal comma here. A measurement is data, so it
// is formatted invariantly like every other number the sample
// prints.
Console.WriteLine(
$" {measurement.BrowseName.Name} = " +
$"{InvariantValue(measurement.Value)}");
}
}
}
static async Task DownloadLatestResultAsync(
MachineryClient machinery,
NodeId machine,
CancellationToken cancellationToken)
{
ResultManagementTypeClient? results = await machinery
.ResultManagementAsync(machine, cancellationToken)
.ConfigureAwait(false);
if (results == null)
{
Console.WriteLine("The machine publishes no OPC 40001-101 ResultManagement.");
return;
}
(uint resultHandle, ResultDataType result, int error) latest = await results
.GetLatestResultAsync(timeout: 5000, ct: cancellationToken)
.ConfigureAwait(false);
if (latest.error != 0)
{
Console.WriteLine("No result has been produced yet.");
return;
}
string? resultId = latest.result?.ResultMetaData?.ResultId;
if (string.IsNullOrEmpty(resultId))
{
Console.WriteLine("No result has been produced yet.");
return;
}
Console.WriteLine($"Latest result: {resultId}");
try
{
ByteString payload = await machinery
.DownloadResultAsync(machine, resultId!, cancellationToken)
.ConfigureAwait(false);
Console.WriteLine(
FormattableString.Invariant($" downloaded {payload.Span.Length} bytes:"));
foreach (string line in Encoding.UTF8
.GetString(payload.Span.ToArray())
.TrimEnd()
.Split('\n'))
{
Console.WriteLine($" {line}");
}
}
finally
{
await results
.ReleaseResultHandleAsync(latest.resultHandle, ct: cancellationToken)
.ConfigureAwait(false);
}
}