Search in sources :

Example 21 with PlcConnection

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

the class GenericCANDriverTest method testSubscribeAndWrite.

@Test
@Disabled("This test requires working virtual CAN transport to be truly platform independent")
void testSubscribeAndWrite() throws Exception {
    // PlcConnection connection1 = new PlcDriverManager().getConnection("genericcan:socketcan://vcan0");
    // PlcConnection connection2 = new PlcDriverManager().getConnection("genericcan:socketcan://vcan0");
    PlcConnection connection1 = new PlcDriverManager().getConnection("genericcan:virtualcan://");
    PlcConnection connection2 = connection1;
    CountDownLatch latch = new CountDownLatch(1);
    Byte field1 = 0x55;
    short field2 = 10;
    short field3 = 50;
    final AtomicReference<PlcSubscriptionEvent> plcEvent = new AtomicReference<>();
    connection1.subscriptionRequestBuilder().addEventField("field1", "200:BYTE").addEventField("field2", "200:UNSIGNED8").addEventField("field3", "200:UNSIGNED8").build().execute().whenComplete((reply, error) -> {
        if (error != null) {
            fail(error);
            return;
        }
        reply.getSubscriptionHandle("field1").register(new Consumer<PlcSubscriptionEvent>() {

            @Override
            public void accept(PlcSubscriptionEvent event) {
                plcEvent.set(event);
                latch.countDown();
            }
        });
    });
    connection2.writeRequestBuilder().addItem("f1", "200:BYTE", field1).addItem("f2", "200:UNSIGNED8", field2).addItem("f3", "200:UNSIGNED8", field3).build().execute().whenComplete((reply, error) -> {
        if (error != null) {
            fail(error);
        }
    }).get();
    latch.await();
    PlcSubscriptionEvent event = plcEvent.get();
    assertEquals(field1, event.getByte("field1"));
    assertEquals(field2, event.getShort("field2"));
    assertEquals(field3, event.getShort("field3"));
}
Also used : Consumer(java.util.function.Consumer) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcSubscriptionEvent(org.apache.plc4x.java.api.messages.PlcSubscriptionEvent) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) PlcConnection(org.apache.plc4x.java.api.PlcConnection) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) Assertions(org.junit.jupiter.api.Assertions) Disabled(org.junit.jupiter.api.Disabled) AtomicReference(java.util.concurrent.atomic.AtomicReference) PlcSubscriptionEvent(org.apache.plc4x.java.api.messages.PlcSubscriptionEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcConnection(org.apache.plc4x.java.api.PlcConnection) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 22 with PlcConnection

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

the class GenericCANDriverTest method testConnection.

@Test
void testConnection() throws PlcConnectionException {
    PlcConnection connection = new PlcDriverManager().getConnection("genericcan:virtualcan://");
    assertNotNull(connection);
    assertTrue(connection.isConnected());
    assertFalse(connection.getMetadata().canRead());
    assertTrue(connection.getMetadata().canWrite());
    assertTrue(connection.getMetadata().canSubscribe());
}
Also used : PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcConnection(org.apache.plc4x.java.api.PlcConnection) Test(org.junit.jupiter.api.Test)

Example 23 with PlcConnection

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

the class OpcuaPlcDriverTest method connectionNoParams.

@Test
public void connectionNoParams() {
    connectionStringValidSet.forEach(connectionString -> {
        try {
            PlcConnection opcuaConnection = new PlcDriverManager().getConnection(connectionString);
            Condition<PlcConnection> is_connected = new Condition<>(PlcConnection::isConnected, "is connected");
            assertThat(opcuaConnection).is(is_connected);
            opcuaConnection.close();
            assertThat(opcuaConnection).isNot(is_connected);
        } catch (PlcConnectionException e) {
            fail("Exception during connectionNoParams while connecting Test EXCEPTION: " + e.getMessage());
        } catch (Exception e) {
            fail("Exception during connectionNoParams while closing Test EXCEPTION: " + e.getMessage());
        }
    });
}
Also used : Condition(org.assertj.core.api.Condition) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcConnection(org.apache.plc4x.java.api.PlcConnection) ExecutionException(java.util.concurrent.ExecutionException) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException)

Example 24 with PlcConnection

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

the class OpcuaPlcDriverTest method writeVariables.

