Search in sources :

Example 1 with UaMethod

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);
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) UaObjectNode(org.eclipse.milo.opcua.sdk.client.nodes.UaObjectNode) UaMethod(org.eclipse.milo.opcua.sdk.client.methods.UaMethod)

Example 2 with UaMethod

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"));
        }
    });
}
Also used : ObjectNode(org.eclipse.milo.opcua.sdk.core.nodes.ObjectNode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) UaMethod(org.eclipse.milo.opcua.sdk.client.methods.UaMethod) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) CompletableFuture(java.util.concurrent.CompletableFuture) ObjectNodeProperties(org.eclipse.milo.opcua.sdk.core.nodes.ObjectNodeProperties) AddressSpace(org.eclipse.milo.opcua.sdk.client.AddressSpace) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) Argument(org.eclipse.milo.opcua.stack.core.types.structured.Argument) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) ConversionUtil.l(org.eclipse.milo.opcua.stack.core.util.ConversionUtil.l) UShort(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort) BrowseDescription(org.eclipse.milo.opcua.stack.core.types.structured.BrowseDescription) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) FutureUtils.failedUaFuture(org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedUaFuture) BrowseResult(org.eclipse.milo.opcua.stack.core.types.structured.BrowseResult) ReferenceDescription(org.eclipse.milo.opcua.stack.core.types.structured.ReferenceDescription) BrowseDirection(org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseDirection) UByte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) LocalizedText(org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText) NamingRuleType(org.eclipse.milo.opcua.stack.core.types.enumerated.NamingRuleType) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) BrowseResultMask(org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseResultMask) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) NodeClass(org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) FutureUtils(org.eclipse.milo.opcua.stack.core.util.FutureUtils) FutureUtils.failedFuture(org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedFuture) UaException(org.eclipse.milo.opcua.stack.core.UaException) Optional(java.util.Optional) StreamUtil.opt2stream(org.eclipse.milo.opcua.sdk.core.util.StreamUtil.opt2stream) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) UaMethod(org.eclipse.milo.opcua.sdk.client.methods.UaMethod) UaException(org.eclipse.milo.opcua.stack.core.UaException) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) AddressSpace(org.eclipse.milo.opcua.sdk.client.AddressSpace) ReferenceDescription(org.eclipse.milo.opcua.stack.core.types.structured.ReferenceDescription) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) BrowseResult(org.eclipse.milo.opcua.stack.core.types.structured.BrowseResult) BrowseDescription(org.eclipse.milo.opcua.stack.core.types.structured.BrowseDescription)

Aggregations

UaMethod (org.eclipse.milo.opcua.sdk.client.methods.UaMethod)2 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)2 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 AddressSpace (org.eclipse.milo.opcua.sdk.client.AddressSpace)1 OpcUaClient (org.eclipse.milo.opcua.sdk.client.OpcUaClient)1 UaObjectNode (org.eclipse.milo.opcua.sdk.client.nodes.UaObjectNode)1 ObjectNode (org.eclipse.milo.opcua.sdk.core.nodes.ObjectNode)1 ObjectNodeProperties (org.eclipse.milo.opcua.sdk.core.nodes.ObjectNodeProperties)1 StreamUtil.opt2stream (org.eclipse.milo.opcua.sdk.core.util.StreamUtil.opt2stream)1 AttributeId (org.eclipse.milo.opcua.stack.core.AttributeId)1 Identifiers (org.eclipse.milo.opcua.stack.core.Identifiers)1 StatusCodes (org.eclipse.milo.opcua.stack.core.StatusCodes)1 UaException (org.eclipse.milo.opcua.stack.core.UaException)1 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)1 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)1 ExpandedNodeId (org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId)1