Skip to main content

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.

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​

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.

CategoryPropertyTypeAccessDescription
Propertiessize()intRNumber of namespaces
get(int index)StringRRetrieves the URI corresponding to the specified Index

Methods​

  • indexOf(String uri) (int, -1 if not found) — Reverse Lookup URI → Index
  • reload()(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:

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.

Next Step​