use of org.apache.plc4x.java.api.exceptions.PlcUnsupportedDataTypeException 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.api.exceptions.PlcUnsupportedDataTypeException in project plc4x by apache.
the class OpcuaField method of.
public static OpcuaField of(String address) {
Matcher matcher = ADDRESS_PATTERN.matcher(address);
if (!matcher.matches()) {
throw new PlcInvalidFieldException(address, ADDRESS_PATTERN, "{address}");
}
String identifier = matcher.group("identifier");
String identifierTypeString = matcher.group("identifierType");
OpcuaIdentifierType identifierType = OpcuaIdentifierType.enumForValue(identifierTypeString);
String namespaceString = matcher.group("namespace");
Integer namespace = namespaceString != null ? Integer.parseInt(namespaceString) : 0;
String dataTypeString = matcher.group("datatype") != null ? matcher.group("datatype").toUpperCase() : "NULL";
if (!EnumUtils.isValidEnum(OpcuaDataType.class, dataTypeString)) {
throw new PlcUnsupportedDataTypeException("Datatype " + dataTypeString + " is unsupported by this protocol");
}
OpcuaDataType dataType = OpcuaDataType.valueOf(dataTypeString);
return new OpcuaField(namespace, identifier, identifierType, dataType);
}
Aggregations