Search in sources :

Example 1 with CANOpenAbortException

use of org.apache.plc4x.java.canopen.transport.CANOpenAbortException in project plc4x by apache.

the class SDODownloadConversation method execute.

public void execute(CompletableFuture<PlcResponseCode> receiver) {
    if (data.length > 4) {
        // segmented
        SDOInitiateSegmentedUploadResponse size = new SDOInitiateSegmentedUploadResponse(data.length, (byte) 0);
        delegate.send(createFrame(new SDOInitiateDownloadRequest(false, true, indexAddress, size))).check(new NodeIdPredicate(answerNodeId)).onTimeout(receiver::completeExceptionally).onError((response, error) -> receiver.completeExceptionally(error)).unwrap(CANOpenFrame::getPayload).only(CANOpenSDOResponse.class).unwrap(CANOpenSDOResponse::getResponse).check(new TypeOrAbortPredicate<>(SDOInitiateDownloadResponse.class)).unwrap(payload -> unwrap(SDOInitiateDownloadResponse.class, payload)).handle(either -> {
            if (either.isLeft()) {
                receiver.completeExceptionally(new CANOpenAbortException("Could not initiate upload", either.getLeft().getCode()));
            } else {
                SDOInitiateDownloadResponse response = either.get();
                if (response.getAddress().equals(indexAddress)) {
                    put(data, receiver, false, 0);
                } else {
                    // TODO find proper error code in spec
                    SDOAbort abort = new SDOAbort(indexAddress, 1000);
                    delegate.sendToWire(createFrame(new SDOAbortRequest(abort)));
                    receiver.complete(PlcResponseCode.REMOTE_ERROR);
                }
            }
        });
        return;
    }
    // expedited
    SDOInitiateDownloadRequest rq = new SDOInitiateDownloadRequest(true, true, indexAddress, new SDOInitiateExpeditedUploadResponse(data, (byte) 0));
    delegate.send(createFrame(rq)).check(new NodeIdPredicate(answerNodeId)).onTimeout(receiver::completeExceptionally).unwrap(CANOpenFrame::getPayload).only(CANOpenSDOResponse.class).onError((response, error) -> onError(receiver, response, error)).unwrap(CANOpenSDOResponse::getResponse).check(new TypeOrAbortPredicate<>(SDOInitiateDownloadResponse.class)).unwrap(payload -> unwrap(SDOInitiateDownloadResponse.class, payload)).handle(either -> {
        if (either.isLeft()) {
            receiver.completeExceptionally(new CANOpenAbortException("Could not initiate upload", either.getLeft().getCode()));
        } else {
            SDOInitiateDownloadResponse response = either.get();
            if (response.getCommand() == SDOResponseCommand.INITIATE_DOWNLOAD) {
                receiver.complete(PlcResponseCode.OK);
            } else {
                receiver.complete(PlcResponseCode.REMOTE_ERROR);
            }
        }
    });
}
Also used : SerializationException(org.apache.plc4x.java.spi.generation.SerializationException) CANOpenAbortException(org.apache.plc4x.java.canopen.transport.CANOpenAbortException) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) org.apache.plc4x.java.canopen.readwrite(org.apache.plc4x.java.canopen.readwrite) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) PlcValue(org.apache.plc4x.java.api.value.PlcValue) ByteOrder(org.apache.plc4x.java.spi.generation.ByteOrder) CompletableFuture(java.util.concurrent.CompletableFuture) WriteBufferByteBased(org.apache.plc4x.java.spi.generation.WriteBufferByteBased) CANOpenAbortException(org.apache.plc4x.java.canopen.transport.CANOpenAbortException)

Example 2 with CANOpenAbortException

use of org.apache.plc4x.java.canopen.transport.CANOpenAbortException in project plc4x by apache.

the class SDOUploadConversation method fetch.

