use of org.apache.plc4x.java.api.model.PlcField in project plc4x by apache.
the class SingleFieldOptimizer method processReadRequest.
@Override
protected List<PlcRequest> processReadRequest(PlcReadRequest readRequest, DriverContext driverContext) {
if (readRequest.getNumberOfFields() == 1) {
return Collections.singletonList(readRequest);
}
List<PlcRequest> subRequests = new ArrayList<>(readRequest.getNumberOfFields());
for (String fieldName : readRequest.getFieldNames()) {
PlcField field = readRequest.getField(fieldName);
PlcReadRequest subRequest = new DefaultPlcReadRequest(((DefaultPlcReadRequest) readRequest).getReader(), new LinkedHashMap<>(Collections.singletonMap(fieldName, field)));
subRequests.add(subRequest);
}
return subRequests;
}
use of org.apache.plc4x.java.api.model.PlcField in project plc4x by apache.
the class SingleFieldOptimizer method processWriteRequest.
@Override
protected List<PlcRequest> processWriteRequest(PlcWriteRequest writeRequest, DriverContext driverContext) {
if (writeRequest.getNumberOfFields() == 1) {
return Collections.singletonList(writeRequest);
}
List<PlcRequest> subRequests = new ArrayList<>(writeRequest.getNumberOfFields());
for (String fieldName : writeRequest.getFieldNames()) {
PlcField field = writeRequest.getField(fieldName);
PlcValue value = writeRequest.getPlcValue(fieldName);
PlcWriteRequest subRequest = new DefaultPlcWriteRequest(((DefaultPlcWriteRequest) writeRequest).getWriter(), new LinkedHashMap<>(Collections.singletonMap(fieldName, new FieldValueItem(field, value))));
subRequests.add(subRequest);
}
return subRequests;
}
use of org.apache.plc4x.java.api.model.PlcField in project plc4x by apache.
the class Plc4x2AdsProtocol method encodeWriteRequest.
private void encodeWriteRequest(PlcRequestContainer<InternalPlcRequest, InternalPlcResponse> msg, List<Object> out) throws PlcException {
InternalPlcWriteRequest writeRequest = (InternalPlcWriteRequest) msg.getRequest();
if (writeRequest.getFields().size() != 1) {
throw new PlcProtocolException("Only one item supported");
}
PlcField field = writeRequest.getFields().get(0);
if (field instanceof SymbolicAdsField) {
DirectAdsField mappedField = fieldMapping.get(field);
LOGGER.debug("Replacing {} with {}", field, mappedField);
field = mappedField;
}
if (!(field instanceof DirectAdsField)) {
throw new PlcProtocolException("PlcField not of type DirectAdsField: " + field.getClass());
}
DirectAdsField directAdsField = (DirectAdsField) field;
Invoke invokeId = Invoke.of(correlationBuilder.incrementAndGet());
IndexGroup indexGroup = IndexGroup.of(directAdsField.getIndexGroup());
IndexOffset indexOffset = IndexOffset.of(directAdsField.getIndexOffset());
Object[] plcValues;
PlcValue plcValue = writeRequest.getPlcValues().get(0);
if (plcValue instanceof PlcList) {
plcValues = ((PlcList) plcValue).getList().toArray(new Object[0]);
} else {
plcValues = new Object[] { plcValue.getObject() };
}
byte[] bytes = encodeData(directAdsField.getAdsDataType(), plcValues);
int bytesToBeWritten = bytes.length;
int maxTheoreticalSize = directAdsField.getAdsDataType().getTargetByteSize() * directAdsField.getNumberOfElements();
if (bytesToBeWritten > maxTheoreticalSize) {
LOGGER.debug("Requested AdsDatatype {} is exceeded by number of bytes {}. Limit {}.", directAdsField.getAdsDataType(), bytesToBeWritten, maxTheoreticalSize);
throw new PlcProtocolPayloadTooBigException("ADS", maxTheoreticalSize, bytesToBeWritten, plcValues);
}
Data data = Data.of(bytes);
AmsPacket amsPacket = AdsWriteRequest.of(targetAmsNetId, targetAmsPort, sourceAmsNetId, sourceAmsPort, invokeId, indexGroup, indexOffset, data);
LOGGER.debug("encoded write request {}", amsPacket);
out.add(amsPacket);
requests.put(invokeId.getAsLong(), msg);
}
use of org.apache.plc4x.java.api.model.PlcField in project plc4x by apache.
the class CANOpenProtocolLogic method read.
public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
CompletableFuture<PlcReadResponse> response = new CompletableFuture<>();
if (readRequest.getFieldNames().size() != 1) {
response.completeExceptionally(new IllegalArgumentException("SDO requires single field to be read"));
return response;
}
PlcField field = readRequest.getFields().get(0);
if (!(field instanceof CANOpenField)) {
response.completeExceptionally(new IllegalArgumentException("Only CANOpenField instances are supported"));
return response;
}
if (!(field instanceof CANOpenSDOField)) {
response.completeExceptionally(new IllegalArgumentException("Only CANOpenSDOField instances are supported"));
return response;
}
;
readInternally(readRequest, (CANOpenSDOField) field, response);
return response;
}
use of org.apache.plc4x.java.api.model.PlcField in project plc4x by apache.
the class CANOpenProtocolLogic method write.
public CompletableFuture<PlcWriteResponse> write(PlcWriteRequest writeRequest) {
CompletableFuture<PlcWriteResponse> response = new CompletableFuture<>();
if (writeRequest.getFieldNames().size() != 1) {
response.completeExceptionally(new IllegalArgumentException("You can write only one field at the time"));
return response;
}
PlcField field = writeRequest.getFields().get(0);
if (!(field instanceof CANOpenField)) {
response.completeExceptionally(new IllegalArgumentException("Only CANOpenField instances are supported"));
return response;
}
if (field instanceof CANOpenSDOField) {
writeInternally((DefaultPlcWriteRequest) writeRequest, (CANOpenSDOField) field, response);
return response;
}
if (field instanceof CANOpenPDOField) {
writeInternally((DefaultPlcWriteRequest) writeRequest, (CANOpenPDOField) field, response);
return response;
}
response.completeExceptionally(new IllegalArgumentException("Only CANOpenSDOField instances are supported"));
return response;
}
Aggregations