Search in sources :

Example 6 with ReadRequest

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

the class ClientServerTest method testClientStateMachine.

@Test
public void testClientStateMachine() 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);
    for (int i = 0; i < 1000; i++) {
        client.connect().get();
        RequestHeader header = new RequestHeader(NodeId.NULL_VALUE, DateTime.now(), uint(i), 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.debug("sending request: {}", request);
        UaResponseMessage response = client.sendRequest(request).get();
        logger.debug("got response: {}", response);
        client.disconnect().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) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) Test(org.testng.annotations.Test)

Example 7 with ReadRequest

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

the class DataTypeDictionaryReader method readNodes.

private CompletableFuture<List<DataValue>> readNodes(List<ReadValueId> readValueIds) {
    RequestHeader requestHeader = stackClient.newRequestHeader(session.getAuthenticationToken(), stackClient.getConfig().getRequestTimeout());
    ReadRequest readRequest = new ReadRequest(requestHeader, 0.0, TimestampsToReturn.Neither, readValueIds.toArray(new ReadValueId[0]));
    return stackClient.sendRequest(readRequest).thenApply(ReadResponse.class::cast).thenApply(r -> l(r.getResults()));
}
Also used : ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest)

Example 8 with ReadRequest

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

the class DataTypeDictionaryReaderTest method testReadDataTypeDictionaryBytes.

private void testReadDataTypeDictionaryBytes(ByteString dictionary, int fragmentSize) throws Exception {
    Mockito.when(stackClient.sendRequest(ArgumentMatchers.any(ReadRequest.class))).then(invocationOnMock -> {
        ReadRequest readRequest = invocationOnMock.getArgument(0);
        List<ReadValueId> readValueIds = Arrays.stream(Objects.requireNonNull(readRequest.getNodesToRead())).collect(Collectors.toList());
        ReadValueId readValueId = readValueIds.get(0);
        NumericRange numericRange = NumericRange.parse(readValueId.getIndexRange());
        try {
            Object fragment = NumericRange.readFromValueAtRange(new Variant(dictionary), numericRange);
            return completedFuture(new ReadResponse(null, new DataValue[] { new DataValue(new Variant(fragment)) }, null));
        } catch (UaException e) {
            return completedFuture(new ReadResponse(null, new DataValue[] { new DataValue(e.getStatusCode()) }, null));
        }
    });
    ByteString typeDictionaryBs = dictionaryReader.readDataTypeDictionaryBytes(NodeId.NULL_VALUE, fragmentSize).get();
    Assertions.assertEquals(typeDictionaryBs, dictionary);
}
Also used : NumericRange(org.eclipse.milo.opcua.sdk.core.NumericRange) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) 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) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest)

Example 9 with ReadRequest

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

the class DataTypeTreeBuilder method readNamespaceTable.

private static CompletableFuture<NamespaceTable> readNamespaceTable(UaStackClient client, OpcUaSession session) {
    RequestHeader requestHeader = client.newRequestHeader(session.getAuthenticationToken(), client.getConfig().getRequestTimeout());
    CompletableFuture<UaResponseMessage> readFuture = client.sendRequest(new ReadRequest(requestHeader, 0.0, TimestampsToReturn.Neither, new ReadValueId[] { new ReadValueId(Identifiers.Server_NamespaceArray, AttributeId.Value.uid(), null, QualifiedName.NULL_VALUE) }));
    return readFuture.thenApply(ReadResponse.class::cast).thenApply(response -> {
        DataValue dataValue = response.getResults()[0];
        String[] namespaceUris = (String[]) dataValue.getValue().getValue();
        NamespaceTable namespaceTable = new NamespaceTable();
        for (String namespaceUri : namespaceUris) {
            namespaceTable.addUri(namespaceUri);
        }
        return namespaceTable;
    });
}
Also used : UaResponseMessage(org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) NamespaceTable(org.eclipse.milo.opcua.stack.core.NamespaceTable) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest)

Example 10 with ReadRequest

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

the class ClientServerTest method testClientReconnect.

@Test
public void testClientReconnect() 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);
    logger.info("initiating a reconnect by closing channel in server...");
    server.getConnectedChannels().forEach(c -> {
        try {
            c.close().await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    logger.info("sending request: {}", request);
    try {
        UaResponseMessage response1 = client.sendRequest(request).get();
        logger.info("got response: {}", response1);
    } catch (Exception e) {
        // try again because close() above is a race condition
        UaResponseMessage response1 = client.sendRequest(request).get();
        logger.info("got response: {}", response1);
    }
    client.disconnect().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) UaException(org.eclipse.milo.opcua.stack.core.UaException) 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