Example #1
0
  /**
   * Handles the given {@link ProcessEvent}. After finding the corresponding Item (by iterating
   * through all known group addresses) this Item is updated. Each item is added to a special list
   * to identify and avoid echo's in the <code>receiveUpdate</code> and <code>receiveCommand</code>
   * methods.
   *
   * @param e the {@link ProcessEvent} to handle.
   */
  private void readFromKNX(ProcessEvent e) {
    try {
      GroupAddress destination = e.getDestination();
      byte[] asdu = e.getASDU();
      if (asdu.length == 0) {
        return;
      }
      String[] itemList = getItemNames(destination);
      if (itemList.length == 0) {
        logger.debug("Received telegram for unknown group address {}", destination.toString());
      }
      for (String itemName : itemList) {
        Iterable<Datapoint> datapoints = getDatapoints(itemName, destination);
        if (datapoints != null) {
          for (Datapoint datapoint : datapoints) {
            Type type = getType(datapoint, asdu);
            if (type != null) {
              // we need to make sure that we won't send out this event to
              // the knx bus again, when receiving it on the openHAB bus
              ignoreEventList.add(itemName + type.toString());
              logger.trace(
                  "Added event (item='{}', type='{}') to the ignore event list",
                  itemName,
                  type.toString());

              if (type instanceof Command && isCommandGA(destination)) {
                eventPublisher.postCommand(itemName, (Command) type);
              } else if (type instanceof State) {
                eventPublisher.postUpdate(itemName, (State) type);
              } else {
                throw new IllegalClassException(
                    "Cannot process datapoint of type " + type.toString());
              }

              logger.trace(
                  "Processed event (item='{}', type='{}', destination='{}')",
                  itemName,
                  type.toString(),
                  destination.toString());
            } else {
              final char[] hexCode = "0123456789ABCDEF".toCharArray();
              StringBuilder sb = new StringBuilder(2 + asdu.length * 2);
              sb.append("0x");
              for (byte b : asdu) {
                sb.append(hexCode[(b >> 4) & 0xF]);
                sb.append(hexCode[(b & 0xF)]);
              }

              logger.debug(
                  "Ignoring KNX bus data: couldn't transform to an openHAB type (not supported). Destination='{}', datapoint='{}', data='{}'",
                  new Object[] {destination.toString(), datapoint.toString(), sb.toString()});
            }
          }
        }
      }
    } catch (RuntimeException re) {
      logger.error("Error while receiving event from KNX bus: " + re.toString());
    }
  }