OpcUaNodeId — Strongly typed NodeId
Overview
OPC UA node IDs are string-encoded by default ("ns=2;s=Foo" / "i=84"). OpcUaNodeId is an immutable dataclass that explicitly carries namespace / id_type / identifier, and converts to and from strings with 100% fidelity.
Corresponding section of the standard: Part 6 §5.2.2.
API
| Members | Category | Reading and Writing | Note |
|---|---|---|---|
| OpcUaNodeId.null() | Static Methods | — | Empty Sentinel (ns=0, NUMERIC, 0) |
| OpcUaNodeId.from_numeric(ns, identifier) | Factory | — | Digital Type (i=NNN) |
| OpcUaNodeId.from_string(ns, identifier) | Factory | — | String Type (s=Foo) |
| OpcUaNodeId.from_guid(ns, identifier) | Factory | — | GUID Type (uuid.UUID or str is also accepted) |
| OpcUaNodeId.from_opaque(ns, identifier) | Factory | — | Opaque Type (bytes) |
| OpcUaNodeId.parse(s) | Static Methods | — | Parse, throw on failure ValueError |
| OpcUaNodeId.try_parse(s) | Static Methods | — | Security Analysis, Returns on Failure None |
| OpcUaNodeId.well, OBJECTS_FOLDER, etc. | Singleton Properties | Read | Strongly typed wrappers for commonly used "well-known" nodes |
| str(nid) | — | — | Serialize (__str__) |
| nid.namespace_index / id_type / identifier | Field | Read | dataclass fields |
| nid.is_null | Properties | Read | Is it empty? NodeId |
Related Structures:
class NodeIdType(IntEnum):
NUMERIC = 0 # i= uint32
STRING = 1 # s= UTF-8
GUID = 2 # g= {xxxx-...}
OPAQUE = 3 # b= base64
Code Example
import uuid
from darra_opcua import OpcUaNodeId, NodeIdType
# 1) Factory Structure
n1 = OpcUaNodeId.from_numeric(0, 84) # i=84
n2 = OpcUaNodeId.from_string(2, "Boiler1.Temp") # ns=2;s=Boiler1.Temp
n3 = OpcUaNodeId.from_guid(3, uuid.uuid4()) # ns=3;g=<uuid>
n4 = OpcUaNodeId.from_opaque(4, bytes([1, 2, 3])) # ns=4;b=AQID
# 2) Analysis
n5 = OpcUaNodeId.parse("ns=2;s=Demo.Tag")
print(f"ns={n5.namespace_index}, type={n5.id_type}, id={n5.identifier}")
n6 = OpcUaNodeId.try_parse("ns=99;i=foo") # Returns None (the character following "i=" is not a number)
if n6 is None:
print("invalid")
# 3) Serialization
s = str(n2) # "ns=2;s=Boiler1.Temp"
# 4) SDK Conversion between and — The SDK interface accepts a string (ua which is concatenated OpcUaSession)
with ua.read(str(n2)) as dv:
pass
# 5) Dictionary key (frozen dataclass automatically __hash__)
d = {n2: "锅炉一温度"}
# 6) Null
print(OpcUaNodeId.null().is_null) # True
# 7) well-known strongly typed encapsulation
print(str(OpcUaNodeId.well.OBJECTS_FOLDER)) # "i=85"
String Encoding Rules
str(nid) The output is as follows; the prefix ns=0 ns=0; is omitted when:
| Coding | Note |
|---|---|
| i=NNN | Number; omitted when ns=0 |
| ns=N;i=NNN | Numbers |
| ns=N;s=XXX | String |
| ns=N;g=GUID | GUID, Output without curly braces |
| ns=N;b=base64== | Bytes (base64) |
parse / try_parse Be a little more flexible: ns= / i= / s= / g= / b= Prefixes are case-insensitive, and curly braces GUID are optional.
Best Practices
- Store strings in the configuration file; use to load them
parse - Use strong typing within the code
- Standard nodes can be specified directly using
OpcUaNodeId.well.*or a string constant well_known_nodes, Don't try to put it together yourselfi=2258 - User input always comes first
try_parse
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| OpcUaNodeId | OpcUaNodeId | OpcUaNodeId | OpcUaNodeId | OpcUaNodeId | DarraUa_NodeId |
| OpcUaNodeId.Null (static property) | OpcUaNodeId.null() | — | — | — | — |
| Parse(string) | parse(s) | parse | Parse | parse | DarraUa_NodeId_Parse |
| TryParse(string, out id) → bool | try_parse(s) → Optional[OpcUaNodeId] | — | — | — | — |
| FromNumeric(ushort, uint) | from_numeric(ns, identifier) | fromNumeric | FromNumeric | from_numeric | DarraUa_NodeId_FromNumeric |
| ToString() | str(nid) | — | — | — | — |
| OpcUaNodeId.Well.ObjectsFolder | OpcUaNodeId.well.OBJECTS_FOLDER | — | — | — | — |
try_parse The return format varies by language: Python returns OpcUaNodeId or directly None; C# returns bool + out parameters. In C#, does not throw an exception when it fails TryParse.
Java / C++ / Rust / C: The four columns marked "read-only, unverified" in this entry — indicate that the information has not been verified; it does not mean that it does not exist.