Search in sources :

Example 1 with ReadRequest

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

the class OpcUaClient method readNamespaceTableAsync.

/**
 * Read the server's NamespaceTable and update the local copy.
 * <p>
 * This call completes asynchronously.
 *
 * @return a {@link CompletableFuture} that completes successfully with the updated
 * {@link NamespaceTable} or completes exceptionally if a service- or operation-level error
 * occurs.
 */
public CompletableFuture<NamespaceTable> readNamespaceTableAsync() {
    return getSession().thenCompose(session -> {
        RequestHeader requestHeader = newRequestHeader(session.getAuthenticationToken());
        ReadRequest readRequest = new ReadRequest(requestHeader, 0.0, TimestampsToReturn.Neither, new ReadValueId[] { new ReadValueId(Identifiers.Server_NamespaceArray, AttributeId.Value.uid(), null, QualifiedName.NULL_VALUE) });
        CompletableFuture<String[]> namespaceArray = sendRequest(readRequest).thenApply(ReadResponse.class::cast).thenApply(response -> Objects.requireNonNull(response.getResults())).thenApply(results -> (String[]) results[0].getValue().getValue());
        return namespaceArray.thenAccept(this::updateNamespaceTable).thenApply(v -> getNamespaceTable());
    });
}
Also used : Arrays(java.util.Arrays) BrowseNextRequest(org.eclipse.milo.opcua.stack.core.types.structured.BrowseNextRequest) WriteValue(org.eclipse.milo.opcua.stack.core.types.structured.WriteValue) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) PublishRequest(org.eclipse.milo.opcua.stack.core.types.structured.PublishRequest) WriteRequest(org.eclipse.milo.opcua.stack.core.types.structured.WriteRequest) MonitoredItemCreateRequest(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest) DataTypeManager(org.eclipse.milo.opcua.stack.core.types.DataTypeManager) WriteResponse(org.eclipse.milo.opcua.stack.core.types.structured.WriteResponse) SessionInitializer(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.SessionInitializer) TranslateBrowsePathsToNodeIdsRequest(org.eclipse.milo.opcua.stack.core.types.structured.TranslateBrowsePathsToNodeIdsRequest) ModifySubscriptionRequest(org.eclipse.milo.opcua.stack.core.types.structured.ModifySubscriptionRequest) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) ModifyMonitoredItemsRequest(org.eclipse.milo.opcua.stack.core.types.structured.ModifyMonitoredItemsRequest) UaResponseMessage(org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage) AddReferencesItem(org.eclipse.milo.opcua.stack.core.types.structured.AddReferencesItem) TransferSubscriptionsResponse(org.eclipse.milo.opcua.stack.core.types.structured.TransferSubscriptionsResponse) ManifestUtil(org.eclipse.milo.opcua.stack.core.util.ManifestUtil) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) SetPublishingModeResponse(org.eclipse.milo.opcua.stack.core.types.structured.SetPublishingModeResponse) SetMonitoringModeRequest(org.eclipse.milo.opcua.stack.core.types.structured.SetMonitoringModeRequest) MonitoringMode(org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode) SetMonitoringModeResponse(org.eclipse.milo.opcua.stack.core.types.structured.SetMonitoringModeResponse) RepublishRequest(org.eclipse.milo.opcua.stack.core.types.structured.RepublishRequest) ModifyMonitoredItemsResponse(org.eclipse.milo.opcua.stack.core.types.structured.ModifyMonitoredItemsResponse) ConversionUtil.a(org.eclipse.milo.opcua.stack.core.util.ConversionUtil.a) OpcUaClientConfigBuilder(org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfigBuilder) MonitoredItemModifyRequest(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemModifyRequest) Stack(org.eclipse.milo.opcua.stack.core.Stack) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) DeleteNodesRequest(org.eclipse.milo.opcua.stack.core.types.structured.DeleteNodesRequest) HistoryUpdateRequest(org.eclipse.milo.opcua.stack.core.types.structured.HistoryUpdateRequest) RegisterNodesRequest(org.eclipse.milo.opcua.stack.core.types.structured.RegisterNodesRequest) UaRequestMessage(org.eclipse.milo.opcua.stack.core.serialization.UaRequestMessage) DeleteNodesItem(org.eclipse.milo.opcua.stack.core.types.structured.DeleteNodesItem) DeleteMonitoredItemsRequest(org.eclipse.milo.opcua.stack.core.types.structured.DeleteMonitoredItemsRequest) DeleteMonitoredItemsResponse(org.eclipse.milo.opcua.stack.core.types.structured.DeleteMonitoredItemsResponse) TransferSubscriptionsRequest(org.eclipse.milo.opcua.stack.core.types.structured.TransferSubscriptionsRequest) DeleteReferencesItem(org.eclipse.milo.opcua.stack.core.types.structured.DeleteReferencesItem) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) RegisterNodesResponse(org.eclipse.milo.opcua.stack.core.types.structured.RegisterNodesResponse) Lists.newCopyOnWriteArrayList(com.google.common.collect.Lists.newCopyOnWriteArrayList) OpcUaClientConfig(org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig) SessionFsm(org.eclipse.milo.opcua.sdk.client.session.SessionFsm) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) SetTriggeringRequest(org.eclipse.milo.opcua.stack.core.types.structured.SetTriggeringRequest) ExecutionException(java.util.concurrent.ExecutionException) DeleteReferencesResponse(org.eclipse.milo.opcua.stack.core.types.structured.DeleteReferencesResponse) AddNodesResponse(org.eclipse.milo.opcua.stack.core.types.structured.AddNodesResponse) AddNodesRequest(org.eclipse.milo.opcua.stack.core.types.structured.AddNodesRequest) DiscoveryClient(org.eclipse.milo.opcua.stack.client.DiscoveryClient) Namespaces(org.eclipse.milo.opcua.stack.core.util.Namespaces) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) LoggerFactory(org.slf4j.LoggerFactory) AddNodesItem(org.eclipse.milo.opcua.stack.core.types.structured.AddNodesItem) DeleteReferencesRequest(org.eclipse.milo.opcua.stack.core.types.structured.DeleteReferencesRequest) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) OpcUaSubscriptionManager(org.eclipse.milo.opcua.sdk.client.subscriptions.OpcUaSubscriptionManager) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) Unit(org.eclipse.milo.opcua.stack.core.util.Unit) BrowseDescription(org.eclipse.milo.opcua.stack.core.types.structured.BrowseDescription) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) CreateSubscriptionResponse(org.eclipse.milo.opcua.stack.core.types.structured.CreateSubscriptionResponse) DeleteNodesResponse(org.eclipse.milo.opcua.stack.core.types.structured.DeleteNodesResponse) VariableTypeInitializer(org.eclipse.milo.opcua.sdk.client.model.VariableTypeInitializer) HistoryReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadRequest) UByte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte) Predicate(java.util.function.Predicate) BrowsePath(org.eclipse.milo.opcua.stack.core.types.structured.BrowsePath) HistoryUpdateDetails(org.eclipse.milo.opcua.stack.core.types.structured.HistoryUpdateDetails) RepublishResponse(org.eclipse.milo.opcua.stack.core.types.structured.RepublishResponse) ServiceFault(org.eclipse.milo.opcua.stack.core.types.structured.ServiceFault) Objects(java.util.Objects) SubscriptionAcknowledgement(org.eclipse.milo.opcua.stack.core.types.structured.SubscriptionAcknowledgement) List(java.util.List) UnregisterNodesResponse(org.eclipse.milo.opcua.stack.core.types.structured.UnregisterNodesResponse) AddReferencesRequest(org.eclipse.milo.opcua.stack.core.types.structured.AddReferencesRequest) AddReferencesResponse(org.eclipse.milo.opcua.stack.core.types.structured.AddReferencesResponse) TranslateBrowsePathsToNodeIdsResponse(org.eclipse.milo.opcua.stack.core.types.structured.TranslateBrowsePathsToNodeIdsResponse) Optional(java.util.Optional) DeleteSubscriptionsResponse(org.eclipse.milo.opcua.stack.core.types.structured.DeleteSubscriptionsResponse) CreateSubscriptionRequest(org.eclipse.milo.opcua.stack.core.types.structured.CreateSubscriptionRequest) BrowseResponse(org.eclipse.milo.opcua.stack.core.types.structured.BrowseResponse) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) BrowseRequest(org.eclipse.milo.opcua.stack.core.types.structured.BrowseRequest) UnregisterNodesRequest(org.eclipse.milo.opcua.stack.core.types.structured.UnregisterNodesRequest) CallMethodRequest(org.eclipse.milo.opcua.stack.core.types.structured.CallMethodRequest) CallResponse(org.eclipse.milo.opcua.stack.core.types.structured.CallResponse) CreateMonitoredItemsResponse(org.eclipse.milo.opcua.stack.core.types.structured.CreateMonitoredItemsResponse) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) NamespaceTable(org.eclipse.milo.opcua.stack.core.NamespaceTable) CallRequest(org.eclipse.milo.opcua.stack.core.types.structured.CallRequest) SerializationContext(org.eclipse.milo.opcua.stack.core.serialization.SerializationContext) UShort(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort) SessionFsmFactory(org.eclipse.milo.opcua.sdk.client.session.SessionFsmFactory) ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) ServiceFaultListener(org.eclipse.milo.opcua.sdk.client.api.ServiceFaultListener) SecurityPolicy(org.eclipse.milo.opcua.stack.core.security.SecurityPolicy) BrowseNextResponse(org.eclipse.milo.opcua.stack.core.types.structured.BrowseNextResponse) DeleteSubscriptionsRequest(org.eclipse.milo.opcua.stack.core.types.structured.DeleteSubscriptionsRequest) HistoryReadDetails(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadDetails) SetTriggeringResponse(org.eclipse.milo.opcua.stack.core.types.structured.SetTriggeringResponse) HistoryReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResponse) HistoryUpdateResponse(org.eclipse.milo.opcua.stack.core.types.structured.HistoryUpdateResponse) ExecutionQueue(org.eclipse.milo.opcua.stack.core.util.ExecutionQueue) Logger(org.slf4j.Logger) PublishResponse(org.eclipse.milo.opcua.stack.core.types.structured.PublishResponse) Unsigned.ushort(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.ushort) ObjectTypeInitializer(org.eclipse.milo.opcua.sdk.client.model.ObjectTypeInitializer) UserTokenType(org.eclipse.milo.opcua.stack.core.types.enumerated.UserTokenType) UaServiceFaultException(org.eclipse.milo.opcua.stack.core.UaServiceFaultException) ViewDescription(org.eclipse.milo.opcua.stack.core.types.structured.ViewDescription) CreateMonitoredItemsRequest(org.eclipse.milo.opcua.stack.core.types.structured.CreateMonitoredItemsRequest) SetPublishingModeRequest(org.eclipse.milo.opcua.stack.core.types.structured.SetPublishingModeRequest) UaClient(org.eclipse.milo.opcua.sdk.client.api.UaClient) ModifySubscriptionResponse(org.eclipse.milo.opcua.stack.core.types.structured.ModifySubscriptionResponse) UaException(org.eclipse.milo.opcua.stack.core.UaException) HistoryReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadValueId) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) HistoryReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadValueId) ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) HistoryReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResponse) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) HistoryReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadRequest)

