Search in sources :

Example 1 with PlcWriteRequest

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

the class Plc4xCommunication method setValue.

public void setValue(String tag, String value, String connectionString) {
    PlcConnection connection = null;
    try {
        connection = driverManager.getConnection(connectionString);
        if (connection.isConnected() == false) {
            logger.debug("getConnection() returned a connection that isn't connected");
            connection.connect();
        }
    } catch (PlcConnectionException e) {
        logger.warn("Failed" + e);
    }
    if (!connection.getMetadata().canWrite()) {
        logger.error("This connection doesn't support writing.");
        try {
            connection.close();
        } catch (Exception e) {
            logger.warn("Closing connection failed with error " + e);
        }
        return;
    }
    // Create a new read request:
    // - Give the single item requested an alias name
    final PlcWriteRequest.Builder builder = connection.writeRequestBuilder();
    // If an array value is passed instead of a single value then convert to a String array
    if ((value.charAt(0) == '[') && (value.charAt(value.length() - 1) == ']')) {
        String[] values = value.substring(1, value.length() - 1).split(",");
        logger.info("Adding Tag " + Arrays.toString(values));
        builder.addItem(tag, tag, values);
    } else {
        builder.addItem(tag, tag, value);
    }
    PlcWriteRequest writeRequest = builder.build();
    try {
        writeRequest.execute().get();
    } catch (InterruptedException | ExecutionException e) {
        logger.warn("Failed" + e);
    }
    try {
        connection.close();
    } catch (Exception e) {
        logger.warn("Closing Connection Failed with error " + e);
    }
    return;
}
Also used : PlcWriteRequest(org.apache.plc4x.java.api.messages.PlcWriteRequest) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) ExecutionException(java.util.concurrent.ExecutionException) PlcConnection(org.apache.plc4x.java.api.PlcConnection) TimeoutException(java.util.concurrent.TimeoutException) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with PlcWriteRequest

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

the class Plc4x2AdsProtocol method decode.

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, AmsPacket amsPacket, List<Object> out) throws Exception {
    LOGGER.trace("(-->IN): {}, {}, {}", channelHandlerContext, amsPacket, out);
    if (amsPacket instanceof AdsDeviceNotificationRequest) {
        LOGGER.debug("Received notification {}", amsPacket);
        handleAdsDeviceNotificationRequest((AdsDeviceNotificationRequest) amsPacket);
        return;
    }
    PlcRequestContainer<InternalPlcRequest, InternalPlcResponse> plcRequestContainer = requests.remove(amsPacket.getAmsHeader().getInvokeId().getAsLong());
    if (plcRequestContainer == null) {
        LOGGER.info("Unmapped packet received {}", amsPacket);
        return;
    }
    PlcRequest request = plcRequestContainer.getRequest();
    final InternalPlcResponse response;
    // Handle the response to a read request.
    if (request instanceof PlcReadRequest) {
        if (amsPacket instanceof AdsReadResponse) {
            response = decodeReadResponse((AdsReadResponse) amsPacket, plcRequestContainer);
        } else {
            throw new PlcProtocolException("Wrong type correlated " + amsPacket);
        }
    } else if (request instanceof PlcWriteRequest) {
        if (amsPacket instanceof AdsWriteResponse) {
            response = decodeWriteResponse((AdsWriteResponse) amsPacket, plcRequestContainer);
        } else {
            throw new PlcProtocolException("Wrong type correlated " + amsPacket);
        }
    } else if (request instanceof PlcProprietaryRequest) {
        response = decodeProprietaryResponse(amsPacket, plcRequestContainer);
    } else {
        response = null;
    }
    LOGGER.debug("Plc4x response {}", response);
    // Confirm the response being handled.
    if (response != null) {
        plcRequestContainer.getResponseFuture().complete(response);
    }
}
Also used : PlcWriteRequest(org.apache.plc4x.java.api.messages.PlcWriteRequest) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) PlcRequest(org.apache.plc4x.java.api.messages.PlcRequest) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest)

Example 3 with PlcWriteRequest

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

the class Plc4x2AdsProtocol method decodeWriteResponse.

