using System.Security.Cryptography;
namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
///
/// Bounds on age since normalized ingress into the origin cache, not sensor measurement age.
/// A null endpoint is unknown. An upper bound alone certifies freshness; a lower bound alone
/// can prove staleness. Original wall-clock timestamps are independent of these bounds.
///
public readonly record struct DataAgeBounds(TimeSpan? Lower, TimeSpan? Upper)
{
public void Validate()
{
if (Lower < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(Lower));
if (Upper < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(Upper));
if (Lower is { } lower && Upper is { } upper && upper < lower)
throw new ArgumentException("Upper age bound must not precede lower bound.");
}
public DataAgeBounds Advance(TimeSpan elapsed)
{
Validate();
if (elapsed < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(elapsed));
return new(Lower is { } lower ? TimeSpan.FromTicks(checked(lower.Ticks + elapsed.Ticks)) : null,
Upper is { } upper ? TimeSpan.FromTicks(checked(upper.Ticks + elapsed.Ticks)) : null);
}
public bool IsFresh(TimeSpan maximumAge)
{
Validate();
if (maximumAge < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(maximumAge));
return Upper is { } upper && upper <= maximumAge;
}
public bool IsStale(TimeSpan maximumAge)
{
Validate();
if (maximumAge < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(maximumAge));
return Lower is { } lower && lower > maximumAge;
}
}
public enum DataValueKind { Unspecified = 0, Null = 1, Boolean = 2, Int8 = 3, Int16 = 4, Int32 = 5, Int64 = 6, UInt8 = 7, UInt16 = 8, UInt32 = 9, UInt64 = 10, Float = 11, Double = 12, String = 13, Bytes = 14, Timestamp = 15, Guid = 16, Array = 17 }
public enum DataItemStatus { Unspecified = 0, Good = 1, Denied = 2, NotFound = 3, Unsupported = 4, InvalidArgument = 5, Unavailable = 6, WaitingForInitialData = 7, Stale = 8, LimitExceeded = 9, InvalidContinuation = 10, TypeMismatch = 11, ResourceExhausted = 12 }
public enum DataReadiness { Unspecified = 0, Ready = 1, WaitingForInitialData = 2, Unavailable = 3, Invalidated = 4 }
public enum DataWriteState { Unspecified = 0, RejectedBeforeDispatch = 1, DriverConfirmedSuccess = 2, DriverConfirmedFailure = 3, AcceptedUnconfirmed = 4, OutcomeUnknown = 5 }
public enum DataChangeTrigger { Unspecified = 0, Status = 1, StatusValue = 2, StatusValueTimestamp = 3 }
public enum DataStreamControlKind { Unspecified = 0, SnapshotBegin = 1, SnapshotEnd = 2, CatalogInvalidated = 3, AuthorityInvalidated = 4, Heartbeat = 5, Reset = 6, Coalesced = 7 }
///
/// Owned immutable value. Factory methods copy mutable inputs; Retain creates an independently
/// disposable lease. Consumers must dispose every lease after asynchronous use completes.
///
public sealed class DataValue : IDisposable
{
private readonly object? _payload;
private bool _disposed;
private DataValue(DataValueKind kind, object? payload, IReadOnlyList? dimensions = null)
{ Kind = kind; _payload = payload; Dimensions = dimensions; }
public DataValueKind Kind { get; }
public IReadOnlyList? Dimensions { get; }
/// A conservative, allocation-free estimate of retained value storage, in bytes.
public long EstimatedRetainedBytes
{
get
{
ThrowIfDisposed();
checked
{
const long baseline = 256;
if (_payload is byte[] bytes) return baseline + 32L + bytes.LongLength;
if (_payload is string text) return baseline + 32L + 2L * text.Length;
if (_payload is ArrayPayload array)
{
long size = baseline + 128L + 8L * (Dimensions?.Count ?? 0) + 48L * array.Elements.LongLength;
if (array.Kind == DataValueKind.String)
foreach (var element in array.Elements) size += 2L * ((string)element).Length;
return size;
}
return baseline + 32L;
}
}
}
public static DataValue Null() => new(DataValueKind.Null, null);
public static DataValue FromBytes(byte[] bytes) => new(DataValueKind.Bytes, (byte[])bytes.Clone());
public static DataValue FromScalar(DataValueKind kind, object value)
{
ArgumentNullException.ThrowIfNull(value);
var valid = kind switch
{
DataValueKind.Boolean => value is bool, DataValueKind.Int8 => value is sbyte,
DataValueKind.Int16 => value is short, DataValueKind.Int32 => value is int,
DataValueKind.Int64 => value is long, DataValueKind.UInt8 => value is byte,
DataValueKind.UInt16 => value is ushort, DataValueKind.UInt32 => value is uint,
DataValueKind.UInt64 => value is ulong, DataValueKind.Float => value is float,
DataValueKind.Double => value is double, DataValueKind.String => value is string,
DataValueKind.Timestamp => value is DateTimeOffset, DataValueKind.Guid => value is Guid,
_ => false
};
if (!valid) throw new ArgumentException("Unsupported value or kind mismatch.", nameof(value));
if (value is DateTimeOffset timestamp && timestamp.Offset != TimeSpan.Zero)
throw new ArgumentException("Timestamp must be UTC.", nameof(value));
return new(kind, value);
}
/// Copies elements and shape; arrays contain one declared scalar kind, including empty arrays.
public static DataValue FromArray(DataValueKind elementKind, IReadOnlyList