Example 2 with ReadRequest

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

the class DefaultAttributeServiceSet method onRead.

@Override
public void onRead(ServiceRequest service) {
    ReadRequest request = (ReadRequest) service.getRequest();
    OpcUaServer server = service.attr(ServiceAttributes.SERVER_KEY).get();
    Session session = service.attr(ServiceAttributes.SESSION_KEY).get();
    List<ReadValueId> nodesToRead = l(request.getNodesToRead());
    if (nodesToRead.isEmpty()) {
        service.setServiceFault(StatusCodes.Bad_NothingToDo);
        return;
    }
    if (nodesToRead.size() > server.getConfig().getLimits().getMaxNodesPerRead().longValue()) {
        service.setServiceFault(StatusCodes.Bad_TooManyOperations);
        return;
    }
    if (request.getMaxAge() < 0d) {
        service.setServiceFault(StatusCodes.Bad_MaxAgeInvalid);
        return;
    }
    if (request.getTimestampsToReturn() == null) {
        service.setServiceFault(StatusCodes.Bad_TimestampsToReturnInvalid);
        return;
    }
    DiagnosticsContext<ReadValueId> diagnosticsContext = new DiagnosticsContext<>();
    ReadContext context = new ReadContext(server, session, diagnosticsContext);
    server.getAddressSpaceManager().read(context, request.getMaxAge(), request.getTimestampsToReturn(), nodesToRead);
    context.getFuture().thenAccept(values -> {
        ResponseHeader header = service.createResponseHeader();
        DiagnosticInfo[] diagnosticInfos = diagnosticsContext.getDiagnosticInfos(nodesToRead);
        ReadResponse response = new ReadResponse(header, values.toArray(new DataValue[0]), diagnosticInfos);
        service.setResponse(response);
    });
}
Also used : OpcUaServer(org.eclipse.milo.opcua.sdk.server.OpcUaServer) ResponseHeader(org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) DiagnosticInfo(org.eclipse.milo.opcua.stack.core.types.builtin.DiagnosticInfo) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) DiagnosticsContext(org.eclipse.milo.opcua.sdk.server.DiagnosticsContext) ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) ReadContext(org.eclipse.milo.opcua.sdk.server.api.services.AttributeServices.ReadContext) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) Session(org.eclipse.milo.opcua.sdk.server.Session)

