title: How do I reliably write to a OPC UA server?
body: <p>I'm trying to write some values to a OPC UA server. In order to do that reliably, I propably need to know the DataType of the node that I'm trying to write to, otherwise it seems I'm prone to getting datatype mismatches.</p>
  
  <p>Let's say my server has a node TestNodeOne with datatype Int16</p>
  
  <p>My API gets told to write the value 3 to that node. Now I need to make a decision: Do I handle the 3 as an Integer or as an UShort? For this, it seems that I would need the data type of my node.</p>
  
  <h2>What I've tried</h2>
  
  <p>My approach was to just browse the server and create a cache of all it's nodes with the corresponding data type. Here's how that looks:</p>
  
  <pre><code>// Recursively browse entire server
  @SuppressWarnings("deprecation")
  private HashMap&lt;String, NodeId&gt; browseNode(String indent, OpcUaClient client, NodeId browseRoot, HashMap&lt;String, NodeId&gt; nodesMap) {
  
      BrowseDescription browse = new BrowseDescription(
              browseRoot,
              BrowseDirection.Forward,
              Identifiers.References,
              true, uint(NodeClass.Object.getValue() | NodeClass.Variable.getValue()),
              uint(BrowseResultMask.All.getValue()));
  
      try {
          BrowseResult browseResult = client.browse(browse).get();
          List&lt;ReferenceDescription&gt; references = toList(browseResult.getReferences());
  
          for (ReferenceDescription rd : references) {
  
              UShort namespaceIndex = rd.getNodeId().getNamespaceIndex();
              String identifier = rd.getNodeId().getIdentifier().toString();
              NodeId node = new NodeId(namespaceIndex, identifier);
              nodesMap.put(rd.getNodeId().getIdentifier().toString(), node);
  
              logger.info("----------------------------------------------------------------");
              logger.info(identifier);
              logger.info("TYPE " + rd.getTypeDefinition()); // Not the right node
              logger.info("TYPE " + rd.getTypeId().getIdentifier().toString()); // Not the right node
              logger.info("TYPE " + rd.getReferenceTypeId().getIdentifier().toString()); // Not the right node
  
              rd.getNodeId().local().ifPresent(nodeId -&gt; {
                  browseNode(indent + "  ", client, nodeId, nodesMap);
              });
          }
  
      } catch (InterruptedException | ExecutionException e) {
          logger.error("Browsing nodeId={} failed: {}", browseRoot, e.getMessage(), e);
      }
  
      return nodesMap;
  }
  </code></pre>
  
  <p>I can't figure out how to get the datatype of a node. I think I'm on the wrong track here.
  How can I find out the data type of a node?</p>