refactor: migrate OPC UA server SDK 2.0 compilation and boundaries
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
using Opc.Ua;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer;
|
||||
|
||||
/// <summary>Copies supported SDK payloads at the server's CLR value boundary.</summary>
|
||||
internal static class SdkValueBoundary
|
||||
{
|
||||
/// <summary>Build a detached Variant for an existing supported scalar or one-dimensional array.</summary>
|
||||
internal static Variant FromClr(object? value, bool byteArray = false)
|
||||
{
|
||||
if (value is null) return Variant.Null;
|
||||
if (value is Array array)
|
||||
{
|
||||
if (array.Rank != 1) throw new ArgumentException("Only one-dimensional values are supported.", nameof(value));
|
||||
value = array.Clone();
|
||||
}
|
||||
if (byteArray && value is byte[] bytes) return Variant.From((ArrayOf<byte>)bytes);
|
||||
if (VariantHelper.TryCastFrom(value, out var variant)) return variant;
|
||||
throw new ArgumentException("Unsupported OPC UA value type.", nameof(value));
|
||||
}
|
||||
|
||||
/// <summary>Restore existing CLR payload types and detach mutable arrays.</summary>
|
||||
internal static object? ToClr(Variant value) => value.AsBoxedObject(Variant.BoxingBehavior.Legacy) switch
|
||||
{
|
||||
ByteString bytes => bytes.IsNull ? null : bytes.ToArray(),
|
||||
DateTimeUtc time => time.ToDateTime(),
|
||||
Uuid id => (Guid)id,
|
||||
DateTimeUtc[] times => times.Select(x => x.ToDateTime()).ToArray(),
|
||||
Uuid[] ids => ids.Select(x => (Guid)x).ToArray(),
|
||||
ByteString[] strings => strings.Select(x => x.IsNull ? null : x.ToArray()).ToArray(),
|
||||
Array array => array.Clone(),
|
||||
var scalar => scalar,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user