Example 3 with ReadRequest

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

the class ClientServerTest method connectAndTest.

private void connectAndTest(Variant input, UaStackClient client) throws InterruptedException, java.util.concurrent.ExecutionException {
    setReadRequestHandler(input);
    client.connect().get();
    List<CompletableFuture<ReadResponse>> responses = Lists.newArrayList();
    for (int i = 0; i < 100; i++) {
        RequestHeader header = new RequestHeader(NodeId.NULL_VALUE, DateTime.now(), uint(i), uint(0), null, uint(10000), null);
        ReadRequest request = new ReadRequest(header, 0.0, TimestampsToReturn.Neither, new ReadValueId[] { new ReadValueId(NodeId.NULL_VALUE, AttributeId.Value.uid(), null, null) });
        responses.add(client.sendRequest(request).thenApply(ReadResponse.class::cast));
    }
    CompletableFuture.allOf(responses.toArray(new CompletableFuture[0])).get();
    FutureUtils.sequence(responses).get().forEach(response -> {
        Variant value = l(response.getResults()).get(0).getValue();
        assertEquals(value, input);
    });
    client.disconnect().get();
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) CompletableFuture(java.util.concurrent.CompletableFuture) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest)

Example 4 with ReadRequest

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

the class ClientServerTest method testClientDisconnect.

