Search in sources :

Example 6 with UaNode

use of org.eclipse.milo.opcua.sdk.client.nodes.UaNode in project iot-tree by bambooww.

the class ConnPtOPCUA method browseNodesOut.

public static void browseNodesOut(Writer w, OpcUaClient client) throws Exception {
    // client.getAddressSpace().
    UaNode uan = client.getAddressSpace().getNode(Identifiers.RootFolder);
    if (uan == null)
        return;
    browseNodesOut(w, "", client, uan);
}
Also used : UaNode(org.eclipse.milo.opcua.sdk.client.nodes.UaNode)

Example 7 with UaNode

use of org.eclipse.milo.opcua.sdk.client.nodes.UaNode in project iot-tree by bambooww.

the class ConnPtOPCUA method listBindBeSelectedItems.

private void listBindBeSelectedItems(List<BindItem> bis, OpcUaClient client, String ppath, UaNode n) throws Exception {
    NodeId nid = n.getNodeId();
    String bn = n.getBrowseName().getName();
    if (n instanceof UaVariableNode) {
        UaVariableNode uvn = (UaVariableNode) n;
        DataValue dataval = uvn.getValue();
        Object valob = dataval.getValue().getValue();
        if (valob != null && valob.getClass().isArray()) {
            Object[] objectArray = (Object[]) valob;
            valob = Arrays.deepToString(objectArray);
        }
        String val_dt = Convert.toFullYMDHMS(dataval.getSourceTime().getJavaDate());
        NodeId dt = uvn.getDataType();
        UaVariableTypeNode vtn = uvn.getTypeDefinition();
        UInteger[] arrdim = vtn.getArrayDimensions();
        UaDataTypeNode tpnode = (UaDataTypeNode) uaClient.getAddressSpace().getNode(dt);
        String datatp = tpnode.getBrowseName().getName();
        String arr_dim = "";
        if (arrdim != null && arrdim.length > 0) {
            arr_dim = Convert.combineWith(arrdim, ',');
        }
        BindItem bi = new BindItem(ppath + "/" + bn, datatp);
        bi.setVal(valob);
        bis.add(bi);
        return;
    }
    List<? extends UaNode> nodes = client.getAddressSpace().browseNodes(n);
    if (nodes != null && nodes.size() > 0) {
        if (Convert.isNullOrEmpty(ppath))
            ppath = bn;
        else
            ppath += "/" + bn;
        for (UaNode node : nodes) {
            String bn0 = node.getBrowseName().getName();
            if (bn0.startsWith("_"))
                continue;
            listBindBeSelectedItems(bis, client, ppath, node);
        }
    }
}
Also used : DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) BindItem(org.iottree.core.conn.ConnPtBinder.BindItem) UaNode(org.eclipse.milo.opcua.sdk.client.nodes.UaNode) UaDataTypeNode(org.eclipse.milo.opcua.sdk.client.nodes.UaDataTypeNode) UaVariableNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode) UaVariableTypeNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableTypeNode) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) JSONObject(org.json.JSONObject)

Example 8 with UaNode

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

the class AddressSpace method browseNodesAsync.

/**
 * Browse from {@code nodeId} using {@code browseOptions}.
 * <p>
 * This call completes asynchronously.
 *
 * @param nodeId        the {@link NodeId} to start the browse from.
 * @param browseOptions the {@link BrowseOptions} to use.
 * @return a CompletableFuture that completes successfully with a List of {@link UaNode}s
 * referenced by {@code node} given the currently configured {@link BrowseOptions} or completes
 * exceptionally if a service-level error occurs.
 */
