mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-22 01:26:20 +08:00
01d6b5d26c
> 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>
267 lines
11 KiB
C#
267 lines
11 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.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using NUnit.Framework;
|
|
using Opc.Ua.Machinery.Server;
|
|
using Opc.Ua.Machinery.Server.Builders;
|
|
using Opc.Ua.Machinery.Server.StateMachines;
|
|
using ItemIds = Opc.Ua.Machinery.MachineryItemState_StateMachineTypeIds;
|
|
using ModeIds = Opc.Ua.Machinery.MachineryOperationModeStateMachineTypeIds;
|
|
|
|
namespace Opc.Ua.Machinery.Tests
|
|
{
|
|
/// <summary>
|
|
/// Covers the two OPC 40001-1 state machines: the tables the driver works
|
|
/// from, and the transitions it applies.
|
|
/// </summary>
|
|
[TestFixture]
|
|
[Category("Machinery")]
|
|
public sealed class MachineryStateMachineTests
|
|
{
|
|
[Test]
|
|
public void ItemStateTableCoversEveryOrderedPair()
|
|
{
|
|
Assert.That(MachineryStateMachineTables.ItemStates, Has.Count.EqualTo(4));
|
|
Assert.That(MachineryStateMachineTables.ItemTransitions, Has.Count.EqualTo(16));
|
|
|
|
var pairs = MachineryStateMachineTables.ItemTransitions
|
|
.Select(transition => (transition.FromStateId, transition.ToStateId))
|
|
.ToHashSet();
|
|
Assert.That(
|
|
pairs,
|
|
Has.Count.EqualTo(16),
|
|
"Every from/to pair appears exactly once, self-transitions included.");
|
|
}
|
|
|
|
[Test]
|
|
public void OperationModeTableCoversEveryOrderedPair()
|
|
{
|
|
Assert.That(MachineryStateMachineTables.OperationModeStates, Has.Count.EqualTo(4));
|
|
Assert.That(
|
|
MachineryStateMachineTables.OperationModeTransitions,
|
|
Has.Count.EqualTo(16));
|
|
}
|
|
|
|
[Test]
|
|
public void StateNumbersMatchThePublishedModel()
|
|
{
|
|
Assert.That(ItemIds.StateNumbers.NotAvailable, Is.Zero);
|
|
Assert.That(ItemIds.StateNumbers.OutOfService, Is.EqualTo(1u));
|
|
Assert.That(ItemIds.StateNumbers.NotExecuting, Is.EqualTo(2u));
|
|
Assert.That(ItemIds.StateNumbers.Executing, Is.EqualTo(3u));
|
|
|
|
Assert.That(ModeIds.StateNumbers.None, Is.Zero);
|
|
Assert.That(ModeIds.StateNumbers.Maintenance, Is.EqualTo(1u));
|
|
Assert.That(ModeIds.StateNumbers.Setup, Is.EqualTo(2u));
|
|
Assert.That(ModeIds.StateNumbers.Processing, Is.EqualTo(3u));
|
|
}
|
|
|
|
[Test]
|
|
public void ValueMappingIsRoundTrippable()
|
|
{
|
|
foreach (MachineryItemStateValue state in new[]
|
|
{
|
|
MachineryItemStateValue.NotAvailable,
|
|
MachineryItemStateValue.OutOfService,
|
|
MachineryItemStateValue.NotExecuting,
|
|
MachineryItemStateValue.Executing
|
|
})
|
|
{
|
|
Assert.That(
|
|
MachineryStateMachineTables.ToItemState(
|
|
MachineryStateMachineTables.ToStateId(state)),
|
|
Is.EqualTo(state));
|
|
}
|
|
|
|
foreach (MachineryOperationModeValue mode in new[]
|
|
{
|
|
MachineryOperationModeValue.None,
|
|
MachineryOperationModeValue.Maintenance,
|
|
MachineryOperationModeValue.Setup,
|
|
MachineryOperationModeValue.Processing
|
|
})
|
|
{
|
|
Assert.That(
|
|
MachineryStateMachineTables.ToOperationMode(
|
|
MachineryStateMachineTables.ToStateId(mode)),
|
|
Is.EqualTo(mode));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task SelfTransitionIsAppliedRatherThanSkipped()
|
|
{
|
|
await using var fixture = new MachineryServerFixture(MachineryParts.BuildingBlocks);
|
|
await fixture.StartAsync();
|
|
IMachineHandle<BaseObjectState> machine = await BuildAsync(fixture);
|
|
|
|
Assert.That(
|
|
await machine.ItemState!.SetStateAsync(MachineryItemStateValue.Executing),
|
|
Is.True);
|
|
Assert.That(
|
|
await machine.ItemState.SetStateAsync(MachineryItemStateValue.Executing),
|
|
Is.True,
|
|
"The model declares FromExecutingToExecuting, so a move to the " +
|
|
"current state is a transition and not a no-op.");
|
|
|
|
var stateMachine = (MachineryItemState_StateMachineState)
|
|
fixture.Manager.FindPredefinedNode(machine.ItemState.NodeId)!;
|
|
Assert.That(
|
|
stateMachine.LastTransition!.Value.Text,
|
|
Is.EqualTo("FromExecutingToExecuting"));
|
|
}
|
|
|
|
[Test]
|
|
public async Task TransitionHandlersSeeEveryMove()
|
|
{
|
|
await using var fixture = new MachineryServerFixture(MachineryParts.BuildingBlocks);
|
|
await fixture.StartAsync();
|
|
IMachineHandle<BaseObjectState> machine = await BuildAsync(fixture);
|
|
|
|
var stateMachine = (MachineryItemState_StateMachineState)
|
|
fixture.Manager.FindPredefinedNode(machine.ItemState!.NodeId)!;
|
|
var observed = new List<uint>();
|
|
stateMachine.OnAfterTransition = (_, _, transitionId, _, _, _) =>
|
|
{
|
|
observed.Add(transitionId);
|
|
return ServiceResult.Good;
|
|
};
|
|
|
|
await machine.ItemState.SetStateAsync(MachineryItemStateValue.Executing);
|
|
await machine.ItemState.SetStateAsync(MachineryItemStateValue.OutOfService);
|
|
|
|
Assert.That(
|
|
observed,
|
|
Is.EqualTo(
|
|
new[]
|
|
{
|
|
ItemIds.TransitionIds.FromNotExecutingToExecuting,
|
|
ItemIds.TransitionIds.FromExecutingToOutOfService
|
|
}),
|
|
"The driver composes with the stack's transition delegates, which " +
|
|
"is where StateMachineBuilder.For installs its observers.");
|
|
}
|
|
|
|
[Test]
|
|
public async Task AVetoingGuardBlocksTheTransition()
|
|
{
|
|
await using var fixture = new MachineryServerFixture(MachineryParts.BuildingBlocks);
|
|
await fixture.StartAsync();
|
|
IMachineHandle<BaseObjectState> machine = await BuildAsync(fixture);
|
|
|
|
var stateMachine = (MachineryItemState_StateMachineState)
|
|
fixture.Manager.FindPredefinedNode(machine.ItemState!.NodeId)!;
|
|
stateMachine.OnBeforeTransition =
|
|
(_, _, _, _, _, _) => StatusCodes.BadUserAccessDenied;
|
|
|
|
Assert.That(
|
|
await machine.ItemState.SetStateAsync(MachineryItemStateValue.Executing),
|
|
Is.False);
|
|
Assert.That(
|
|
machine.ItemState.CurrentState,
|
|
Is.EqualTo(MachineryItemStateValue.NotExecuting),
|
|
"A vetoed transition must leave the state alone.");
|
|
}
|
|
|
|
[Test]
|
|
public async Task ATransitionCommittedWhileAGuardRunsIsNotOverwritten()
|
|
{
|
|
await using var fixture = new MachineryServerFixture(MachineryParts.BuildingBlocks);
|
|
await fixture.StartAsync().ConfigureAwait(false);
|
|
IMachineHandle<BaseObjectState> machine = await BuildAsync(fixture).ConfigureAwait(false);
|
|
|
|
var stateMachine = (MachineryItemState_StateMachineState)
|
|
fixture.Manager.FindPredefinedNode(machine.ItemState!.NodeId)!;
|
|
var guarded = new List<uint>();
|
|
bool interleaved = false;
|
|
stateMachine.OnBeforeTransition = (_, _, transitionId, _, _, _) =>
|
|
{
|
|
guarded.Add(transitionId);
|
|
if (!interleaved)
|
|
{
|
|
// Another caller commits while this guard runs outside the
|
|
// driver's lock.
|
|
interleaved = true;
|
|
_ = machine.ItemState
|
|
.SetStateAsync(MachineryItemStateValue.OutOfService)
|
|
.AsTask()
|
|
.GetAwaiter()
|
|
.GetResult();
|
|
}
|
|
return ServiceResult.Good;
|
|
};
|
|
|
|
Assert.That(
|
|
await machine.ItemState.SetStateAsync(MachineryItemStateValue.Executing).ConfigureAwait(false),
|
|
Is.True);
|
|
|
|
NodeId lastTransition = stateMachine.LastTransition!.Id!.Value;
|
|
Assert.That(
|
|
machine.ItemState.CurrentState,
|
|
Is.EqualTo(MachineryItemStateValue.Executing));
|
|
Assert.That(
|
|
lastTransition,
|
|
Is.EqualTo(new NodeId(
|
|
ItemIds.TransitionIds.FromOutOfServiceToExecuting,
|
|
lastTransition.NamespaceIndex)),
|
|
"The stale NotExecuting to Executing transition must not be committed.");
|
|
Assert.That(
|
|
guarded,
|
|
Is.EqualTo(
|
|
new[]
|
|
{
|
|
ItemIds.TransitionIds.FromNotExecutingToExecuting,
|
|
ItemIds.TransitionIds.FromNotExecutingToOutOfService,
|
|
ItemIds.TransitionIds.FromOutOfServiceToExecuting
|
|
}),
|
|
"The transition from the new current state is guarded again.");
|
|
}
|
|
|
|
private static ValueTask<IMachineHandle<BaseObjectState>> BuildAsync(
|
|
MachineryServerFixture fixture)
|
|
{
|
|
return fixture.CreateBuildContext()
|
|
.AddMachine(new QualifiedName("StateMachine-Host"))
|
|
.WithIdentification(id =>
|
|
{
|
|
id.Manufacturer = new LocalizedText("Acme");
|
|
id.SerialNumber = "SN-SM";
|
|
id.ProductInstanceUri = "urn:acme:sm";
|
|
})
|
|
.WithMonitoring(monitoring => monitoring
|
|
.WithMachineryItemState(MachineryItemStateValue.NotExecuting)
|
|
.WithOperationMode())
|
|
.BuildAsync();
|
|
}
|
|
}
|
|
}
|