@Test
public void testClientDisconnect() throws Exception {
    EndpointDescription endpoint = endpoints[0];
    Variant input = new Variant(42);
    logger.info("SecurityPolicy={}, MessageSecurityMode={}, input={}", SecurityPolicy.fromUri(endpoint.getSecurityPolicyUri()), endpoint.getSecurityMode(), input);
    UaStackClient client = createClient(endpoint);
    client.connect().get();
    RequestHeader header = new RequestHeader(NodeId.NULL_VALUE, DateTime.now(), uint(0), uint(0), null, DEFAULT_TIMEOUT_HINT, null);
    ReadRequest request = new ReadRequest(header, 0.0, TimestampsToReturn.Neither, new ReadValueId[] { new ReadValueId(NodeId.NULL_VALUE, AttributeId.Value.uid(), null, null) });
    logger.info("sending request: {}", request);
    UaResponseMessage response0 = client.sendRequest(request).get();
    logger.info("got response: {}", response0);
    client.disconnect().get();
    assertThrows(() -> client.sendRequest(request).get());
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) UaResponseMessage(org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) Test(org.testng.annotations.Test)

Example 5 with ReadRequest

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

the class ClientServerTest method testClientTimeout.

@Test
public void testClientTimeout() throws Exception {
    EndpointDescription endpoint = endpoints[0];
    logger.info("SecurityPolicy={}, MessageSecurityMode={}", SecurityPolicy.fromUri(endpoint.getSecurityPolicyUri()), endpoint.getSecurityMode());
    UaStackClientConfig config = UaStackClientConfig.builder().setEndpoint(endpoint).setKeyPair(clientKeyPair).setCertificate(clientCertificate).build();
    UaStackClient client = UaStackClient.create(config);
    client.connect().get();
    server.addServiceHandler("/test", ReadRequest.TYPE_ID, service -> {
        // intentionally do nothing so the request can timeout
        logger.info("received {}; ignoring...", service.getRequest());
    });
    RequestHeader header = new RequestHeader(NodeId.NULL_VALUE, DateTime.now(), uint(0), uint(0), null, uint(1000), null);
    ReadRequest request = new ReadRequest(header, 0.0, TimestampsToReturn.Neither, new ReadValueId[] { new ReadValueId(NodeId.NULL_VALUE, AttributeId.Value.uid(), null, null) });
    try {
        client.sendRequest(request).get();
        fail("expected response to timeout");
    } catch (Throwable t) {
        StatusCode statusCode = UaException.extractStatusCode(t).orElse(StatusCode.BAD);
        assertEquals(statusCode.getValue(), StatusCodes.Bad_Timeout);
    }
}
Also used : ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) UaStackClientConfig(org.eclipse.milo.opcua.stack.client.UaStackClientConfig) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) Test(org.testng.annotations.Test)

Aggregations

ReadRequest (org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest)11 ReadValueId (org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId)11 RequestHeader (org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader)8 UaStackClient (org.eclipse.milo.opcua.stack.client.UaStackClient)5 UaResponseMessage (org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage)5 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)5 EndpointDescription (org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription)5 ReadResponse (org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse)4 Test (org.testng.annotations.Test)4 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 NamespaceTable (org.eclipse.milo.opcua.stack.core.NamespaceTable)2 UaException (org.eclipse.milo.opcua.stack.core.UaException)2 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)2 Lists.newCopyOnWriteArrayList (com.google.common.collect.Lists.newCopyOnWriteArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 ExecutionException (java.util.concurrent.ExecutionException)1