Search in sources :

Example 1 with SDODownloadConversation

use of org.apache.plc4x.java.canopen.api.conversation.canopen.SDODownloadConversation in project plc4x by apache.

the class CANOpenProtocolLogic method writeInternally.

private void writeInternally(DefaultPlcWriteRequest writeRequest, CANOpenSDOField field, CompletableFuture<PlcWriteResponse> response) {
    final RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
    String fieldName = writeRequest.getFieldNames().iterator().next();
    CompletableFuture<PlcResponseCode> callback = new CompletableFuture<>();
    callback.whenComplete((code, error) -> {
        if (error != null) {
            if (error instanceof CANOpenAbortException) {
                response.complete(new DefaultPlcWriteResponse(writeRequest, Collections.singletonMap(fieldName, PlcResponseCode.REMOTE_ERROR)));
            } else {
                response.complete(new DefaultPlcWriteResponse(writeRequest, Collections.singletonMap(fieldName, PlcResponseCode.INTERNAL_ERROR)));
            }
            transaction.endRequest();
            return;
        }
        response.complete(new DefaultPlcWriteResponse(writeRequest, Collections.singletonMap(fieldName, code)));
        transaction.endRequest();
    });
    PlcValue writeValue = writeRequest.getPlcValues().get(0);
    SDODownloadConversation download = new SDODownloadConversation(conversation, field.getNodeId(), field.getAnswerNodeId(), new IndexAddress(field.getIndex(), field.getSubIndex()), writeValue, field.getCanOpenDataType());
    transaction.submit(() -> download.execute(callback));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PlcValue(org.apache.plc4x.java.api.value.PlcValue) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) IndexAddress(org.apache.plc4x.java.canopen.readwrite.IndexAddress) CANOpenAbortException(org.apache.plc4x.java.canopen.transport.CANOpenAbortException) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) SDODownloadConversation(org.apache.plc4x.java.canopen.api.conversation.canopen.SDODownloadConversation)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)1 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)1 PlcValue (org.apache.plc4x.java.api.value.PlcValue)1 SDODownloadConversation (org.apache.plc4x.java.canopen.api.conversation.canopen.SDODownloadConversation)1 IndexAddress (org.apache.plc4x.java.canopen.readwrite.IndexAddress)1 CANOpenAbortException (org.apache.plc4x.java.canopen.transport.CANOpenAbortException)1 DefaultPlcWriteResponse (org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse)1 RequestTransactionManager (org.apache.plc4x.java.spi.transaction.RequestTransactionManager)1