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;
}
}
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;
}
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;
}
}
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;
}
}
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()));
}
}
Aggregations