Search in sources :

Example 6 with AddressSpace

use of org.eclipse.milo.opcua.sdk.client.AddressSpace in project milo by eclipse.

the class UaMethodTest method findMethod.

@Test
public void findMethod() throws UaException {
    ManagedSubscription subscription = ManagedSubscription.create(client);
    ManagedDataItem dataItem = subscription.createDataItem(Identifiers.Server_ServerStatus_CurrentTime);
    AddressSpace addressSpace = client.getAddressSpace();
    UaObjectNode serverNode = addressSpace.getObjectNode(Identifiers.Server);
    UaMethod getMonitoredItems = serverNode.getMethod("GetMonitoredItems");
    assertNotNull(getMonitoredItems);
    Argument[] inputArguments = getMonitoredItems.getInputArguments();
    Argument[] outputArguments = getMonitoredItems.getOutputArguments();
    assertEquals(1, inputArguments.length);
    assertEquals("SubscriptionId", inputArguments[0].getName());
    assertEquals(2, outputArguments.length);
    assertEquals("ServerHandles", outputArguments[0].getName());
    assertEquals("ClientHandles", outputArguments[1].getName());
    Variant[] outputs = getMonitoredItems.call(new Variant[] { new Variant(subscription.getSubscription().getSubscriptionId()) });
    UInteger[] expected0 = { dataItem.getMonitoredItem().getMonitoredItemId() };
    UInteger[] expected1 = { dataItem.getMonitoredItem().getClientHandle() };
    assertArrayEquals(expected0, (UInteger[]) outputs[0].getValue());
    assertArrayEquals(expected1, (UInteger[]) outputs[1].getValue());
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) AddressSpace(org.eclipse.milo.opcua.sdk.client.AddressSpace) UaObjectNode(org.eclipse.milo.opcua.sdk.client.nodes.UaObjectNode) Argument(org.eclipse.milo.opcua.stack.core.types.structured.Argument) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) ManagedDataItem(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedDataItem) ManagedSubscription(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription) Test(org.junit.jupiter.api.Test) AbstractClientServerTest(org.eclipse.milo.opcua.sdk.test.AbstractClientServerTest)

Example 7 with AddressSpace

use of org.eclipse.milo.opcua.sdk.client.AddressSpace in project milo by eclipse.

the class UaMethodTest method callMethodWithHasOrderedComponentReference.

@Test
public void callMethodWithHasOrderedComponentReference() throws UaException {
    AddressSpace addressSpace = client.getAddressSpace();
    UaObjectNode objectsNode = addressSpace.getObjectNode(Identifiers.ObjectsFolder);
    Variant[] outputs = objectsNode.callMethod(new QualifiedName(2, "sqrt2(x)"), new Variant[] { new Variant(16.0) });
    assertEquals(4.0, outputs[0].getValue());
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) AddressSpace(org.eclipse.milo.opcua.sdk.client.AddressSpace) UaObjectNode(org.eclipse.milo.opcua.sdk.client.nodes.UaObjectNode) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) Test(org.junit.jupiter.api.Test) AbstractClientServerTest(org.eclipse.milo.opcua.sdk.test.AbstractClientServerTest)

Example 8 with AddressSpace

use of org.eclipse.milo.opcua.sdk.client.AddressSpace in project milo by eclipse.

the class UaMethodTest method callMethodException.

@Test
public void callMethodException() throws UaException {
    AddressSpace addressSpace = client.getAddressSpace();
    UaObjectNode serverNode = addressSpace.getObjectNode(Identifiers.Server);
    assertThrows(UaMethodException.class, () -> serverNode.callMethod("GetMonitoredItems", new Variant[] { new Variant(uint(0)) }));
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) AddressSpace(org.eclipse.milo.opcua.sdk.client.AddressSpace) UaObjectNode(org.eclipse.milo.opcua.sdk.client.nodes.UaObjectNode) Test(org.junit.jupiter.api.Test) AbstractClientServerTest(org.eclipse.milo.opcua.sdk.test.AbstractClientServerTest)

Example 9 with AddressSpace

use of org.eclipse.milo.opcua.sdk.client.AddressSpace in project milo by eclipse.

the class UaMethodTest method callMethod.

@Test
public void callMethod() throws UaException {
    ManagedSubscription subscription = ManagedSubscription.create(client);
    ManagedDataItem dataItem = subscription.createDataItem(Identifiers.Server_ServerStatus_CurrentTime);
    AddressSpace addressSpace = client.getAddressSpace();
    UaObjectNode serverNode = addressSpace.getObjectNode(Identifiers.Server);
    Variant[] outputs = serverNode.callMethod("GetMonitoredItems", new Variant[] { new Variant(subscription.getSubscription().getSubscriptionId()) });
    UInteger[] expected0 = { dataItem.getMonitoredItem().getMonitoredItemId() };
    UInteger[] expected1 = { dataItem.getMonitoredItem().getClientHandle() };
    assertArrayEquals(expected0, (UInteger[]) outputs[0].getValue());
    assertArrayEquals(expected1, (UInteger[]) outputs[1].getValue());
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) AddressSpace(org.eclipse.milo.opcua.sdk.client.AddressSpace) UaObjectNode(org.eclipse.milo.opcua.sdk.client.nodes.UaObjectNode) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) ManagedDataItem(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedDataItem) ManagedSubscription(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription) Test(org.junit.jupiter.api.Test) AbstractClientServerTest(org.eclipse.milo.opcua.sdk.test.AbstractClientServerTest)

Example 10 with AddressSpace

use of org.eclipse.milo.opcua.sdk.client.AddressSpace 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

AddressSpace (org.eclipse.milo.opcua.sdk.client.AddressSpace)10 UaObjectNode (org.eclipse.milo.opcua.sdk.client.nodes.UaObjectNode)9 AbstractClientServerTest (org.eclipse.milo.opcua.sdk.test.AbstractClientServerTest)9 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)9 Test (org.junit.jupiter.api.Test)9 QualifiedName (org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName)6 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)3 ManagedDataItem (org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedDataItem)2 ManagedSubscription (org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription)2 Argument (org.eclipse.milo.opcua.stack.core.types.structured.Argument)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 OpcUaClient (org.eclipse.milo.opcua.sdk.client.OpcUaClient)1 UaMethod (org.eclipse.milo.opcua.sdk.client.methods.UaMethod)1 UaMethodException (org.eclipse.milo.opcua.sdk.client.methods.UaMethodException)1 ObjectNode (org.eclipse.milo.opcua.sdk.core.nodes.ObjectNode)1 ObjectNodeProperties (org.eclipse.milo.opcua.sdk.core.nodes.ObjectNodeProperties)1