milo icon indicating copy to clipboard operation
milo copied to clipboard

How to decoding the data to be subscribed

Open 0DennisLee0 opened this issue 6 years ago • 5 comments

Hello kevin, I want to subcribe a node which is encoded.Such as the CustomDataTypeNode in the example.And the codec is

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");
            UInteger bar = decoder.readUInt32("Bar");
            boolean baz = decoder.readBoolean("Baz");

            return new CustomStructType(foo, bar, baz);
        }

        @Override
        public void encode(
            SerializationContext context,
            UaEncoder encoder, CustomStructType value) throws UaSerializationException {

            encoder.writeString("Foo", value.foo);
            encoder.writeUInt32("Bar", value.bar);
            encoder.writeBoolean("Baz", value.baz);
        }
    }

And the decoding step I have already know

Variant variant = value.getValue();
        ExtensionObject xo = (ExtensionObject) variant.getValue();

        CustomStructType decoded = (CustomStructType) xo.decode(
            client.getSerializationContext()
        );

However, if I want to subscribe another node, I have to repeat in this way to match the data decoding.Is there any easier way to implement this?Or is the there any generic decoding utils in milo?

0DennisLee0 avatar Feb 24 '20 08:02 0DennisLee0

If the server you're connecting to has defined all the custom types in its DataTypeDictionary and they are properly exposed in the address space then yes, the client will automatically read those and you can access them generically.

By default the client is configured with a GenericBsdParser instance that reads custom types as a org.eclipse.milo.opcua.binaryschema.Struct instance. There's also a JsonBsdParser you can configure that will read them as a GSON JsonObject.

This won't work against the Milo example server (at least not in 0.3.x... should work in dev/0.4 branch), but it generally works with demo servers from other vendors.

kevinherron avatar Feb 24 '20 16:02 kevinherron

If the server you're connecting to has defined all the custom types in its DataTypeDictionary and they are properly exposed in the address space then yes, the client will automatically read those and you can access them generically.

By default the client is configured with a GenericBsdParser instance that reads custom types as a org.eclipse.milo.opcua.binaryschema.Struct instance. There's also a JsonBsdParser you can configure that will read them as a GSON JsonObject.

This won't work against the Milo example server (at least not in 0.3.x... should work in dev/0.4 branch), but it generally works with demo servers from other vendors.

3Q,I will work on it then

0DennisLee0 avatar Feb 25 '20 05:02 0DennisLee0

Take a look at UnifiedAutomationReadCustomDataTypeExample.

kevinherron avatar Feb 25 '20 05:02 kevinherron

Take a look at UnifiedAutomationReadCustomDataTypeExample.

OK,and I want to know if it works well on the open62541 server,too?

0DennisLee0 avatar Feb 26 '20 01:02 0DennisLee0

It should work with open62541 or any other server as long as they put the information about the datatype into the address space.

kevinherron avatar Feb 26 '20 14:02 kevinherron