Search in sources :

Example 26 with PlcConnection

use of org.apache.plc4x.java.api.PlcConnection 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 27 with PlcConnection

use of org.apache.plc4x.java.api.PlcConnection in project plc4x by apache.

the class Plc4xWriteClient 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 PlcWriteRequest.Builder requestBuilder = connection.writeRequestBuilder();
        requestBuilder.addItem("test-BOOL", "STDOUT/foo:BOOL", true);
        requestBuilder.addItem("test-BYTE", "STDOUT/foo:BYTE", new boolean[] { true, true, false, true, false, true, false, true });
        requestBuilder.addItem("test-WORD", "STDOUT/foo:WORD", new boolean[] { true, true, false, true, false, true, false, true, false, false, false, false, true, true, true, true });
        requestBuilder.addItem("test-DWORD", "STDOUT/foo:DWORD", new boolean[] { true, true, false, true, false, true, false, true, false, false, false, false, true, true, true, true, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false });
        requestBuilder.addItem("test-USINT", "STDOUT/foo:USINT", 12);
        requestBuilder.addItem("test-UINT", "STDOUT/foo:UINT", 12345);
        requestBuilder.addItem("test-UDINT", "STDOUT/foo:UDINT", 1234567890);
        requestBuilder.addItem("test-ULINT", "STDOUT/foo:ULINT", new BigInteger("12345678901234567"));
        requestBuilder.addItem("test-SINT", "STDOUT/foo:SINT", -12);
        requestBuilder.addItem("test-INT", "STDOUT/foo:INT", -12345);
        requestBuilder.addItem("test-DINT", "STDOUT/foo:DINT", -1234567890);
        requestBuilder.addItem("test-LINT", "STDOUT/foo:LINT", new BigInteger("-12345678901234567"));
        requestBuilder.addItem("test-REAL", "STDOUT/foo:REAL", 3.14159f);
        requestBuilder.addItem("test-LREAL", "STDOUT/foo:LREAL", 2.71828182845904523536028747135d);
        requestBuilder.addItem("test-CHAR", "STDOUT/foo:CHAR", "P");
        requestBuilder.addItem("test-WCHAR", "STDOUT/foo:WCHAR", "Ϡ");
        final PlcWriteRequest writeRequest = requestBuilder.build();
        final PlcWriteResponse writeResponse = writeRequest.execute().get();
        System.out.println(writeResponse);
    }
}
Also used : PlcWriteRequest(org.apache.plc4x.java.api.messages.PlcWriteRequest) BigInteger(java.math.BigInteger) PlcWriteResponse(org.apache.plc4x.java.api.messages.PlcWriteResponse) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcConnection(org.apache.plc4x.java.api.PlcConnection)

Example 28 with PlcConnection

use of org.apache.plc4x.java.api.PlcConnection 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 29 with PlcConnection

use of org.apache.plc4x.java.api.PlcConnection in project plc4x by apache.

the class MockDriver method getConnection.

