Search in sources :

Example 1 with PlcUnsupportedDataTypeException

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);
    }
}
Also used : Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PlcUnsupportedDataTypeException(org.apache.plc4x.java.api.exceptions.PlcUnsupportedDataTypeException) PlcProtocolPayloadTooBigException(org.apache.plc4x.java.api.exceptions.PlcProtocolPayloadTooBigException) ArrayUtils(org.apache.commons.lang3.ArrayUtils) IOException(java.io.IOException) java.time(java.time) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) ChronoUnit(java.time.temporal.ChronoUnit) Stream(java.util.stream.Stream) Charset(java.nio.charset.Charset) BigInteger(java.math.BigInteger) AdsDataType(org.apache.plc4x.java.ads.model.AdsDataType) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) IOException(java.io.IOException) BigInteger(java.math.BigInteger) PlcUnsupportedDataTypeException(org.apache.plc4x.java.api.exceptions.PlcUnsupportedDataTypeException) BigInteger(java.math.BigInteger)

Example 2 with PlcUnsupportedDataTypeException

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);
}
Also used : Matcher(java.util.regex.Matcher) OpcuaIdentifierType(org.apache.plc4x.java.opcua.readwrite.OpcuaIdentifierType) OpcuaDataType(org.apache.plc4x.java.opcua.readwrite.OpcuaDataType) PlcInvalidFieldException(org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException) PlcUnsupportedDataTypeException(org.apache.plc4x.java.api.exceptions.PlcUnsupportedDataTypeException)

Aggregations

PlcUnsupportedDataTypeException (org.apache.plc4x.java.api.exceptions.PlcUnsupportedDataTypeException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 Charset (java.nio.charset.Charset)1 java.time (java.time)1 ChronoUnit (java.time.temporal.ChronoUnit)1 Arrays (java.util.Arrays)1 Matcher (java.util.regex.Matcher)1 Stream (java.util.stream.Stream)1 ArrayUtils (org.apache.commons.lang3.ArrayUtils)1 AdsDataType (org.apache.plc4x.java.ads.model.AdsDataType)1 PlcInvalidFieldException (org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException)1 PlcProtocolException (org.apache.plc4x.java.api.exceptions.PlcProtocolException)1 PlcProtocolPayloadTooBigException (org.apache.plc4x.java.api.exceptions.PlcProtocolPayloadTooBigException)1 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)1 OpcuaDataType (org.apache.plc4x.java.opcua.readwrite.OpcuaDataType)1 OpcuaIdentifierType (org.apache.plc4x.java.opcua.readwrite.OpcuaIdentifierType)1