public CompletableFuture<List<? extends UaNode>> browseNodesAsync(NodeId nodeId, BrowseOptions browseOptions) {
    BrowseDescription browseDescription = new BrowseDescription(nodeId, browseOptions.getBrowseDirection(), browseOptions.getReferenceTypeId(), browseOptions.isIncludeSubtypes(), browseOptions.getNodeClassMask(), uint(BrowseResultMask.All.getValue()));
    CompletableFuture<List<ReferenceDescription>> browse = BrowseHelper.browse(client, browseDescription, browseOptions.getMaxReferencesPerNode());
    return browse.thenCompose(references -> {
        List<CompletableFuture<? extends UaNode>> cfs = references.stream().map(reference -> {
            NodeClass nodeClass = reference.getNodeClass();
            ExpandedNodeId xNodeId = reference.getNodeId();
            ExpandedNodeId xTypeDefinitionId = reference.getTypeDefinition();
            switch(nodeClass) {
                case Object:
                case Variable:
                    {
                        CompletableFuture<CompletableFuture<? extends UaNode>> ff = toNodeIdAsync(xNodeId).thenCombine(toNodeIdAsync(xTypeDefinitionId), (targetNodeId, typeDefinitionId) -> {
                            if (nodeClass == NodeClass.Object) {
                                return getObjectNodeAsync(targetNodeId, typeDefinitionId);
                            } else {
                                return getVariableNodeAsync(targetNodeId, typeDefinitionId);
                            }
                        });
                        return unwrap(ff).exceptionally(ex -> {
                            logger.warn("Failed to create Node from Reference to {}", reference.getNodeId(), ex);
                            return null;
                        });
                    }
                default:
                    {
                        // TODO specialized getNode for other NodeClasses?
                        return toNodeIdAsync(xNodeId).thenCompose(this::getNodeAsync).exceptionally(ex -> {
                            logger.warn("Failed to create Node from Reference to {}", reference.getNodeId(), ex);
                            return null;
                        });
                    }
            }
        }).collect(Collectors.toList());
        return sequence(cfs);
    });
}
Also used : BuiltinReferenceType(org.eclipse.milo.opcua.stack.core.BuiltinReferenceType) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) UaDataTypeNode(org.eclipse.milo.opcua.sdk.client.nodes.UaDataTypeNode) LoggerFactory(org.slf4j.LoggerFactory) UaObjectTypeNode(org.eclipse.milo.opcua.sdk.client.nodes.UaObjectTypeNode) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) BrowseDescription(org.eclipse.milo.opcua.stack.core.types.structured.BrowseDescription) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) UaMethodNode(org.eclipse.milo.opcua.sdk.client.nodes.UaMethodNode) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) 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) Set(java.util.Set) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) Objects(java.util.Objects) UaVariableTypeNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableTypeNode) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) NodeClass(org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass) UaNode(org.eclipse.milo.opcua.sdk.client.nodes.UaNode) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) Optional(java.util.Optional) UaVariableNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) CompletableFuture(java.util.concurrent.CompletableFuture) UaObjectNode(org.eclipse.milo.opcua.sdk.client.nodes.UaObjectNode) ArrayList(java.util.ArrayList) ConversionUtil.l(org.eclipse.milo.opcua.stack.core.util.ConversionUtil.l) ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) FutureUtils.failedUaFuture(org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedUaFuture) Logger(org.slf4j.Logger) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) LocalizedText(org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) BrowseResultMask(org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseResultMask) FutureUtils.failedFuture(org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedFuture) ObjectNodeConstructor(org.eclipse.milo.opcua.sdk.client.ObjectTypeManager.ObjectNodeConstructor) UaException(org.eclipse.milo.opcua.stack.core.UaException) Preconditions(com.google.common.base.Preconditions) UaReferenceTypeNode(org.eclipse.milo.opcua.sdk.client.nodes.UaReferenceTypeNode) Collections(java.util.Collections) UaViewNode(org.eclipse.milo.opcua.sdk.client.nodes.UaViewNode) NodeClass(org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) CompletableFuture(java.util.concurrent.CompletableFuture) UaNode(org.eclipse.milo.opcua.sdk.client.nodes.UaNode) List(java.util.List) ArrayList(java.util.ArrayList) BrowseDescription(org.eclipse.milo.opcua.stack.core.types.structured.BrowseDescription)

Example 9 with UaNode

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

the class AddressSpace method getVariableNodeAsync.

/**
 * Get a {@link UaVariableNode} instance for the VariableNode identified by {@code nodeId},
 * assuming the type definition identified by {@code typeDefinitionId}.
 * <p>
 * If this type definition is registered with the {@link VariableTypeManager} a
 * {@link UaVariableNode} of the appropriate subclass will be returned.
 * <p>
 * This call completes asynchronously.
 *
 * @param nodeId           the {@link NodeId} identifying the VariableNode to get.
 * @param typeDefinitionId the {@link NodeId} identifying the type definition.
 * @return a CompletableFuture that completes successfully with a {@link UaVariableNode}
 * instance for the VariableNode identified by {@code nodeId} or completes exceptionally if an
 * error occurs while creating the VariableNode.
 */
