Search in sources :

Example 1 with IndexAddress

use of org.apache.plc4x.java.canopen.readwrite.IndexAddress 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)

Example 2 with IndexAddress

use of org.apache.plc4x.java.canopen.readwrite.IndexAddress 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)2 PlcValue (org.apache.plc4x.java.api.value.PlcValue)2 IndexAddress (org.apache.plc4x.java.canopen.readwrite.IndexAddress)2 CANOpenAbortException (org.apache.plc4x.java.canopen.transport.CANOpenAbortException)2 RequestTransactionManager (org.apache.plc4x.java.spi.transaction.RequestTransactionManager)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)1 SDODownloadConversation (org.apache.plc4x.java.canopen.api.conversation.canopen.SDODownloadConversation)1 SDOUploadConversation (org.apache.plc4x.java.canopen.api.conversation.canopen.SDOUploadConversation)1 DefaultPlcReadResponse (org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse)1 DefaultPlcWriteResponse (org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse)1 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)1 PlcLINT (org.apache.plc4x.java.spi.values.PlcLINT)1