Files
Marc Schier 0374e19121 Add MachineTool, WMTP, GMS and CranesHoists companions (#4765)
# Description

_Describe the changes here to communicate to the maintainers why they
should accept this pull request. By default - this will become the
Commit message after merging and thus define history._

Add provider-backed MachineTool, bundled WMTP, GMS and CranesHoists
companions using the existing Machinery/MachineVision fluent, DI and
typed-client approach. **The PR is open/non-draft by user choice.
Current head `ecc4a7d6ca3e8004cf28ab54da605da302d026bf` is mergeable and
has 108 successful checks, six intentionally skipped checks, zero
failures and zero pending checks. All five primary workflows completed
successfully at that exact head.**

| Family | Released specification/model | Packaging |
| --- | --- | --- |
| MachineTool | OPC 40501-1, 1.02.0 | Model, Server, Client and MCP;
normal root versions |
| WMTP | OPC 40505, 1.0.0 | Bundled in MachineTool; distinct CLR/wire
namespace and independent WMTP-only hosting |
| GMS | OPC 40210 reference 1.0.1, normative model 1.0.0 | Model,
Server, Client and MCP; normal root versions |
| CranesHoists | OPC 40020-1, 1.00 | Model, Server, Client and MCP; all
four plus Debug variants forced preview |

There are **12 new packable projects**. No separate WMTP
package/profile/guide, root version bump, tag, release or package
publication is included. Mandatory instance structure is the default;
selected optional features require explicit providers. The
implementation reuses Machinery results/jobs/files/ProcessValues and
Robotics topology, including retained MachineTool Production, defined
GMS jobs/parts, modern ISA95/Machinery jobs, shared-owner hosting and
isolated instances alongside MachineVision.

Runtime and integration fixes preserve raw quality/timestamps, coherent
acquisition, namespace ownership, accepted-configuration state,
borrowed-resource lifetime, session/node/file cleanup, terminal-value
publication, event propagation and endpoint-aware capability
publication. Firmware/licence transfer means provider acceptance, not
installation/activation. Cancellation does not silently abort another
caller's operation. The later upstream MCP expansion is integrated
without losing either family set or its default-off privileged/live
execution policy.

### Conformance and scope

Canonical model inputs are pinned to UA-Nodeset revision
`96a08945425d0295edb3607d2a38f6841ef44046`.

**GMS upstream exception:** [Annex
A.1](https://reference.opcfoundation.org/specs/OPC-40210/annex-a) makes
the released NodeSet normative, but §12.3.2.7 requires undefined
`GMS_ProductionStatisticsType`. Inherited statistics and defined
jobs/parts are implemented without inventing a standard type/NodeId; the
complete GMS Production facet is withheld.

The requirement ledger records **102 positively evidenced CU rows, 96
directly negative-tested rows and six explicit negative-evidence
limitations**. All 648 test-class/name/TRX links were independently
resolved to passing cases. The six limitations concern Stacklight,
retained legacy PowerOnDuration, Machinery PowerOnDuration, WorkingUnit,
Obligation and MaintenanceMode. They are not claims that every
invalid-input or hardware-control behavior was tested. The undefined GMS
requirement is outside the 102 defined CUs. No external certification
claim is made.

Retained baseline complete-assembly line coverage, with generated code
retained and no source exclusions:

| Family | Model | Server | Client |
| --- | ---: | ---: | ---: |
| MachineTool/WMTP | 83.80% | 91.98% | 84.74% |
| GMS | 95.51% | 90.63% | 88.38% |
| CranesHoists | 92.76% | 94.45% | 92.24% |

These nine measurements predate the later focused review fixes and
merges; they are not relabelled as new `ecc4a7d6c` percentages. The
current head's separate CI code-coverage check succeeded. No new
exclusions or threshold relaxations were introduced.

## Related Issues

- Fixes
[#4764](https://github.com/OPCFoundation/UA-.NETStandard/issues/4764),
the implementation/design and acceptance tracker.

## Checklist

_Put an `x` in the boxes that apply. You can complete these step by step
after opening the PR._

- [ ] I have signed the
[CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf)
and read the
[CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md)
doc.
- [x] I have added tests that prove my fix is effective or that my
feature works and increased code coverage.
- [x] I have added all necessary documentation.
- [ ] I have verified that my changes do not introduce (new) build or
analyzer warnings.
- [ ] I ran **all** tests locally using the **UA.slnx** solution against
at least .net **framework** and .net **10**, and all passed.
- [ ] I fixed **all** failing and flaky tests in the CI pipelines and
**all** CodeQL warnings.
- [x] I have addressed **all** PR feedback received.
2026-10-15 15:15:46 +02:00

348 lines
18 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.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Opc.Ua;
using Opc.Ua.Client.FileSystem;
using Opc.Ua.MachineTool;
using Opc.Ua.MachineTool.Client;
using Opc.Ua.MachineTool.WMTP;
using TypedClient = Opc.Ua.MachineTool.Client.MachineToolClient;
using V2 = Opc.Ua.ISA95.JobControl.V2;
namespace MachineToolSample.Client
{
/// <summary>
/// Demonstrates generated proxies and bounded typed workflows without reimplementing OPC UA services.
/// </summary>
internal static class DemoReader
{
/// <summary>
/// Discovers actual identities, reads selected instances, then performs at most one bounded observation.
/// </summary>
internal static async Task RunAsync(TypedClient client, ClientOptions options, CancellationToken ct)
{
ArrayOf<MachineToolDeviceEntry> devices = await client.DiscoverAsync(options.MaximumDevices, ct)
.ConfigureAwait(false);
Console.WriteLine($"DISCOVERED count={devices.Count} models={client.AvailableParts}");
var selected = new List<MachineToolDeviceEntry>();
for (int index = 0; index < devices.Count; index++)
{
MachineToolDeviceEntry entry = devices[index];
Console.WriteLine($"DEVICE name={entry.BrowseName} id={entry.NodeId} kind={entry.Kind}");
if (options.Target == null || entry.BrowseName.Name == options.Target)
{
selected.Add(entry);
}
}
if (options.Target != null && selected.Count != 1)
{
throw new ArgumentException("The explicit target must identify exactly one discovered instance.");
}
var variables = new List<NodeId>();
foreach (MachineToolDeviceEntry entry in selected)
{
if (entry.Kind == MachineToolDeviceKind.MachineTool)
{
await ReadMachineAsync(client, entry, options, ct).ConfigureAwait(false);
}
else
{
await ReadPeripheralAsync(client, entry, options, variables, ct).ConfigureAwait(false);
}
}
if (options.Seconds > 0 && selected.Count > 0)
{
var settings = new MachineToolObservationOptions
{
Window = TimeSpan.FromSeconds(options.Seconds),
MaximumNotifications = options.Count
};
MachineToolDeviceEntry? machine = selected.Find(
entry => entry.Kind == MachineToolDeviceKind.MachineTool);
MachineToolDataObservation? observation = machine != null
? await client.ObserveDataAsync(machine.NodeId, settings, ct).ConfigureAwait(false)
: variables.Count != 0
? await client.ObserveValuesAsync(variables.ToArrayOf(), settings, ct).ConfigureAwait(false)
: null;
if (observation != null)
{
Console.WriteLine($"OBSERVED count={observation.Changes.Count} limit={observation.LimitReached} " +
$"dropped={observation.DroppedNotifications}");
for (int index = 0; index < observation.Changes.Count; index++)
{
Print(client, observation.Changes[index].NodeId.ToString(), observation.Changes[index].Value);
}
}
else
{
Console.WriteLine(
"OBSERVATION_ABSENT: selected peripheral has no configured measurement variables.");
}
}
}
private static async Task ReadMachineAsync(
TypedClient client, MachineToolDeviceEntry entry, ClientOptions options, CancellationToken ct)
{
if (options.Command is not ("none" or "job-store-start"))
{
throw new ArgumentException("This command requires a wireless peripheral, not a machine.");
}
MachineToolReadSnapshot snapshot = await client.ReadSnapshotAsync(entry.NodeId, ct).ConfigureAwait(false);
Print(client, "OperationMode", snapshot.OperationMode);
Print(client, "ActiveProgram", snapshot.ActiveProgramName);
MachineToolCapabilityReadSnapshot capabilities = await client.GetCapabilitiesAsync(entry.NodeId, ct)
.ConfigureAwait(false);
Console.WriteLine($"CAPABILITIES declared={capabilities.DeclarationAvailable}");
MachineToolEquipmentReadSnapshot equipment = await client.ReadEquipmentAsync(
entry.NodeId, 32, 128, ct).ConfigureAwait(false);
MachineToolPrognosisReadSnapshot prognoses = await client.ReadPrognosesAsync(entry.NodeId, 32, ct)
.ConfigureAwait(false);
MachineToolProductionReadSnapshot production = await client.ReadProductionAsync(entry.NodeId,
new MachineToolProductionReadOptions { MaximumElements = 128, MaximumReferences = 1024 }, ct)
.ConfigureAwait(false);
Console.WriteLine($"MACHINE tools={equipment.Tools.Count} prognoses={prognoses.Entries.Count} " +
$"jobs={production.Jobs.Count}");
if (options.ReadFiles)
{
FileDirectoryTypeClient root =
await client.GetWorkMastersAsync(entry.NodeId, ct).ConfigureAwait(false) ??
throw new ServiceResultException(StatusCodes.BadNotSupported, "WorkMasters is not configured.");
var files = new FileSystemClient(client.Session, root.ObjectId,
new FileSystemClientOptions { MaxBufferedReadSize = 4096 });
string path = FormattableString.Invariant($"{root.ObjectId.NamespaceIndex}:{DemoContract.ProgramName}");
byte[] contents = await files.ReadAllBytesAsync(path, ct).ConfigureAwait(false);
if (Encoding.UTF8.GetString(contents) != DemoContract.ProgramContents)
{
throw new InvalidOperationException("The remote program is not the documented simulation content.");
}
Console.WriteLine($"FILE_COPIED bytes={contents.Length}; read-only simulation data, never executed.");
}
if (options.Command == "job-store-start")
{
Opc.Ua.Machinery.Jobs.JobManagementTypeClient management =
await client.GetJobManagementAsync(entry.NodeId, ct).ConfigureAwait(false) ??
throw new ServiceResultException(StatusCodes.BadNotSupported, "Machinery Jobs is not configured.");
V2.ISA95JobOrderReceiverObjectTypeClient receiver =
await management.GetJobOrderControlAsync(client.Telemetry, ct).ConfigureAwait(false) ??
throw new ServiceResultException(StatusCodes.BadConfigurationError, "JobOrderControl is missing.");
ulong result = await receiver.StoreAndStartAsync(new V2.ISA95JobOrderDataType
{
EncodingMask = (uint)V2.ISA95JobOrderDataTypeFields.WorkMasterID,
JobOrderID = DemoContract.JobOrderId,
WorkMasterID = [new V2.ISA95WorkMasterDataType { ID = DemoContract.WorkMasterId }]
}, [new LocalizedText("Explicitly consented simulated order.")], ct).ConfigureAwait(false);
if (result != 1)
{
throw new ServiceResultException(StatusCodes.BadInvalidState,
FormattableString.Invariant($"ISA95 request refused with ReturnStatus {result}."));
}
Console.WriteLine("JOB_ACCEPTED: StoreAndStart accepted; this does not assert CNC execution.");
}
}
private static async Task ReadPeripheralAsync(
TypedClient owner, MachineToolDeviceEntry entry, ClientOptions options, List<NodeId> variables,
CancellationToken ct)
{
if (options.ReadFiles || options.Command == "job-store-start")
{
throw new ArgumentException("The selected workflow requires a machine, not a wireless peripheral.");
}
WmtpPeripheralClient client = owner.GetWirelessPeripheral(entry.NodeId);
WmtpPeripheralSnapshot snapshot = await client.ReadSnapshotAsync(ct).ConfigureAwait(false);
Print(owner, "SerialNumber", snapshot.SerialNumber);
Print(owner, "Battery", snapshot.Battery);
Print(owner, "CalibrationMode", snapshot.CalibrationMode);
ArrayOf<WmtpMeasurementReading> sensors = await client.ReadMeasurementsAsync(32, ct).ConfigureAwait(false);
for (int index = 0; index < sensors.Count; index++)
{
WmtpMeasurementReading sensor = sensors[index];
Print(owner, sensor.BrowseName.ToString(), sensor.Sample);
variables.Add(sensor.ValueNodeId);
if (variables.Count > 256)
{
throw new ServiceResultException(StatusCodes.BadEncodingLimitsExceeded,
"The complete selected measurement inventory exceeds the observation bound.");
}
}
if (await client.Proxy.GetWMTPWorkCycleDataAsync(owner.Telemetry, ct).ConfigureAwait(false) != null)
{
await ReadRecordsAsync(await client.GetWorkRecordsAsync(ct).ConfigureAwait(false), "work", ct)
.ConfigureAwait(false);
}
if (await client.Proxy.GetWMTPServiceCycleDataAsync(owner.Telemetry, ct).ConfigureAwait(false) != null)
{
await ReadRecordsAsync(await client.GetServiceRecordsAsync(ct).ConfigureAwait(false), "service", ct)
.ConfigureAwait(false);
}
await RequestAsync(client, sensors, options, ct).ConfigureAwait(false);
}
private static async Task ReadRecordsAsync(WmtpRecordClient records, string dataset, CancellationToken ct)
{
var all = new WmtpRecordQuery();
uint before = await records.CountAsync(all, ct).ConfigureAwait(false);
ArrayOf<WMTPOutputDataType> copy = await records.ReadAsync(all, 16, ct).ConfigureAwait(false);
uint after = await records.CountAsync(all, ct).ConfigureAwait(false);
if (copy.Count != before || after != before)
{
throw new InvalidOperationException("The sample dataset changed during its nonconsuming report.");
}
Console.WriteLine($"RECORDS dataset={dataset} count={copy.Count} retained={after}");
}
private static async Task RequestAsync(
WmtpPeripheralClient client, ArrayOf<WmtpMeasurementReading> sensors,
ClientOptions options, CancellationToken ct)
{
switch (options.Command)
{
case "none":
return;
case "configure":
await ConfigureAsync(client, sensors, options, ct).ConfigureAwait(false);
break;
case "calibrate-on":
case "calibrate-off":
await client.SetCalibrationAsync(options.Command == "calibrate-on", ct).ConfigureAwait(false);
break;
case "set-time":
await client.SetDeviceTimeAsync(TimeProvider.System.GetUtcNow(),
new TimeZoneDataType { Offset = 0, DaylightSavingInOffset = false }, ct).ConfigureAwait(false);
break;
case "delete-work":
WmtpRecordClient deleting = await client.GetWorkRecordsAsync(ct).ConfigureAwait(false);
await deleting.DeleteAsync(new WmtpRecordQuery
{
Selection = WmtpRecordSelection.Index,
FromIndex = (uint)options.DeleteIndex!.Value,
ToIndex = (uint)options.DeleteIndex.Value
}, ct).ConfigureAwait(false);
break;
case "abort-work":
WmtpRecordClient aborting = await client.GetWorkRecordsAsync(ct).ConfigureAwait(false);
await aborting.AbortAsync(ct).ConfigureAwait(false);
break;
case "firmware":
case "license":
byte[] bytes = new byte[options.TransferBytes!.Value];
bytes.AsSpan().Fill((byte)'D');
await client.TransferAsync(options.Command == "firmware"
? WmtpTransferKind.Firmware : WmtpTransferKind.License, ByteString.From(bytes),
cancellationToken: ct)
.ConfigureAwait(false);
Console.WriteLine("TRANSFER_ACCEPTED: provider received demonstration bytes; nothing installed.");
break;
default:
throw new ArgumentException("Unsupported wireless command.");
}
Console.WriteLine($"REQUEST_ACCEPTED command={options.Command} target={options.Target}");
}
private static async Task ConfigureAsync(
WmtpPeripheralClient client, ArrayOf<WmtpMeasurementReading> sensors,
ClientOptions options, CancellationToken ct)
{
NodeId selected = default;
for (int index = 0; index < sensors.Count; index++)
{
if (sensors[index].BrowseName.Name == options.Sensor)
{
if (!selected.IsNull)
{
throw new ArgumentException("The sensor name is ambiguous.");
}
selected = sensors[index].NodeId;
}
}
if (selected.IsNull)
{
throw new ArgumentException("The selected sensor is not present.");
}
WMTPMeasurementTypeClient proxy = client.GetMeasurementProxy(selected);
await proxy.SetTriggerSettingsAsync(options.TriggerMilliseconds!.Value, ct).ConfigureAwait(false);
if (options.ProveRefusal)
{
bool refused = false;
try
{
await proxy.SetTriggerSettingsAsync(options.TriggerMilliseconds.Value + 1, ct)
.ConfigureAwait(false);
}
catch (ServiceResultException error) when (error.StatusCode == StatusCodes.BadResourceUnavailable)
{
refused = true;
}
if (!refused)
{
throw new InvalidOperationException("The second simulated configuration was not refused.");
}
ArrayOf<WmtpMeasurementReading> after =
await client.ReadMeasurementsAsync(32, ct).ConfigureAwait(false);
bool retained = false;
for (int index = 0; index < after.Count; index++)
{
if (after[index].NodeId == selected)
{
if (!after[index].TriggerSettings.WrappedValue.TryGetValue(out uint trigger) ||
trigger != options.TriggerMilliseconds.Value)
{
throw new InvalidOperationException("Refusal changed the previously accepted trigger.");
}
retained = true;
}
}
if (!retained)
{
throw new InvalidOperationException("The configured sensor disappeared before readback.");
}
Console.WriteLine("REFUSAL_CONFIRMED: second request rejected; accepted trigger retained.");
}
}
private static void Print(TypedClient client, string label, in DataValue value)
{
if (value.IsNull)
{
Console.WriteLine($"{label}: ABSENT");
return;
}
using var encoder = new JsonEncoder(client.Session.MessageContext);
encoder.WriteDataValue("DataValue", value);
Console.WriteLine($"{label}: {encoder.CloseAndReturnText()}");
}
}
}