public CompletableFuture<UaVariableNode> getVariableNodeAsync(NodeId nodeId, NodeId typeDefinitionId) {
    UaNode cachedNode = nodeCache.getIfPresent(nodeId);
    if (cachedNode instanceof UaVariableNode) {
        return completedFuture((UaVariableNode) cachedNode);
    } else {
        CompletableFuture<ReadResponse> future = readAttributes(nodeId, AttributeId.VARIABLE_ATTRIBUTES);
        return future.thenCompose(response -> {
            List<DataValue> attributeValues = l(response.getResults());
            try {
                UaVariableNode node = newVariableNode(nodeId, typeDefinitionId, attributeValues);
                nodeCache.put(node.getNodeId(), node);
                return completedFuture(node);
            } catch (UaException e) {
                return failedFuture(e);
            }
        });
    }
}
Also used : UaVariableNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode) ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) UaException(org.eclipse.milo.opcua.stack.core.UaException) UaNode(org.eclipse.milo.opcua.sdk.client.nodes.UaNode)

Example 10 with UaNode

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

the class AddressSpaceTest method modifyBrowseOptions.

@Test
public void modifyBrowseOptions() throws UaException {
    AddressSpace addressSpace = client.getAddressSpace();
    UaNode serverNode = addressSpace.getNode(Identifiers.Server);
    {
        addressSpace.modifyBrowseOptions(b -> b.setNodeClassMask(EnumSet.of(NodeClass.Method)));
        List<? extends UaNode> nodes = addressSpace.browseNodes(serverNode);
        assertFalse(nodes.isEmpty());
        assertTrue(nodes.stream().allMatch(n -> n.getNodeClass() == NodeClass.Method));
    }
    {
        addressSpace.modifyBrowseOptions(b -> b.setNodeClassMask(EnumSet.of(NodeClass.Object)));
        List<? extends UaNode> nodes = addressSpace.browseNodes(serverNode);
        assertFalse(nodes.isEmpty());
        assertTrue(nodes.stream().allMatch(n -> n.getNodeClass() == NodeClass.Object));
    }
    {
        addressSpace.modifyBrowseOptions(b -> b.setNodeClassMask(EnumSet.of(NodeClass.Variable)));
        List<? extends UaNode> nodes = addressSpace.browseNodes(serverNode);
        assertFalse(nodes.isEmpty());
        assertTrue(nodes.stream().allMatch(n -> n.getNodeClass() == NodeClass.Variable));
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BrowseOptions(org.eclipse.milo.opcua.sdk.client.AddressSpace.BrowseOptions) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) BuiltinReferenceType(org.eclipse.milo.opcua.stack.core.BuiltinReferenceType) BrowseDirection(org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseDirection) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) Test(org.junit.jupiter.api.Test) List(java.util.List) NodeClass(org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) UaNode(org.eclipse.milo.opcua.sdk.client.nodes.UaNode) AbstractClientServerTest(org.eclipse.milo.opcua.sdk.test.AbstractClientServerTest) UaException(org.eclipse.milo.opcua.stack.core.UaException) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ServerStatusTypeNode(org.eclipse.milo.opcua.sdk.client.model.nodes.variables.ServerStatusTypeNode) ServerTypeNode(org.eclipse.milo.opcua.sdk.client.model.nodes.objects.ServerTypeNode) UaVariableNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EnumSet(java.util.EnumSet) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) UaNode(org.eclipse.milo.opcua.sdk.client.nodes.UaNode) List(java.util.List) Test(org.junit.jupiter.api.Test) AbstractClientServerTest(org.eclipse.milo.opcua.sdk.test.AbstractClientServerTest)

Aggregations

UaNode (org.eclipse.milo.opcua.sdk.client.nodes.UaNode)28 UaException (org.eclipse.milo.opcua.stack.core.UaException)16 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)15 UaVariableNode (org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode)12 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)9 AbstractClientServerTest (org.eclipse.milo.opcua.sdk.test.AbstractClientServerTest)8 Test (org.junit.jupiter.api.Test)8 List (java.util.List)7 ExecutionException (java.util.concurrent.ExecutionException)7 ServerTypeNode (org.eclipse.milo.opcua.sdk.client.model.nodes.objects.ServerTypeNode)6 Identifiers (org.eclipse.milo.opcua.stack.core.Identifiers)6 EnumSet (java.util.EnumSet)5 BrowseOptions (org.eclipse.milo.opcua.sdk.client.AddressSpace.BrowseOptions)5 StatusCodes (org.eclipse.milo.opcua.stack.core.StatusCodes)5 BrowseDirection (org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseDirection)5 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)5 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)5 Assertions.assertSame (org.junit.jupiter.api.Assertions.assertSame)5 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)5 IOException (java.io.IOException)4