【问题标题】:OPCUA milo custom Data Type Structure in Structure possible?结构中的 OPCUA milo 自定义数据类型结构可能吗?
【发布时间】:2021-06-07 08:56:44
【问题描述】:

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

【问题讨论】:

  • 应该是可以的。你能把所有的代码分享到我可以玩的地方看看发生了什么吗?
  • 啊,我实际上认为问题在于,如果您手动注册编解码器而不注册 DataTypeDictionary,您应该同时调用 registerCodec 重载,因为当它被嵌入时像这样,它试图通过其 DataType id 获取编解码器实例,而不是它的编码 id。
  • 抱歉,我无法共享整个代码,因为它嵌入在企业软件中。我不完全知道你的意思,但我在创建命名空间时也在 DataTypeDictionary 中注册了这两个结构。可以举个例子吗?
  • 我的意思是你必须调用client.getDataTypeManager().registerCodec() 使用方法的两个重载的两个结构 - 你正在使用的一个要求 encoding id 以及另一个它要求输入编码名称、数据类型 ID 和编解码器。查看DefaultDataTypeManager::registerTypeDictionary 的实现以了解我的意思 - 它的作用与调用两种注册方法的作用相同。

标签: java opc-ua milo


【解决方案1】:

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

更新后的示例如下所示:

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

【讨论】:

  • 您好,我调用了两个寄存器重载,但这也没有用。我不得不将自定义结构类型的父类从 GenericDataTypeCodec 更改为 OpcUaBinaryDataTypeCodec。现在我可以在结构中阅读我的结构:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多