use of org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse in project plc4x by apache.
the class ModbusRtuProtocolLogic method read.
@Override
public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
CompletableFuture<PlcReadResponse> future = new CompletableFuture<>();
DefaultPlcReadRequest request = (DefaultPlcReadRequest) readRequest;
// Example for sending a request ...
if (request.getFieldNames().size() == 1) {
String fieldName = request.getFieldNames().iterator().next();
ModbusField field = (ModbusField) request.getField(fieldName);
final ModbusPDU requestPdu = getReadRequestPdu(field);
ModbusRtuADU modbusRtuADU = new ModbusRtuADU(unitIdentifier, requestPdu, false);
RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
transaction.submit(() -> context.sendRequest(modbusRtuADU).expectResponse(ModbusRtuADU.class, requestTimeout).onTimeout(future::completeExceptionally).onError((p, e) -> future.completeExceptionally(e)).unwrap(ModbusRtuADU::getPdu).handle(responsePdu -> {
// Try to decode the response data based on the corresponding request.
PlcValue plcValue = null;
PlcResponseCode responseCode;
// Check if the response was an error response.
if (responsePdu instanceof ModbusPDUError) {
ModbusPDUError errorResponse = (ModbusPDUError) responsePdu;
responseCode = getErrorCode(errorResponse);
} else {
try {
plcValue = toPlcValue(requestPdu, responsePdu, field.getDataType());
responseCode = PlcResponseCode.OK;
} catch (ParseException e) {
// Add an error response code ...
responseCode = PlcResponseCode.INTERNAL_ERROR;
}
}
// Prepare the response.
PlcReadResponse response = new DefaultPlcReadResponse(request, Collections.singletonMap(fieldName, new ResponseItem<>(responseCode, plcValue)));
// Pass the response back to the application.
future.complete(response);
// Finish the request-transaction.
transaction.endRequest();
}));
} else {
future.completeExceptionally(new PlcRuntimeException("Modbus only supports single filed requests"));
}
return future;
}
use of org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse in project plc4x by apache.
the class ModbusTcpProtocolLogic method read.
@Override
public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
CompletableFuture<PlcReadResponse> future = new CompletableFuture<>();
DefaultPlcReadRequest request = (DefaultPlcReadRequest) readRequest;
// Example for sending a request ...
if (request.getFieldNames().size() == 1) {
String fieldName = request.getFieldNames().iterator().next();
ModbusField field = (ModbusField) request.getField(fieldName);
final ModbusPDU requestPdu = getReadRequestPdu(field);
int transactionIdentifier = transactionIdentifierGenerator.getAndIncrement();
// If we've reached the max value for a 16 bit transaction identifier, reset back to 1
if (transactionIdentifierGenerator.get() == 0xFFFF) {
transactionIdentifierGenerator.set(1);
}
ModbusTcpADU modbusTcpADU = new ModbusTcpADU(transactionIdentifier, unitIdentifier, requestPdu, false);
RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
transaction.submit(() -> context.sendRequest(modbusTcpADU).expectResponse(ModbusTcpADU.class, requestTimeout).onTimeout(future::completeExceptionally).onError((p, e) -> future.completeExceptionally(e)).check(p -> ((p.getTransactionIdentifier() == transactionIdentifier) && (p.getUnitIdentifier() == unitIdentifier))).unwrap(ModbusTcpADU::getPdu).handle(responsePdu -> {
// Try to decode the response data based on the corresponding request.
PlcValue plcValue = null;
PlcResponseCode responseCode;
// Check if the response was an error response.
if (responsePdu instanceof ModbusPDUError) {
ModbusPDUError errorResponse = (ModbusPDUError) responsePdu;
responseCode = getErrorCode(errorResponse);
} else {
try {
plcValue = toPlcValue(requestPdu, responsePdu, field.getDataType());
responseCode = PlcResponseCode.OK;
} catch (ParseException e) {
// Add an error response code ...
responseCode = PlcResponseCode.INTERNAL_ERROR;
}
}
// Prepare the response.
PlcReadResponse response = new DefaultPlcReadResponse(request, Collections.singletonMap(fieldName, new ResponseItem<>(responseCode, plcValue)));
// Pass the response back to the application.
future.complete(response);
// Finish the request-transaction.
transaction.endRequest();
}));
} else {
future.completeExceptionally(new PlcRuntimeException("Modbus only supports single filed requests"));
}
return future;
}
use of org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse in project plc4x by apache.
the class ModbusAsciiProtocolLogic method read.
@Override
public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
CompletableFuture<PlcReadResponse> future = new CompletableFuture<>();
DefaultPlcReadRequest request = (DefaultPlcReadRequest) readRequest;
// Example for sending a request ...
if (request.getFieldNames().size() == 1) {
String fieldName = request.getFieldNames().iterator().next();
ModbusField field = (ModbusField) request.getField(fieldName);
final ModbusPDU requestPdu = getReadRequestPdu(field);
ModbusAsciiADU modbusAsciiADU = new ModbusAsciiADU(unitIdentifier, requestPdu, false);
RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
transaction.submit(() -> context.sendRequest(modbusAsciiADU).expectResponse(ModbusAsciiADU.class, requestTimeout).onTimeout(future::completeExceptionally).onError((p, e) -> future.completeExceptionally(e)).unwrap(ModbusAsciiADU::getPdu).handle(responsePdu -> {
// Try to decode the response data based on the corresponding request.
PlcValue plcValue = null;
PlcResponseCode responseCode;
// Check if the response was an error response.
if (responsePdu instanceof ModbusPDUError) {
ModbusPDUError errorResponse = (ModbusPDUError) responsePdu;
responseCode = getErrorCode(errorResponse);
} else {
try {
plcValue = toPlcValue(requestPdu, responsePdu, field.getDataType());
responseCode = PlcResponseCode.OK;
} catch (ParseException e) {
// Add an error response code ...
responseCode = PlcResponseCode.INTERNAL_ERROR;
}
}
// Prepare the response.
PlcReadResponse response = new DefaultPlcReadResponse(request, Collections.singletonMap(fieldName, new ResponseItem<>(responseCode, plcValue)));
// Pass the response back to the application.
future.complete(response);
// Finish the request-transaction.
transaction.endRequest();
}));
} else {
future.completeExceptionally(new PlcRuntimeException("Modbus only supports single filed requests"));
}
return future;
}
use of org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse in project plc4x by apache.
the class Plc4XDf1Protocol method decode.
@Override
protected void decode(ChannelHandlerContext ctx, DF1Symbol msg, List<Object> out) throws Exception {
logger.debug("Received DF1 Command incoming {}", msg);
if (msg instanceof DF1SymbolMessageFrameNAK) {
logger.warn("Received a response NAK, notify all requests");
for (Map.Entry<Integer, PlcRequestContainer> entry : requests.entrySet()) {
entry.getValue().getResponseFuture().complete(new DefaultPlcReadResponse((PlcReadRequest) entry.getValue().getRequest(), Collections.singletonMap("erster", new ResponseItem<>(PlcResponseCode.INTERNAL_ERROR, new PlcDINT(-1)))));
}
return;
} else if (msg instanceof DF1SymbolMessageFrameACK) {
logger.warn("Received a response ACK :D");
return;
}
assert msg instanceof DF1SymbolMessageFrame;
DF1Command command = ((DF1SymbolMessageFrame) msg).getCommand();
int transactionId = command.getTransactionCounter();
if (!requests.containsKey(transactionId)) {
logger.warn("Received a response to unknown transaction id {}", transactionId);
ctx.fireExceptionCaught(new RuntimeException("Received a response to unknown transaction id"));
ctx.close();
return;
}
// As every response has a matching request, get this request based on the tpdu.
PlcRequestContainer requestContainer = requests.remove(transactionId);
PlcRequest request = requestContainer.getRequest();
// Handle the response.
PlcResponse response = null;
if (request instanceof PlcReadRequest) {
/*
Things to do
- check response code (if there is something like that?
- cast the bytes to right datatype
- create Response
*/
// We can do this as we have only one fieldName in DF1
final String fieldName = ((PlcReadRequest) request).getFieldNames().iterator().next();
// TODO can there be another code than ok?
final PlcResponseCode responseCode = PlcResponseCode.OK;
// TODO maybe check for different status bytes
final Df1Field field = (Df1Field) ((PlcReadRequest) request).getField(fieldName);
// Cast byte and create response item
PlcValue responseItem = null;
byte[] data = ((DF1UnprotectedReadResponse) command).getData();
switch(field.getDataType()) {
case BIT:
break;
case INTEGER:
// TODO: type conversion is untested
responseItem = new PlcDINT((int) data[0] + ((int) data[1] << 8));
break;
case FLOAT:
break;
case BIT_STRING:
break;
case ARRAY:
break;
// TODO add all other cases here...
default:
throw new NotImplementedException("The DataType " + field.getDataType() + " is currently not implemented!");
}
response = new DefaultPlcReadResponse(((PlcReadRequest) request), Collections.singletonMap(fieldName, new ResponseItem<>(responseCode, responseItem)));
} else if (request instanceof PlcWriteRequest) {
logger.warn("Writing is currently not implemented but received a write response?!");
ctx.close();
throw new NotImplementedException("This is currently not implemented!");
}
// Confirm the response being handled.
if (response != null) {
requestContainer.getResponseFuture().complete(response);
}
}
use of org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse in project plc4x by apache.
the class Plc4xProtocolLogic method read.
@Override
public CompletableFuture<PlcReadResponse> read(PlcReadRequest apiReadRequest) {
CompletableFuture<PlcReadResponse> future = new CompletableFuture<>();
// Prepare the request.
List<Plc4xFieldRequest> plc4xFields = new ArrayList<>(apiReadRequest.getNumberOfFields());
for (String fieldName : apiReadRequest.getFieldNames()) {
final org.apache.plc4x.java.plc4x.field.Plc4xField plc4xField = (org.apache.plc4x.java.plc4x.field.Plc4xField) apiReadRequest.getField(fieldName);
Plc4xFieldRequest plc4xFieldRequest = new Plc4xFieldRequest(new Plc4xField(fieldName, plc4xField.getAddress() + ":" + plc4xField.getPlcDataType()));
plc4xFields.add(plc4xFieldRequest);
}
final int requestId = txIdGenerator.getAndIncrement();
Plc4xReadRequest plc4xReadRequest = new Plc4xReadRequest(requestId, connectionId, plc4xFields);
// Send the request and await a response.
RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
context.sendRequest(plc4xReadRequest).expectResponse(Plc4xMessage.class, requestTimeout).onTimeout(future::completeExceptionally).check(plc4xMessage -> plc4xMessage.getRequestId() == requestId).unwrap(plc4xMessage -> (Plc4xReadResponse) plc4xMessage).check(plc4xReadResponse -> plc4xReadResponse.getConnectionId() == connectionId).handle(plc4xReadResponse -> {
Map<String, ResponseItem<PlcValue>> apiResponses = new HashMap<>();
// Create the API response from the incoming message.
for (Plc4xFieldValueResponse plc4xField : plc4xReadResponse.getFields()) {
final Plc4xResponseCode plc4xResponseCode = plc4xField.getResponseCode();
final PlcResponseCode apiResponseCode = PlcResponseCode.valueOf(plc4xResponseCode.name());
apiResponses.put(plc4xField.getField().getName(), new ResponseItem<>(apiResponseCode, plc4xField.getValue()));
}
// Send it back to the calling process.
future.complete(new DefaultPlcReadResponse(apiReadRequest, apiResponses));
// Finish the request-transaction.
transaction.endRequest();
});
return future;
}
Aggregations