diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/IDataServices.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/IDataServices.cs index 8bef99472..ebaf564a4 100644 --- a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/IDataServices.cs +++ b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/IDataServices.cs @@ -186,3 +186,86 @@ public interface IDataSubscriptionService /// Stream lifetime is independent of unary deadlines; cancellation ends the stream. Task SubscribeAsync(CancellationToken cancellationToken); } + +// --------------------------------------------------------------------------------------------------------------------- +// Remote history (gRPC data history plan §2.1, §3.1–§3.3). Declared by H1; the origin implementation over the node +// manager's override-level history port and the wire arrive with H2. SDK-free by construction: aggregates are their +// standard ns=0 numeric ids, statuses the full OPC UA uint32, an unset Part 11 bound is ABSENT (null), never MinValue — +// the origin's end-only / open-end rules depend on the difference. +// --------------------------------------------------------------------------------------------------------------------- + +/// The variable-history request shape. HistoryUpdate and event history are not served remotely. +public abstract record DataHistoryDetails; + +/// Part 11 ReadRawModifiedDetails without the modified flag (a remote raw read is never a modified read). +/// StartTime; null = unset. +/// EndTime; null = unset. +/// Per-page cap; 0 = all (the origin still pages at its ceiling). +/// Part 11 bounding values. +public sealed record DataHistoryRawDetails(DateTimeOffset? StartUtc, DateTimeOffset? EndUtc, uint NumValuesPerNode, bool ReturnBounds) + : DataHistoryDetails; + +/// The Part 13 aggregate configuration a processed read asks for. +public sealed record DataAggregateConfiguration( + bool UseServerCapabilitiesDefaults, bool TreatUncertainAsBad, byte PercentDataBad, byte PercentDataGood, bool UseSlopedExtrapolation); + +/// Part 11 ReadProcessedDetails. The aggregate is per item (). +/// StartTime; null = unset. +/// EndTime; null = unset. +/// The interval. +/// The configuration; null = server defaults. +public sealed record DataHistoryProcessedDetails( + DateTimeOffset? StartUtc, DateTimeOffset? EndUtc, TimeSpan ProcessingInterval, DataAggregateConfiguration? Configuration) + : DataHistoryDetails; + +/// Part 11 ReadAtTimeDetails. +/// The requested times, in order. +public sealed record DataHistoryAtTimeDetails(IReadOnlyList RequestedTimesUtc) : DataHistoryDetails; + +/// One node of a remote history read. +/// Echoed on the result. +/// The exported view being read. +/// Processed only: the standard aggregate function's ns=0 numeric NodeId. +/// The origin's opaque continuation token from a previous page; null = a fresh read. +public sealed record DataHistoryReadItem(string CorrelationId, DataView View, uint? AggregateId = null, byte[]? Continuation = null); + +/// A batch of nodes read with one set of details. +public sealed record DataHistoryReadRequest(DataHistoryDetails Details, IReadOnlyList Items, DataBudget Budget); + +/// +/// One returned history value, verbatim from the origin's Part 11 answer: the full status (Calculated / Interpolated / +/// Partial bits included) and timestamps that stay absent when absent. Owns . +/// +public sealed record DataHistorySample(DataValue Value, uint StatusCode, DateTimeOffset? SourceTimestampUtc, DateTimeOffset? ServerTimestampUtc) + : IDisposable +{ + public void Dispose() => Value.Dispose(); +} + +/// +/// One node's answer: the Part 11 operation status (full uint32 — BadContinuationPointInvalid, +/// BadAggregateNotSupported, GoodNoData …), its samples and the origin's next continuation token (null +/// when the read is complete). Owns its samples. +/// +public sealed record DataHistoryReadResult(string CorrelationId, uint StatusCode, IReadOnlyList Samples, byte[]? Continuation) + : IDisposable +{ + public void Dispose() + { + foreach (var sample in Samples) sample.Dispose(); + } +} + +/// +/// Remote HistoryRead (Raw / Processed / AtTime) served by the origin's own historian pipeline. Operations use the +/// authenticated caller bound to the containing connection facade; continuations are bound to that caller, the serving +/// incarnation, the history epoch, the view and the request details, and are single-use. +/// +public interface IDataHistoryService +{ + /// Reads every item, returning results in request order. + Task> ReadAsync(DataHistoryReadRequest request, CancellationToken cancellationToken); + + /// Releases continuation tokens without reading (Part 11 releaseContinuationPoints); best effort, idempotent. + Task ReleaseAsync(IReadOnlyList continuations, CancellationToken cancellationToken); +} diff --git a/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests/DataConnections/GrpcDataHistoryContractTests.cs b/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests/DataConnections/GrpcDataHistoryContractTests.cs new file mode 100644 index 000000000..bdc86bbaa --- /dev/null +++ b/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests/DataConnections/GrpcDataHistoryContractTests.cs @@ -0,0 +1,59 @@ +using Shouldly; +using Xunit; +using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; + +namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests.DataConnections; + +/// H1: the SDK-free remote-history contract H2 implements on the origin side. +[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "H1")] +public sealed class GrpcDataHistoryContractTests +{ + [Fact] + public void The_contract_lives_in_the_data_connection_namespace_and_names_no_sdk_type() + { + var types = new[] + { + typeof(IDataHistoryService), typeof(DataHistoryDetails), typeof(DataHistoryRawDetails), typeof(DataHistoryProcessedDetails), + typeof(DataHistoryAtTimeDetails), typeof(DataAggregateConfiguration), typeof(DataHistoryReadItem), + typeof(DataHistoryReadRequest), typeof(DataHistorySample), typeof(DataHistoryReadResult), + }; + foreach (var type in types) + { + type.Namespace.ShouldBe("ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections"); + type.IsPublic.ShouldBeTrue(type.Name); + var referenced = type.GetProperties().Select(p => p.PropertyType) + .Concat(type.GetMethods().SelectMany(m => m.GetParameters().Select(p => p.ParameterType).Append(m.ReturnType))); + referenced.ShouldAllBe(t => t.Namespace == null || !t.Namespace.StartsWith("Opc.", StringComparison.Ordinal), type.Name); + } + + typeof(DataHistoryDetails).IsAbstract.ShouldBeTrue(); + typeof(DataHistoryRawDetails).BaseType.ShouldBe(typeof(DataHistoryDetails)); + typeof(DataHistoryProcessedDetails).BaseType.ShouldBe(typeof(DataHistoryDetails)); + typeof(DataHistoryAtTimeDetails).BaseType.ShouldBe(typeof(DataHistoryDetails)); + } + + [Fact] + public void Unset_bounds_are_absent_not_MinValue_and_statuses_are_full_uint32() + { + // §3.3: the origin's end-only / open-end rules depend on "absent", so the contract cannot spell it MinValue. + typeof(DataHistoryRawDetails).GetProperty(nameof(DataHistoryRawDetails.StartUtc))!.PropertyType.ShouldBe(typeof(DateTimeOffset?)); + typeof(DataHistoryRawDetails).GetProperty(nameof(DataHistoryRawDetails.EndUtc))!.PropertyType.ShouldBe(typeof(DateTimeOffset?)); + typeof(DataHistoryProcessedDetails).GetProperty(nameof(DataHistoryProcessedDetails.StartUtc))!.PropertyType.ShouldBe(typeof(DateTimeOffset?)); + typeof(DataHistorySample).GetProperty(nameof(DataHistorySample.SourceTimestampUtc))!.PropertyType.ShouldBe(typeof(DateTimeOffset?)); + typeof(DataHistoryReadResult).GetProperty(nameof(DataHistoryReadResult.StatusCode))!.PropertyType.ShouldBe(typeof(uint)); + typeof(DataHistorySample).GetProperty(nameof(DataHistorySample.StatusCode))!.PropertyType.ShouldBe(typeof(uint)); + + var endOnly = new DataHistoryRawDetails(null, DateTimeOffset.UnixEpoch, 5, ReturnBounds: false); + endOnly.StartUtc.ShouldBeNull(); + } + + [Fact] + public void A_result_owns_and_disposes_its_samples() + { + var value = DataValue.FromScalar(DataValueKind.Double, 1.5); + var result = new DataHistoryReadResult("c1", 0x00A50000u, + [new DataHistorySample(value, 0x00000000u, DateTimeOffset.UnixEpoch, null)], Continuation: null); + result.Dispose(); + Should.Throw(() => value.GetScalar()); + } +}