private void fetch(ByteStorage.SDOUploadStorage storage, BiConsumer<Integer, byte[]> valueCallback, CompletableFuture<PlcValue> receiver, boolean toggle, int size) {
    logger.info("Request next data block for address {}/{}", Integer.toHexString(address.getIndex()), Integer.toHexString(address.getSubindex()));
    delegate.send(createFrame(new SDOSegmentUploadRequest(toggle))).check(new NodeIdPredicate(answerNodeId)).onTimeout(receiver::completeExceptionally).unwrap(CANOpenFrame::getPayload).only(CANOpenSDOResponse.class).onError((payload, error) -> onError(receiver, payload, error)).unwrap(CANOpenSDOResponse::getResponse).check(new TypeOrAbortPredicate<>(SDOSegmentUploadResponse.class)).unwrap(payload -> unwrap(SDOSegmentUploadResponse.class, payload)).handle(either -> {
        if (either.isLeft()) {
            SDOAbort abort = either.getLeft();
            receiver.completeExceptionally(new CANOpenAbortException("Could not complete operation", abort.getCode()));
        } else {
            SDOSegmentUploadResponse response = either.get();
            if (response.getToggle() != toggle) {
                // TODO find proper error code in specs
                receiver.completeExceptionally(new CANOpenAbortException("Remote operation failed", 1000));
                SDOAbort abort = new SDOAbort(address, 1000);
                delegate.sendToWire(createFrame(new SDOAbortRequest(abort)));
                return;
            }
            storage.append(either.get());
            if (response.getLast()) {
                // validate size
                logger.trace("Completed reading of data from {}/{}, collected {}, wanted {}", Integer.toHexString(address.getIndex()), Integer.toHexString(address.getSubindex()), storage.size(), size);
                valueCallback.accept(Long.valueOf(size).intValue(), storage.get());
            } else {
                logger.trace("Continue reading of data from {}/{}, collected {}, wanted {}", Integer.toHexString(address.getIndex()), Integer.toHexString(address.getSubindex()), storage.size(), size);
                fetch(storage, valueCallback, receiver, !toggle, size);
            }
        }
    });
}
Also used : PlcException(org.apache.plc4x.java.api.exceptions.PlcException) CANOpenAbortException(org.apache.plc4x.java.canopen.transport.CANOpenAbortException) ParseException(org.apache.plc4x.java.spi.generation.ParseException) Logger(org.slf4j.Logger) CANOpenFrame(org.apache.plc4x.java.canopen.readwrite.CANOpenFrame) org.apache.plc4x.java.canopen.readwrite(org.apache.plc4x.java.canopen.readwrite) LoggerFactory(org.slf4j.LoggerFactory) BiConsumer(java.util.function.BiConsumer) PlcValue(org.apache.plc4x.java.api.value.PlcValue) CompletableFuture(java.util.concurrent.CompletableFuture) ByteStorage(org.apache.plc4x.java.canopen.api.segmentation.accumulator.ByteStorage) CANOpenAbortException(org.apache.plc4x.java.canopen.transport.CANOpenAbortException) CANOpenFrame(org.apache.plc4x.java.canopen.readwrite.CANOpenFrame)

Example 3 with CANOpenAbortException

use of org.apache.plc4x.java.canopen.transport.CANOpenAbortException 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 4 with CANOpenAbortException

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

Example 5 with CANOpenAbortException

use of org.apache.plc4x.java.canopen.transport.CANOpenAbortException in project plc4x by apache.

the class SDOUploadConversation method execute.

public void execute(CompletableFuture<PlcValue> receiver) {
    SDOInitiateUploadRequest rq = new SDOInitiateUploadRequest(address);
    delegate.send(createFrame(rq)).check(new NodeIdPredicate(answerNodeId)).onTimeout(receiver::completeExceptionally).unwrap(CANOpenFrame::getPayload).only(CANOpenSDOResponse.class).onError((payload, error) -> onError(receiver, payload, error)).unwrap(CANOpenSDOResponse::getResponse).check(new TypeOrAbortPredicate<>(SDOInitiateUploadResponse.class)).unwrap(payload -> unwrap(SDOInitiateUploadResponse.class, payload)).check(either -> either.isLeft() || either.get().getAddress().equals(address)).handle(either -> {
        if (either.isLeft()) {
            SDOAbort abort = either.getLeft();
            receiver.completeExceptionally(new CANOpenAbortException("Could not complete operation", abort.getCode()));
        } else {
            handle(receiver, either.get());
        }
    });
}
Also used : PlcException(org.apache.plc4x.java.api.exceptions.PlcException) CANOpenAbortException(org.apache.plc4x.java.canopen.transport.CANOpenAbortException) ParseException(org.apache.plc4x.java.spi.generation.ParseException) Logger(org.slf4j.Logger) CANOpenFrame(org.apache.plc4x.java.canopen.readwrite.CANOpenFrame) org.apache.plc4x.java.canopen.readwrite(org.apache.plc4x.java.canopen.readwrite) LoggerFactory(org.slf4j.LoggerFactory) BiConsumer(java.util.function.BiConsumer) PlcValue(org.apache.plc4x.java.api.value.PlcValue) CompletableFuture(java.util.concurrent.CompletableFuture) ByteStorage(org.apache.plc4x.java.canopen.api.segmentation.accumulator.ByteStorage) CANOpenAbortException(org.apache.plc4x.java.canopen.transport.CANOpenAbortException)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)5 PlcValue (org.apache.plc4x.java.api.value.PlcValue)5 CANOpenAbortException (org.apache.plc4x.java.canopen.transport.CANOpenAbortException)5 org.apache.plc4x.java.canopen.readwrite (org.apache.plc4x.java.canopen.readwrite)3 BiConsumer (java.util.function.BiConsumer)2 PlcException (org.apache.plc4x.java.api.exceptions.PlcException)2 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)2 ByteStorage (org.apache.plc4x.java.canopen.api.segmentation.accumulator.ByteStorage)2 CANOpenFrame (org.apache.plc4x.java.canopen.readwrite.CANOpenFrame)2 IndexAddress (org.apache.plc4x.java.canopen.readwrite.IndexAddress)2 ParseException (org.apache.plc4x.java.spi.generation.ParseException)2 RequestTransactionManager (org.apache.plc4x.java.spi.transaction.RequestTransactionManager)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)1 SDODownloadConversation (org.apache.plc4x.java.canopen.api.conversation.canopen.SDODownloadConversation)1 SDOUploadConversation (org.apache.plc4x.java.canopen.api.conversation.canopen.SDOUploadConversation)1