I'm using milo 0.5.4 to setup my own OpCUA server now I try to use a complex Datatype which should include a structure inside a structure.
What works so far is a structure with standard datatypes.
My Custom Structure should include another Structure called StatusStructType which also implements 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);
}
}
}
When I want to read this CustomStructType Node:
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());
when it comes to xo.decode I get a org.eclipse.milo.opcua.stack.core.UaSerializationException: no codec registered: NodeId{ns=2, id=DataType.StatusStructType} Exception
But I register the codec beforehand with:
NodeId statusbinaryEncodingId = StatusStructType.BINARY_ENCODING_ID.toNodeId(client.getNamespaceTable()).orElseThrow(() -> new IllegalStateException("namespace not found"));
client.getDataTypeManager().registerCodec(statusbinaryEncodingId, new StatusStructType.Codec().asBinaryCodec());
So my question is if it is even possible to have a structure in structure construct with milo UaStructures? And when Yes what am I missing?
When you register your codecs make sure to call both register overloads.
The updated example would look like this:
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()
);