Search in sources :

Example 6 with PlcException

use of org.apache.plc4x.java.api.exceptions.PlcException in project plc4x by apache.

the class AdsProtocolLogic method subscribe.

@Override
public CompletableFuture<PlcSubscriptionResponse> subscribe(PlcSubscriptionRequest subscriptionRequest) {
    // Get all ADS addresses in their resolved state.
    final CompletableFuture<List<DirectAdsField>> directAdsFieldsFuture = getDirectAddresses(subscriptionRequest.getFields().stream().map(field -> ((DefaultPlcSubscriptionField) field).getPlcField()).collect(Collectors.toList()));
    // If all addresses were already resolved we can send the request immediately.
    if (directAdsFieldsFuture.isDone()) {
        final List<DirectAdsField> fields = directAdsFieldsFuture.getNow(null);
        if (fields != null) {
            return executeSubscribe(subscriptionRequest);
        } else {
            final CompletableFuture<PlcSubscriptionResponse> errorFuture = new CompletableFuture<>();
            errorFuture.completeExceptionally(new PlcException("Fields are null"));
            return errorFuture;
        }
    } else // If there are still symbolic addresses that have to be resolved, send the
    // request as soon as the resolution is done.
    // In order to instantly be able to return a future, for the final result we have to
    // create a new one which is then completed later on. Unfortunately as soon as the
    // directAdsFieldsFuture is completed we still don't have the end result, but we can
    // now actually send the delayed read request ... as soon as that future completes
    // we can complete the initial one.
    {
        CompletableFuture<PlcSubscriptionResponse> delayedSubscribe = new CompletableFuture<>();
        directAdsFieldsFuture.handle((directAdsFields, throwable) -> {
            if (directAdsFields != null) {
                final CompletableFuture<PlcSubscriptionResponse> delayedResponse = executeSubscribe(subscriptionRequest);
                delayedResponse.handle((plcSubscribeResponse, throwable1) -> {
                    if (plcSubscribeResponse != null) {
                        delayedSubscribe.complete(plcSubscribeResponse);
                    } else {
                        delayedSubscribe.completeExceptionally(throwable1);
                    }
                    return this;
                });
            } else {
                delayedSubscribe.completeExceptionally(throwable);
            }
            return this;
        });
        return delayedSubscribe;
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PlcException(org.apache.plc4x.java.api.exceptions.PlcException)

Example 7 with PlcException

use of org.apache.plc4x.java.api.exceptions.PlcException in project plc4x by apache.

the class AdsProtocolLogic method singleWrite.

protected CompletableFuture<PlcWriteResponse> singleWrite(PlcWriteRequest writeRequest, DirectAdsField directAdsField) {
    CompletableFuture<PlcWriteResponse> future = new CompletableFuture<>();
    final String fieldName = writeRequest.getFieldNames().iterator().next();
    final AdsField plcField = (AdsField) writeRequest.getField(fieldName);
    final PlcValue plcValue = writeRequest.getPlcValue(fieldName);
    final int stringLength;
    if (directAdsField.getAdsDataType() == AdsDataType.STRING) {
        stringLength = plcValue.getString().length() + 1;
    } else {
        if (directAdsField.getAdsDataType() == AdsDataType.WSTRING) {
            stringLength = (plcValue.getString().length() + 1) * 2;
        } else {
            stringLength = 0;
        }
    }
    try {
        WriteBufferByteBased writeBuffer = new WriteBufferByteBased(DataItem.getLengthInBytes(plcValue, plcField.getAdsDataType().getDataFormatName(), stringLength));
        DataItem.staticSerialize(writeBuffer, plcValue, plcField.getAdsDataType().getDataFormatName(), stringLength, ByteOrder.LITTLE_ENDIAN);
        AdsData adsData = new AdsWriteRequest(directAdsField.getIndexGroup(), directAdsField.getIndexOffset(), writeBuffer.getData());
        AmsPacket amsPacket = new AmsPacket(configuration.getTargetAmsNetId(), configuration.getTargetAmsPort(), configuration.getSourceAmsNetId(), configuration.getSourceAmsPort(), CommandId.ADS_WRITE, DEFAULT_COMMAND_STATE, 0, getInvokeId(), adsData);
        AmsTCPPacket amsTCPPacket = new AmsTCPPacket(amsPacket);
        // Start a new request-transaction (Is ended in the response-handler)
        RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
        transaction.submit(() -> context.sendRequest(amsTCPPacket).expectResponse(AmsTCPPacket.class, Duration.ofMillis(configuration.getTimeoutRequest())).onTimeout(future::completeExceptionally).onError((p, e) -> future.completeExceptionally(e)).check(responseAmsPacket -> responseAmsPacket.getUserdata().getInvokeId() == amsPacket.getInvokeId()).unwrap(response -> (AdsWriteResponse) response.getUserdata().getData()).handle(responseAdsData -> {
            if (responseAdsData.getResult() == ReturnCode.OK) {
                final PlcWriteResponse plcWriteResponse = convertToPlc4xWriteResponse(writeRequest, responseAdsData);
                // Convert the response from the PLC into a PLC4X Response ...
                future.complete(plcWriteResponse);
            } else {
                // TODO: Implement this correctly.
                future.completeExceptionally(new PlcException("Unexpected return code " + responseAdsData.getResult()));
            }
            // Finish the request-transaction.
            transaction.endRequest();
        }));
    } catch (Exception e) {
        future.completeExceptionally(new PlcException("Error"));
    }
    return future;
}
Also used : IntStream(java.util.stream.IntStream) PlcSubscriptionType(org.apache.plc4x.java.api.types.PlcSubscriptionType) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) PlcValue(org.apache.plc4x.java.api.value.PlcValue) DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) CompletableFuture(java.util.concurrent.CompletableFuture) PlcConsumerRegistration(org.apache.plc4x.java.api.model.PlcConsumerRegistration) AdsSubscriptionHandle(org.apache.plc4x.java.ads.model.AdsSubscriptionHandle) PlcException(org.apache.plc4x.java.api.exceptions.PlcException) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) ConversationContext(org.apache.plc4x.java.spi.ConversationContext) DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) Duration(java.time.Duration) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) DataItem(org.apache.plc4x.java.ads.readwrite.DataItem) Plc4xProtocolBase(org.apache.plc4x.java.spi.Plc4xProtocolBase) BigInteger(java.math.BigInteger) Logger(org.slf4j.Logger) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) org.apache.plc4x.java.spi.messages(org.apache.plc4x.java.spi.messages) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) org.apache.plc4x.java.ads.readwrite(org.apache.plc4x.java.ads.readwrite) PlcField(org.apache.plc4x.java.api.model.PlcField) IEC61131ValueHandler(org.apache.plc4x.java.spi.values.IEC61131ValueHandler) HasConfiguration(org.apache.plc4x.java.spi.configuration.HasConfiguration) org.apache.plc4x.java.spi.generation(org.apache.plc4x.java.spi.generation) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) org.apache.plc4x.java.ads.field(org.apache.plc4x.java.ads.field) AdsConfiguration(org.apache.plc4x.java.ads.configuration.AdsConfiguration) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) org.apache.plc4x.java.api.messages(org.apache.plc4x.java.api.messages) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) PlcException(org.apache.plc4x.java.api.exceptions.PlcException) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) PlcException(org.apache.plc4x.java.api.exceptions.PlcException) CompletableFuture(java.util.concurrent.CompletableFuture) PlcValue(org.apache.plc4x.java.api.value.PlcValue)

