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.2.0
    License
    Apache-2.0
    Last updated
    7 hours ago
    Downloads
    12

    #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 编码
    • QualifiedName 的 namespace index 和名称编码
    • LocalizedText 的 locale/text 掩码组合
    • 类型化错误、边界检查、已知字节向量测试和可运行示例

    #快速开始

    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、Variant、DataValue 或 ExtensionObject。这些能力会在后续阶段逐步加入。

    #验证

    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)
    }

    Errors raised while decoding OPC UA Binary Encoding data.

    UaError::message

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

    Human-readable error message.

    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.

    UaLimits

    pub(all) struct UaLimits {
    max_byte_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_double

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

    Read a little-endian Double.

    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_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::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_double

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

    Write a little-endian Double.

    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_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.