问题描述
我正在使用 milo 0.5.4 来设置我自己的 Opcua 服务器,现在我尝试使用一个复杂的数据类型,它应该在结构中包含一个结构。
目前有效的是具有标准数据类型的结构。
我的自定义结构应该包含另一个名为 StatusstructType 的结构,它也实现了 UaStructure。
public class CustomStructType implements UaStructure {
public static final ExpandednodeId TYPE_ID = ExpandednodeId.parse(String.format("nsu=%s;s=%s",Namespace.NAMESPACE_URI,"DataType.CustomStructType"));
public static final ExpandednodeId BINARY_ENCODING_ID = ExpandednodeId.parse(String.format("nsu=%s;s=%s","DataType.CustomStructType.BinaryEncoding"));
private final String foo;
private final Integer bar;
private final boolean baz;
private final StatusstructType status;
@Override
public ExpandednodeId getTypeId() {
return TYPE_ID;
}
public CustomStructType(String foo,Integer bar,boolean baz,StatusstructType status) {
this.foo = foo;
this.bar = bar;
this.baz = baz;
this.status = status;
}
public CustomStructType() {
this(null,false,new StatusstructType());
}
public static class Codec extends GenericDataTypeCodec<CustomStructType> {
@Override
public Class<CustomStructType> getType() {
return CustomStructType.class;
}
@Override
public CustomStructType decode(SerializationContext context,UaDecoder decoder) throws UaSerializationException {
String foo = decoder.readString("Foo");
Integer bar = decoder.readInt32("Bar");
boolean baz = decoder.readBoolean("Baz");
Object statusstruct = decoder.readStruct("Status",StatusstructType.TYPE_ID);
StatusstructType statusstructure = new StatusstructType();
if (statusstruct.getClass().isAssignableFrom(StatusstructType.class)) {
statusstructure = (StatusstructType) statusstruct;
}
return new CustomStructType(foo,bar,baz,statusstructure);
}
@Override
public void encode(SerializationContext context,UaEncoder encoder,CustomStructType value) throws UaSerializationException {
encoder.writeString("Foo",value.foo);
encoder.writeInt32("Bar",value.bar);
encoder.writeBoolean("Baz",value.baz);
encoder.writeStruct("Status",value.status,StatusstructType.TYPE_ID);
}
}
}
当我想读取这个 CustomStructType 节点时:
UaVariableNode node = client.getAddressspace().getvariableNode(new NodeId(nodeNamespaceIndex,nodeIdentifier));
DataValue value = node.readValue();
Variant variant = value.getValue();
ExtensionObject xo = (ExtensionObject) variant.getValue();
CustomStructType decoded = (CustomStructType) xo.decode(client.getSerializationContext());
当谈到 xo.decode 我得到一个 org.eclipse.milo.opcua.stack.core.UaSerializationException: no codec registered: NodeId{ns=2,id=DataType.StatusstructType} Exception
但我事先注册了编解码器:
NodeId statusbinaryEncodingId = StatusstructType.BINARY_ENCODING_ID.toNodeId(client.getNamespaceTable()).orElseThrow(() -> new IllegalStateException("namespace not found"));
client.getDataTypeManager().registerCodec(statusbinaryEncodingId,new StatusstructType.Codec().asBinaryCodec());
所以我的问题是是否有可能在结构中使用 milo UaStructures 构建结构?当是的时候,我错过了什么?
解决方法
当您注册编解码器时,请确保调用两个寄存器重载。
更新后的示例如下所示:
NodeId dataTypeId = CustomStructType.TYPE_ID
.toNodeId(client.getNamespaceTable())
.orElseThrow(() -> new IllegalStateException("namespace not found"));
NodeId binaryEncodingId = CustomStructType.BINARY_ENCODING_ID
.toNodeId(client.getNamespaceTable())
.orElseThrow(() -> new IllegalStateException("namespace not found"));
// Register codec with the client DataTypeManager instance
client.getDataTypeManager().registerCodec(
binaryEncodingId,new CustomStructType.Codec().asBinaryCodec()
);
client.getDataTypeManager().registerCodec(
new QualifiedName(dataTypeId.getNamespaceIndex(),"CustomStructType"),dataTypeId,new CustomStructType.Codec().asBinaryCodec()
);