use of org.apache.plc4x.java.ads.model.AdsDataType in project plc4x by apache.
the class LittleEndianEncoder method encodeData.
public static byte[] encodeData(AdsDataType adsDataType, Object... values) throws PlcProtocolException {
if (values.length == 0) {
return new byte[] {};
}
Class<?> valueType = values[0].getClass();
Stream<byte[]> result;
if (valueType == Boolean.class) {
result = encodeBoolean(adsDataType, Arrays.stream(values).map(Boolean.class::cast));
} else if (valueType == Byte.class) {
result = encodeByte(adsDataType, Arrays.stream(values).map(Byte.class::cast));
} else if (valueType == Short.class) {
result = encodeShort(adsDataType, Arrays.stream(values).map(Short.class::cast));
} else if (valueType == Integer.class) {
result = encodeInteger(adsDataType, Arrays.stream(values).map(Integer.class::cast));
} else if (valueType == Long.class) {
result = encodeLong(adsDataType, Arrays.stream(values).map(Long.class::cast));
} else if (valueType == BigInteger.class) {
result = encodeBigInteger(adsDataType, Arrays.stream(values).map(BigInteger.class::cast));
} else if (valueType == LocalTime.class) {
result = encodeLocalTime(adsDataType, Arrays.stream(values).map(LocalTime.class::cast));
} else if (valueType == LocalDate.class) {
result = encodeLocalDate(adsDataType, Arrays.stream(values).map(LocalDate.class::cast));
} else if (valueType == LocalDateTime.class) {
result = encodeLocalDateTime(adsDataType, Arrays.stream(values).map(LocalDateTime.class::cast));
} else if (valueType == Float.class) {
result = encodeFloat(adsDataType, Arrays.stream(values).map(Float.class::cast));
} else if (valueType == Double.class) {
result = encodeDouble(adsDataType, Arrays.stream(values).map(Double.class::cast));
} else if (valueType == String.class) {
result = encodeString(adsDataType, Arrays.stream(values).map(String.class::cast));
} else if (valueType == byte[].class) {
result = encodeByteArray(adsDataType, Arrays.stream(values).map(byte[].class::cast));
} else if (valueType == Byte[].class) {
result = encodeBigByteArray(adsDataType, Arrays.stream(values).map(Byte[].class::cast));
} else {
throw new PlcUnsupportedDataTypeException(valueType);
}
// TODO: maybe we can replace this by a smarter flatmap
try {
return result.collect(ByteArrayOutputStream::new, (bos, byteValue) -> {
try {
bos.write(byteValue);
} catch (IOException e) {
throw new PlcRuntimeException(e);
}
}, (a, b) -> {
}).toByteArray();
} catch (PlcRuntimeException e) {
throw new PlcProtocolException("Error encoding data", e);
}
}
use of org.apache.plc4x.java.ads.model.AdsDataType in project plc4x by apache.
the class Plc4x2AdsProtocol method encodeReadRequest.
private void encodeReadRequest(PlcRequestContainer<InternalPlcRequest, InternalPlcResponse> msg, List<Object> out) throws PlcException {
PlcReadRequest readRequest = (PlcReadRequest) msg.getRequest();
if (readRequest.getFields().size() != 1) {
throw new PlcProtocolException("Only one item supported");
}
PlcField field = readRequest.getFields().get(0);
if (field instanceof SymbolicAdsField) {
DirectAdsField mappedField = fieldMapping.get(field);
if (mappedField == null) {
throw new PlcProtocolException("No field mapping for " + 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());
AdsDataType adsDataType = directAdsField.getAdsDataType();
int numberOfElements = directAdsField.getNumberOfElements();
int readLength = adsDataType.getTargetByteSize() * numberOfElements;
Length length = Length.of(readLength);
AmsPacket amsPacket = AdsReadRequest.of(targetAmsNetId, targetAmsPort, sourceAmsNetId, sourceAmsPort, invokeId, indexGroup, indexOffset, length);
LOGGER.debug("encoded read request {}", amsPacket);
out.add(amsPacket);
requests.put(invokeId.getAsLong(), msg);
}
Aggregations