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

436 lines
22 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.Threading.Tasks;
using NUnit.Framework;
using Opc.Ua.MachineTool.Server;
namespace Opc.Ua.MachineTool.Tests
{
/// <summary>
/// Exercises OPC 40501-1 section 8.4 through the composed production controller and generated nodes.
/// </summary>
[TestFixture]
[Category("MachineTool")]
[NonParallelizable]
public sealed class MachineToolProductionTests
{
/// <summary>
/// The mandatory program uses canonical state numbers rather than CLR enum ordinals.
/// </summary>
[Test]
public async Task MandatoryProgramUsesCanonicalStateAndAcquisitionMetadataAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
MachineToolState machine = fixture.Context.Context.CreateInstanceOfMachineToolType(
fixture.Context.MachinesFolder,
new QualifiedName("Production", fixture.Context.InstanceNamespaceIndex));
await using var production = new MachineToolProductionController(
fixture.Context, machine, new MachineToolProductionOptions(), TimeProvider.System);
var sample = new MachineToolSampleInfo(
new DateTime(2026, 3, 4, 5, 6, 7, DateTimeKind.Utc), StatusCodes.UncertainLastUsableValue);
await production.InitializeAsync(new MachineToolSnapshot
{
ActiveProgram = new MachineToolProgram
{
Name = "MAIN.NC",
Phase = MachineToolProductionPhase.Ended,
Sample = sample
}
}).ConfigureAwait(false);
ProductionActiveProgramState active = machine.Production!.ActiveProgram!;
Assert.That(active.NumberInList!.Value, Is.Zero);
Assert.That(active.Name!.Value, Is.EqualTo("MAIN.NC"));
Assert.That(active.State!.CurrentState!.Number!.Value, Is.EqualTo(2u));
Assert.That(active.State.CurrentState.Id!.Value,
Is.EqualTo(NodeId.Create(5038u, Namespaces.MachineTool, fixture.Context.Context.NamespaceUris)));
Assert.That(active.State.CurrentState.Timestamp, Is.EqualTo(sample.SourceTimestamp));
Assert.That(active.State.CurrentState.StatusCode, Is.EqualTo(sample.StatusCode));
Assert.That(active.State.LastTransition!.Id!.Value.IsNull, Is.True);
Assert.That(machine.Production.ProductionPlan, Is.Null);
}
/// <summary>
/// Tables 38, 41 and 50 require ordered programs, active-job links and a transition for consecutive runs.
/// </summary>
[Test]
public async Task JobsLinkActiveProgramAndCountConsecutiveRunsAtomicallyAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
MachineToolState machine = fixture.Context.Context.CreateInstanceOfMachineToolType(
fixture.Context.MachinesFolder, new QualifiedName("Jobs", fixture.Context.InstanceNamespaceIndex));
await using var production = new MachineToolProductionController(fixture.Context, machine,
new MachineToolProductionOptions { Features = MachineToolFeatures.Production }, TimeProvider.System);
var sample = new MachineToolSampleInfo(
new DateTime(2026, 3, 4, 5, 6, 7, DateTimeKind.Utc), StatusCodes.Good);
var active = new MachineToolProgram
{
Name = "SECOND.NC",
JobIdentifier = "Job-1",
Phase = MachineToolProductionPhase.Running,
Sample = sample
};
var job = new MachineToolProductionJob
{
Identifier = "Job-1",
Phase = MachineToolProductionPhase.Running,
RunsPlanned = 2,
RunsPlannedIsValid = true,
Sample = sample,
Programs = [new MachineToolProgram { Name = "FIRST.NC", Sample = sample }, active]
};
var snapshot = new MachineToolSnapshot
{
ActiveProgram = active,
Production = new MachineToolProductionObservation { Jobs = [job] }
};
await production.InitializeAsync(snapshot).ConfigureAwait(false);
Assert.That(machine.Production!.ProductionPlan, Is.Not.Null);
ProductionJobState published = Children<ProductionJobState>(
machine.Production.ProductionPlan!, fixture.Context.Context)[0];
Assert.That(machine.Production.ActiveProgram!.NumberInList!.Value, Is.EqualTo(1));
Assert.That(machine.Production.ActiveProgram.JobIdentifier!.Value, Is.EqualTo("Job-1"));
Assert.That(machine.Production.ActiveProgram.JobNodeId!.Value, Is.EqualTo(published.NodeId));
Assert.That(Children<ProductionProgramState>(published.ProductionPrograms!, fixture.Context.Context)[1]
.NumberInList!.Value, Is.EqualTo(1));
var reported = new List<ProductionJobTransitionEventState>();
machine.OnReportEventAsync = (_, _, e, _) =>
{
if (e is ProductionJobTransitionEventState transition)
{
reported.Add(transition);
}
return default;
};
var next = snapshot with
{
Revision = 1,
Production = snapshot.Production with
{
Jobs = [job with { RunsCompleted = 1, TransitionSequence = 1 }]
}
};
await production.ApplyAsync(next).ConfigureAwait(false);
Assert.That(published.RunsCompleted!.Value, Is.EqualTo(1));
Assert.That(published.State!.LastTransition!.Number!.Value, Is.EqualTo(3));
Assert.That(published.State.LastTransition.Id!.Value,
Is.EqualTo(NodeId.Create(151u, Namespaces.MachineTool, fixture.Context.Context.NamespaceUris)));
Assert.That(published.State.LastTransition.TransitionTime!.Value, Is.EqualTo(sample.SourceTimestamp));
Assert.That(reported, Has.Count.EqualTo(1));
Assert.That(reported[0].RunsCompleted!.Value, Is.EqualTo(1));
Assert.That(reported[0].SourceNode!.Value, Is.EqualTo(published.State.NodeId));
Assert.That(reported[0].Identifier!.Value, Is.EqualTo("Job-1"));
ServiceResultException invalid = Assert.ThrowsAsync<ServiceResultException>(async () =>
await production.ApplyAsync(next with
{
Revision = 2,
ActiveProgram = active with { Name = "INVALID" },
Production = snapshot.Production with
{
Jobs = [job with { RunsCompleted = 9, TransitionSequence = 2 }]
}
}).ConfigureAwait(false))!;
Assert.That(invalid.StatusCode, Is.EqualTo(StatusCodes.BadInvalidState));
Assert.That(published.RunsCompleted.Value, Is.EqualTo(1));
Assert.That(machine.Production.ActiveProgram.Name!.Value, Is.EqualTo("SECOND.NC"));
Assert.That(production.Observation, Is.SameAs(next));
}
/// <summary>
/// Section 8.4.3 and Table 110 count only the three specified acceptable quality categories.
/// </summary>
[TestCase(PartQuality.Good, 1u)]
[TestCase(PartQuality.CapabilityUnavailable, 1u)]
[TestCase(PartQuality.WillNotBeMeasured, 1u)]
[TestCase(PartQuality.Bad, 0u)]
[TestCase(PartQuality.NotYetMeasured, 0u)]
public async Task PartsCountersAndTransitionEventsUseSpecifiedQualityCategoriesAsync(
PartQuality quality, uint good)
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
MachineToolState machine = fixture.Context.Context.CreateInstanceOfMachineToolType(
fixture.Context.MachinesFolder, new QualifiedName("Parts", fixture.Context.InstanceNamespaceIndex));
await using var production = new MachineToolProductionController(fixture.Context, machine,
new MachineToolProductionOptions
{
Features = MachineToolFeatures.Production | MachineToolFeatures.ProductionParts |
MachineToolFeatures.ProductionStatistics
}, TimeProvider.System);
MachineToolSnapshot initial = Example();
await production.InitializeAsync(initial).ConfigureAwait(false);
ProductionJobState node = Children<ProductionJobState>(production.State.ProductionPlan!,
fixture.Context.Context)[0];
ProductionPartSetState setNode =
Children<ProductionPartSetState>(node.PartSets!, fixture.Context.Context)[0];
ProductionPartState partNode =
Children<ProductionPartState>(setNode.PartsPerRun!, fixture.Context.Context)[0];
var parts = new List<ProductionPartTransitionEventState>();
var programs = new List<ProductionProgramTransitionEventState>();
machine.OnReportEventAsync = (_, _, e, _) =>
{
if (e is ProductionPartTransitionEventState partEvent)
{
parts.Add(partEvent);
}
if (e is ProductionProgramTransitionEventState programEvent)
{
programs.Add(programEvent);
}
return default;
};
MachineToolProductionJob job = initial.Production.Jobs[0];
MachineToolProductionPartSet set = job.PartSets[0];
MachineToolProductionPart part = set.Parts[0] with
{
Phase = MachineToolProductionPhase.Running,
TransitionSequence = 1
};
MachineToolProgram program = job.Programs[0] with
{
Phase = MachineToolProductionPhase.Running,
TransitionSequence = 1
};
var running = initial with
{
Revision = 1,
ActiveProgram = program,
Production = initial.Production with
{
Jobs =
[
job with
{
Phase = MachineToolProductionPhase.Running, TransitionSequence = 1,
Programs = [program], PartSets = [set with { Parts = [part] }]
}
]
}
};
await production.ApplyAsync(running).ConfigureAwait(false);
part = part with { Phase = MachineToolProductionPhase.Ended, TransitionSequence = 2, Quality = quality };
job = running.Production.Jobs[0] with
{
PartsCompleted = 1,
PartsGood = good,
PartSets = [set with { Parts = [part], PartsCompletedPerRun = 1 }]
};
var finishedPart = running with { Revision = 2, Production = running.Production with { Jobs = [job] } };
await production.ApplyAsync(finishedPart).ConfigureAwait(false);
Assert.That(node.PartsCompleted!.Value, Is.EqualTo(1));
Assert.That(node.PartsGood!.Value, Is.EqualTo(good));
Assert.That(setNode.PartsCompletedPerRun!.Value, Is.EqualTo(1));
Assert.That(partNode.PartQuality!.Value, Is.EqualTo(quality));
Assert.That(partNode.State!.LastTransition!.Number!.Value, Is.EqualTo(1));
Assert.That(production.State.Statistics!.PartsProducedInLifetime!.Value, Is.EqualTo(100));
Assert.That(parts, Has.Count.EqualTo(2));
Assert.That(parts[1].PartQuality!.Value, Is.EqualTo(quality));
Assert.That(parts[1].JobIdentifier!.Value, Is.EqualTo("Job"));
Assert.That(parts[1].Identifier!.Value, Is.EqualTo("Part-1"));
Assert.That(parts[1].ProcessIrregularity!.Value, Is.EqualTo(ProcessIrregularity.NotDetected));
Assert.That(parts[1].ToState!.Number!.Value, Is.EqualTo(2));
Assert.That(parts[1].Time!.Value, Is.EqualTo(part.Sample.SourceTimestamp));
Assert.That(programs, Has.Count.EqualTo(2), "One event from the plan program and one from ActiveProgram.");
Assert.That(programs[0].JobIdentifier!.Value, Is.EqualTo("Job"));
Assert.That(programs[0].Name!.Value, Is.EqualTo("MAIN.NC"));
Assert.ThrowsAsync<ServiceResultException>(async () => await production.ApplyAsync(finishedPart with
{
Revision = 3,
Production = finishedPart.Production with { Jobs = [job with { PartsGood = 2 }] }
}).ConfigureAwait(false));
Assert.That(node.PartsGood.Value, Is.EqualTo(good));
Assert.That(parts, Has.Count.EqualTo(2), "Rejected observations must not emit transitions.");
}
/// <summary>
/// Section 8.4.9 requires every known reason to be cleared before InterruptedToRunning.
/// </summary>
[Test]
public async Task InterruptionConditionsRemainRetainedUntilEveryReasonIsClearedAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
MachineToolState machine = fixture.Context.Context.CreateInstanceOfMachineToolType(
fixture.Context.MachinesFolder,
new QualifiedName("Interruptions", fixture.Context.InstanceNamespaceIndex));
await using var production = new MachineToolProductionController(fixture.Context, machine,
new MachineToolProductionOptions
{
Features = MachineToolFeatures.Production | MachineToolFeatures.ProductionParts |
MachineToolFeatures.ProductionStatistics | MachineToolFeatures.ProductionInterruptions
}, TimeProvider.System);
MachineToolSnapshot initial = Example();
await production.InitializeAsync(initial).ConfigureAwait(false);
MachineToolProductionJob job = initial.Production.Jobs[0] with
{
Phase = MachineToolProductionPhase.Running,
TransitionSequence = 1
};
var running = initial with { Revision = 1, Production = initial.Production with { Jobs = [job] } };
await production.ApplyAsync(running).ConfigureAwait(false);
NodeId missing = NodeId.Create(54u, Namespaces.MachineTool, fixture.Context.Context.NamespaceUris);
NodeId toolChange = NodeId.Create(58u, Namespaces.MachineTool, fixture.Context.Context.NamespaceUris);
var first = new MachineToolProductionInterruption
{
Identifier = "Missing",
ConditionClassId = missing,
Message = new LocalizedText("Load a part"),
IsActive = true,
Sample = job.Sample
};
var second = first with
{
Identifier = "Tool",
ConditionClassId = toolChange,
Message = new LocalizedText("Changing tool"),
IsAutomated = true
};
job = job with
{
Phase = MachineToolProductionPhase.Interrupted,
TransitionSequence = 2,
Interruptions = [first, second]
};
var interrupted = running with { Revision = 2, Production = running.Production with { Jobs = [job] } };
await production.ApplyAsync(interrupted).ConfigureAwait(false);
ProductionJobState node = Children<ProductionJobState>(production.State.ProductionPlan!,
fixture.Context.Context)[0];
List<InterruptionConditionState> conditions =
Children<InterruptionConditionState>(node, fixture.Context.Context);
Assert.That(conditions, Has.Count.EqualTo(2));
Assert.That(conditions[0].ConditionClassId!.Value, Is.EqualTo(missing));
Assert.That(conditions[0].ConditionClassName!.Value.Text, Is.EqualTo("PartMissingConditionClassType"));
Assert.That(conditions[0].Retain!.Value, Is.True);
Assert.That(conditions[1].IsAutomated!.Value, Is.True);
Assert.That(conditions[1].SourceNode!.Value, Is.EqualTo(node.NodeId));
var retained = new List<IFilterTarget>();
conditions[0].ConditionRefresh(fixture.Context.Context, retained, true);
Assert.That(retained, Has.Count.EqualTo(1));
Assert.ThrowsAsync<ServiceResultException>(async () => await production.ApplyAsync(interrupted with
{
Revision = 3,
Production = interrupted.Production with
{
Jobs = [job with { Phase = MachineToolProductionPhase.Running, TransitionSequence = 3 }]
}
}).ConfigureAwait(false));
Assert.That(node.State!.CurrentState!.Value.Text, Is.EqualTo("Interrupted"));
first = first with { IsActive = false };
second = second with { IsActive = false };
job = job with { Interruptions = [first, second] };
var cleared = interrupted with { Revision = 3, Production = interrupted.Production with { Jobs = [job] } };
await production.ApplyAsync(cleared).ConfigureAwait(false);
Assert.That(conditions[0].Retain!.Value, Is.False);
Assert.That(conditions[1].Retain!.Value, Is.False);
retained.Clear();
conditions[0].ConditionRefresh(fixture.Context.Context, retained, true);
Assert.That(retained, Is.Empty);
await production.ApplyAsync(cleared with
{
Revision = 4,
Production = cleared.Production with
{
Jobs = [job with { Phase = MachineToolProductionPhase.Running, TransitionSequence = 3 }]
}
}).ConfigureAwait(false);
Assert.That(node.State.LastTransition!.Number!.Value, Is.EqualTo(5));
}
/// <summary>
/// Supplies one independently worked example with all retained part and statistics observations.
/// </summary>
internal static MachineToolSnapshot Example()
{
var sample = new MachineToolSampleInfo(
new DateTime(2026, 4, 5, 6, 7, 8, DateTimeKind.Utc), StatusCodes.Good);
var program = new MachineToolProgram { Name = "MAIN.NC", JobIdentifier = "Job", Sample = sample };
return new MachineToolSnapshot
{
ActiveProgram = program,
Operation = new MachineToolOperation { PartsProducedInLifetime = 100, Sample = sample },
Production = new MachineToolProductionObservation
{
Statistics = new MachineToolProductionStatistics(100, sample),
Jobs =
[
new MachineToolProductionJob
{
Identifier = "Job", RunsPlanned = 2, RunsPlannedIsValid = true, Sample = sample,
Programs = [program],
PartSets =
[
new MachineToolProductionPartSet
{
Key = "Set", Name = "Pallet", PartsPlannedPerRun = 1, Sample = sample,
Parts =
[
new MachineToolProductionPart
{
Key = "Part", Name = "Bracket", Identifier = "Part-1",
Irregularity = ProcessIrregularity.NotDetected, Sample = sample
}
]
}
]
}
]
}
};
}
/// <summary>
/// Reads real generated children at the public address-space seam.
/// </summary>
private static List<TState> Children<TState>(NodeState parent, ISystemContext context)
where TState : BaseInstanceState
{
var children = new List<BaseInstanceState>();
parent.GetChildren(context, children);
var result = new List<TState>();
foreach (BaseInstanceState child in children)
{
if (child is TState typed)
{
result.Add(typed);
}
}
return result;
}
}
}