using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.Sql.Common;
/// A stored sample as read back: source time, typed value and stored status.
/// Source timestamp (UTC).
/// Typed value ( for an outage marker).
/// Stored OPC UA status code.
public readonly record struct StoredSample(DateTime TimeUtc, HistorianValue Value, uint Status)
{
/// Severity Good or Uncertain, and a value present (an Empty sample is an outage marker).
public bool IsUsable => !StatusBits.IsBad(Status) && Value.Kind != HistorianValueKind.Empty;
/// Usable with severity Good.
public bool IsGood => IsUsable && StatusBits.IsGood(Status);
///
/// Builds a sample from stored columns, applying the quality clamp: an Empty (no-value) row whose stored status
/// is not Bad is served as BadNoData — defence in depth for rows written before the writer forced every
/// Empty sample Bad.
///
/// Source timestamp (UTC).
/// Stored value.
/// Stored status code.
/// The sample, clamped.
public static StoredSample FromStored(DateTime timeUtc, HistorianValue value, uint status)
{
if (value.Kind == HistorianValueKind.Empty && !StatusBits.IsBad(status))
status = HistorianStatusCodes.BadNoData;
return new StoredSample(timeUtc, value, status);
}
}