Async OPC-UA Types
Part of async-opcua, a general purpose OPC-UA library in rust.
This library contains a framework for encoding and decoding OPC-UA messages, as well as generated code for all types defined in the standard.
This includes:
- All of the built-in data types described in OPC Part 6 Chapter 5 that are encodable.
- All of the standard data types described in OPC Part 3 Chapter 8 (if not covered by 1.).
- Autogenerated data types and request / responses as described in OPC Part 4.
Notable types include
Variant, a discriminated union of a number of primitive types.ExtensionObjecta wrapper around an OPC-UA structure identified by its encoding ID.
Features
json, enables OPC-UA JSON encoding and decoding.xml, enables OPC-UA XML decoding, notably this is not yet full support for OPC-UA XML, only a limited subset intended for use withNodeSet2XML files.
Usage
Usually this library is used as part of an OPC-UA client or server.
Encoding is done by writing to a type implementing std::io::Write, and decoding
by reading from a type implementing std::io::Read:
let context_owned = default;
let context = context_owned.context;
let my_opcua_value = from;
// Get the byte length before encoding.
// This is not actually required, but can be useful.
let byte_len = my_opcua_value.byte_len;
let mut stream = new;
// Encode to a stream.
let start_pos = stream.position;
value.encode?;
stream.seek?;
let decoded = decode?;
assert_eq!;
Custom types
In order to make a custom OPC-UA structure, it must implement a number of traits depending on which features are enabled:
BinaryEncodableandBinaryDecodable, implementing encoding using the OPC-UA Binary protocol.JsonEncodableandJsonDecodable, implementing encoding using OPC-UA JSON, if the"json"feature is enabled.FromXml, loading the type from a NodeSet2 XML file, if the"xml"feature is enabled.Clone,Send,Sync,Debug,PartialEqare all required.ExpandedMessageInfo, which provides full encoding IDs.
A type that satisfies these requirements can be stored in an ExtensionObject and sent to OPC-UA. In order to receive the type, it must be added to a TypeLoader.
BinaryEncodable, BinaryDecodable, JsonEncodable, JsonDecodable, and FromXml all have derive macros.
Enums are simpler, the easiest way to make a custom OPC-UA enum (not a union) is to derive the UaEnum trait, which also implements a few other traits needed for numeric OPC-UA enums.