@SuppressWarnings("unchecked")
private InternalPlcResponse decodeWriteResponse(AdsWriteResponse responseMessage, PlcRequestContainer<InternalPlcRequest, InternalPlcResponse> requestContainer) {
    InternalPlcWriteRequest plcWriteRequest = (InternalPlcWriteRequest) requestContainer.getRequest();
    PlcResponseCode responseCode = decodeResponseCode(responseMessage.getResult());
    // TODO: does every item has the same ads response or is this whole aggregation broken?
    Map<String, PlcResponseCode> responseItems = plcWriteRequest.getFieldNames().stream().collect(Collectors.toMap(fieldName -> fieldName, ignore -> responseCode));
    return new DefaultPlcWriteResponse(plcWriteRequest, responseItems);
}
Also used : DirectAdsField(org.apache.plc4x.java.ads.model.DirectAdsField) PlcIoException(org.apache.plc4x.java.api.exceptions.PlcIoException) PlcWriteRequest(org.apache.plc4x.java.api.messages.PlcWriteRequest) PlcRequest(org.apache.plc4x.java.api.messages.PlcRequest) LoggerFactory(org.slf4j.LoggerFactory) PlcValue(org.apache.plc4x.java.api.value.PlcValue) ConcurrentMap(java.util.concurrent.ConcurrentMap) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) AdsException(org.apache.plc4x.java.ads.protocol.exception.AdsException) PlcException(org.apache.plc4x.java.api.exceptions.PlcException) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) LittleEndianEncoder.encodeData(org.apache.plc4x.java.ads.protocol.util.LittleEndianEncoder.encodeData) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) org.apache.plc4x.java.ads.api.commands(org.apache.plc4x.java.ads.api.commands) PlcList(org.apache.plc4x.java.api.value.PlcList) LinkedList(java.util.LinkedList) AdsDataType(org.apache.plc4x.java.ads.model.AdsDataType) Invoke(org.apache.plc4x.java.ads.api.generic.types.Invoke) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) AmsPacket(org.apache.plc4x.java.ads.api.generic.AmsPacket) Logger(org.slf4j.Logger) AmsNetId(org.apache.plc4x.java.ads.api.generic.types.AmsNetId) PlcProtocolPayloadTooBigException(org.apache.plc4x.java.api.exceptions.PlcProtocolPayloadTooBigException) org.apache.plc4x.java.ads.api.commands.types(org.apache.plc4x.java.ads.api.commands.types) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) org.apache.plc4x.java.spi.messages(org.apache.plc4x.java.spi.messages) SymbolicAdsField(org.apache.plc4x.java.ads.model.SymbolicAdsField) Collectors(java.util.stream.Collectors) PlcField(org.apache.plc4x.java.api.model.PlcField) MessageToMessageCodec(io.netty.handler.codec.MessageToMessageCodec) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) AmsPort(org.apache.plc4x.java.ads.api.generic.types.AmsPort) AdsField(org.apache.plc4x.java.ads.model.AdsField) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode)

Example 4 with PlcWriteRequest

use of org.apache.plc4x.java.api.messages.PlcWriteRequest 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 5 with PlcWriteRequest

use of org.apache.plc4x.java.api.messages.PlcWriteRequest 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)

Aggregations

PlcWriteRequest (org.apache.plc4x.java.api.messages.PlcWriteRequest)14 PlcConnection (org.apache.plc4x.java.api.PlcConnection)8 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)7 PlcWriteResponse (org.apache.plc4x.java.api.messages.PlcWriteResponse)7 PlcRequest (org.apache.plc4x.java.api.messages.PlcRequest)5 PlcValue (org.apache.plc4x.java.api.value.PlcValue)5 PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)4 PlcProtocolException (org.apache.plc4x.java.api.exceptions.PlcProtocolException)4 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)4 BigInteger (java.math.BigInteger)3 PlcReadResponse (org.apache.plc4x.java.api.messages.PlcReadResponse)3 PlcField (org.apache.plc4x.java.api.model.PlcField)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Consumer (java.util.function.Consumer)2