Search in sources :

Example 1 with KnxGroupAddress

use of org.apache.plc4x.java.knxnetip.readwrite.KnxGroupAddress in project plc4x by apache.

the class KnxNetIpProtocolLogic method processCemiData.

protected void processCemiData(KnxAddress sourceAddress, byte[] destinationGroupAddress, byte firstByte, byte[] restBytes) throws ParseException {
    // The first byte is actually just 6 bit long, but we'll treat it as a full one.
    // So here we create a byte array containing the first and all the following bytes.
    byte[] payload = new byte[1 + restBytes.length];
    payload[0] = firstByte;
    System.arraycopy(restBytes, 0, payload, 1, restBytes.length);
    // Decode the group address depending on the project settings.
    ReadBuffer addressBuffer = new ReadBufferByteBased(destinationGroupAddress);
    final KnxGroupAddress knxGroupAddress = KnxGroupAddress.staticParse(addressBuffer, knxNetIpDriverContext.getGroupAddressType());
    final String destinationAddress = toString(knxGroupAddress);
    // If there is an ETS model provided, continue decoding the payload.
    if (knxNetIpDriverContext.getEtsModel() != null) {
        final EtsModel etsModel = knxNetIpDriverContext.getEtsModel();
        final GroupAddress groupAddress = etsModel.getGroupAddresses().get(destinationAddress);
        final String areaName = etsModel.getTopologyName(destinationAddress.substring(0, destinationAddress.indexOf('/')));
        final String lineName = etsModel.getTopologyName(destinationAddress.substring(0, destinationAddress.indexOf('/', destinationAddress.indexOf('/') + 1)));
        if ((groupAddress != null) && (groupAddress.getType() != null)) {
            LOGGER.trace(String.format("Message from: '%s' to: '%s'", toString(sourceAddress), destinationAddress));
            // Parse the payload depending on the type of the group-address.
            ReadBuffer rawDataReader = new ReadBufferByteBased(payload);
            final PlcValue value = KnxDatapoint.staticParse(rawDataReader, groupAddress.getType());
            // Assemble the plc4x return data-structure.
            Map<String, PlcValue> dataPointMap = new HashMap<>();
            dataPointMap.put("sourceAddress", new PlcSTRING(toString(sourceAddress)));
            dataPointMap.put("targetAddress", new PlcSTRING(groupAddress.getGroupAddress()));
            if (groupAddress.getFunction() != null) {
                dataPointMap.put("location", new PlcSTRING(groupAddress.getFunction().getSpaceName()));
                dataPointMap.put("function", new PlcSTRING(groupAddress.getFunction().getName()));
            } else {
                dataPointMap.put("location", null);
                dataPointMap.put("function", null);
            }
            if (areaName != null) {
                dataPointMap.put("area", new PlcSTRING(areaName));
            }
            if (lineName != null) {
                dataPointMap.put("line", new PlcSTRING(lineName));
            }
            dataPointMap.put("description", new PlcSTRING(groupAddress.getName()));
            dataPointMap.put("unitOfMeasurement", new PlcSTRING(groupAddress.getType().getName()));
            dataPointMap.put("value", value);
            final PlcStruct dataPoint = new PlcStruct(dataPointMap);
            // Send the data-structure.
            publishEvent(groupAddress, dataPoint);
        } else {
            LOGGER.warn(String.format("Message from: '%s' to unknown group address: '%s'%n payload: '%s'", toString(sourceAddress), destinationAddress, Hex.encodeHexString(payload)));
        }
    } else // Else just output the raw payload.
    {
        LOGGER.info(String.format("Raw Message: '%s' to: '%s'%n payload: '%s'", KnxNetIpProtocolLogic.toString(sourceAddress), destinationAddress, Hex.encodeHexString(payload)));
    }
}
Also used : KnxGroupAddress(org.apache.plc4x.java.knxnetip.readwrite.KnxGroupAddress) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EtsModel(org.apache.plc4x.java.knxnetip.ets.model.EtsModel) PlcStruct(org.apache.plc4x.java.spi.values.PlcStruct) GroupAddress(org.apache.plc4x.java.knxnetip.ets.model.GroupAddress) KnxGroupAddress(org.apache.plc4x.java.knxnetip.readwrite.KnxGroupAddress) PlcSTRING(org.apache.plc4x.java.spi.values.PlcSTRING)

Aggregations

ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 EtsModel (org.apache.plc4x.java.knxnetip.ets.model.EtsModel)1 GroupAddress (org.apache.plc4x.java.knxnetip.ets.model.GroupAddress)1 KnxGroupAddress (org.apache.plc4x.java.knxnetip.readwrite.KnxGroupAddress)1 PlcSTRING (org.apache.plc4x.java.spi.values.PlcSTRING)1 PlcStruct (org.apache.plc4x.java.spi.values.PlcStruct)1