Skip to main content

Namespaces — s.namespaces()

OPC UA A node consists NamespaceIndex + Identifier of NodeId, but NamespaceIndex Unstable — A server restart may cause changes. Production code must use reverse lookup NamespaceUri of the index; it must not be hard-coded ns=2.

Prerequisite Reading
  • Standard = ns0; please refer to the node constants WellKnownNodes.
  • For information on node and Browse path resolution, please refer to Nodes.

NamespaceCollection Public Members​

s.namespaces() Returns NamespaceCollection ; it is automatically loaded from the server NamespaceArray (i=2255 ) when Connect is called.

CategoryPropertyTypeAccessDescription
Searchcount()u32RNumber of namespaces
get(index)Option<String>RRetrieve the URI by index; returns None if out of bounds
index_of(uri)i32RReverse lookup URI → Index; returns -1 if not found
iter()NamespaceIterRIterates over all URI, implements Iterator<Item=String>
Refreshreload()StatusCodeRWProactively reload the server NamespaceArray (when the server changes dynamically)

Common Uses​

Reverse Lookup: Index Spelling NodeId​

s.connect()?;
let ns = s.namespaces();
let idx = ns.index_of("urn:my-company:plc1");
if idx < 0 {
return Err("Server does not have my namespace".into());
}

let node_id = format!("ns={};s=Temperature", idx);
let dv = s.read(&node_id)?;

List all namespaces (debug)​

let ns = s.namespaces();
for (i, uri) in ns.iter().enumerate() {
println!(" [{}] {}", i, uri);
}

Output example:

[0] http://opcfoundation.org/UA/
[1] urn:server-host
[2] urn:my-company:plc1
[3] http://my-company.com/UA/

Dynamic Namespace Changes​

Some servers register their namespaces only after they start up (plug-in style), so Connect does not NamespaceArray receive the complete set. In this case, call the re-read reload() method:

let ns = s.namespaces();
ns.reload();
let idx = ns.index_of("urn:plugin:loaded-after-startup");

Through NodeCollection Automatic Construction NodeId​

NodeCollection::make_numeric(uri, identifier) And make_string(uri, identifier) you can pass the namespace directly URI, without having to look up the index yourself:

// The number NodeId: "ns=2;i=1234"
let node = s.nodes().make_numeric("urn:my-company:plc1", 1234)?;

// String NodeId: "ns=2;s=Temperature"
let node = s.nodes().make_string("urn:my-company:plc1", "Temperature")?;

Returns this value if the namespace does not exist Err(OpcUaError::with_message(StatusCode::INVALID_ARGUMENT, "Namespace URI 'xxx' not found")).

Standard namespace (ns=0)​

OPC Foundation It is specified that Index 0 is always http://opcfoundation.org/UA/, and all standard nodes are at ns=0:

NodeNodeIdNote
Rooti=84Root
Objectsi=85Business Data Root
Typesi=86Type Definition Root
Serveri=2253The Server Object Itself
ServerStatusi=2256Server Status Structure
ServerStatus.CurrentTimei=2258Current server time
NamespaceArrayi=2255Namespace Array
ServerArrayi=2254Server-side array

For more information, see WellKnownNodes.

use darra_opcua::well_known_nodes;

let dv = s.read(well_known_nodes::SERVER_NAMESPACE_ARRAY)?;
// dv.variant() is a String[] array whose contents are identical to those of s.namespaces().iter()

Next Step​