/* ========================================================================
* Copyright (c) 2005-2025 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;
namespace Opc.Ua.Server.Fluent
{
///
/// Strongly-typed fluent builder for an alarm/condition state
/// instance. Returned by the CreateLimitAlarm /
/// CreateExclusiveLimitAlarm / CreateOffNormalAlarm
/// helpers on .
///
///
///
/// MVP scope: the builder surfaces only the most common alarm
/// settings (limits, source variable, acknowledge / confirm
/// callbacks). For anything more advanced use the
///
/// escape hatch to mutate the underlying
/// directly.
///
///
///
/// Concrete alarm state class — must derive from
/// .
///
public interface IAlarmBuilder where TState : ConditionState
{
///
/// The underlying alarm state instance.
///
TState Alarm { get; }
///
/// The owning node builder, for chain termination.
///
INodeBuilder Builder { get; }
///
/// Sets the /
/// /
/// /
/// property values on
/// a limit-style alarm. Throws
/// when invoked on
/// a non-limit alarm. Pass double.NaN for any limit you
/// don't want to set.
///
IAlarmBuilder WithLimits(
double highHigh = double.NaN,
double high = double.NaN,
double low = double.NaN,
double lowLow = double.NaN);
///
/// Sets the alarm's SourceNode reference and
/// SourceName to the supplied target. Equivalent to
/// setting the alarm's "InputNode" semantics from the spec.
///
IAlarmBuilder MonitorVariable(NodeState source);
///
/// Wires .
/// Return from the handler to
/// permit the acknowledge transition; any other code cancels it.
///
IAlarmBuilder OnAcknowledge(ConditionAddCommentEventHandler handler);
///
/// Wires .
///
IAlarmBuilder OnConfirm(ConditionAddCommentEventHandler handler);
}
///
/// Extension methods that create alarm instances under a parent node
/// resolved through the fluent builder.
///
public static class AlarmBuilderExtensions
{
///
/// Creates a new child
/// under the resolved parent. The alarm is attached via
/// ; the
/// owning manager is responsible for indexing it via
/// AddPredefinedNodeAsync if direct NodeId lookup is
/// required.
///
public static IAlarmBuilder CreateLimitAlarm(
this INodeBuilder parent,
QualifiedName browseName)
{
NonExclusiveLimitAlarmState alarm = AttachAlarm(
parent, browseName, p => new NonExclusiveLimitAlarmState(p));
return new AlarmBuilder(parent, alarm);
}
///
/// Creates a new child.
///
public static IAlarmBuilder CreateExclusiveLimitAlarm(
this INodeBuilder parent,
QualifiedName browseName)
{
ExclusiveLimitAlarmState alarm = AttachAlarm(
parent, browseName, p => new ExclusiveLimitAlarmState(p));
return new AlarmBuilder(parent, alarm);
}
///
/// Creates a new child.
///
public static IAlarmBuilder CreateOffNormalAlarm(
this INodeBuilder parent,
QualifiedName browseName)
{
OffNormalAlarmState alarm = AttachAlarm(
parent, browseName, p => new OffNormalAlarmState(p));
return new AlarmBuilder(parent, alarm);
}
///
/// Escape hatch: directly mutate the underlying alarm state.
/// Use for properties not covered by the narrow MVP surface
/// (e.g. severity table, retain flag, branches).
///
/// Concrete alarm condition state type.
public static IAlarmBuilder ConfigureAlarm(
this IAlarmBuilder builder,
Action configure)
where TState : ConditionState
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
configure(builder.Alarm);
return builder;
}
///
/// Returns control to the owning node builder so subsequent
/// fluent calls operate on the alarm's parent again. Use this
/// when chaining multiple alarms.
///
/// Concrete alarm condition state type.
public static INodeBuilder Done(this IAlarmBuilder builder)
where TState : ConditionState
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
return builder.Builder;
}
private static TState AttachAlarm(
INodeBuilder parent,
QualifiedName browseName,
Func factory)
where TState : ConditionState
{
if (parent == null)
{
throw new ArgumentNullException(nameof(parent));
}
if (browseName.IsNull)
{
throw new ArgumentNullException(nameof(browseName));
}
if (factory == null)
{
throw new ArgumentNullException(nameof(factory));
}
string symbolicName = browseName.Name ?? string.Empty;
TState alarm = factory(parent.Node);
alarm.SymbolicName = symbolicName;
alarm.BrowseName = browseName;
alarm.DisplayName = new LocalizedText(symbolicName);
string parentIdentifier = parent.Node.NodeId.IdentifierAsString;
alarm.NodeId = new NodeId(
string.Concat(parentIdentifier, "_", symbolicName),
parent.Node.NodeId.NamespaceIndex);
// Initialize standard alarm state surface so the alarm is
// immediately addressable as a valid OPC UA condition.
alarm.Create(
parent.Builder.Context,
alarm.NodeId,
browseName,
displayName: new LocalizedText(symbolicName),
assignNodeIds: false);
parent.Node.AddChild(alarm);
return alarm;
}
}
///
/// Internal implementation of .
///
/// Concrete alarm condition state type.
internal sealed class AlarmBuilder : IAlarmBuilder
where TState : ConditionState
{
public AlarmBuilder(INodeBuilder parent, TState alarm)
{
Builder = parent ?? throw new ArgumentNullException(nameof(parent));
Alarm = alarm ?? throw new ArgumentNullException(nameof(alarm));
}
public TState Alarm { get; }
public INodeBuilder Builder { get; }
public IAlarmBuilder WithLimits(
double highHigh = double.NaN,
double high = double.NaN,
double low = double.NaN,
double lowLow = double.NaN)
{
if (Alarm is not LimitAlarmState limit)
{
throw ServiceResultException.Create(
StatusCodes.BadTypeMismatch,
"WithLimits requires a LimitAlarmState; alarm '{0}' is '{1}'.",
Alarm.BrowseName,
Alarm.GetType().Name);
}
_ = Builder.Builder.Context;
if (!double.IsNaN(highHigh))
{
limit.HighHighLimit ??=
new PropertyState.Implementation(limit);
limit.HighHighLimit.Value = highHigh;
}
if (!double.IsNaN(high))
{
limit.HighLimit ??=
new PropertyState.Implementation(limit);
limit.HighLimit.Value = high;
}
if (!double.IsNaN(low))
{
limit.LowLimit ??=
new PropertyState.Implementation(limit);
limit.LowLimit.Value = low;
}
if (!double.IsNaN(lowLow))
{
limit.LowLowLimit ??=
new PropertyState.Implementation(limit);
limit.LowLowLimit.Value = lowLow;
}
return this;
}
public IAlarmBuilder MonitorVariable(NodeState source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
Alarm.SourceNode!.Value = source.NodeId;
QualifiedName srcName = source.BrowseName;
Alarm.SourceName!.Value = srcName.IsNull ? string.Empty : (srcName.Name ?? string.Empty);
return this;
}
public IAlarmBuilder OnAcknowledge(ConditionAddCommentEventHandler handler)
{
if (Alarm is not AcknowledgeableConditionState ack)
{
throw ServiceResultException.Create(
StatusCodes.BadTypeMismatch,
"OnAcknowledge requires an AcknowledgeableConditionState; alarm '{0}' is '{1}'.",
Alarm.BrowseName,
Alarm.GetType().Name);
}
ack.OnAcknowledge = handler ?? throw new ArgumentNullException(nameof(handler));
return this;
}
public IAlarmBuilder OnConfirm(ConditionAddCommentEventHandler handler)
{
if (Alarm is not AcknowledgeableConditionState ack)
{
throw ServiceResultException.Create(
StatusCodes.BadTypeMismatch,
"OnConfirm requires an AcknowledgeableConditionState; alarm '{0}' is '{1}'.",
Alarm.BrowseName,
Alarm.GetType().Name);
}
ack.OnConfirm = handler ?? throw new ArgumentNullException(nameof(handler));
return this;
}
}
}