Example #1
0
 private void writeToKNX(String itemName, Type value) {
   Iterable<Datapoint> datapoints = getDatapoints(itemName, value.getClass());
   if (datapoints != null) {
     ProcessCommunicator pc = KNXConnection.getCommunicator();
     if (pc != null) {
       for (Datapoint datapoint : datapoints) {
         try {
           pc.write(datapoint, toDPTValue(value, datapoint.getDPT()));
           logger.debug("Wrote value '{}' to datapoint '{}'", value, datapoint);
         } catch (KNXException e) {
           logger.warn(
               "Value '{}' could not be sent to the KNX bus using datapoint '{}' - retrying one time: {}",
               new Object[] {value, datapoint, e.getMessage()});
           try {
             // do a second try, maybe the reconnection was successful
             pc = KNXConnection.getCommunicator();
             pc.write(datapoint, toDPTValue(value, datapoint.getDPT()));
             logger.debug("Wrote value '{}' to datapoint '{}' on second try", value, datapoint);
           } catch (KNXException e1) {
             logger.error(
                 "Value '{}' could not be sent to the KNX bus using datapoint '{}' - giving up after second try: {}",
                 new Object[] {value, datapoint, e1.getMessage()});
           }
         }
       }
     }
   }
 }
Example #2
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());
    }
  }
Example #3
0
 /* (non-Javadoc)
  * @see org.openhab.core.binding.AbstractBinding#bindingChanged(org.openhab.core.binding.BindingProvider, java.lang.String)
  */
 @Override
 public void bindingChanged(BindingProvider provider, String itemName) {
   logger.trace("bindingChanged() for item {} msg received.", itemName);
   if (mKNXConnectionEstablished) {
     if (provider instanceof KNXBindingProvider) {
       KNXBindingProvider knxProvider = (KNXBindingProvider) provider;
       for (Datapoint datapoint : knxProvider.getReadableDatapoints()) {
         if (datapoint.getName().equals(itemName)) {
           logger.debug("Initializing read of item {}.", itemName);
           if (!mKNXBusReaderScheduler.scheduleRead(
               datapoint, knxProvider.getAutoRefreshTime(datapoint))) {
             logger.warn(
                 "Couldn't add to KNX bus reader scheduler (bindingChanged, datapoint='{}')",
                 datapoint);
           }
           break;
         }
       }
     }
   }
 }
Example #4
0
 /** Test method for {@link tuwien.auto.calimero.datapoint.Datapoint#getPriority()}. */
 public final void testGetPriority() {
   final Datapoint dp = new StateDP(ga, "name1");
   assertEquals(Priority.LOW, dp.getPriority());
   dp.setPriority(Priority.SYSTEM);
   assertEquals(Priority.SYSTEM, dp.getPriority());
 }
Example #5
0
 /** Test method for {@link tuwien.auto.calimero.datapoint.Datapoint#setName(java.lang.String)}. */
 public final void testSetName() {
   final Datapoint dp = new StateDP(ga, "name1");
   assertEquals("name1", dp.getName());
   dp.setName("changedName");
   assertEquals("changedName", dp.getName());
 }