结构体解码
概述
OPC UA 1.04+ 把每个自定义结构体的字段定义放到 DataType 节点的 DataTypeDefinition 属性 (AttributeId=27, Part 5 §6.4.2)。StructDecoder 按服务端给的 StructureDefinition 把 ExtensionObject 的 UA Binary 字节流 (Part 6 §5.2) 反序列化成字段字典;可解的 Builtin 范围与限制见下文「实现限制」。
API
StructDecoder 是 darra::opcua 命名空间下的类, 头文件 darra/opcua/struct_decoder.hpp. 解码结果是 std::unordered_map<std::string, std::any> 字段字典 — 没有 DynamicExtensionObject 这类包装类型, 取字段就是 map 的 operator[].
| 成员 | 类别 | 读写 | 说明 |
|---|---|---|---|
| StructDecoder(Session& ua) | 构造 | — | 绑定到会话 (explicit) |
| decoder.Decode(variant) | 方法 | 读 | 返回 std::unordered_map<std::string, std::any> |
| decoder.DecodeBody(body, data_type_node_id) | 方法 | 读 | 已知 body 字节流 + DataType NodeId 时直接解码 |
| decoder.ClearCache() | 方法 | 写 | 清 DataTypeDefinition 缓存 |
| decoder.CachedDefinitionCount() | 方法 | 读 | 已缓存的定义数 |
| map[key] | 索引 | 读 | 取字段, 值是 std::any |
| StructDecoder::kStatusKey | 常量 | — | "_status" — 解码失败 / 占位结果的 key |
| StructDecoder::kTypeIdKey | 常量 | — | "_typeId" — 占位结果存 TypeId 的 key |
| StructDecoder::kAttrDataTypeDefinition | 常量 | — | 27 — DataTypeDefinition 的 AttributeId |
相关结构 (StructureKind, Part 3 §8.49 StructureType):
- Structure (0) — 普通
- StructureWithSubtypedValues (1) — 含子类型
- StructureWithOptionalFields (2) — 含可选 (前置 EncodingMask)
- Union (3) — Union (前置 selector)
- UnionWithSubtypedValues (4) — Union + 子类型
代码示例
#include <darra/opcua/struct_decoder.hpp>
#include <any>
#include <iostream>
using namespace darra::opcua;
Stack::Init();
Session s("opc.tcp://localhost:4840");
s.Connect();
StructDecoder dec(s);
// 1) 读自定义结构 — Decode 吃 DataValue 里的 Variant
auto dv = s.Read("ns=2;s=Robot.CurrentPose");
auto pose = dec.Decode(dv.Value()); // std::unordered_map<std::string, std::any>
std::cout << "x = " << std::any_cast<double>(pose["X"]) << "\n";
std::cout << "y = " << std::any_cast<double>(pose["Y"]) << "\n";
// 2) 全字段遍历 — 结果就是 map, 直接迭代
for (auto& [name, value] : pose)
std::cout << name << "\n";
// 3) 嵌套结构字段的值是 std::unordered_map<std::string, std::any>
auto quat = std::any_cast<std::unordered_map<std::string, std::any>>(pose["Quaternion"]);
std::cout << "qw = " << std::any_cast<double>(quat["W"]) << "\n";
// 4) 数组字段的值是 std::vector<std::any>
auto arr = std::any_cast<std::vector<std::any>>(pose["JointAngles"]);
for (auto& a : arr)
std::cout << std::any_cast<double>(a) << "\n";
// 5) 已经从别的渠道拿到 EO body 时直接解
std::vector<uint8_t> body = /* ... */;
auto manual = dec.DecodeBody(body, "ns=2;i=3001");
实现限制
- ExtensionObject 取不到字节时, Decode(Variant) 返回带
_status字段的部分结果 - 已有原始字节时用 DecodeBody(bytes, type_id) 直接解码
- 字段定义取自
LoadDefinition, 该缓存填充当前未接通 (恒返回nullptr), 所以现阶段DecodeBody返回空 map — 缓存接通后签名不变 - Builtin 未覆盖的类型 (如 DiagnosticInfo, i=25) 抛
std::runtime_error("Builtin type not implemented in StructDecoder"), 不是静默跳过
最佳实践
- 共享一个 decoder, 同 server 同 type 的定义只加载一次
- 服务端 DataType 定义改过之后调
ClearCache()再重新解码 - 嵌套结构的值是 map, 用
std::any_cast<std::unordered_map<std::string, std::any>>取出来再访问 - Union 看 selector: 解码后只有一个字段, 按字段名判断选中的是哪一支
跨语言对照
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| StructDecoder | StructDecoder | StructDecoder | StructDecoder | StructDecoder | DarraUa_StructDecoderHandle |
| Decode | decode | decode | Decode | decode | DarraUa_StructDecoder_Decode |
| DecodeBody | decode_body | decodeBody | DecodeBody | decode_body | DarraUa_StructDecoder_DecodeBody |
| ClearCache | clear_cache | — | ClearCache | clear_cache | DarraUa_StructDecoder_ClearCache |
| CachedDefinitionCount | cached_definition_count | — | CachedDefinitionCount | cached_definition_count | DarraUa_StructDecoder_CachedCount |