Search in sources :

Example 1 with AdsDataType

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);
    }
}
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 AdsDataType

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);
}
Also used : PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) SymbolicAdsField(org.apache.plc4x.java.ads.model.SymbolicAdsField) AdsDataType(org.apache.plc4x.java.ads.model.AdsDataType) PlcField(org.apache.plc4x.java.api.model.PlcField) DirectAdsField(org.apache.plc4x.java.ads.model.DirectAdsField) AmsPacket(org.apache.plc4x.java.ads.api.generic.AmsPacket) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) Invoke(org.apache.plc4x.java.ads.api.generic.types.Invoke)

Aggregations

AdsDataType (org.apache.plc4x.java.ads.model.AdsDataType)2 PlcProtocolException (org.apache.plc4x.java.api.exceptions.PlcProtocolException)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 Stream (java.util.stream.Stream)1 ArrayUtils (org.apache.commons.lang3.ArrayUtils)1 AmsPacket (org.apache.plc4x.java.ads.api.generic.AmsPacket)1 Invoke (org.apache.plc4x.java.ads.api.generic.types.Invoke)1 DirectAdsField (org.apache.plc4x.java.ads.model.DirectAdsField)1 SymbolicAdsField (org.apache.plc4x.java.ads.model.SymbolicAdsField)1 PlcProtocolPayloadTooBigException (org.apache.plc4x.java.api.exceptions.PlcProtocolPayloadTooBigException)1 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)1 PlcUnsupportedDataTypeException (org.apache.plc4x.java.api.exceptions.PlcUnsupportedDataTypeException)1 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)1 PlcField (org.apache.plc4x.java.api.model.PlcField)1