Skip to content

How to get the DataType of an UaNode?

I am using the eclipse-milo`s jars, version 0.5.3.

I would like to read specific nodes, and store their values in a database. I intend to dynamically construct the database column, taking into consideration the data type. For example: a node of type Float (Identifiers.Float) would be a FLOAT in the database.

I can connect to an OPCUA Server, retrieve the AddressSpace and read the node value.

UaNode node = uaClient.getAddressSpace().getNode(nodeId);
DataValue dataValue = node.readAttribute(AttributeId.Value);
Object value = dataValue.getValue().getValue();

How can I read the type of the value of a given Node? In the above example, the datatype of node. I have tried the following:

Optional<ExpandedNodeId> dataType = dataValue.getValue().getDataType();        
if (dataType.isPresent()) {
    ExpandedNodeId nodeDataType = dataType.get();
    nodeDataType.getIdentifier();
}

The identifier of the data type is received (ns=0;i=10), but not the type.

Answer

DataType’s in OPC UA are identified by a NodeId, so what you’re seeing is normal.

If you need assistance resolving a datatype to a “backing” class you might look at the DataTypeTree class for assistance:

https://github.com/eclipse/milo/blob/master/opc-ua-sdk/sdk-core/src/main/java/org/eclipse/milo/opcua/sdk/core/DataTypeTree.java

https://github.com/eclipse/milo/blob/master/opc-ua-sdk/integration-tests/src/test/java/org/eclipse/milo/opcua/sdk/core/DataTypeTreeTest.java


DataTypeTree tree = DataTypeTreeBuilder.build(client);

UaVariableNode currentTimeNode = client.getAddressSpace()
    .getVariableNode(Identifiers.Server_ServerStatus_CurrentTime);

NodeId dataType = currentTimeNode.getDataType();

Class<?> clazz = tree.getBackingClass(dataType);

System.out.println(clazz); // class org.eclipse.milo.opcua.stack.core.types.builtin.DateTime