Example 8 with PlcException

use of org.apache.plc4x.java.api.exceptions.PlcException in project plc4x by apache.

the class AdsProtocolLogic method read.

@Override
public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
    // Get all ADS addresses in their resolved state.
    final CompletableFuture<List<DirectAdsField>> directAdsFieldsFuture = getDirectAddresses(readRequest.getFields());
    // If all addresses were already resolved we can send the request immediately.
    if (directAdsFieldsFuture.isDone()) {
        final List<DirectAdsField> fields = directAdsFieldsFuture.getNow(null);
        if (fields != null) {
            return executeRead(readRequest, fields);
        } else {
            final CompletableFuture<PlcReadResponse> errorFuture = new CompletableFuture<>();
            errorFuture.completeExceptionally(new PlcException("Fields are null"));
            return errorFuture;
        }
    } else {
        // If there are still symbolic addresses that have to be resolved, send the
        // request as soon as the resolution is done.
        // In order to instantly be able to return a future, for the final result we have to
        // create a new one which is then completed later on. Unfortunately as soon as the
        // directAdsFieldsFuture is completed we still don't have the end result, but we can
        // now actually send the delayed read request ... as soon as that future completes
        // we can complete the initial one.
        CompletableFuture<PlcReadResponse> delayedRead = new CompletableFuture<>();
        directAdsFieldsFuture.handle((directAdsFields, throwable) -> {
            if (directAdsFields != null) {
                final CompletableFuture<PlcReadResponse> delayedResponse = executeRead(readRequest, directAdsFields);
                delayedResponse.handle((plcReadResponse, throwable1) -> {
                    if (plcReadResponse != null) {
                        delayedRead.complete(plcReadResponse);
                    } else {
                        delayedRead.completeExceptionally(throwable1);
                    }
                    return this;
                });
            } else {
                delayedRead.completeExceptionally(throwable);
            }
            return this;
        });
        return delayedRead;
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PlcException(org.apache.plc4x.java.api.exceptions.PlcException)

Example 9 with PlcException

use of org.apache.plc4x.java.api.exceptions.PlcException in project plc4x by apache.

the class AdsProtocolLogic method write.

@Override
public CompletableFuture<PlcWriteResponse> write(PlcWriteRequest writeRequest) {
    // Get all ADS addresses in their resolved state.
    final CompletableFuture<List<DirectAdsField>> directAdsFieldsFuture = getDirectAddresses(writeRequest.getFields());
    // If all addresses were already resolved we can send the request immediately.
    if (directAdsFieldsFuture.isDone()) {
        final List<DirectAdsField> fields = directAdsFieldsFuture.getNow(null);
        if (fields != null) {
            return executeWrite(writeRequest, fields);
        } else {
            final CompletableFuture<PlcWriteResponse> errorFuture = new CompletableFuture<>();
            errorFuture.completeExceptionally(new PlcException("Fields are null"));
            return errorFuture;
        }
    } else // If there are still symbolic addresses that have to be resolved, send the
    // request as soon as the resolution is done.
    // In order to instantly be able to return a future, for the final result we have to
    // create a new one which is then completed later on. Unfortunately as soon as the
    // directAdsFieldsFuture is completed we still don't have the end result, but we can
    // now actually send the delayed read request ... as soon as that future completes
    // we can complete the initial one.
    {
        CompletableFuture<PlcWriteResponse> delayedWrite = new CompletableFuture<>();
        directAdsFieldsFuture.handle((directAdsFields, throwable) -> {
            if (directAdsFields != null) {
                final CompletableFuture<PlcWriteResponse> delayedResponse = executeWrite(writeRequest, directAdsFields);
                delayedResponse.handle((plcReadResponse, throwable1) -> {
                    if (plcReadResponse != null) {
                        delayedWrite.complete(plcReadResponse);
                    } else {
                        delayedWrite.completeExceptionally(throwable1);
                    }
                    return this;
                });
            } else {
                delayedWrite.completeExceptionally(throwable);
            }
            return this;
        });
        return delayedWrite;
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PlcException(org.apache.plc4x.java.api.exceptions.PlcException)

Example 10 with PlcException

use of org.apache.plc4x.java.api.exceptions.PlcException in project plc4x by apache.

the class CANOpenConversationBase method onError.

protected <T> void onError(CompletableFuture<T> receiver, CANOpenSDOResponse response, Throwable error) {
    if (error != null) {
        receiver.completeExceptionally(error);
        return;
    }
    if (response.getResponse() instanceof SDOAbortResponse) {
        SDOAbortResponse abort = (SDOAbortResponse) response.getResponse();
        SDOAbort sdoAbort = abort.getAbort();
        receiver.completeExceptionally(new PlcException("Could not read value. Remote party reported code " + sdoAbort.getCode()));
    }
}
Also used : PlcException(org.apache.plc4x.java.api.exceptions.PlcException)

Aggregations

PlcException (org.apache.plc4x.java.api.exceptions.PlcException)12 CompletableFuture (java.util.concurrent.CompletableFuture)6 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)4 java.util (java.util)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 IOException (java.io.IOException)2 BigInteger (java.math.BigInteger)2 Duration (java.time.Duration)2 Instant (java.time.Instant)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Consumer (java.util.function.Consumer)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)2 org.apache.plc4x.java.spi.generation (org.apache.plc4x.java.spi.generation)2 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1