首页 > 解决方案 > 结构中的 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", Namespace.NAMESPACE_URI, "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, 0, 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} 异常

但我事先注册了编解码器:

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 在结构中构建结构?什么时候是的,我错过了什么?

标签: javaopc-uamilo

解决方案


注册编解码器时,请确保调用两个寄存器重载。

更新后的示例如下所示:

    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()
    );

推荐阅读