Sign in

    moonua

    Pure MoonBit OPC UA Binary Encoding primitives and data-model codecs.

    opc-ua
    binary-encoding
    node-id
    industrial
    iot
    protocol
    Download zip
    Version
    0.3.0
    License
    Apache-2.0
    Last updated
    9 hours ago
    Downloads
    21

    No dependencies

    #MoonUA

    MoonUA 是一个纯 MoonBit 的 OPC UA Binary Encoding 基础库,面向工业设备、边缘网关、协议测试工具和数字孪生应用。

    #当前功能

    • 小端序 Boolean、SByte、Byte、Int16、UInt16
    • Int32、UInt32、Int64、UInt64、Float、Double
    • OPC UA String 和 ByteString,包含 null、长度和 UTF-8 检查
    • NodeId 的 TwoByte、FourByte、Numeric、String、Guid、Opaque 编码
    • ExpandedNodeId 的 NamespaceUri 和 ServerIndex 标志
    • QualifiedName 的 namespace index 和名称编码
    • LocalizedText 的 locale/text 掩码组合
    • DateTime、StatusCode 和带可选字段的 DataValue
    • 标量 Variant、ExtensionObject 和一维/多维 Variant 数组
    • 类型化错误、边界检查、已知字节向量测试和可运行示例

    #快速开始

    import {
    "oilleelssq-wq/moonua" @moonua,
    }

    let writer = @moonua.UaWriter::new()
    writer.write_node_id(@moonua.NodeId::numeric(1, 1000))
    writer.write_qualified_name(@moonua.QualifiedName::new(1, "Device"))
    let bytes = writer.to_bytes()

    当前范围不包含网络传输、SecureChannel、Session 或递归内建类型。这些能力会在后续阶段逐步加入。

    #验证

    moon fmt --check moon check --deny-warn moon test --deny-warn moon test --target wasm moon info moon run examples/nodeid_demo --target wasm-gc

    #许可

    Apache License 2.0。

    UaError

    pub(all) suberror UaError {
    Truncated(Int, Int)
    InvalidLength(Int)
    LimitExceeded(Int, Int)
    InvalidUtf8
    InvalidNodeIdEncoding(UInt)
    InvalidGuidLength(Int)
    InvalidValue(String)
    UnsupportedType(UInt)
    InvalidMask(String, UInt)
    InvalidExtensionObjectEncoding(UInt)
    }

    Errors raised while decoding OPC UA Binary Encoding data.

    UaError::message

    fn UaError::message(self : UaError) -> String

    Human-readable error message.

    DataValue

    pub(all) struct DataValue {
    value : VariantValue?
    status : StatusCode?
    source_timestamp : UaDateTime?
    server_timestamp : UaDateTime?
    source_picoseconds : UInt16?
    server_picoseconds : UInt16?
    }

    OPC UA DataValue with optional value, status, timestamps, and picoseconds.

    DataValue::empty

    fn DataValue::empty() -> DataValue

    Construct an empty DataValue.

    DataValue::is_empty

    fn DataValue::is_empty(self : DataValue) -> Bool

    Whether the DataValue has no optional fields.

    DataValue::of

    fn DataValue::of(value : VariantValue) -> DataValue

    Construct a DataValue carrying only a Variant value.

    ExpandedNodeId

    pub(all) struct ExpandedNodeId {
    node_id : NodeId
    namespace_uri : String?
    server_index : UInt?
    }

    OPC UA ExpandedNodeId with optional NamespaceUri and ServerIndex.

    ExpandedNodeId::new

    fn ExpandedNodeId::new(node_id : NodeId) -> ExpandedNodeId

    Construct an ExpandedNodeId without a URI or server index.

    ExpandedNodeId::to_string

    fn ExpandedNodeId::to_string(self : ExpandedNodeId) -> String

    Render an ExpandedNodeId for diagnostics.

    ExpandedNodeId::with_namespace_uri

    fn ExpandedNodeId::with_namespace_uri(self : ExpandedNodeId, namespace_uri : String) -> ExpandedNodeId

    Add a NamespaceUri.

    ExpandedNodeId::with_server_index

    fn ExpandedNodeId::with_server_index(self : ExpandedNodeId, server_index : UInt) -> ExpandedNodeId

    Add a ServerIndex.

    ExtensionObject

    pub(all) struct ExtensionObject {
    type_id : NodeId
    body : ExtensionObjectBody
    }

    OPC UA ExtensionObject.

    ExtensionObject::binary

    fn ExtensionObject::binary(type_id : NodeId, body : Bytes) -> ExtensionObject

    Construct an ExtensionObject with a binary body.

    ExtensionObject::empty

    fn ExtensionObject::empty(type_id : NodeId) -> ExtensionObject

    Construct an empty ExtensionObject.

    ExtensionObject::xml

    fn ExtensionObject::xml(type_id : NodeId, body : String) -> ExtensionObject

    Construct an ExtensionObject with an XML body.

    ExtensionObjectBody

    pub(all) enum ExtensionObjectBody {
    NoBody
    ByteString(Bytes)
    XmlElement(String)
    }

    The encoded body of an OPC UA ExtensionObject.

    LocalizedText

    pub(all) struct LocalizedText {
    locale : String?
    text : String?
    }

    An OPC UA LocalizedText.

    LocalizedText::new

    fn LocalizedText::new(locale : String?, text : String?) -> LocalizedText

    Construct a LocalizedText.

    LocalizedText::text_only

    fn LocalizedText::text_only(text : String) -> LocalizedText

    Construct a text-only LocalizedText.

    LocalizedText::to_string

    fn LocalizedText::to_string(self : LocalizedText) -> String

    Render a LocalizedText for diagnostics.

    NodeId

    pub(all) struct NodeId {
    namespace_index : UInt16
    identifier : NodeIdIdentifier
    }

    An OPC UA NodeId.

    NodeId::bytes

    fn NodeId::bytes(namespace_index : UInt16, identifier : Bytes) -> NodeId

    Construct an opaque NodeId.

    NodeId::guid

    fn NodeId::guid(namespace_index : UInt16, identifier : Bytes) -> NodeId

    Construct a Guid NodeId.

    NodeId::numeric

    fn NodeId::numeric(namespace_index : UInt16, identifier : UInt) -> NodeId

    Construct a numeric NodeId.

    NodeId::string

    fn NodeId::string(namespace_index : UInt16, identifier : String) -> NodeId

    Construct a string NodeId.

    NodeId::to_string

    fn NodeId::to_string(self : NodeId) -> String

    Render a NodeId for diagnostics.

    NodeIdIdentifier

    pub(all) enum NodeIdIdentifier {
    Numeric(UInt)
    String(String)
    Guid(Bytes)
    Opaque(Bytes)
    }

    The identifier portion of an OPC UA NodeId.

    QualifiedName

    pub(all) struct QualifiedName {
    namespace_index : UInt16
    name : String
    }

    An OPC UA QualifiedName: a namespace index plus a name.

    QualifiedName::new

    fn QualifiedName::new(namespace_index : UInt16, name : String) -> QualifiedName

    Construct a QualifiedName.

    QualifiedName::to_string

    fn QualifiedName::to_string(self : QualifiedName) -> String

    Render a QualifiedName for diagnostics.

    StatusCode

    pub(all) struct StatusCode {
    value : UInt
    }

    OPC UA StatusCode.

    StatusCode::is_good

    fn StatusCode::is_good(self : StatusCode) -> Bool

    Whether the status severity is Good.

    StatusCode::new

    fn StatusCode::new(value : UInt) -> StatusCode

    Construct a StatusCode from its raw wire value.

    StatusCode::raw

    fn StatusCode::raw(self : StatusCode) -> UInt

    Raw wire value.

    StatusCode::severity

    fn StatusCode::severity(self : StatusCode) -> StatusSeverity

    Decode the severity from the top two bits.

    StatusCode::severity_name

    fn StatusCode::severity_name(self : StatusCode) -> String

    Human-readable status severity.

    StatusSeverity

    pub(all) enum StatusSeverity {
    Good
    Uncertain
    Bad
    }

    OPC UA StatusCode severity is encoded in the two most significant bits.

    UaDateTime

    pub(all) struct UaDateTime {
    ticks : Int64
    }

    OPC UA DateTime stored as 100-nanosecond intervals since 1601-01-01 UTC.

    UaDateTime::from_ticks

    fn UaDateTime::from_ticks(ticks : Int64) -> UaDateTime

    Construct a DateTime from raw OPC UA ticks.

    UaDateTime::from_unix_ms

    fn UaDateTime::from_unix_ms(unix_ms : Int64) -> UaDateTime raise UaError

    Convert Unix milliseconds to OPC UA ticks.

    UaDateTime::to_unix_ms

    fn UaDateTime::to_unix_ms(self : UaDateTime) -> Int64

    Convert OPC UA ticks to Unix milliseconds, truncating sub-millisecond values.

    UaLimits

    pub(all) struct UaLimits {
    max_byte_length : Int
    max_array_length : Int
    }

    Decoder resource limits.

    UaLimits::default

    fn UaLimits::default() -> UaLimits

    Conservative defaults for untrusted binary input.

    UaReader

    pub(all) struct UaReader {
    data : BytesView
    offset : Int
    limits : UaLimits
    }

    A bounds-checked reader for OPC UA Binary Encoding.

    UaReader::from_bytes

    fn UaReader::from_bytes(data : Bytes) -> UaReader

    Create a reader from owned bytes.

    UaReader::new

    fn UaReader::new(data : BytesView) -> UaReader

    Create a reader with default limits.

    UaReader::position

    fn UaReader::position(self : UaReader) -> Int

    Current byte offset.

    UaReader::read_bool

    fn UaReader::read_bool(self : UaReader) -> Bool raise UaError

    Read a Boolean. OPC UA accepts zero and one only.

    UaReader::read_bytes

    fn UaReader::read_bytes(self : UaReader) -> Bytes? raise UaError

    Read a nullable ByteString.

    UaReader::read_data_value

    fn UaReader::read_data_value(self : UaReader) -> DataValue raise UaError

    Decode a DataValue and validate its optional-field mask.

    UaReader::read_datetime

    fn UaReader::read_datetime(self : UaReader) -> UaDateTime raise UaError

    Decode a DateTime.

    UaReader::read_double

    fn UaReader::read_double(self : UaReader) -> Double raise UaError

    Read a little-endian Double.

    UaReader::read_expanded_node_id

    fn UaReader::read_expanded_node_id(self : UaReader) -> ExpandedNodeId raise UaError

    Decode an ExpandedNodeId.

    UaReader::read_extension_object

    fn UaReader::read_extension_object(self : UaReader) -> ExtensionObject raise UaError

    Decode an ExtensionObject.

    UaReader::read_float

    fn UaReader::read_float(self : UaReader) -> Float raise UaError

    Read a little-endian Float.

    UaReader::read_int16

    fn UaReader::read_int16(self : UaReader) -> Int raise UaError

    Read a little-endian Int16.

    UaReader::read_int32

    fn UaReader::read_int32(self : UaReader) -> Int raise UaError

    Read a little-endian Int32.

    UaReader::read_int64

    fn UaReader::read_int64(self : UaReader) -> Int64 raise UaError

    Read a little-endian Int64.

    UaReader::read_localized_text

    fn UaReader::read_localized_text(self : UaReader) -> LocalizedText raise UaError

    Decode a LocalizedText.

    UaReader::read_node_id

    fn UaReader::read_node_id(self : UaReader) -> NodeId raise UaError

    Decode a NodeId.

    UaReader::read_qualified_name

    fn UaReader::read_qualified_name(self : UaReader) -> QualifiedName raise UaError

    Decode a QualifiedName.

    UaReader::read_required_bytes

    fn UaReader::read_required_bytes(self : UaReader) -> Bytes raise UaError

    Read a ByteString and reject null.

    UaReader::read_required_string

    fn UaReader::read_required_string(self : UaReader) -> String raise UaError

    Read a String and reject null.

    UaReader::read_sbyte

    fn UaReader::read_sbyte(self : UaReader) -> Int raise UaError

    Read a signed byte.

    UaReader::read_status_code

    fn UaReader::read_status_code(self : UaReader) -> StatusCode raise UaError

    Decode a StatusCode.

    UaReader::read_string

    fn UaReader::read_string(self : UaReader) -> String? raise UaError

    Read a nullable OPC UA String.

    UaReader::read_u8

    fn UaReader::read_u8(self : UaReader) -> UInt raise UaError

    Read an unsigned byte.

    UaReader::read_uint16

    fn UaReader::read_uint16(self : UaReader) -> UInt16 raise UaError

    Read a little-endian UInt16.

    UaReader::read_uint32

    fn UaReader::read_uint32(self : UaReader) -> UInt raise UaError

    Read a little-endian UInt32.

    UaReader::read_uint64

    fn UaReader::read_uint64(self : UaReader) -> UInt64 raise UaError

    Read a little-endian UInt64.

    UaReader::read_variant

    fn UaReader::read_variant(self : UaReader) -> VariantValue raise UaError

    Decode a scalar Variant or a one-/multi-dimensional Variant array.

    UaReader::remaining

    fn UaReader::remaining(self : UaReader) -> Int

    Remaining bytes.

    UaReader::with_limits

    fn UaReader::with_limits(data : BytesView, limits : UaLimits) -> UaReader

    Create a reader with explicit limits.

    UaWriter

    pub(all) struct UaWriter {
    buffer :
    Buffer

    }

    A deterministic writer for OPC UA Binary Encoding.

    UaWriter::new

    fn UaWriter::new() -> UaWriter

    Create an empty writer.

    UaWriter::to_bytes

    fn UaWriter::to_bytes(self : UaWriter) -> Bytes

    Return the encoded bytes.

    UaWriter::write_bool

    fn UaWriter::write_bool(self : UaWriter, value : Bool) -> Unit

    Write a Boolean.

    UaWriter::write_bytes

    fn UaWriter::write_bytes(self : UaWriter, value : Bytes) -> Unit raise UaError

    Write a non-null ByteString.

    UaWriter::write_data_value

    fn UaWriter::write_data_value(self : UaWriter, value : DataValue) -> Unit raise UaError

    Encode a DataValue and its optional-field mask.

    UaWriter::write_datetime

    fn UaWriter::write_datetime(self : UaWriter, value : UaDateTime) -> Unit

    Encode a DateTime.

    UaWriter::write_double

    fn UaWriter::write_double(self : UaWriter, value : Double) -> Unit

    Write a little-endian Double.

    UaWriter::write_expanded_node_id

    fn UaWriter::write_expanded_node_id(self : UaWriter, value : ExpandedNodeId) -> Unit raise UaError

    Encode an ExpandedNodeId.

    UaWriter::write_extension_object

    fn UaWriter::write_extension_object(self : UaWriter, value : ExtensionObject) -> Unit raise UaError

    Encode an ExtensionObject.

    UaWriter::write_float

    fn UaWriter::write_float(self : UaWriter, value : Float) -> Unit

    Write a little-endian Float.

    UaWriter::write_int16

    fn UaWriter::write_int16(self : UaWriter, value : Int) -> Unit raise UaError

    Write a little-endian Int16.

    UaWriter::write_int32

    fn UaWriter::write_int32(self : UaWriter, value : Int) -> Unit

    Write a little-endian Int32.

    UaWriter::write_int64

    fn UaWriter::write_int64(self : UaWriter, value : Int64) -> Unit

    Write a little-endian Int64.

    UaWriter::write_localized_text

    fn UaWriter::write_localized_text(self : UaWriter, value : LocalizedText) -> Unit raise UaError

    Encode a LocalizedText.

    UaWriter::write_node_id

    fn UaWriter::write_node_id(self : UaWriter, value : NodeId) -> Unit raise UaError

    Encode a NodeId using the most compact numeric representation.

    UaWriter::write_nullable_bytes

    fn UaWriter::write_nullable_bytes(self : UaWriter, value : Bytes?) -> Unit raise UaError

    Write a nullable ByteString.

    UaWriter::write_nullable_string

    fn UaWriter::write_nullable_string(self : UaWriter, value : String?) -> Unit raise UaError

    Write a nullable OPC UA String.

    UaWriter::write_qualified_name

    fn UaWriter::write_qualified_name(self : UaWriter, value : QualifiedName) -> Unit raise UaError

    Encode a QualifiedName.

    UaWriter::write_sbyte

    fn UaWriter::write_sbyte(self : UaWriter, value : Int) -> Unit raise UaError

    Write one signed byte.

    UaWriter::write_status_code

    fn UaWriter::write_status_code(self : UaWriter, value : StatusCode) -> Unit

    Encode a StatusCode.

    UaWriter::write_string

    fn UaWriter::write_string(self : UaWriter, value : String) -> Unit raise UaError

    Write a non-null OPC UA String.

    UaWriter::write_u8

    fn UaWriter::write_u8(self : UaWriter, value : UInt) -> Unit

    Write one unsigned byte.

    UaWriter::write_uint16

    fn UaWriter::write_uint16(self : UaWriter, value : UInt16) -> Unit

    Write a little-endian UInt16.

    UaWriter::write_uint32

    fn UaWriter::write_uint32(self : UaWriter, value : UInt) -> Unit

    Write a little-endian UInt32.

    UaWriter::write_uint64

    fn UaWriter::write_uint64(self : UaWriter, value : UInt64) -> Unit

    Write a little-endian UInt64.

    UaWriter::write_variant

    fn UaWriter::write_variant(self : UaWriter, value : VariantValue) -> Unit raise UaError

    Encode a Variant with its built-in type id.

    VariantArray

    pub(all) struct VariantArray {
    element_type : UInt
    values : Array[VariantValue]
    dimensions : Array[Int]?
    }

    A homogeneous Variant array with optional dimensions.

    VariantArray::empty

    fn VariantArray::empty(element_type : UInt) -> VariantArray raise UaError

    Construct an empty array with an explicit built-in element type.

    VariantArray::from_dimensions

    fn VariantArray::from_dimensions(values : Array[VariantValue], dimensions : Array[Int]) -> VariantArray raise UaError

    Construct a multi-dimensional array from flattened values and dimensions.

    VariantArray::from_values

    fn VariantArray::from_values(values : Array[VariantValue]) -> VariantArray raise UaError

    Construct an array and infer its element type from the first value.

    VariantArray::is_multidimensional

    fn VariantArray::is_multidimensional(self : VariantArray) -> Bool

    Whether this array declares multiple dimensions.

    VariantArray::length

    fn VariantArray::length(self : VariantArray) -> Int

    Number of elements.

    VariantValue

    pub(all) enum VariantValue {
    Null
    BoolValue(Bool)
    SByteValue(Int)
    ByteValue(UInt)
    Int16Value(Int)
    UInt16Value(UInt16)
    Int32Value(Int)
    UInt32Value(UInt)
    Int64Value(Int64)
    UInt64Value(UInt64)
    FloatValue(Float)
    DoubleValue(Double)
    StringValue(String?)
    DateTimeValue(UaDateTime)
    GuidValue(Bytes)
    ByteStringValue(Bytes?)
    XmlElementValue(String?)
    NodeIdValue(NodeId)
    ExpandedNodeIdValue(ExpandedNodeId)
    StatusCodeValue(StatusCode)
    QualifiedNameValue(QualifiedName)
    LocalizedTextValue(LocalizedText)
    ExtensionObjectValue(ExtensionObject)
    ArrayValue(VariantArray)
    }

    A practical subset of an OPC UA Variant.

    One- and multi-dimensional arrays are supported. Recursive built-in types and nested arrays are rejected explicitly.

    VariantValue::type_id

    fn VariantValue::type_id(self : VariantValue) -> UInt

    OPC UA built-in type id for this Variant value.

    VariantValue::type_name

    fn VariantValue::type_name(self : VariantValue) -> String

    Human-readable built-in type name.

    UA_UNIX_EPOCH_TICKS

    let UA_UNIX_EPOCH_TICKS : Int64

    Ticks between 1601-01-01 and 1970-01-01.