Skip to main content

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

Input Methods​

MethodsReturnNote
s.nodes()NodeCollectionCloning is cheap

Public Methods​

MethodsReturnNote
get(&self, node_id: &str)NodeString 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:

CategoryPropertyTypeAccessDescription
Standard Entryroot()NodeRAddress Space Root (i=84)
objects()NodeRBusiness Data Root (i=85)
types()NodeRRoot of type definitions (i=86)
views()NodeRView root (i=87)
server()NodeRServer 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());
// 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()​

ScenarioRecommendations
Known to be accurate NodeId, single operations.read("ns=2;s=...")
It is necessary to access different properties of the same node multiple timeslet n = s.nodes().get(...); n.value(); n.display_name(); ...
Standard Nodes (Server Status, etc.)s.nodes().server().child("...")
Browse Tree / Write a One-Time Scripts.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()?;

Next Step​