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.
- 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.
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Search | count() | u32 | R | Number of namespaces |
get(index) | Option<String> | R | Retrieve the URI by index; returns None if out of bounds | |
index_of(uri) | i32 | R | Reverse lookup URI → Index; returns -1 if not found | |
iter() | NamespaceIter | R | Iterates over all URI, implements Iterator<Item=String> | |
| Refresh | reload() | StatusCode | RW | Proactively 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:
| Node | NodeId | Note |
|---|---|---|
| Root | i=84 | Root |
| Objects | i=85 | Business Data Root |
| Types | i=86 | Type Definition Root |
| Server | i=2253 | The Server Object Itself |
| ServerStatus | i=2256 | Server Status Structure |
| ServerStatus.CurrentTime | i=2258 | Current server time |
| NamespaceArray | i=2255 | Namespace Array |
| ServerArray | i=2254 | Server-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()