/** {@inheritDoc} */
 @Override
 public void receiveUpdate(String itemName, State newStatus) {
   if (itemRegistry != null) {
     try {
       GenericItem item = (GenericItem) itemRegistry.getItem(itemName);
       boolean isAccepted = false;
       if (item.getAcceptedDataTypes().contains(newStatus.getClass())) {
         isAccepted = true;
       } else {
         // Look for class hierarchy
         for (Class<? extends State> state : item.getAcceptedDataTypes()) {
           try {
             if (!state.isEnum()
                 && state.newInstance().getClass().isAssignableFrom(newStatus.getClass())) {
               isAccepted = true;
               break;
             }
           } catch (InstantiationException e) {
             logger.warn("InstantiationException on {}", e.getMessage()); // Should never happen
           } catch (IllegalAccessException e) {
             logger.warn("IllegalAccessException on {}", e.getMessage()); // Should never happen
           }
         }
       }
       if (isAccepted) {
         item.setState(newStatus);
       } else {
         logger.debug(
             "Received update of a not accepted type ("
                 + newStatus.getClass().getSimpleName()
                 + ") for item "
                 + itemName);
       }
     } catch (ItemNotFoundException e) {
       logger.debug("Received update for non-existing item: {}", e.getMessage());
     }
   }
 }
Exemple #2
0
 /** {@inheritDoc} */
 @Override
 public void setState(State state) {
   // we map ON/OFF values to the percent values 0 and 100
   if (state == OnOffType.OFF) {
     super.setState(PercentType.ZERO);
   } else if (state == OnOffType.ON) {
     super.setState(PercentType.HUNDRED);
   } else if (state.getClass() == DecimalType.class) {
     super.setState(
         new PercentType(((DecimalType) state).toBigDecimal().multiply(new BigDecimal(100))));
   } else {
     super.setState(state);
   }
 }