Search in sources :

Example 11 with ReadResponse

use of org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse in project milo by eclipse.

the class AddressSpace method createVariableNodeFromBaseAttributes.

private CompletableFuture<UaVariableNode> createVariableNodeFromBaseAttributes(NodeId nodeId, List<DataValue> baseAttributeValues) {
    Set<AttributeId> remainingAttributes = Sets.difference(AttributeId.VARIABLE_ATTRIBUTES, AttributeId.BASE_ATTRIBUTES);
    CompletableFuture<ReadResponse> attributesFuture = readAttributes(nodeId, remainingAttributes);
    CompletableFuture<NodeId> typeDefinitionFuture = readTypeDefinition(nodeId);
    return CompletableFuture.allOf(attributesFuture, typeDefinitionFuture).thenCompose(ignored -> {
        ReadResponse response = attributesFuture.join();
        NodeId typeDefinitionId = typeDefinitionFuture.join();
        List<DataValue> attributeValues = new ArrayList<>(baseAttributeValues);
        Collections.addAll(attributeValues, 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) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) UaException(org.eclipse.milo.opcua.stack.core.UaException) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) ArrayList(java.util.ArrayList)

Example 12 with ReadResponse

use of org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse in project milo by eclipse.

the class AddressSpace method getObjectNodeAsync.

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

use of org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse in project milo by eclipse.

the class AddressSpace method createReferenceTypeNodeFromBaseAttributes.

private CompletableFuture<UaReferenceTypeNode> createReferenceTypeNodeFromBaseAttributes(NodeId nodeId, List<DataValue> baseAttributeValues) {
    Set<AttributeId> remainingAttributes = Sets.difference(AttributeId.REFERENCE_TYPE_ATTRIBUTES, AttributeId.BASE_ATTRIBUTES);
    CompletableFuture<ReadResponse> attributesFuture = readAttributes(nodeId, remainingAttributes);
    return attributesFuture.thenCompose(response -> {
        List<DataValue> attributeValues = new ArrayList<>(baseAttributeValues);
        Collections.addAll(attributeValues, response.getResults());
        try {
            UaReferenceTypeNode node = newReferenceTypeNode(nodeId, attributeValues);
            nodeCache.put(node.getNodeId(), node);
            return completedFuture(node);
        } catch (UaException e) {
            return failedFuture(e);
        }
    });
}
Also used : ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) UaReferenceTypeNode(org.eclipse.milo.opcua.sdk.client.nodes.UaReferenceTypeNode) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) UaException(org.eclipse.milo.opcua.stack.core.UaException) ArrayList(java.util.ArrayList)

Example 14 with ReadResponse

use of org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse in project milo by eclipse.

the class AddressSpace method createObjectTypeNodeFromBaseAttributes.

private CompletableFuture<UaObjectTypeNode> createObjectTypeNodeFromBaseAttributes(NodeId nodeId, List<DataValue> baseAttributeValues) {
    Set<AttributeId> remainingAttributes = Sets.difference(AttributeId.OBJECT_TYPE_ATTRIBUTES, AttributeId.BASE_ATTRIBUTES);
    CompletableFuture<ReadResponse> attributesFuture = readAttributes(nodeId, remainingAttributes);
    return attributesFuture.thenCompose(response -> {
        List<DataValue> attributeValues = new ArrayList<>(baseAttributeValues);
        Collections.addAll(attributeValues, response.getResults());
        try {
            UaObjectTypeNode node = newObjectTypeNode(nodeId, attributeValues);
            nodeCache.put(node.getNodeId(), node);
            return completedFuture(node);
        } catch (UaException e) {
            return failedFuture(e);
        }
    });
}
Also used : UaObjectTypeNode(org.eclipse.milo.opcua.sdk.client.nodes.UaObjectTypeNode) ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) UaException(org.eclipse.milo.opcua.stack.core.UaException) ArrayList(java.util.ArrayList)

Example 15 with ReadResponse

use of org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse in project milo by eclipse.

the class ClientServerTest method setReadRequestHandler.

private void setReadRequestHandler(Variant variant) {
    server.addServiceHandler("/test", ReadRequest.TYPE_ID, service -> {
        ReadRequest request = (ReadRequest) service.getRequest();
        ResponseHeader header = new ResponseHeader(DateTime.now(), request.getRequestHeader().getRequestHandle(), StatusCode.GOOD, null, null, null);
        List<ReadValueId> nodesToRead = l(request.getNodesToRead());
        List<DataValue> results = Collections.nCopies(nodesToRead.size(), new DataValue(variant));
        ReadResponse response = new ReadResponse(header, a(results, DataValue.class), null);
        service.setResponse(response);
    });
}
Also used : ResponseHeader(org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest)

Aggregations

ReadResponse (org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse)15 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)14 UaException (org.eclipse.milo.opcua.stack.core.UaException)12 ArrayList (java.util.ArrayList)9 AttributeId (org.eclipse.milo.opcua.stack.core.AttributeId)9 ReadValueId (org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId)5 ExpandedNodeId (org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId)3 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)3 UaNode (org.eclipse.milo.opcua.sdk.client.nodes.UaNode)2 UaObjectNode (org.eclipse.milo.opcua.sdk.client.nodes.UaObjectNode)2 UaVariableNode (org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode)2 ReadRequest (org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 Array (java.lang.reflect.Array)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Arrays (java.util.Arrays)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1