@Override
public PlcConnection getConnection(String url) throws PlcConnectionException {
    // Mock a connection.
    PlcConnection plcConnectionMock = mock(PlcConnection.class, RETURNS_DEEP_STUBS);
    when(plcConnectionMock.getMetadata().canRead()).thenReturn(true);
    when(plcConnectionMock.getMetadata().canWrite()).thenReturn(true);
    when(plcConnectionMock.readRequestBuilder()).thenReturn(mock(PlcReadRequest.Builder.class, RETURNS_DEEP_STUBS));
    when(plcConnectionMock.writeRequestBuilder()).thenReturn(mock(PlcWriteRequest.Builder.class, RETURNS_DEEP_STUBS));
    when(plcConnectionMock.subscriptionRequestBuilder()).thenReturn(mock(PlcSubscriptionRequest.Builder.class, RETURNS_DEEP_STUBS));
    when(plcConnectionMock.unsubscriptionRequestBuilder()).thenReturn(mock(PlcUnsubscriptionRequest.Builder.class, RETURNS_DEEP_STUBS));
    // Mock a typical subscriber.
    PlcSubscriber plcSubscriber = mock(PlcSubscriber.class, RETURNS_DEEP_STUBS);
    when(plcSubscriber.subscribe(any(PlcSubscriptionRequest.class))).thenAnswer(invocation -> {
        LOGGER.info("Received {}", invocation);
        // TODO: Translate this so it actually does something ...
        /*PlcSubscriptionRequest subscriptionRequest = invocation.getArgument(0);
            List<PlcSubscriptionResponse> responseItems =
                subscriptionRequest.getFieldNames().stream().map(
                    fieldName -> subscriptionRequest.getField(fieldName)).map(field -> {
                    Consumer consumer = subscriptionRequestItem.getConsumer();
                    executorService.submit(() -> {
                        while (!Thread.currentThread().isInterrupted()) {
                            consumer.accept(new SubscriptionEventItem<>(null, Calendar.getInstance(), Collections.singletonList("HelloWorld")));
                            try {
                                TimeUnit.MILLISECONDS.sleep(100);
                            } catch (InterruptedException e) {
                                Thread.currentThread().interrupt();
                                throw new RuntimeException(e);
                            }
                        }
                    });
                    return new SubscriptionResponseItem<>(subscriptionRequestItem,
                        mock(PlcSubscriptionHandle.class, RETURNS_DEEP_STUBS), PlcResponseCode.OK);
                }).collect(Collectors.toList());
            PlcSubscriptionResponse response = new PlcSubscriptionResponse(subscriptionRequest, responseItems);*/
        PlcSubscriptionResponse response = new DefaultPlcSubscriptionResponse(mock(PlcSubscriptionRequest.class), new HashMap<>());
        CompletableFuture<PlcSubscriptionResponse> responseFuture = new CompletableFuture<>();
        responseFuture.complete(response);
        return responseFuture;
    });
    return plcConnectionMock;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) DefaultPlcSubscriptionResponse(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse) DefaultPlcSubscriptionResponse(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse) PlcSubscriber(org.apache.plc4x.java.spi.messages.PlcSubscriber) PlcConnection(org.apache.plc4x.java.api.PlcConnection)

Example 30 with PlcConnection

use of org.apache.plc4x.java.api.PlcConnection in project plc4x by apache.

the class Plc4XProducerTest method doStopBadConnection.

@Test
public void doStopBadConnection() throws Exception {
    Field openRequests = SUT.getClass().getDeclaredField("plcConnection");
    openRequests.setAccessible(true);
    PlcConnection plcConnectionMock = mock(PlcConnection.class);
    doThrow(new RuntimeException("oh noes")).when(plcConnectionMock).close();
    openRequests.set(SUT, plcConnectionMock);
    SUT.doStop();
}
Also used : Field(java.lang.reflect.Field) PlcConnection(org.apache.plc4x.java.api.PlcConnection) Test(org.junit.jupiter.api.Test)

Aggregations

PlcConnection (org.apache.plc4x.java.api.PlcConnection)80 PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)36 PlcConnectionException (org.apache.plc4x.java.api.exceptions.PlcConnectionException)30 PlcReadResponse (org.apache.plc4x.java.api.messages.PlcReadResponse)23 Test (org.junit.jupiter.api.Test)22 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)20 ExecutionException (java.util.concurrent.ExecutionException)18 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)12 TimeoutException (java.util.concurrent.TimeoutException)9 BigInteger (java.math.BigInteger)8 PlcSubscriptionResponse (org.apache.plc4x.java.api.messages.PlcSubscriptionResponse)8 PlcWriteRequest (org.apache.plc4x.java.api.messages.PlcWriteRequest)8 Map (java.util.Map)7 HashMap (java.util.HashMap)6 MockConnection (org.apache.plc4x.java.mock.connection.MockConnection)6 CompletableFuture (java.util.concurrent.CompletableFuture)5 Condition (org.assertj.core.api.Condition)5 Disabled (org.junit.jupiter.api.Disabled)5 Field (java.lang.reflect.Field)4 LocalDateTime (java.time.LocalDateTime)4