174 lines
11 KiB
C#
174 lines
11 KiB
C#
namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
// A2 condition streaming (grpc-data history/alarms plan §4.2, §5.5). Root-scoped condition items on Subscribe: one item
|
|
// per mapping = "all conditions under this export root". Negotiated per call through x-otopcua-data-features
|
|
// `conditions`; a call without it never sees any of these shapes.
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/// <summary>What an origin's condition streams offer (<c>CapabilitiesResponse.conditions</c>); null when not negotiated
|
|
/// or not servable (no condition-key secret, no condition-state hub).</summary>
|
|
/// <param name="RootScopeItems">Root-scoped condition items are accepted.</param>
|
|
/// <param name="Methods">Supported condition methods (bitmask; A2b). 0 in A2a.</param>
|
|
/// <param name="MaxConditions">The most conditions one root-scoped item admits for this caller (the largest
|
|
/// <c>MaxConditions</c> of its AlarmSubscribe exports); 0 = undisclosed (no grant).</param>
|
|
public sealed record DataConditionCapabilities(bool RootScopeItems, uint Methods, int MaxConditions)
|
|
{
|
|
public void Validate()
|
|
{
|
|
if (MaxConditions < 0) throw new ArgumentOutOfRangeException(nameof(MaxConditions));
|
|
}
|
|
}
|
|
|
|
/// <summary>A root-scoped condition item (<c>SubscriptionItem.condition_scope</c>); the item's view is the export root.</summary>
|
|
/// <param name="IncludeInfo">Carry <see cref="DataConditionInfo"/> on first appearance and in every snapshot record.</param>
|
|
public sealed record DataConditionScope(bool IncludeInfo);
|
|
|
|
/// <summary>Where an origin condition comes from (wire <c>ConditionInfo.source_kind</c>).</summary>
|
|
public enum DataConditionSourceKind { Unspecified = 0, Native = 1, Scripted = 2 }
|
|
|
|
/// <summary>Static facts about one condition, carried on first appearance in an epoch and in every snapshot record.</summary>
|
|
/// <param name="ConditionType">The condition type, namespace-URI qualified.</param>
|
|
/// <param name="ConditionName">The Part 9 ConditionName.</param>
|
|
/// <param name="SourceKind">Native (driver-fed) or scripted.</param>
|
|
/// <param name="Placements">Realm-local placement addresses inside the listing export (§5.2), never cross-realm.</param>
|
|
/// <param name="SupportedMethods">The methods this condition supports (A2b).</param>
|
|
public sealed record DataConditionInfo(string ConditionType, string ConditionName, DataConditionSourceKind SourceKind,
|
|
IReadOnlyList<DataView> Placements, uint SupportedMethods);
|
|
|
|
/// <summary>The origin-owned activation marker (T3): (serving incarnation, per-condition occurrence sequence).</summary>
|
|
public sealed record DataActivationMarker(string Incarnation, ulong Sequence);
|
|
|
|
/// <summary>One condition record of a root-scoped item (<c>ConditionState</c>).</summary>
|
|
/// <remarks>A record with <see cref="StateKnown"/> = false never changes a consumer's last state (§5.1 T2).</remarks>
|
|
public sealed record DataConditionState(string ConditionKey, long Version, bool Removed, bool StateKnown, DataConditionInfo? Info,
|
|
bool Active, bool Acknowledged, bool Confirmed, bool Enabled, int Shelving, uint Severity, string Message, uint Quality,
|
|
DateTimeOffset? UnshelveAtUtc, int ConditionClass, DateTimeOffset? TimeUtc, bool Retain, DataActivationMarker? Activation,
|
|
string? Comment = null, string? ClientUserId = null);
|
|
|
|
/// <summary>
|
|
/// A condition frame (<c>SubscriptionFrame.condition_states</c>, oneof arm 9) for one root-scoped item. A snapshot begins
|
|
/// with <see cref="IsSnapshot"/> and <see cref="Total"/> set and no records (so capacity is decided before anything is
|
|
/// materialised); records follow; <see cref="SnapshotComplete"/> ends it. Later frames are delta records.
|
|
/// </summary>
|
|
public sealed record DataSubscriptionConditions(string SubscriptionId, long StreamEpoch, long Sequence, DataItemHandle Handle,
|
|
bool IsSnapshot, int? Total, IReadOnlyList<DataConditionState> Items, bool SnapshotComplete)
|
|
: DataSubscriptionFrame(SubscriptionId, StreamEpoch, Sequence);
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
// A2b condition methods (plan §4.2 CallConditionMethod, §5.6). Header-gated by the same `conditions` feature: a call
|
|
// without it is refused FAILED_PRECONDITION and never reaches the origin.
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/// <summary>
|
|
/// The remote condition methods. The values are wire values: <c>ConditionMethodRequest.method</c> carries exactly one,
|
|
/// <c>ConditionCapabilities.methods</c> and <c>ConditionInfo.supported_methods</c> a mask. Never renumber.
|
|
/// </summary>
|
|
[Flags]
|
|
public enum DataConditionMethods : uint
|
|
{
|
|
None = 0,
|
|
/// <summary>Part 9 Acknowledge (grant <see cref="DataOperations.AlarmAcknowledge"/>).</summary>
|
|
Acknowledge = 1,
|
|
/// <summary>Part 9 AddComment (grant <see cref="DataOperations.AlarmAcknowledge"/>).</summary>
|
|
AddComment = 2,
|
|
/// <summary>Part 9 Confirm (grant <see cref="DataOperations.AlarmConfirm"/>; scripted conditions only).</summary>
|
|
Confirm = 4,
|
|
}
|
|
|
|
/// <summary>A2b: the one authority mapping a condition method onto its grant (M8, mirrors <c>AlarmOperationFor</c>).</summary>
|
|
public static class DataConditionMethodSets
|
|
{
|
|
/// <summary>Every method this build implements.</summary>
|
|
public const DataConditionMethods All = DataConditionMethods.Acknowledge | DataConditionMethods.AddComment | DataConditionMethods.Confirm;
|
|
|
|
/// <summary>What a native (driver-fed) condition supports: Confirm has no upstream path (<c>BadNotSupported</c>).</summary>
|
|
public const DataConditionMethods Native = DataConditionMethods.Acknowledge | DataConditionMethods.AddComment;
|
|
|
|
/// <summary>What a scripted condition supports.</summary>
|
|
public const DataConditionMethods Scripted = All;
|
|
|
|
/// <summary>The data operation a method requires.</summary>
|
|
public static DataOperations OperationFor(DataConditionMethods method) => method switch
|
|
{
|
|
DataConditionMethods.Confirm => DataOperations.AlarmConfirm,
|
|
DataConditionMethods.Acknowledge or DataConditionMethods.AddComment => DataOperations.AlarmAcknowledge,
|
|
_ => DataOperations.None,
|
|
};
|
|
|
|
/// <summary>The methods a grant set allows.</summary>
|
|
public static DataConditionMethods AllowedBy(DataOperations operations) =>
|
|
((operations & DataOperations.AlarmAcknowledge) != 0 ? DataConditionMethods.Acknowledge | DataConditionMethods.AddComment : DataConditionMethods.None)
|
|
| ((operations & DataOperations.AlarmConfirm) != 0 ? DataConditionMethods.Confirm : DataConditionMethods.None);
|
|
|
|
/// <summary>Whether <paramref name="method"/> names exactly one implemented method.</summary>
|
|
public static bool IsSingle(DataConditionMethods method) =>
|
|
method is DataConditionMethods.Acknowledge or DataConditionMethods.AddComment or DataConditionMethods.Confirm;
|
|
|
|
/// <summary>The Part 9 method name (the operation name the node manager and <c>AlarmOperationFor</c> use).</summary>
|
|
public static string Part9Name(DataConditionMethods method) => method switch
|
|
{
|
|
DataConditionMethods.Acknowledge => "Acknowledge",
|
|
DataConditionMethods.AddComment => "AddComment",
|
|
DataConditionMethods.Confirm => "Confirm",
|
|
_ => throw new ArgumentOutOfRangeException(nameof(method)),
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// One remote condition method call (<c>ConditionMethodRequest</c>).
|
|
/// </summary>
|
|
/// <param name="OperationId">Caller-chosen id; a duplicate (same principal) joins the first call (§5.6 step 5).</param>
|
|
/// <param name="Placement">A realm-local placement address of the condition (from a listing), never a condition key.</param>
|
|
/// <param name="Method">Exactly one method.</param>
|
|
/// <param name="ExpectedActivation">The activation marker of the latest record the caller applied (T3).</param>
|
|
/// <param name="Comment">The Part 9 comment, or null.</param>
|
|
/// <param name="ExpectedServingIncarnation">The serving incarnation the caller selected (a restarted origin refuses).</param>
|
|
/// <param name="RemainingBudget">What is left of the caller's deadline; ≤ 0 is refused before dispatch.</param>
|
|
/// <param name="OnBehalfOf">Untrusted audit text: capped, sanitised, written only to the origin's audit log (§6).</param>
|
|
public sealed record DataConditionMethodRequest(string OperationId, DataView Placement, DataConditionMethods Method,
|
|
DataActivationMarker ExpectedActivation, string? Comment, string ExpectedServingIncarnation, TimeSpan RemainingBudget,
|
|
string? OnBehalfOf = null)
|
|
{
|
|
/// <summary>The most characters of <see cref="OnBehalfOf"/> the origin keeps (§6).</summary>
|
|
public const int MaxOnBehalfOfLength = 128;
|
|
/// <summary>The most characters of an operation id.</summary>
|
|
public const int MaxOperationIdLength = 128;
|
|
/// <summary>The most characters of a comment (a Part 9 LocalizedText, bounded like a value string).</summary>
|
|
public const int MaxCommentLength = 4096;
|
|
|
|
public void Validate()
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(OperationId);
|
|
if (OperationId.Length > MaxOperationIdLength) throw new ArgumentException("Operation id too long.", nameof(OperationId));
|
|
ArgumentNullException.ThrowIfNull(Placement);
|
|
Placement.Validate();
|
|
if (!DataConditionMethodSets.IsSingle(Method)) throw new ArgumentException("Exactly one known method.", nameof(Method));
|
|
ArgumentNullException.ThrowIfNull(ExpectedActivation);
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(ExpectedActivation.Incarnation);
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(ExpectedServingIncarnation);
|
|
if (Comment is { Length: > MaxCommentLength }) throw new ArgumentException("Comment too long.", nameof(Comment));
|
|
// OnBehalfOf is untrusted free text: never refused for its content, only capped and sanitised by the origin.
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The answer to one condition method call (<c>ConditionMethodResponse</c>). <see cref="State"/> reuses the write
|
|
/// outcome states: <see cref="DataWriteState.RejectedBeforeDispatch"/> = refused before any state change (status says
|
|
/// why); <see cref="DataWriteState.DriverConfirmedSuccess"/> = applied at the origin (scripted);
|
|
/// <see cref="DataWriteState.AcceptedUnconfirmed"/> = applied and routed to the driver, whose confirmation (or revert)
|
|
/// arrives on the condition stream (native); <see cref="DataWriteState.OutcomeUnknown"/> = the transport failed after
|
|
/// the request was sent (never retried).
|
|
/// </summary>
|
|
public sealed record DataConditionMethodResult(DataWriteState State, uint StatusCode, string? Diagnostic, ServingIdentity? Serving = null)
|
|
{
|
|
public void Validate() => DataContractValidation.Required(State);
|
|
}
|
|
|
|
/// <summary>A2b: remote condition methods (<c>CallConditionMethod</c>), bound to the caller of the containing facade.</summary>
|
|
public interface IDataConditionMethodService
|
|
{
|
|
/// <summary>Calls one method. Never retried: a duplicate <see cref="DataConditionMethodRequest.OperationId"/> joins.</summary>
|
|
Task<DataConditionMethodResult> CallConditionMethodAsync(DataConditionMethodRequest request, CancellationToken cancellationToken);
|
|
}
|