Namespaces — s.NamespacesRef()
OPC UA A node consists NamespaceIndex + Identifier of NodeId, but NamespaceIndex Unstable — A server restart may cause changes. Production code must use NamespaceUri reverse lookup indexes; do not hard-code them ns=2.
- Standard = ns0; please refer to the node constants WellKnownNodes.
- For information on node and Browse path resolution, please refer to Nodes.
Namespaces Public Members
Session::NamespacesRef() returns Namespaces&; at Connect() time it is automatically loaded from the server's NamespaceArray (i=2255) (if loading fails, only a warning is logged; the connection is not blocked).
The Namespaces value retrieval interface: all are member functions, no fields:
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Methods | Count() | int | R | Number of namespaces |
operator[](int i) | std::string | R | Index-based access; returns the URI corresponding to the given Index; returns an empty string if out of bounds | |
IndexOf(uri) | int | R | Lookup URI → Index; returns -1 if not found | |
Reload() | Status | R | Proactively re-read the server's NamespaceArray (when the Server changes dynamically) |
Common Uses
Reverse Lookup: Index Spelling NodeId
#include <darra/opcua/client.hpp>
#include <stdexcept>
#include <string>
s.Connect();
int idx = s.NamespacesRef().IndexOf("urn:my-company:plc1");
if (idx < 0) {
throw std::runtime_error("Server does not have my namespace");
}
std::string node_id = "ns=" + std::to_string(idx) + ";s=Temperature";
auto dv = s.Read(node_id);
List all namespaces (debug)
#include <darra/opcua/client.hpp>
#include <iostream>
auto& nss = s.NamespacesRef();
for (int i = 0; i < nss.Count(); ++i) {
std::cout << " [" << i << "] " << nss[i] << "\n";
}
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 receive the complete NamespaceArray set. In this case, call the re-read Reload() method:
s.NamespacesRef().Reload();
int idx = s.NamespacesRef().IndexOf("urn:plugin:loaded-after-startup");
Standard namespace (ns=0)
OPC Foundation It is specified that Index 0 is always http://opcfoundation.org/UA/, and all standard nodes have 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.