namespace ZB.MOM.WW.OtOpcUa.Driver.TwinCAT;
///
/// Converts the IEC 61131-3 temporal types between their 32-bit unsigned (UDINT) wire counters and the
/// values the driver publishes. TIME and TOD surface as a of
/// milliseconds (OPC UA Double; no new DriverDataType member — see
/// docs/drivers/TwinCAT.md); DATE and DT surface as a UTC .
///
internal static class TwinCATTemporalCodec
{
private static readonly DateTime Epoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
/// True for TIME / DATE / DT / TOD.
/// The data type to test.
/// when is a temporal type.
public static bool IsTemporal(TwinCATDataType type) =>
type is TwinCATDataType.Time or TwinCATDataType.Date
or TwinCATDataType.DateTime or TwinCATDataType.TimeOfDay;
/// Decodes a raw UDINT counter into the published value for .
/// The temporal data type.
/// The raw UDINT counter read from the PLC.
/// A of milliseconds for TIME/TOD, or a UTC for DATE/DT.
public static object Decode(TwinCATDataType type, uint raw) => type switch
{
TwinCATDataType.Time or TwinCATDataType.TimeOfDay => (double)raw,
TwinCATDataType.Date or TwinCATDataType.DateTime => Epoch.AddSeconds(raw),
_ => throw new ArgumentOutOfRangeException(nameof(type), type, "not a temporal type"),
};
///
/// Encodes a published/written value into the UDINT counter. Accepts the native shape
/// ( ms / / ) and any other
/// numeric as the raw counter, so a client writing an integer keeps working.
///
/// The temporal data type.
/// The value to encode.
/// The UDINT counter to write to the PLC.
/// Out of the UDINT range, negative, or NaN/infinite.
/// A value of no accepted shape.
public static uint Encode(TwinCATDataType type, object? value)
{
switch (type)
{
case TwinCATDataType.Time or TwinCATDataType.TimeOfDay:
var ms = value switch
{
TimeSpan ts => ToUInt(ts.TotalMilliseconds),
DateTime => throw new InvalidCastException($"{type} takes a duration in milliseconds, not a DateTime"),
double d => ToUInt(d),
float f => ToUInt(f),
_ => Convert.ToUInt32(value),
};
// TOD is a time of day: a full day or more is not representable.
if (type == TwinCATDataType.TimeOfDay && ms >= 86_400_000u)
throw new OverflowException($"{ms} ms is not a time of day (< 86 400 000)");
return ms;
case TwinCATDataType.Date or TwinCATDataType.DateTime:
if (value is DateTime dt)
{
var utc = dt.Kind == DateTimeKind.Local ? dt.ToUniversalTime() : dt;
var secs = ToUInt(Math.Floor((utc - Epoch).TotalSeconds));
// DATE is a whole-day counter.
return type == TwinCATDataType.Date ? secs - secs % 86400u : secs;
}
if (value is TimeSpan) throw new InvalidCastException($"{type} takes a DateTime, not a duration");
return value switch
{
double d => ToUInt(d),
float f => ToUInt(f),
_ => Convert.ToUInt32(value),
};
default:
throw new ArgumentOutOfRangeException(nameof(type), type, "not a temporal type");
}
}
private static uint ToUInt(double v)
{
if (double.IsNaN(v) || double.IsInfinity(v) || v < 0 || v > uint.MaxValue)
throw new OverflowException($"{v} is outside the 32-bit unsigned range of an IEC temporal value");
return (uint)Math.Round(v, MidpointRounding.AwayFromZero);
}
}