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. // --------------------------------------------------------------------------------------------------------------------- /// What an origin's condition streams offer (CapabilitiesResponse.conditions); null when not negotiated /// or not servable (no condition-key secret, no condition-state hub). /// Root-scoped condition items are accepted. /// Supported condition methods (bitmask; A2b). 0 in A2a. /// The most conditions one root-scoped item admits for this caller (the largest /// MaxConditions of its AlarmSubscribe exports); 0 = undisclosed (no grant). public sealed record DataConditionCapabilities(bool RootScopeItems, uint Methods, int MaxConditions) { public void Validate() { if (MaxConditions < 0) throw new ArgumentOutOfRangeException(nameof(MaxConditions)); } } /// A root-scoped condition item (SubscriptionItem.condition_scope); the item's view is the export root. /// Carry on first appearance and in every snapshot record. public sealed record DataConditionScope(bool IncludeInfo); /// Where an origin condition comes from (wire ConditionInfo.source_kind). public enum DataConditionSourceKind { Unspecified = 0, Native = 1, Scripted = 2 } /// Static facts about one condition, carried on first appearance in an epoch and in every snapshot record. /// The condition type, namespace-URI qualified. /// The Part 9 ConditionName. /// Native (driver-fed) or scripted. /// Realm-local placement addresses inside the listing export (§5.2), never cross-realm. /// The methods this condition supports (A2b). public sealed record DataConditionInfo(string ConditionType, string ConditionName, DataConditionSourceKind SourceKind, IReadOnlyList Placements, uint SupportedMethods); /// The origin-owned activation marker (T3): (serving incarnation, per-condition occurrence sequence). public sealed record DataActivationMarker(string Incarnation, ulong Sequence); /// One condition record of a root-scoped item (ConditionState). /// A record with = false never changes a consumer's last state (§5.1 T2). 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); /// /// A condition frame (SubscriptionFrame.condition_states, oneof arm 9) for one root-scoped item. A snapshot begins /// with and set and no records (so capacity is decided before anything is /// materialised); records follow; ends it. Later frames are delta records. /// public sealed record DataSubscriptionConditions(string SubscriptionId, long StreamEpoch, long Sequence, DataItemHandle Handle, bool IsSnapshot, int? Total, IReadOnlyList 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. // --------------------------------------------------------------------------------------------------------------------- /// /// The remote condition methods. The values are wire values: ConditionMethodRequest.method carries exactly one, /// ConditionCapabilities.methods and ConditionInfo.supported_methods a mask. Never renumber. /// [Flags] public enum DataConditionMethods : uint { None = 0, /// Part 9 Acknowledge (grant ). Acknowledge = 1, /// Part 9 AddComment (grant ). AddComment = 2, /// Part 9 Confirm (grant ; scripted conditions only). Confirm = 4, } /// A2b: the one authority mapping a condition method onto its grant (M8, mirrors AlarmOperationFor). public static class DataConditionMethodSets { /// Every method this build implements. public const DataConditionMethods All = DataConditionMethods.Acknowledge | DataConditionMethods.AddComment | DataConditionMethods.Confirm; /// What a native (driver-fed) condition supports: Confirm has no upstream path (BadNotSupported). public const DataConditionMethods Native = DataConditionMethods.Acknowledge | DataConditionMethods.AddComment; /// What a scripted condition supports. public const DataConditionMethods Scripted = All; /// The data operation a method requires. public static DataOperations OperationFor(DataConditionMethods method) => method switch { DataConditionMethods.Confirm => DataOperations.AlarmConfirm, DataConditionMethods.Acknowledge or DataConditionMethods.AddComment => DataOperations.AlarmAcknowledge, _ => DataOperations.None, }; /// The methods a grant set allows. public static DataConditionMethods AllowedBy(DataOperations operations) => ((operations & DataOperations.AlarmAcknowledge) != 0 ? DataConditionMethods.Acknowledge | DataConditionMethods.AddComment : DataConditionMethods.None) | ((operations & DataOperations.AlarmConfirm) != 0 ? DataConditionMethods.Confirm : DataConditionMethods.None); /// Whether names exactly one implemented method. public static bool IsSingle(DataConditionMethods method) => method is DataConditionMethods.Acknowledge or DataConditionMethods.AddComment or DataConditionMethods.Confirm; /// The Part 9 method name (the operation name the node manager and AlarmOperationFor use). public static string Part9Name(DataConditionMethods method) => method switch { DataConditionMethods.Acknowledge => "Acknowledge", DataConditionMethods.AddComment => "AddComment", DataConditionMethods.Confirm => "Confirm", _ => throw new ArgumentOutOfRangeException(nameof(method)), }; } /// /// One remote condition method call (ConditionMethodRequest). /// /// Caller-chosen id; a duplicate (same principal) joins the first call (§5.6 step 5). /// A realm-local placement address of the condition (from a listing), never a condition key. /// Exactly one method. /// The activation marker of the latest record the caller applied (T3). /// The Part 9 comment, or null. /// The serving incarnation the caller selected (a restarted origin refuses). /// What is left of the caller's deadline; ≤ 0 is refused before dispatch. /// Untrusted audit text: capped, sanitised, written only to the origin's audit log (§6). public sealed record DataConditionMethodRequest(string OperationId, DataView Placement, DataConditionMethods Method, DataActivationMarker ExpectedActivation, string? Comment, string ExpectedServingIncarnation, TimeSpan RemainingBudget, string? OnBehalfOf = null) { /// The most characters of the origin keeps (§6). public const int MaxOnBehalfOfLength = 128; /// The most characters of an operation id. public const int MaxOperationIdLength = 128; /// The most characters of a comment (a Part 9 LocalizedText, bounded like a value string). 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. } } /// /// The answer to one condition method call (ConditionMethodResponse). reuses the write /// outcome states: = refused before any state change (status says /// why); = applied at the origin (scripted); /// = applied and routed to the driver, whose confirmation (or revert) /// arrives on the condition stream (native); = the transport failed after /// the request was sent (never retried). /// public sealed record DataConditionMethodResult(DataWriteState State, uint StatusCode, string? Diagnostic, ServingIdentity? Serving = null) { public void Validate() => DataContractValidation.Required(State); } /// A2b: remote condition methods (CallConditionMethod), bound to the caller of the containing facade. public interface IDataConditionMethodService { /// Calls one method. Never retried: a duplicate joins. Task CallConditionMethodAsync(DataConditionMethodRequest request, CancellationToken cancellationToken); }