Step 1: Set the input arguments. For example, the method below multiplies two Doubles. You will set your values appropriately, e.g. Integer and Byte:
NodeId objectId = new NodeId(4, "Demo.Method");
NodeId methodId = new NodeId(4, "Demo.Method.Multiply");
List<Object> inputArguments = new ArrayList<>();
inputArguments.add(new Double(4.0));
inputArguments.add(new Double(2.0));
Step 2: Call the method and get the result (8.0):
List<Object> outputs = uaClient.callMethodSynch(objectId, methodId, inputArguments);
callMethodSynch() is implemented as below:
private static final int REQUEST_TIMEOUT = 5000;
private static final TimeUnit REQUEST_TIMEOUT_UNIT = TimeUnit.MILLISECONDS;
public synchronized List<Object> callMethodSynch(NodeId objectId, NodeId methodId, List<Object> inputArguments)
throws Exception {
try {
Variant[] variants = new Variant[inputArguments.size()];
for (int i = 0; i < inputArguments.size(); i++) {
variants[i] = new Variant(inputArguments.get(i));
}
CallMethodRequest request = new CallMethodRequest(objectId, methodId, variants);
CompletableFuture<List<Object>> cf = opcUaClient.call(request).thenCompose(result -> {
StatusCode statusCode = result.getStatusCode();
if (statusCode.isGood()) {
Variant[] outputArguments = result.getOutputArguments();
List<Object> outputs = new ArrayList<>(outputArguments.length);
for (int i = 0; i < outputArguments.length; i++) {
outputs.add(outputArguments[i].getValue());
}
return CompletableFuture.completedFuture(outputs);
} else {
CompletableFuture<List<Object>> f = new CompletableFuture<>();
f.completeExceptionally(new org.eclipse.milo.opcua.stack.core.UaException(statusCode));
return f;
}
});
List<Object> results = cf.get(REQUEST_TIMEOUT, REQUEST_TIMEOUT_UNIT);
clientFuture.complete(opcUaClient);
return results;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}