mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-17 05:11:13 +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>
289 lines
11 KiB
C#
289 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;
|
|
using Opc.Ua.Machinery.ProcessValues;
|
|
|
|
namespace Opc.Ua.Machinery.Server.Builders
|
|
{
|
|
/// <summary>
|
|
/// Couples one of the two exclusive alarms OPC 40001-2 declares on a
|
|
/// process value to the value it watches.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// The generated factory creates the <c>LimitAlarm</c> /
|
|
/// <c>DeviationAlarm</c> object and its condition machinery, but nothing
|
|
/// more: no limits, no source, no condition lifecycle. An alarm in that
|
|
/// state is a node, not an alarm — it never activates and never reports.
|
|
/// This type supplies the missing half.
|
|
/// </para>
|
|
/// <para>
|
|
/// The four limit Properties are optional on <c>LimitAlarmType</c> and are
|
|
/// not part of the OPC 40001-2 instance declaration, so they are
|
|
/// materialised here with NodeIds minted from the build context; doing it
|
|
/// before the machine is registered is what gets them indexed with the
|
|
/// rest of the tree.
|
|
/// </para>
|
|
/// </remarks>
|
|
internal sealed class MachineryProcessValueAlarm
|
|
{
|
|
public MachineryProcessValueAlarm(
|
|
MachineryBuildScope scope,
|
|
ProcessValueState processValue,
|
|
BaseVariableState signal,
|
|
ExclusiveLimitAlarmState alarm,
|
|
bool deviation)
|
|
{
|
|
m_scope = scope;
|
|
m_processValue = processValue;
|
|
m_signal = signal;
|
|
m_alarm = alarm;
|
|
m_deviation = deviation;
|
|
Initialize();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the alarm being driven.
|
|
/// </summary>
|
|
public ExclusiveLimitAlarmState Alarm => m_alarm;
|
|
|
|
/// <summary>
|
|
/// Gets whether the alarm watches the deviation from the setpoint
|
|
/// rather than the value itself.
|
|
/// </summary>
|
|
public bool IsDeviation => m_deviation;
|
|
|
|
/// <summary>
|
|
/// Writes the four limits, materialising the Properties the model
|
|
/// leaves optional.
|
|
/// </summary>
|
|
public void SetLimits(double? lowLow, double? low, double? high, double? highHigh)
|
|
{
|
|
m_scope.EnsureMutable();
|
|
if (highHigh.HasValue)
|
|
{
|
|
m_alarm.HighHighLimit = CreateLimit(
|
|
Opc.Ua.BrowseNames.HighHighLimit,
|
|
highHigh.Value);
|
|
}
|
|
if (high.HasValue)
|
|
{
|
|
m_alarm.HighLimit = CreateLimit(Opc.Ua.BrowseNames.HighLimit, high.Value);
|
|
}
|
|
if (low.HasValue)
|
|
{
|
|
m_alarm.LowLimit = CreateLimit(Opc.Ua.BrowseNames.LowLimit, low.Value);
|
|
}
|
|
if (lowLow.HasValue)
|
|
{
|
|
m_alarm.LowLowLimit = CreateLimit(
|
|
Opc.Ua.BrowseNames.LowLowLimit,
|
|
lowLow.Value);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Points the deviation alarm at the setpoint it measures against.
|
|
/// </summary>
|
|
public void SetSetpointNode(BaseVariableState setpoint)
|
|
{
|
|
if (m_alarm is ExclusiveDeviationAlarmState deviationAlarm &&
|
|
deviationAlarm.SetpointNode != null)
|
|
{
|
|
deviationAlarm.SetpointNode.Value = setpoint.NodeId;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Re-evaluates the alarm against a new reading and reports an event
|
|
/// when the limit state changed.
|
|
/// </summary>
|
|
/// <param name="context">The system context.</param>
|
|
/// <param name="observed">
|
|
/// The value to compare — the process value for a limit alarm, the
|
|
/// deviation from the setpoint for a deviation alarm.
|
|
/// </param>
|
|
/// <returns>
|
|
/// <see langword="true"/> when the limit state changed and an event
|
|
/// was reported.
|
|
/// </returns>
|
|
public bool Evaluate(ISystemContext context, double observed)
|
|
{
|
|
LimitAlarmStates state = Classify(observed);
|
|
if (state == m_state)
|
|
{
|
|
return false;
|
|
}
|
|
m_state = state;
|
|
m_alarm.SetLimitState(context, state);
|
|
|
|
bool active = state != LimitAlarmStates.Inactive;
|
|
m_alarm.Retain!.Value = active;
|
|
m_alarm.SetSeverity(context, SeverityOf(state));
|
|
m_alarm.Message!.Value = new LocalizedText(
|
|
active
|
|
? FormattableString.Invariant(
|
|
$"{m_processValue.BrowseName.Name} {state} at {observed:G6}.")
|
|
: FormattableString.Invariant(
|
|
$"{m_processValue.BrowseName.Name} back within limits at {observed:G6}."));
|
|
ReportEvent(context);
|
|
return true;
|
|
}
|
|
|
|
private LimitAlarmStates Classify(double observed)
|
|
{
|
|
if (m_alarm.HighHighLimit != null && observed >= m_alarm.HighHighLimit.Value)
|
|
{
|
|
return LimitAlarmStates.HighHigh;
|
|
}
|
|
if (m_alarm.LowLowLimit != null && observed <= m_alarm.LowLowLimit.Value)
|
|
{
|
|
return LimitAlarmStates.LowLow;
|
|
}
|
|
if (m_alarm.HighLimit != null && observed >= m_alarm.HighLimit.Value)
|
|
{
|
|
return LimitAlarmStates.High;
|
|
}
|
|
if (m_alarm.LowLimit != null && observed <= m_alarm.LowLimit.Value)
|
|
{
|
|
return LimitAlarmStates.Low;
|
|
}
|
|
return LimitAlarmStates.Inactive;
|
|
}
|
|
|
|
private static EventSeverity SeverityOf(LimitAlarmStates state)
|
|
{
|
|
return state switch
|
|
{
|
|
LimitAlarmStates.HighHigh or LimitAlarmStates.LowLow => EventSeverity.High,
|
|
LimitAlarmStates.High or LimitAlarmStates.Low => EventSeverity.Medium,
|
|
_ => EventSeverity.Low
|
|
};
|
|
}
|
|
|
|
private void ReportEvent(ISystemContext context)
|
|
{
|
|
if (m_alarm.EnabledState?.Id?.Value != true)
|
|
{
|
|
return;
|
|
}
|
|
m_alarm.EventId!.Value = Uuid.NewUuid().ToByteString();
|
|
m_alarm.Time!.Value = DateTimeUtc.Now;
|
|
m_alarm.ReceiveTime!.Value = m_alarm.Time.Value;
|
|
m_alarm.ClearChangeMasks(context, true);
|
|
|
|
var snapshot = new InstanceStateSnapshot();
|
|
snapshot.Initialize(context, m_alarm);
|
|
m_alarm.ReportEvent(context, snapshot);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gives the condition the identity OPC 10000-9 expects before it can
|
|
/// be enabled: a source, an input, a name, and the event-source edge
|
|
/// that makes it reachable from the process value a client subscribes
|
|
/// to.
|
|
/// </summary>
|
|
private void Initialize()
|
|
{
|
|
ISystemContext context = m_scope.Context;
|
|
|
|
m_alarm.SourceNode!.Value = m_processValue.NodeId;
|
|
m_alarm.SourceName!.Value = m_processValue.BrowseName.Name ?? string.Empty;
|
|
m_alarm.ConditionName!.Value = m_alarm.BrowseName.Name ?? string.Empty;
|
|
m_alarm.InputNode!.Value = m_signal.NodeId;
|
|
m_alarm.BranchId!.Value = NodeId.Null;
|
|
m_alarm.ClientUserId!.Value = string.Empty;
|
|
m_alarm.Quality!.Value = StatusCodes.Good;
|
|
m_alarm.Retain!.Value = false;
|
|
m_alarm.Comment!.Value = new LocalizedText(string.Empty);
|
|
|
|
m_alarm.SetEnableState(context, enabled: true);
|
|
m_alarm.SetLimitState(context, LimitAlarmStates.Inactive);
|
|
m_alarm.SetSeverity(context, EventSeverity.Low);
|
|
|
|
// OPC 10000-9 reaches a condition from its source through
|
|
// HasEventSource; without it a client browsing the process value
|
|
// cannot discover the alarm it owns.
|
|
if (!m_processValue.ReferenceExists(
|
|
Opc.Ua.ReferenceTypeIds.HasEventSource,
|
|
false,
|
|
m_alarm.NodeId))
|
|
{
|
|
m_processValue.AddReference(
|
|
Opc.Ua.ReferenceTypeIds.HasEventSource,
|
|
false,
|
|
m_alarm.NodeId);
|
|
m_alarm.AddReference(
|
|
Opc.Ua.ReferenceTypeIds.HasEventSource,
|
|
true,
|
|
m_processValue.NodeId);
|
|
}
|
|
}
|
|
|
|
private PropertyState<double> CreateLimit(string browseName, double value)
|
|
{
|
|
PropertyState<double>? existing =
|
|
MachineryBuilderUtilities.FindChild<PropertyState<double>>(
|
|
m_scope.Context,
|
|
m_alarm,
|
|
new QualifiedName(browseName));
|
|
if (existing != null)
|
|
{
|
|
existing.Value = value;
|
|
return existing;
|
|
}
|
|
|
|
PropertyState<double> property =
|
|
PropertyState<double>.With<VariantBuilder>(m_alarm);
|
|
property.SymbolicName = browseName;
|
|
property.BrowseName = new QualifiedName(browseName);
|
|
property.DisplayName = new LocalizedText(browseName);
|
|
property.TypeDefinitionId = Opc.Ua.VariableTypeIds.PropertyType;
|
|
property.ReferenceTypeId = Opc.Ua.ReferenceTypeIds.HasProperty;
|
|
property.DataType = Opc.Ua.DataTypeIds.Double;
|
|
property.ValueRank = ValueRanks.Scalar;
|
|
property.AccessLevel = AccessLevels.CurrentRead;
|
|
property.UserAccessLevel = AccessLevels.CurrentRead;
|
|
property.MinimumSamplingInterval = MinimumSamplingIntervals.Indeterminate;
|
|
property.ModellingRuleId = NodeId.Null;
|
|
property.Value = value;
|
|
property.NodeId = m_scope.Context.NodeIdFactory!.New(m_scope.Context, property);
|
|
return property;
|
|
}
|
|
|
|
private readonly MachineryBuildScope m_scope;
|
|
private readonly ProcessValueState m_processValue;
|
|
private readonly BaseVariableState m_signal;
|
|
private readonly ExclusiveLimitAlarmState m_alarm;
|
|
private readonly bool m_deviation;
|
|
private LimitAlarmStates m_state = LimitAlarmStates.Inactive;
|
|
}
|
|
}
|