跳到主要内容

NodeId — 强类型 NodeId

概述​

OPC UA 节点 ID 默认用字符串编码 ("ns=2;s=Foo")。NodeId 是普通值类型 (struct, 公有字段可直接读写), 显式带 namespace_index / type / identifier 三件套, 与字符串编码可互转。

对应规范段: Part 6 §5.2.2。

头文件​

#include <darra/opcua/node_id.hpp>
using namespace darra::opcua;

API​

成员类别读写说明
NodeId::Null()静态工厂—构造空 NodeId (i=0), 是函数不是常量
NodeId::FromNumeric(uint16_t ns, uint32_t id)静态工厂—数字型
NodeId::FromString(uint16_t ns, std::string id)静态工厂—字符串型
NodeId::FromGuid(uint16_t ns, std::array<uint8_t,16> guid_bytes)静态工厂—GUID 型 (裸 16 字节, 不是 uuid 字符串)
NodeId::FromOpaque(uint16_t ns, std::vector<uint8_t> bytes)静态工厂—不透明型
NodeId::Parse(std::string const& s)静态—解析, 失败抛 std::invalid_argument
NodeId::TryParse(std::string const& s)静态—安全解析, 返回 std::optional<NodeId>, 不是 bool + out 参数
nid.ToString()方法—序列化为规范字符串
nid.IsNull()方法—是否为空 NodeId
nid.namespace_index字段读写uint16_t
nid.type字段读写NodeIdType (字段名是 type, 不是 id_type)
nid.identifier字段读写std::variant<uint32_t, std::string, std::array<uint8_t,16>, std::vector<uint8_t>>

相关结构 — NodeIdType​

NodeIdType值编码前缀identifier 里存的类型
Numeric0i=uint32_t
String1s=std::string (UTF-8)
Guid2g=std::array<uint8_t,16>
Opaque3b=std::vector<uint8_t> (base64)

字符串编码规则 (照 ToString / TryParse 实际行为):

  • namespace_index == 0 时 ToString() 省略 ns=0; 前缀
  • Guid 输出为 g= + 36 字符 hex, 不带花括号; Parse 时花括号可有可无
  • Parse / TryParse 的 ns=N 前缀可省 (省略即 ns=0), 编号前缀大小写不敏感

已知限制: node_id.hpp 没有为 NodeId 定义 operator< / operator==, 也没有 std::hash 特化 (C++17 聚合 struct), 不能直接当 std::map / std::unordered_map 的 key —— 用 ToString() 当 key。

内置 well-known 封装: NodeId::Well 提供返回 NodeId const& 的静态方法 (Well::RootFolder() / Well::ObjectsFolder() / Well::Server() / Well::ServerStatus() / Well::CurrentTime() / Well::Organizes() 等), 见 Well-Known Nodes。

代码示例​

#include <darra/opcua/node_id.hpp>
#include <darra/opcua/client.hpp>
#include <array>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace darra::opcua;

// 1) 工厂 (ns 是 uint16_t, 数字 id 是 uint32_t)
NodeId n1 = NodeId::FromNumeric(0, 84); // i=84
NodeId n2 = NodeId::FromString(2, "Boiler1.Temp"); // ns=2;s=Boiler1.Temp
NodeId n3 = NodeId::FromGuid(3, std::array<uint8_t, 16>{ // 裸 16 字节, 非 uuid 字符串
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10});
NodeId n4 = NodeId::FromOpaque(4, std::vector<uint8_t>{1, 2, 3});

// 2) 解析 — namespace_index / type / identifier 是公有字段, 直接取值
NodeId n5 = NodeId::Parse("ns=2;s=Demo.Tag");
std::cout << "ns=" << n5.namespace_index
<< ", type=" << static_cast<int>(n5.type) << "\n";

// TryParse 返回 std::optional<NodeId>, 不是 bool + out 参数
if (auto n6 = NodeId::TryParse("ns=99;i=foo"); !n6)
std::cout << "invalid\n";

// 3) 序列化 (ns=0 时省略 "ns=0;" 前缀)
std::string s = n2.ToString(); // "ns=2;s=Boiler1.Temp"

// 4) 与 Session 互操作 — Session::Read/Write 只吃字符串 NodeId
Stack::Init();
Session ua("opc.tcp://localhost:4840");
ua.Connect();
DataValue dv = ua.Read(n2.ToString());

// 5) 作 map key — NodeId 无 operator< / std::hash, 用 ToString() 当 key
std::map<std::string, std::string> dict;
dict[n2.ToString()] = "锅炉一温度";

// 6) 空 NodeId — Null() 是静态函数, 不是常量
std::cout << NodeId::Null().IsNull() << "\n"; // 1

最佳实践​

  • 配置文件存字符串, 加载用 Parse
  • 代码内部用强类型, 传给 Session::Read / Write 前调 ToString()
  • Numeric 优先 (identifier 走 uint32_t, 最常见)
  • 用户输入永远先 TryParse (返回 std::optional, 不会抛)
  • 作 map / set key 时用 ToString(), 不要指望 NodeId 自带比较

跨语言对照​

C#PythonJavaC++RustC
OpcUaNodeIdOpcUaNodeIdOpcUaNodeIdNodeIdOpcUaNodeIdDarraUa_OwnedNodeId
ParseparseparseNodeId::ParseparseDarraUa_NodeId_Parse
FromNumericfrom_numericfromNumericNodeId::FromNumericfrom_numericDarraUa_NodeId_FromNumeric

下一步​