Namespaces — ua.namespaces
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.
NamespaceCollection Public Members
OpcUaSession.namespaces is NamespaceCollection, it is automatically loaded from the server NamespaceArray (i=2255) when connecting. To implement this Iterable<String>, you can simply use a for-each loop.
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Properties | size() | int | R | Number of namespaces |
get(int index) | String | R | Retrieves the URI corresponding to the specified Index |
Methods
indexOf(String uri)(int, -1 if not found)— Reverse Lookup URI → Indexreload()(Enums.StatusCode)— Proactive server re-read NamespaceArray (when the server changes dynamically)iterator()(Iterator<String>)— for-each: Iterate through all URI
Common Uses
Reverse Lookup: Index Spelling NodeId
ua.connect();
int idx = ua.namespaces.indexOf("urn:my-company:plc1");
if (idx < 0) {
throw new IllegalStateException("Server does not have my namespace");
}
String nodeId = "ns=" + idx + ";s=Temperature";
try (OpcUaDataValue dv = ua.read(nodeId)) {
// ...
}
Use NodeCollection.makeString / makeNumeric
NodeCollection Provides a convenient structure, with internal routing indexOf:
OpcUaNode n1 = ua.nodes.makeString("urn:my-company:plc1", "Temperature");
OpcUaNode n2 = ua.nodes.makeNumeric("urn:my-company:plc1", 1234);
try (OpcUaDataValue dv = n1.getValue()) {
// ...
}
List all namespaces (debug)
int i = 0;
for (String uri : ua.namespaces) {
System.out.println(" [" + i + "] " + uri);
i++;
}
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 the information obtained by connect is incomplete NamespaceArray. In this case, call re-read reload()reload():
ua.namespaces.reload();
int idx = ua.namespaces.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.