Search in sources :

Example 1 with CustomStructType

use of org.eclipse.milo.examples.server.types.CustomStructType in project milo by eclipse.

the class ExampleNamespace method addCustomStructTypeVariable.

private void addCustomStructTypeVariable(UaFolderNode rootFolder) throws Exception {
    NodeId dataTypeId = CustomStructType.TYPE_ID.toNodeIdOrThrow(getServer().getNamespaceTable());
    NodeId binaryEncodingId = CustomStructType.BINARY_ENCODING_ID.toNodeIdOrThrow(getServer().getNamespaceTable());
    UaVariableNode customStructTypeVariable = UaVariableNode.builder(getNodeContext()).setNodeId(newNodeId("HelloWorld/CustomStructTypeVariable")).setAccessLevel(AccessLevel.READ_WRITE).setUserAccessLevel(AccessLevel.READ_WRITE).setBrowseName(newQualifiedName("CustomStructTypeVariable")).setDisplayName(LocalizedText.english("CustomStructTypeVariable")).setDataType(dataTypeId).setTypeDefinition(Identifiers.BaseDataVariableType).build();
    CustomStructType value = new CustomStructType("foo", uint(42), true);
    ExtensionObject xo = ExtensionObject.encodeDefaultBinary(getServer().getSerializationContext(), value, binaryEncodingId);
    customStructTypeVariable.setValue(new DataValue(new Variant(xo)));
    getNodeManager().addNode(customStructTypeVariable);
    customStructTypeVariable.addReference(new Reference(customStructTypeVariable.getNodeId(), Identifiers.Organizes, rootFolder.getNodeId().expanded(), false));
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) UaVariableNode(org.eclipse.milo.opcua.sdk.server.nodes.UaVariableNode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) Reference(org.eclipse.milo.opcua.sdk.core.Reference) CustomStructType(org.eclipse.milo.examples.server.types.CustomStructType) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)

Example 2 with CustomStructType

use of org.eclipse.milo.examples.server.types.CustomStructType in project milo by eclipse.

the class ReadWriteCustomDataTypeNodeExample method run.

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
    // synchronous connect
    client.connect().get();
    registerCustomCodec(client);
    // synchronous read request via VariableNode
    UaVariableNode node = client.getAddressSpace().getVariableNode(new NodeId(2, "HelloWorld/CustomStructTypeVariable"));
    logger.info("DataType={}", node.getDataType());
    // Read the current value
    DataValue value = node.readValue();
    logger.info("Value={}", value);
    Variant variant = value.getValue();
    ExtensionObject xo = (ExtensionObject) variant.getValue();
    CustomStructType decoded = (CustomStructType) xo.decode(client.getStaticSerializationContext());
    logger.info("Decoded={}", decoded);
    // Write a modified value
    CustomStructType modified = new CustomStructType(decoded.getFoo() + "bar", uint(decoded.getBar().intValue() + 1), !decoded.isBaz());
    ExtensionObject modifiedXo = ExtensionObject.encode(client.getStaticSerializationContext(), modified, xo.getEncodingId(), OpcUaDefaultBinaryEncoding.getInstance());
    node.writeValue(new DataValue(new Variant(modifiedXo)));
    // Read the modified value back
    value = node.readValue();
    logger.info("Value={}", value);
    variant = value.getValue();
    xo = (ExtensionObject) variant.getValue();
    decoded = (CustomStructType) xo.decode(client.getStaticSerializationContext());
    logger.info("Decoded={}", decoded);
    future.complete(client);
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) UaVariableNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) CustomStructType(org.eclipse.milo.examples.server.types.CustomStructType) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)

Aggregations

CustomStructType (org.eclipse.milo.examples.server.types.CustomStructType)2 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)2 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)2 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)2 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)2 UaVariableNode (org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode)1 Reference (org.eclipse.milo.opcua.sdk.core.Reference)1 UaVariableNode (org.eclipse.milo.opcua.sdk.server.nodes.UaVariableNode)1