Search in sources :

Example 1 with PlcReadResponse

use of org.apache.plc4x.java.api.messages.PlcReadResponse in project plc4x by apache.

the class CANOpenProtocolLogic method read.

public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
    CompletableFuture<PlcReadResponse> response = new CompletableFuture<>();
    if (readRequest.getFieldNames().size() != 1) {
        response.completeExceptionally(new IllegalArgumentException("SDO requires single field to be read"));
        return response;
    }
    PlcField field = readRequest.getFields().get(0);
    if (!(field instanceof CANOpenField)) {
        response.completeExceptionally(new IllegalArgumentException("Only CANOpenField instances are supported"));
        return response;
    }
    if (!(field instanceof CANOpenSDOField)) {
        response.completeExceptionally(new IllegalArgumentException("Only CANOpenSDOField instances are supported"));
        return response;
    }
    ;
    readInternally(readRequest, (CANOpenSDOField) field, response);
    return response;
}
Also used : PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) CompletableFuture(java.util.concurrent.CompletableFuture) CANOpenField(org.apache.plc4x.java.canopen.field.CANOpenField) CANOpenSDOField(org.apache.plc4x.java.canopen.field.CANOpenSDOField) PlcField(org.apache.plc4x.java.api.model.PlcField)

Example 2 with PlcReadResponse

use of org.apache.plc4x.java.api.messages.PlcReadResponse in project plc4x by apache.

the class PlcConnectionAdapterTest method checkRead.

private <T> void checkRead(MockConnection connection, PlcReadRequest request, T value) throws InterruptedException, ExecutionException {
    // this is really a tests of our mock tooling but knowing it's behaving as expected
    // will help identify problems in the adapter/supplier/consumer
    PlcField plcField = request.getFields().get(0);
    // TODO: smart value conversion
    connection.setPlcValue(plcField, new PlcLong(0L));
    CompletableFuture<PlcReadResponse> cf = connection.read(request);
    assertThat(cf.isDone(), is(true));
    PlcReadResponse response = cf.get();
// TODO: fixme
// assertThat(value, equalTo(response.getResponseItems().get(0).getValues().get(0)));
}
Also used : PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) PlcField(org.apache.plc4x.java.api.model.PlcField)

Example 3 with PlcReadResponse

use of org.apache.plc4x.java.api.messages.PlcReadResponse in project plc4x by apache.

the class ManualProfinetIoTest method main.

public static void main(String[] args) throws Exception {
    final PlcConnection connection = new PlcDriverManager().getConnection("profinet://192.168.24.31");
    final PlcReadRequest readRequest = connection.readRequestBuilder().addItem("test", "").build();
    final PlcReadResponse plcReadResponse = readRequest.execute().get();
    System.out.println(plcReadResponse);
}
Also used : PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcConnection(org.apache.plc4x.java.api.PlcConnection)

Example 4 with PlcReadResponse

use of org.apache.plc4x.java.api.messages.PlcReadResponse in project plc4x by apache.

the class Plc4xReadClient method main.

public static void main(String[] args) throws Exception {
    try (final PlcConnection connection = new PlcDriverManager().getConnection("plc4x://localhost?remote-connection-string=simulated%3A%2F%2Flocalhost")) {
        final PlcReadRequest.Builder requestBuilder = connection.readRequestBuilder();
        requestBuilder.addItem("test-BOOL", "RANDOM/foo:BOOL");
        requestBuilder.addItem("test-BYTE", "RANDOM/foo:BYTE");
        requestBuilder.addItem("test-WORD", "RANDOM/foo:WORD");
        requestBuilder.addItem("test-DWORD", "RANDOM/foo:DWORD");
        requestBuilder.addItem("test-USINT", "RANDOM/foo:USINT");
        requestBuilder.addItem("test-UINT", "RANDOM/foo:UINT");
        requestBuilder.addItem("test-UDINT", "RANDOM/foo:UDINT");
        requestBuilder.addItem("test-ULINT", "RANDOM/foo:ULINT");
        requestBuilder.addItem("test-SINT", "RANDOM/foo:SINT");
        requestBuilder.addItem("test-INT", "RANDOM/foo:INT");
        requestBuilder.addItem("test-DINT", "RANDOM/foo:DINT");
        requestBuilder.addItem("test-LINT", "RANDOM/foo:LINT");
        requestBuilder.addItem("test-REAL", "RANDOM/foo:REAL");
        requestBuilder.addItem("test-LREAL", "RANDOM/foo:LREAL");
        requestBuilder.addItem("test-CHAR", "RANDOM/foo:CHAR");
        requestBuilder.addItem("test-WCHAR", "RANDOM/foo:WCHAR");
        final PlcReadRequest readRequest = requestBuilder.build();
        final PlcReadResponse readResponse = readRequest.execute().get();
        System.out.println(readResponse);
    }
}
Also used : PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcConnection(org.apache.plc4x.java.api.PlcConnection)

Example 5 with PlcReadResponse

use of org.apache.plc4x.java.api.messages.PlcReadResponse in project plc4x by apache.

the class SimulatedConnection method read.

@Override
public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
    Map<String, ResponseItem<PlcValue>> fields = new HashMap<>();
    for (String fieldName : readRequest.getFieldNames()) {
        SimulatedField field = (SimulatedField) readRequest.getField(fieldName);
        Optional<PlcValue> valueOptional = device.get(field);
        ResponseItem<PlcValue> fieldPair;
        boolean present = valueOptional.isPresent();
        fieldPair = present ? new ResponseItem<>(PlcResponseCode.OK, valueOptional.get()) : new ResponseItem<>(PlcResponseCode.NOT_FOUND, null);
        fields.put(fieldName, fieldPair);
    }
    PlcReadResponse response = new DefaultPlcReadResponse(readRequest, fields);
    return CompletableFuture.completedFuture(response);
}
Also used : PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) PlcValue(org.apache.plc4x.java.api.value.PlcValue) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) SimulatedField(org.apache.plc4x.java.simulated.field.SimulatedField)

Aggregations

PlcReadResponse (org.apache.plc4x.java.api.messages.PlcReadResponse)28 PlcConnection (org.apache.plc4x.java.api.PlcConnection)23 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)19 PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)13 ExecutionException (java.util.concurrent.ExecutionException)9 PlcConnectionException (org.apache.plc4x.java.api.exceptions.PlcConnectionException)8 TimeoutException (java.util.concurrent.TimeoutException)4 Test (org.junit.jupiter.api.Test)4 HashMap (java.util.HashMap)3 List (java.util.List)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 LocalDateTime (java.time.LocalDateTime)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 PlcException (org.apache.plc4x.java.api.exceptions.PlcException)2 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)2 PlcWriteRequest (org.apache.plc4x.java.api.messages.PlcWriteRequest)2 PlcWriteResponse (org.apache.plc4x.java.api.messages.PlcWriteResponse)2