结构体解码
概述
OPC UA 1.04+ 把每个自定义结构体的字段定义放到 DataType 节点的 DataTypeDefinition 属性 (Part 3 §5.10; Java 侧 Enums.AttributeId 尚未含该属性)。StructDecoder 解 UA Binary: 基本类型 + 嵌套结构 + 数组 + Union + Optional 字段 (枚举按其底层整数解出)。
API
| 成员 | 类别 | 读写 | 说明 |
|---|---|---|---|
| new StructDecoder(OpcUaSession ua) | 构造 | — | 绑定到会话 |
| decoder.decode(OpcUaVariant variant) | 方法 | 读 | 解码 Variant (ExtensionObject), 返回 Map<String, Object> |
| decoder.decodeBody(byte[] body, String dataTypeId) | 方法 | 读 | 直接给字节 + DataType 解码, 返回 Map<String, Object> |
| decoder.registerDefinition(StructDefinition def) | 方法 | 写 | 注册一份字段定义 (按 dataTypeId 缓存) |
| map.get(fieldName) | 方法 | 读 | 取字段 (Map 标准方法) |
| map.entrySet() | 方法 | 读 | 遍历全部字段名→值 |
相关结构 StructDefinition.Kind (嵌套 enum):
public enum Kind {
Structure, // 普通
StructureWithSubtypedValues, // 含子类型字段
StructureWithOptionalFields, // 含可选字段 (前置 UInt32 EncodingMask)
Union, // Union (前置 UInt32 selector)
UnionWithSubtypedValues // Union + 子类型字段
}
解出的 Map<String, Object> 按字段定义取值: 标量字段直接是 Boolean / Integer / Long / Float / Double / String / Instant / UUID / byte[] / Enums.StatusCode (NodeId 解为字符串形式); 嵌套结构体是 Map<String, Object>; 数组字段是 List<Object>。
代码示例
import com.darra.opcua.*;
import java.util.List;
import java.util.Map;
try (OpcUaSession ua = new OpcUaSession("opc.tcp://localhost:4840")) {
ua.connect();
StructDecoder dec = new StructDecoder(ua);
// 1) 先注册字段定义 (按 dataTypeId 缓存, 一个类型一条)
StructDefinition def = new StructDefinition();
def.name = "CartesianCoordinates";
def.dataTypeId = "ns=2;i=3001";
def.kind = StructDefinition.Kind.Structure;
def.fields.add(new StructField("X", "i=11", -1)); // i=11 = Double, -1 = 标量
def.fields.add(new StructField("Y", "i=11", -1));
dec.registerDefinition(def);
// 2) 读 + 解码 (已有原始字节用 dec.decodeBody(bytes, dataTypeId))
try (OpcUaDataValue dv = ua.read("ns=2;s=Robot.CurrentPose")) {
Map<String, Object> pose = dec.decode(dv.variant());
System.out.println("x = " + pose.get("X"));
System.out.println("y = " + pose.get("Y"));
// 3) 全字段遍历
for (Map.Entry<String, Object> e : pose.entrySet())
System.out.println(e.getKey() + " = " + e.getValue());
// 4) 嵌套结构体也是 Map<String, Object>
Map<String, Object> quat = (Map<String, Object>) pose.get("Quaternion");
System.out.println("qw = " + quat.get("W"));
// 5) 数组字段是 List<Object>
List<Object> arr = (List<Object>) pose.get("JointAngles");
for (Object a : arr) System.out.println(a);
}
}
实现限制
decode(OpcUaVariant)拿不到 ExtensionObject 的原始字节流时, 返回带_status字段的 Map (值为提示文本)- 已有原始字节时用
decodeBody(byte[], String dataTypeId)直接解码 (走同一份注册定义) - 字段定义需手动
registerDefinition(StructDefinition)注册; 服务端DataTypeDefinition属性读取尚未接入 - 嵌套结构体未注册定义时该字段取不到 (返回 null); 递归深度上限 100, 数组长度上限 1M, String/ByteString 上限 16MB, 超限走
_status - DiagnosticInfo 跳过
最佳实践
- 共享一个 decoder, 字段表缓存复用
- 先
registerDefinition再decodeBody, 定义按 dataTypeId 逐条注册 - 嵌套结构体取出后强转
Map<String, Object>; 数组字段取出后强转List<Object> - Union 解出的 Map 只有 selector 选中的那一个字段
跨语言对照
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| StructDecoder | StructDecoder | StructDecoder | StructDecoder | StructDecoder | DarraUa_StructDecoderHandle |
| Decode | decode | decode | Decode | decode | DarraUa_StructDecoder_Decode |