NodeCollection — s.nodes()
NodeCollection This is the entry point for accessing Session nodes; it returns get(node_id)
Node Items to be packaged, Not triggered immediately RPC (Lazy Load).
Prerequisite Reading
- Single node API Please see Node.
- For standard node constants, see WellKnownNodes.
Input Methods
| Methods | Return | Note |
|---|---|---|
| s.nodes() | NodeCollection | Cloning is cheap |
Public Methods
| Methods | Return | Note |
|---|---|---|
| get(&self, node_id: &str) | Node | String NodeId Access |
| make_numeric(uri, id) | Result<Node, OpcUaError> | Constructed using NamespaceUri + a numeric ID |
| make_string(uri, id) | Result<Node, OpcUaError> | Construct using the + NamespaceUri string ID |
Predefined Entry Properties
For your convenience, here are some commonly used shortcuts:
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Standard Entry | root() | Node | R | Address Space Root (i=84) |
objects() | Node | R | Business Data Root (i=85) | |
types() | Node | R | Root of type definitions (i=86) | |
views() | Node | R | View root (i=87) | |
server() | Node | R | Server object (i=2253) |
let server = s.nodes().server();
println!("Server NodeId = {}", server.node_id());
Usage
String NodeId
let nodes = s.nodes();
let temp = nodes.get("ns=2;s=Temperature");
let dv = temp.value()?;
println!("{} = {:?}", temp.node_id(), dv.variant());
Use NamespaceUri construct (recommended, does not depend on Index)
// Number NodeId: Automatic Reverse Lookup ns=
let temp = s.nodes().make_numeric("urn:my-company:plc1", 1234)?;
// String NodeId
let press = s.nodes().make_string("urn:my-company:plc1", "Pressure")?;
Returns when the namespace does not URI exist
Err(OpcUaError::with_message(StatusCode::INVALID_ARGUMENT, "Namespace URI 'xxx' not found")).
Lazy Load Mechanism
let node = s.nodes().get("ns=2;s=Temperature"); // ← Do not trigger RPC
let dv = node.value()?; // ← The first call to .value() triggers a Read
On the second access, it will still .value() re-read (without caching) because the variable value can change at any time. I want to cache the variable myself.
When to use it nodes().get(), when to use it s.read()
| Scenario | Recommendations |
|---|---|
| Known to be accurate NodeId, single operation | s.read("ns=2;s=...") |
| It is necessary to access different properties of the same node multiple times | let n = s.nodes().get(...); n.value(); n.display_name(); ... |
| Standard Nodes (Server Status, etc.) | s.nodes().server().child("...") |
| Browse Tree / Write a One-Time Script | s.nodes().get(...).children() |
| Batch (100+ nodes) | s.read_many(...) / s.browse_many(...), no need to loop |
Cross-thread usage
Node is Send + Sync; safe to clone and use across threads:
let nodes = s.nodes();
let n1 = nodes.get("ns=2;s=T1");
let n2 = nodes.get("ns=2;s=T2");
let h1 = std::thread::spawn(move || n1.value());
let h2 = std::thread::spawn(move || n2.value());
let v1 = h1.join().unwrap()?;
let v2 = h2.join().unwrap()?;