Search in sources :

Example 31 with PlcReadRequest

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

the class DatatypesTest method main.

public static void main(String[] args) throws Exception {
    try (PlcConnection connection = new PlcDriverManager().getConnection("s7://192.168.23.30")) {
        final PlcReadRequest.Builder builder = connection.readRequestBuilder();
        // true
        builder.addItem("bool-value-1", "%DB2:0.0:BOOL");
        // false
        builder.addItem("bool-value-2", "%DB2:2.1:BOOL");
        // It seems S7 PLCs ignores the array notation for BOOL
        // builder.addItem("bool-array", "%DB2:2.1:BOOL[4]");
        builder.addItem("byte-value", "%DB2:2:BYTE");
        builder.addItem("byte-array", "%DB2:2:BYTE[2]");
        builder.addItem("word-value", "%DB2:2:WORD");
        builder.addItem("word-array", "%DB2:2:WORD[2]");
        builder.addItem("dword-value", "%DB2:2:DWORD");
        builder.addItem("dword-array", "%DB2:2:DWORD[2]");
        // 7
        builder.addItem("sint-value", "%DB2:12:SINT");
        // 1, -2
        builder.addItem("sint-array", "%DB2:14:SINT[2]");
        // 23
        builder.addItem("int-value", "%DB2:18:INT");
        // 123, -142
        builder.addItem("int-array", "%DB2:20:INT[2]");
        // 24
        builder.addItem("dint-value", "%DB2:24:DINT");
        // 1234, -2345
        builder.addItem("dint-array", "%DB2:28:DINT[2]");
        // 42
        builder.addItem("usint-value", "%DB2:36:USINT");
        // 3, 4
        builder.addItem("usint-array", "%DB2:38:USINT[2]");
        // 3
        builder.addItem("uint-value", "%DB2:40:UINT");
        // 242, 223
        builder.addItem("uint-array", "%DB2:42:UINT[2]");
        // 815
        builder.addItem("udint-value", "%DB2:46:UDINT");
        // 12345, 23456
        builder.addItem("udint-array", "%DB2:50:UDINT[2]");
        // 3.14159
        builder.addItem("real-value", "%DB2:58:REAL");
        // 12.345, 12.345
        builder.addItem("real-array", "%DB2:62:REAL[2]");
        // 3.14159265358979
        builder.addItem("lreal-value", "%DB2:70:LREAL");
        // 1.2345, -1.2345
        builder.addItem("lreal-array", "%DB2:78:LREAL[2]");
        // "Hurz"
        builder.addItem("string-value", "%DB2:94:STRING(10)");
        // When reading a sized STRING string array, this has to be translated into multiple items
        // builder.addItem("string-array", "%DB2:350:STRING(10)[2]"); // "Wolf", "Lamm"
        // 1234ms
        builder.addItem("time-value", "%DB2:862:TIME");
        // 123ms, 234ms
        builder.addItem("time-array", "%DB2:866:TIME[2]");
        // D#2020-08-20
        builder.addItem("date-value", "%DB2:874:DATE");
        // D#1990-03-28, D#2020-10-25
        builder.addItem("date-array", "%DB2:876:DATE[2]");
        // TOD#12:34:56
        builder.addItem("time-of-day-value", "%DB2:880:TIME_OF_DAY");
        // TOD#16:34:56, TOD#08:15:00
        builder.addItem("time-of-day-array", "%DB2:884:TIME_OF_DAY[2]");
        // DTL#1978-03-28-12:34:56
        builder.addItem("date-and-time-value", "%DB2:892:DATE_AND_TIME");
        // DTL#1978-03-28-12:34:56, DTL#1978-03-28-12:34:56
        builder.addItem("date-and-time-array", "%DB2:904:DATE_AND_TIME[2]");
        // "H"
        builder.addItem("char-value", "%DB2:928:CHAR");
        // "H", "u", "r", "z"
        builder.addItem("char-array", "%DB2:930:CHAR[4]");
        final PlcReadRequest readRequest = builder.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 32 with PlcReadRequest

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

the class Plc4XConsumer method startUnTriggered.

private void startUnTriggered() {
    PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
    for (Map.Entry<String, Object> tag : tags.entrySet()) {
        try {
            builder.addItem(tag.getKey(), (String) tag.getValue());
        } catch (PlcIncompatibleDatatypeException e) {
            LOGGER.error("For consumer, please use Map<String,String>, currently using {}", tags.getClass().getSimpleName());
        }
    }
    PlcReadRequest request = builder.build();
    future = executorService.schedule(() -> request.execute().thenAccept(response -> {
        try {
            Exchange exchange = plc4XEndpoint.createExchange();
            Map<String, Object> rsp = new HashMap<>();
            for (String field : response.getFieldNames()) {
                rsp.put(field, response.getObject(field));
            }
            exchange.getIn().setBody(rsp);
            getProcessor().process(exchange);
        } catch (Exception e) {
            getExceptionHandler().handleException(e);
        }
    }), 500, TimeUnit.MILLISECONDS);
}
Also used : Exchange(org.apache.camel.Exchange) PlcIncompatibleDatatypeException(org.apache.plc4x.java.api.exceptions.PlcIncompatibleDatatypeException) HashMap(java.util.HashMap) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) HashMap(java.util.HashMap) Map(java.util.Map) ScraperException(org.apache.plc4x.java.scraper.exception.ScraperException) PlcIncompatibleDatatypeException(org.apache.plc4x.java.api.exceptions.PlcIncompatibleDatatypeException)

Example 33 with PlcReadRequest

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

the class PlcEntityInterceptorTest method getPlcReadResponse_timeoutOnGet_rethrows.

@Test
public void getPlcReadResponse_timeoutOnGet_rethrows() {
    PlcReadRequest request = mock(PlcReadRequest.class);
    when(request.execute()).thenReturn(new CompletableFuture<>());
    assertThatThrownBy(() -> PlcEntityInterceptor.getPlcReadResponse(request)).isInstanceOf(OPMException.class);
}
Also used : PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) Test(org.junit.jupiter.api.Test)

