use of org.eclipse.milo.opcua.sdk.client.methods.UaMethod in project milo by eclipse.
the class MethodExample2 method run.
@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connect().get();
UaObjectNode objectNode = client.getAddressSpace().getObjectNode(NodeId.parse("ns=2;s=HelloWorld"));
UaMethod sqrtMethod = objectNode.getMethod("sqrt(x)");
logArguments(client, sqrtMethod);
Variant[] inputs = { new Variant(16.0) };
Variant[] outputs = sqrtMethod.call(inputs);
logger.info("Input values: " + Arrays.toString(inputs));
logger.info("Output values: " + Arrays.toString(outputs));
future.complete(client);
}
use of org.eclipse.milo.opcua.sdk.client.methods.UaMethod in project milo by eclipse.
the class UaObjectNode method getMethodAsync.
/**
* Get the method named {@code methodName} on this Object, if it exists.
* <p>
* This call completes asynchronously.
*
* @param methodName the name of the method.
* @return a {@link CompletableFuture} that completes successfully with a {@link UaMethod} or
* completes exceptionally if an operation- or service-level error occurs or if a method named
* {@code methodName} could not be found.
*/
public CompletableFuture<UaMethod> getMethodAsync(QualifiedName methodName) {
UInteger nodeClassMask = uint(NodeClass.Method.getValue());
UInteger resultMask = uint(BrowseResultMask.All.getValue());
CompletableFuture<BrowseResult> future = client.browse(new BrowseDescription(getNodeId(), BrowseDirection.Forward, Identifiers.HasComponent, true, nodeClassMask, resultMask));
return future.thenCompose(result -> {
StatusCode statusCode = result.getStatusCode();
if (statusCode != null && statusCode.isGood()) {
Optional<ExpandedNodeId> methodNodeId = l(result.getReferences()).stream().filter(rd -> Objects.equals(methodName, rd.getBrowseName())).findFirst().map(ReferenceDescription::getNodeId);
return methodNodeId.map(xni -> {
AddressSpace addressSpace = client.getAddressSpace();
return addressSpace.toNodeIdAsync(xni).thenCompose(nodeId -> addressSpace.getNodeAsync(nodeId).thenCompose(node -> {
UaMethodNode methodNode = (UaMethodNode) node;
CompletableFuture<Argument[]> inputArguments = methodNode.readInputArgumentsAsync().exceptionally(t -> new Argument[0]);
CompletableFuture<Argument[]> outputArguments = methodNode.readOutputArgumentsAsync().exceptionally(t -> new Argument[0]);
return inputArguments.thenCombine(outputArguments, (in, out) -> new UaMethod(client, this, methodNode, in, out));
}));
}).orElse(failedFuture(new UaException(StatusCodes.Bad_NotFound, "method not found: " + methodName)));
} else {
return failedFuture(new UaException(statusCode, "browsing for MethodNodes failed"));
}
});
}
Aggregations