OpcUaNodeId — Strongly typed NodeId
Overview
OPC UA node IDs are string-encoded by default ("ns=2;s=Foo"). OpcUaNodeId is a struct plus an enum: OpcUaNodeId { namespace_index: u16, identifier: NodeIdValue }, explicitly carrying namespace / identifier, with id_type() derived from identifier.
Corresponding section of the standard: Part 6 §5.2.2.
API
| Members | Category | Reading and Writing | Note |
|---|---|---|---|
| OpcUaNodeId::null() | Factory | — | Empty Sentinel (ns=0, i=0) |
| OpcUaNodeId::from_numeric(ns, id) | Factory | — | Numeric (u32) |
| OpcUaNodeId::from_string(ns, id) | Factory | — | String type |
| OpcUaNodeId::from_guid(ns, [u8; 16]) | Factory | — | GUID Type (16-byte array, no uuid crate) |
| OpcUaNodeId::from_opaque(ns, Vec<u8>) | Factory | — | Opaque Type |
| OpcUaNodeId::parse(s) | Static | — | Parse, Returns Result<Self, ParseError> |
| OpcUaNodeId::try_parse(s) | Static | — | Parsing, Returns on Failure Option::None |
| nid.to_string() / nid.to_canonical() | Methods | Read | Serialization (impl Display) |
| nid.namespace_index / nid.identifier | Field | Read | Public Fields, Direct Access |
| nid.id_type() / nid.is_null() | Methods | Read | Identifier Type / Is Null |
Related Structures:
pub enum NodeIdType {
Numeric = 0, // Code i=, u32
String = 1, // Code s=, UTF-8
Guid = 2, // Code g=, {xxxx-...}
Opaque = 3, // Code b=, base64
}
Code Example
use darra_opcua::{OpcUaNodeId, NodeIdType, NodeIdValue, Session};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut ua = Session::new("opc.tcp://localhost:4840")?;
ua.connect()?;
// 1) Factory
let n1 = OpcUaNodeId::from_numeric(0, 84); // i=84
let n2 = OpcUaNodeId::from_string(2, "Boiler1.Temp");
let n3 = OpcUaNodeId::from_guid(3, [0u8; 16]); // 16-byte array
let n4 = OpcUaNodeId::from_opaque(4, vec![1, 2, 3]);
// 2) Analysis
let n5 = OpcUaNodeId::parse("ns=2;s=Demo.Tag")?;
println!("ns={}, type={:?}", n5.namespace_index, n5.id_type());
match OpcUaNodeId::try_parse("ns=99;i=foo") {
Some(n) => println!("ok: {}", n),
None => println!("invalid"),
}
// 3) Serialization
let s = n2.to_string(); // "ns=2;s=Boiler1.Temp"
// 4) Convert SDK between and (the `read` function takes only one argument: a string NodeId )
let dv = ua.read(&n2.to_string())?;
let dv = ua.read_attr(&n2.to_string(), darra_opcua::AttributeId::Value)?;
// 5) When a dictionary key —— OpcUaNodeId has not been derived into a Hash or Ord,
// You cannot use HashMap / BTreeMap as a key directly; use a string to_canonical() instead.
use std::collections::HashMap;
let mut dict: HashMap<String, String> = HashMap::new();
dict.insert(n2.to_canonical(), "锅炉一温度".into());
// 6) Null
println!("{}", OpcUaNodeId::null().is_null()); // true
let _ = (n1, n3, n4, s, dv);
Ok(())
}
Best Practices
- Store strings in the configuration file; use
parseto load them - Use strong typing within the code
- Numeric is preferred (u32 is faster)
- User input uses
try_parse(Option<Self>, returnsNone) on failure orparse(Result<Self, ParseError>), and neither will cause a panic.
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| OpcUaNodeId | OpcUaNodeId | OpcUaNodeId | OpcUaNodeId | OpcUaNodeId | DarraUa_NodeId |
| Parse | parse | parse | Parse | parse | DarraUa_NodeId_Parse |
| FromNumeric | from_numeric | fromNumeric | FromNumeric | from_numeric | DarraUa_NodeId_FromNumeric |