Example 34 with PlcReadRequest

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

the class CachedDriverManagerTest method useClosedConnection2.

@Test
void useClosedConnection2() throws Exception {
    PlcConnectionFactory mock = Mockito.mock(PlcConnectionFactory.class);
    MockConnection plcMockConnection = mock(MockConnection.class);
    when(mock.create()).thenReturn(plcMockConnection);
    when(plcMockConnection.readRequestBuilder()).thenReturn(Mockito.mock(PlcReadRequest.Builder.class));
    CachedDriverManager driverManager = new CachedDriverManager("", mock);
    // Get Connmection
    PlcConnection connection = driverManager.getConnection("");
    // Close the Connection
    PlcReadRequest.Builder builder = connection.readRequestBuilder();
    PlcReadRequest request = builder.addItem("", "").build();
    connection.close();
    assertThrows(IllegalStateException.class, () -> request.execute());
}
Also used : PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) MockConnection(org.apache.plc4x.java.mock.connection.MockConnection) PlcConnection(org.apache.plc4x.java.api.PlcConnection) Test(org.junit.jupiter.api.Test)

Example 35 with PlcReadRequest

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

the class ManualTest method run.

public void run() throws Exception {
    try (PlcConnection plcConnection = new PlcDriverManager().getConnection(connectionString)) {
        System.out.println("Reading all types in separate requests");
        // Run all entries separately:
        for (TestCase testCase : testCases) {
            String fieldName = testCase.address;
            // Prepare the read-request
            final PlcReadRequest readRequest = plcConnection.readRequestBuilder().addItem(fieldName, testCase.address).build();
            // Execute the read request
            final PlcReadResponse readResponse = readRequest.execute().get();
            // Check the result
            Assertions.assertEquals(1, readResponse.getFieldNames().size(), fieldName);
            Assertions.assertEquals(fieldName, readResponse.getFieldNames().iterator().next(), fieldName);
            Assertions.assertEquals(PlcResponseCode.OK, readResponse.getResponseCode(fieldName), fieldName);
            Assertions.assertNotNull(readResponse.getPlcValue(fieldName), fieldName);
            if (readResponse.getPlcValue(fieldName) instanceof PlcList) {
                PlcList plcList = (PlcList) readResponse.getPlcValue(fieldName);
                if (testCase.expectedReadValue instanceof List) {
                    List<Object> expectedValues = (List<Object>) testCase.expectedReadValue;
                    for (int j = 0; j < expectedValues.size(); j++) {
                        Assertions.assertEquals(expectedValues.get(j), plcList.getIndex(j).getObject(), fieldName + "[" + j + "]");
                    }
                } else {
                    Assertions.fail("Got a list of values, but only expected one.");
                }
            } else {
                Assertions.assertEquals(testCase.expectedReadValue.toString(), readResponse.getPlcValue(fieldName).getObject().toString(), fieldName);
            }
        }
        System.out.println("Success");
        // Read all items in one big request.
        // Shuffle the list of test cases and run the test 10 times.
        System.out.println("Reading all items together in random order");
        for (int i = 0; i < 100; i++) {
            System.out.println(" - run number " + i + " of " + 100);
            final List<TestCase> shuffledTestcases = new ArrayList<>(testCases);
            Collections.shuffle(shuffledTestcases);
            StringBuilder sb = new StringBuilder();
            for (TestCase testCase : shuffledTestcases) {
                sb.append(testCase.address).append(", ");
            }
            System.out.println("       using order: " + sb.toString());
            final PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
            for (TestCase testCase : shuffledTestcases) {
                String fieldName = testCase.address;
                builder.addItem(fieldName, testCase.address);
            }
            final PlcReadRequest readRequest = builder.build();
            // Execute the read request
            final PlcReadResponse readResponse = readRequest.execute().get();
            // Check the result
            Assertions.assertEquals(shuffledTestcases.size(), readResponse.getFieldNames().size());
            for (TestCase testCase : shuffledTestcases) {
                String fieldName = testCase.address;
                Assertions.assertEquals(PlcResponseCode.OK, readResponse.getResponseCode(fieldName));
                Assertions.assertNotNull(readResponse.getPlcValue(fieldName));
                if (readResponse.getPlcValue(fieldName) instanceof PlcList) {
                    PlcList plcList = (PlcList) readResponse.getPlcValue(fieldName);
                    List<Object> expectedValues = (List<Object>) testCase.expectedReadValue;
                    for (int j = 0; j < expectedValues.size(); j++) {
                        Assertions.assertEquals(expectedValues.get(j), plcList.getIndex(j).getObject());
                    }
                } else {
                    Assertions.assertEquals(testCase.expectedReadValue.toString(), readResponse.getPlcValue(fieldName).getObject().toString());
                }
            }
        }
        System.out.println("Success");
    } catch (Exception e) {
        Assertions.fail(e);
    }
}
Also used : ArrayList(java.util.ArrayList) PlcConnection(org.apache.plc4x.java.api.PlcConnection) PlcList(org.apache.plc4x.java.spi.values.PlcList) PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) PlcList(org.apache.plc4x.java.spi.values.PlcList) List(java.util.List) ArrayList(java.util.ArrayList) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager)

Aggregations

PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)37 PlcReadResponse (org.apache.plc4x.java.api.messages.PlcReadResponse)21 PlcConnection (org.apache.plc4x.java.api.PlcConnection)17 PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)12 PlcWriteRequest (org.apache.plc4x.java.api.messages.PlcWriteRequest)9 PlcField (org.apache.plc4x.java.api.model.PlcField)9 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)8 PlcProtocolException (org.apache.plc4x.java.api.exceptions.PlcProtocolException)7 PlcRequest (org.apache.plc4x.java.api.messages.PlcRequest)7 ExecutionException (java.util.concurrent.ExecutionException)5 PlcWriteResponse (org.apache.plc4x.java.api.messages.PlcWriteResponse)5 PlcValue (org.apache.plc4x.java.api.value.PlcValue)5 DefaultPlcReadResponse (org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse)5 Duration (java.time.Duration)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 PlcConnectionException (org.apache.plc4x.java.api.exceptions.PlcConnectionException)4 DefaultPlcReadRequest (org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest)4 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)4