use of org.apache.plc4x.java.can.generic.transport.GenericFrame in project plc4x by apache.
the class GenericCANProtocolLogic method write.
@Override
public CompletableFuture<PlcWriteResponse> write(PlcWriteRequest writeRequest) {
final RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
CompletableFuture<PlcWriteResponse> response = new CompletableFuture<>();
transaction.submit(() -> {
Map<Integer, WriteBufferByteBased> messages = new LinkedHashMap<>();
Map<Integer, Map<String, PlcResponseCode>> responses = new HashMap<>();
for (String field : writeRequest.getFieldNames()) {
PlcField plcField = writeRequest.getField(field);
if (!(plcField instanceof GenericCANField)) {
responses.computeIfAbsent(-1, (node) -> new HashMap<>()).put(field, PlcResponseCode.UNSUPPORTED);
continue;
}
GenericCANField canField = (GenericCANField) plcField;
WriteBuffer buffer = messages.computeIfAbsent(canField.getNodeId(), (node) -> new WriteBufferByteBased(8, ByteOrder.LITTLE_ENDIAN));
Map<String, PlcResponseCode> statusMap = responses.computeIfAbsent(canField.getNodeId(), (node) -> new HashMap<>());
PlcValue value = writeRequest.getPlcValue(field);
try {
write(buffer, canField, value);
statusMap.put(field, PlcResponseCode.OK);
} catch (SerializationException e) {
statusMap.put(field, PlcResponseCode.INVALID_DATA);
}
}
Map<String, PlcResponseCode> codes = new HashMap<>();
for (Map.Entry<Integer, WriteBufferByteBased> message : messages.entrySet()) {
boolean discarded = false;
for (Map.Entry<String, PlcResponseCode> entry : responses.get(message.getKey()).entrySet()) {
codes.put(entry.getKey(), entry.getValue());
if (!discarded && entry.getValue() != PlcResponseCode.OK) {
logger.info("Discarding writing of frame with field {}. Node {} will not be communicated.", entry.getKey(), message.getKey());
discarded = true;
}
}
if (!discarded) {
byte[] data = message.getValue().getData();
logger.debug("Writing message with id {} and {} bytes of data", message.getKey(), data.length);
context.sendToWire(new GenericFrame(message.getKey(), data));
}
}
response.complete(new DefaultPlcWriteResponse(writeRequest, codes));
transaction.endRequest();
});
return response;
}
Aggregations