如何解决OPCUA milo 自定义结构中的数据类型结构可能吗?
我正在使用 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()
);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。