/** {@inheritDoc} */
  @Override
  public void handleCommand(ChannelUID channelUID, Command command) {
    MaxCubeBridgeHandler maxCubeBridge = getMaxCubeBridgeHandler();
    if (maxCubeBridge == null) {
      logger.warn(
          "MAX! Cube LAN gateway bridge handler not found. Cannot handle command without bridge.");
      return;
    }
    if (command instanceof RefreshType) {
      forceRefresh = true;
      maxCubeBridge.handleCommand(channelUID, command);
      return;
    }
    if (maxDeviceSerial == null) {
      logger.warn("Serial number missing. Can't send command to device '{}'", getThing());
      return;
    }

    if (channelUID.getId().equals(CHANNEL_SETTEMP) || channelUID.getId().equals(CHANNEL_MODE)) {
      if (refreshingActuals) {
        refreshActualsRestore();
      }
      SendCommand sendCommand = new SendCommand(maxDeviceSerial, channelUID, command);
      maxCubeBridge.queueCommand(sendCommand);
    } else {
      logger.warn("Setting of channel {} not possible. Read-only", channelUID);
    }
  }
 /** sends the T command to the Cube to disassociate the device from the MAX! Cube. */
 private void deviceDelete() {
   MaxCubeBridgeHandler maxCubeBridge = getMaxCubeBridgeHandler();
   if (maxCubeBridge != null) {
     maxCubeBridge.sendDeviceDelete(maxDeviceSerial);
     dispose();
   }
 }
 private void sendPropertyUpdate(
     Map<String, Object> configurationParameters, HashMap<String, Object> deviceProperties) {
   try {
     Device device = getMaxCubeBridgeHandler().getDevice(maxDeviceSerial);
     rfAddress = device.getRFAddress();
     int roomId = device.getRoomId();
     BigDecimal tempComfort =
         (BigDecimal)
             configurationParameters.getOrDefault(
                 PROPERTY_THERMO_COMFORT_TEMP, deviceProperties.get(PROPERTY_THERMO_COMFORT_TEMP));
     BigDecimal tempEco =
         (BigDecimal)
             configurationParameters.getOrDefault(
                 PROPERTY_THERMO_ECO_TEMP, deviceProperties.get(PROPERTY_THERMO_ECO_TEMP));
     BigDecimal tempSetpointMax =
         (BigDecimal)
             configurationParameters.getOrDefault(
                 PROPERTY_THERMO_MAX_TEMP_SETPOINT,
                 deviceProperties.get(PROPERTY_THERMO_MAX_TEMP_SETPOINT));
     BigDecimal tempSetpointMin =
         (BigDecimal)
             configurationParameters.getOrDefault(
                 PROPERTY_THERMO_MIN_TEMP_SETPOINT,
                 deviceProperties.get(PROPERTY_THERMO_MIN_TEMP_SETPOINT));
     BigDecimal tempOffset =
         (BigDecimal)
             configurationParameters.getOrDefault(
                 PROPERTY_THERMO_OFFSET_TEMP, deviceProperties.get(PROPERTY_THERMO_OFFSET_TEMP));
     BigDecimal tempOpenWindow =
         (BigDecimal)
             configurationParameters.getOrDefault(
                 PROPERTY_THERMO_WINDOW_OPEN_TEMP,
                 deviceProperties.get(PROPERTY_THERMO_WINDOW_OPEN_TEMP));
     BigDecimal durationOpenWindow =
         (BigDecimal)
             configurationParameters.getOrDefault(
                 PROPERTY_THERMO_WINDOW_OPEN_DURATION,
                 deviceProperties.get(PROPERTY_THERMO_WINDOW_OPEN_DURATION));
     S_ConfigCommand cmd =
         new S_ConfigCommand(
             rfAddress,
             roomId,
             tempComfort.doubleValue(),
             tempEco.doubleValue(),
             tempSetpointMax.doubleValue(),
             tempSetpointMin.doubleValue(),
             tempOffset.doubleValue(),
             tempOpenWindow.doubleValue(),
             durationOpenWindow.intValue());
     bridgeHandler.queueCommand(
         new SendCommand(maxDeviceSerial, cmd, "Update Thermostat Properties"));
     sendCCommand();
   } catch (NullPointerException e) {
     logger.warn(
         "MAX! Cube LAN gateway bridge handler not found. Cannot handle update without bridge.");
   } catch (Exception e) {
     logger.debug("Exception occurred during execution: {}", e.getMessage(), e);
   }
 }
 @Override
 public void onDeviceRemoved(MaxCubeBridgeHandler bridge, Device device) {
   if (device.getSerialNumber().equals(maxDeviceSerial)) {
     bridgeHandler.unregisterDeviceStatusListener(this);
     bridgeHandler = null;
     forceRefresh = true;
     updateStatus(ThingStatus.OFFLINE);
   }
 }
 /*
  * (non-Javadoc)
  *
  * @see org.eclipse.smarthome.core.thing.binding.BaseThingHandler#dispose()
  */
 @Override
 public void dispose() {
   logger.debug("Disposing MAX! device {} {}.", getThing().getUID(), maxDeviceSerial);
   if (refreshingActuals) {
     refreshActualsRestore();
   }
   if (refreshActualsJob != null && !refreshActualsJob.isCancelled()) {
     refreshActualsJob.cancel(true);
     refreshActualsJob = null;
   }
   if (bridgeHandler != null) {
     logger.trace("Clear MAX! device {} {} from bridge.", getThing().getUID(), maxDeviceSerial);
     bridgeHandler.clearDeviceList();
     bridgeHandler.unregisterDeviceStatusListener(this);
     bridgeHandler = null;
   }
   updateStatus(ThingStatus.OFFLINE);
   logger.debug("Disposed MAX! device {} {}.", getThing().getUID(), maxDeviceSerial);
   super.dispose();
 }
 /*
  * (non-Javadoc)
  *
  * @see org.eclipse.smarthome.core.thing.binding.BaseThingHandler#
  * bridgeHandlerInitialized
  * (org.eclipse.smarthome.core.thing.binding.ThingHandler,
  * org.eclipse.smarthome.core.thing.Bridge)
  */
 @Override
 public void bridgeHandlerInitialized(ThingHandler thingHandler, Bridge bridge) {
   logger.debug(
       "Bridge {} initialized for device: {}",
       bridge.getUID().toString(),
       getThing().getUID().toString());
   if (bridgeHandler != null) {
     bridgeHandler.unregisterDeviceStatusListener(this);
     bridgeHandler = null;
   }
   this.bridgeHandler = (MaxCubeBridgeHandler) thingHandler;
   this.bridgeHandler.registerDeviceStatusListener(this);
   forceRefresh = true;
   super.bridgeHandlerInitialized(thingHandler, bridge);
 }
  /**
   * Updates the device & roomname
   *
   * @param configurationParameter
   */
  private void updateDeviceName(Entry<String, Object> configurationParameter) {
    try {
      Device device = getMaxCubeBridgeHandler().getDevice(maxDeviceSerial);
      String name = configurationParameter.getValue().toString();
      if (configurationParameter.getKey().equals(PROPERTY_DEVICENAME)
          && !(name.equals(device.getName()))) {
        logger.info("Updating device name for {} to {}", getThing().getUID().toString(), name);
        device.setName(name);
        bridgeHandler.sendDeviceAndRoomNameUpdate(name);
        SendCommand sendCommand = new SendCommand(maxDeviceSerial, new Q_Command(), "Reload Data");
        bridgeHandler.queueCommand(sendCommand);
      }
      if (configurationParameter.getKey().equals(PROPERTY_ROOMID)
          || configurationParameter.getKey().equals(PROPERTY_ROOMNAME)) {
        int roomId = ((BigDecimal) configurationParameter.getValue()).intValue();
        if (roomId != device.getRoomId()) {
          logger.info("Updating room for {} to {}", getThing().getUID().getAsString(), roomId);
          device.setRoomId(roomId);
          // TODO: handle if a room has no more devices, probably should be deleted. Also handle if
          // room rfId
          // is no longer valid as the related device is movd to another room
          bridgeHandler.sendDeviceAndRoomNameUpdate(name);
          SendCommand sendCommand =
              new SendCommand(
                  maxDeviceSerial,
                  Z_Command.wakeupDevice(device.getRFAddress()),
                  "WakeUp device" + getThing().getUID().getAsString());
          bridgeHandler.queueCommand(sendCommand);
          sendCommand =
              new SendCommand(
                  maxDeviceSerial,
                  new S_ConfigCommand(device.getRFAddress(), roomId, ConfigCommandType.SetRoom),
                  "Set Room");
          bridgeHandler.queueCommand(sendCommand);

          sendCommand = new SendCommand(maxDeviceSerial, new Q_Command(), "Reload Data");
          bridgeHandler.queueCommand(sendCommand);
          sendCCommand();
        }
      }
    } catch (NullPointerException e) {
      logger.warn(
          "MAX! Cube LAN gateway bridge handler not found. Cannot handle update without bridge.");
    } catch (Exception e) {
      logger.debug("Exception occurred during execution: {}", e.getMessage(), e);
    }
  }