Search in sources :

Example 1 with SDOUploadConversation

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

the class CANOpenProtocolLogic method readInternally.

private void readInternally(PlcReadRequest readRequest, CANOpenSDOField field, CompletableFuture<PlcReadResponse> response) {
    String fieldName = readRequest.getFieldNames().iterator().next();
    final RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
    CompletableFuture<PlcValue> callback = new CompletableFuture<>();
    callback.whenComplete((value, error) -> {
        if (error != null) {
            Map<String, ResponseItem<PlcValue>> fields = new HashMap<>();
            if (error instanceof CANOpenAbortException) {
                fields.put(fieldName, new ResponseItem<>(PlcResponseCode.REMOTE_ERROR, new PlcLINT(((CANOpenAbortException) error).getAbortCode())));
            } else {
                fields.put(fieldName, new ResponseItem<>(PlcResponseCode.REMOTE_ERROR, null));
            }
            response.complete(new DefaultPlcReadResponse(readRequest, fields));
            transaction.endRequest();
            return;
        }
        Map<String, ResponseItem<PlcValue>> fields = new HashMap<>();
        fields.put(fieldName, new ResponseItem<>(PlcResponseCode.OK, value));
        response.complete(new DefaultPlcReadResponse(readRequest, fields));
        transaction.endRequest();
    });
    SDOUploadConversation upload = new SDOUploadConversation(conversation, field.getNodeId(), field.getAnswerNodeId(), new IndexAddress(field.getIndex(), field.getSubIndex()), field.getCanOpenDataType());
    transaction.submit(() -> upload.execute(callback));
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CANOpenAbortException(org.apache.plc4x.java.canopen.transport.CANOpenAbortException) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) PlcValue(org.apache.plc4x.java.api.value.PlcValue) CompletableFuture(java.util.concurrent.CompletableFuture) SDOUploadConversation(org.apache.plc4x.java.canopen.api.conversation.canopen.SDOUploadConversation) IndexAddress(org.apache.plc4x.java.canopen.readwrite.IndexAddress) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) PlcLINT(org.apache.plc4x.java.spi.values.PlcLINT)

Aggregations

HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 PlcValue (org.apache.plc4x.java.api.value.PlcValue)1 SDOUploadConversation (org.apache.plc4x.java.canopen.api.conversation.canopen.SDOUploadConversation)1 IndexAddress (org.apache.plc4x.java.canopen.readwrite.IndexAddress)1 CANOpenAbortException (org.apache.plc4x.java.canopen.transport.CANOpenAbortException)1 DefaultPlcReadResponse (org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse)1 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)1 RequestTransactionManager (org.apache.plc4x.java.spi.transaction.RequestTransactionManager)1 PlcLINT (org.apache.plc4x.java.spi.values.PlcLINT)1