@Test
public void writeVariables() throws Exception {
    PlcConnection opcuaConnection = new PlcDriverManager().getConnection(tcpConnectionAddress);
    Condition<PlcConnection> is_connected = new Condition<>(PlcConnection::isConnected, "is connected");
    assertThat(opcuaConnection).is(is_connected);
    PlcWriteRequest.Builder builder = opcuaConnection.writeRequestBuilder();
    builder.addItem("Bool", BOOL_IDENTIFIER_READ_WRITE, true);
    builder.addItem("Byte", BYTE_IDENTIFIER_READ_WRITE + ";BYTE", (short) 3);
    builder.addItem("Double", DOUBLE_IDENTIFIER_READ_WRITE, 0.5d);
    builder.addItem("Float", FLOAT_IDENTIFIER_READ_WRITE, 0.5f);
    // builder.addItem("Int16", INT16_IDENTIFIER_READ_WRITE + "", (short) 1);
    builder.addItem("Int32", INT32_IDENTIFIER_READ_WRITE, 42);
    builder.addItem("Int64", INT64_IDENTIFIER_READ_WRITE, 42L);
    builder.addItem("Integer", INTEGER_IDENTIFIER_READ_WRITE, 42);
    builder.addItem("SByte", SBYTE_IDENTIFIER_READ_WRITE + ";SINT", -127);
    builder.addItem("String", STRING_IDENTIFIER_READ_WRITE, "Helllo Toddy!");
    builder.addItem("UInt16", UINT16_IDENTIFIER_READ_WRITE + ";UINT", 65535);
    builder.addItem("UInt32", UINT32_IDENTIFIER_READ_WRITE + ";UDINT", 101010101L);
    builder.addItem("UInt64", UINT64_IDENTIFIER_READ_WRITE + ";ULINT", new BigInteger("1337"));
    builder.addItem("UInteger", UINTEGER_IDENTIFIER_READ_WRITE + ";UDINT", 102020202L);
    builder.addItem("BooleanArray", BOOL_ARRAY_IDENTIFIER, new Boolean[] { true, true, true, true, true });
    builder.addItem("ByteArray", BYTE_ARRAY_IDENTIFIER + ";BYTE", new Short[] { 1, 100, 100, 255, 123 });
    builder.addItem("DoubleArray", DOUBLE_ARRAY_IDENTIFIER, new Double[] { 1.0, 2.0, 3.0, 4.0, 5.0 });
    builder.addItem("FloatArray", FLOAT_ARRAY_IDENTIFIER, new Float[] { 1.0F, 2.0F, 3.0F, 4.0F, 5.0F });
    builder.addItem("Int16Array", INT16_ARRAY_IDENTIFIER, new Short[] { 1, 2, 3, 4, 5 });
    builder.addItem("Int32Array", INT32_ARRAY_IDENTIFIER, new Integer[] { 1, 2, 3, 4, 5 });
    builder.addItem("Int64Array", INT64_ARRAY_IDENTIFIER, new Long[] { 1L, 2L, 3L, 4L, 5L });
    builder.addItem("IntegerArray", INT32_ARRAY_IDENTIFIER, new Integer[] { 1, 2, 3, 4, 5 });
    builder.addItem("SByteArray", SBYTE_ARRAY_IDENTIFIER, new Byte[] { 1, 2, 3, 4, 5 });
    builder.addItem("StringArray", STRING_ARRAY_IDENTIFIER, new String[] { "1", "2", "3", "4", "5" });
    builder.addItem("UInt16Array", UINT16_ARRAY_IDENTIFIER + ";UINT", new Short[] { 1, 2, 3, 4, 5 });
    builder.addItem("UInt32Array", UINT32_ARRAY_IDENTIFIER + ";UDINT", new Integer[] { 1, 2, 3, 4, 5 });
    builder.addItem("UInt64Array", UINT64_ARRAY_IDENTIFIER + ";ULINT", new Long[] { 1L, 2L, 3L, 4L, 5L });
    builder.addItem("DoesNotExists", DOES_NOT_EXIST_IDENTIFIER_READ_WRITE, "11");
    PlcWriteRequest request = builder.build();
    PlcWriteResponse response = request.execute().get();
    assertThat(response.getResponseCode("Bool")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("Byte")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("Double")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("Float")).isEqualTo(PlcResponseCode.OK);
    // assertThat(response.getResponseCode("Int16")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("Int32")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("Int64")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("Integer")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("SByte")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("String")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("UInt16")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("UInt32")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("UInt64")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("UInteger")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("BooleanArray")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("ByteArray")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("DoubleArray")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("FloatArray")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("Int16Array")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("Int32Array")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("Int64Array")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("IntegerArray")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("SByteArray")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("StringArray")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("UInt16Array")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("UInt32Array")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("UInt64Array")).isEqualTo(PlcResponseCode.OK);
    assertThat(response.getResponseCode("DoesNotExists")).isEqualTo(PlcResponseCode.NOT_FOUND);
    opcuaConnection.close();
    assert !opcuaConnection.isConnected();
}
Also used : Condition(org.assertj.core.api.Condition) 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 25 with PlcConnection

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

the class OpcuaPlcDriverTest method connectionWithDiscoveryParam.

@Test
public void connectionWithDiscoveryParam() {
    connectionStringValidSet.forEach(connectionAddress -> {
        discoveryParamValidSet.forEach(discoveryParam -> {
            String connectionString = connectionAddress + paramSectionDivider + discoveryParam;
            try {
                PlcConnection opcuaConnection = new PlcDriverManager().getConnection(connectionString);
                Condition<PlcConnection> is_connected = new Condition<>(PlcConnection::isConnected, "is connected");
                assertThat(opcuaConnection).is(is_connected);
                opcuaConnection.close();
                assertThat(opcuaConnection).isNot(is_connected);
            } catch (PlcConnectionException e) {
                fail("Exception during connectionWithDiscoveryParam while connecting Test EXCEPTION: " + e.getMessage());
            } catch (Exception e) {
                fail("Exception during connectionWithDiscoveryParam while closing Test EXCEPTION: " + e.getMessage());
            }
        });
    });
}
Also used : Condition(org.assertj.core.api.Condition) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcConnection(org.apache.plc4x.java.api.PlcConnection) ExecutionException(java.util.concurrent.ExecutionException) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException)

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