Example #1
0
  public void receiveCommand(
      String itemName, Command command, BticinoBindingConfig itemBindingConfig) {
    try {
      synchronized (m_lock) {
        // An command is received from the openHab system
        // analyse it and execute it
        logger.debug(
            "Gateway [" + m_gateway_id + "], Command '{}' received for item {}",
            (Object[]) new String[] {command.toString(), itemName});

        ProtocolRead l_pr = new ProtocolRead(itemBindingConfig.who + "*" + itemBindingConfig.where);
        l_pr.addProperty("who", itemBindingConfig.who);
        l_pr.addProperty("address", itemBindingConfig.where);

        int l_who = Integer.parseInt(itemBindingConfig.who);
        switch (l_who) {
            // Lights
          case 1:
            {
              if (OnOffType.ON.equals(command)) l_pr.addProperty("what", "1");
              else l_pr.addProperty("what", "0");
              break;
            }
            // Shutter
          case 2:
            {
              if (UpDownType.UP.equals(command)) l_pr.addProperty("what", "1");
              else if (UpDownType.DOWN.equals(command)) l_pr.addProperty("what", "2");
              else if (StopMoveType.STOP.equals(command)) l_pr.addProperty("what", "0");
              break;
            }
            // CEN Basic & Evolved
          case 15:
            {
              // Only for the on type, send a CEN event (aka a pushbutton
              // device)
              // the CEN can start a scenario on eg. a MH200N gateway
              // device
              if (OnOffType.ON.equals(command)) l_pr.addProperty("what", itemBindingConfig.what);
              break;
            }
        }

        m_open_web_net.onCommand(l_pr);
      }
    } catch (Exception e) {
      logger.error(
          "Gateway [" + m_gateway_id + "], Error processing receiveCommand '{}'",
          (Object[]) new String[] {e.getMessage()});
    }
  }
Example #2
0
 /**
  * Push a value into all openHAB Items that match a given MiOS Property name (from the Item
  * Binding declaration).
  *
  * <p>In the process, this routine will perform Datatype conversions from Java types to openHAB's
  * type system. These conversions are as follows:
  *
  * <p>
  *
  * <ul>
  *   <li>{@code String} -> {@code StringType}
  *   <li>{@code Integer} -> {@code DecimalType}
  *   <li>{@code Double} -> {@code DecimalType}
  *   <li>{@code Boolean} -> {@code StringType} (true == ON, false == OFF)
  *   <li>{@code Calendar} -> {@code DateTimeType}
  * </ul>
  *
  * @param property the MiOS Property name
  * @param value the value to push, per the supported types.
  * @exception IllegalArgumentException thrown if the value isn't one of the supported types.
  */
 public void postPropertyUpdate(String property, Object value, boolean incremental)
     throws Exception {
   if (value instanceof String) {
     internalPropertyUpdate(
         property, new StringType(value == null ? "" : (String) value), incremental);
   } else if (value instanceof Integer) {
     internalPropertyUpdate(property, new DecimalType((Integer) value), incremental);
   } else if (value instanceof Calendar) {
     internalPropertyUpdate(property, new DateTimeType((Calendar) value), incremental);
   } else if (value instanceof Double) {
     internalPropertyUpdate(property, new DecimalType((Double) value), incremental);
   } else if (value instanceof Boolean) {
     postPropertyUpdate(
         property,
         ((Boolean) value).booleanValue() ? OnOffType.ON.toString() : OnOffType.OFF.toString(),
         incremental);
   } else {
     throw new IllegalArgumentException(
         String.format(
             "Unexpected Datatype, property=%s datatype=%s",
             property, value.getClass().toString()));
   }
 }
Example #3
0
  /** {@inheritDoc} */
  @Override
  public void internalReceiveCommand(String itemName, Command command) {
    logger.debug("Received command from {}", itemName);

    // resolve serial number for item
    String serialNumber = null;

    for (MaxCubeBindingProvider provider : providers) {
      serialNumber = provider.getSerialNumber(itemName);

      if (StringUtils.isBlank(serialNumber)) {
        continue;
      }

      // send command to MAX!Cube LAN Gateway
      Device device = findDevice(serialNumber, devices);

      if (device == null) {
        logger.debug(
            "Cannot send command to device with serial number {}, device not listed.",
            serialNumber);
        continue;
      }

      String rfAddress = device.getRFAddress();
      String commandString = null;

      if (command instanceof DecimalType || command instanceof OnOffType) {
        DecimalType decimalType = DEFAULT_OFF_TEMPERATURE;
        if (command instanceof DecimalType) {
          decimalType = (DecimalType) command;
        } else if (command instanceof OnOffType) {
          decimalType =
              OnOffType.ON.equals(command) ? DEFAULT_ON_TEMPERATURE : DEFAULT_OFF_TEMPERATURE;
        }

        S_Command cmd =
            new S_Command(
                rfAddress,
                device.getRoomId(),
                ((HeatingThermostat) device).getMode(),
                decimalType.doubleValue());
        commandString = cmd.getCommandString();
      } else if (command instanceof StringType) {
        String commandContent = command.toString().trim().toUpperCase();
        S_Command cmd = null;
        ThermostatModeType commandThermoType = null;
        if (commandContent.contentEquals(ThermostatModeType.AUTOMATIC.toString())) {
          commandThermoType = ThermostatModeType.AUTOMATIC;
          cmd = new S_Command(rfAddress, device.getRoomId(), commandThermoType);
        } else if (commandContent.contentEquals(ThermostatModeType.BOOST.toString())) {
          commandThermoType = ThermostatModeType.BOOST;
          Double setTemp =
              Double.parseDouble(((HeatingThermostat) device).getTemperatureSetpoint().toString());
          cmd = new S_Command(rfAddress, device.getRoomId(), commandThermoType, setTemp);
        } else if (commandContent.contentEquals(ThermostatModeType.MANUAL.toString())) {
          commandThermoType = ThermostatModeType.MANUAL;
          Double setTemp =
              Double.parseDouble(((HeatingThermostat) device).getTemperatureSetpoint().toString());
          cmd = new S_Command(rfAddress, device.getRoomId(), commandThermoType, setTemp);
          logger.debug("updates to MANUAL mode with temperature '{}'", setTemp);
        } else {
          logger.debug(
              "Only updates to AUTOMATIC, MANUAL & BOOST supported, received value ;'{}'",
              commandContent);
          continue;
        }
        commandString = cmd.getCommandString();
      }

      if (commandString != null) {

        try {
          if (socket == null) {
            this.socketConnect();
          }
          writer.write(commandString);
          logger.debug(commandString);
          writer.flush();

          Message message = null;
          String raw = reader.readLine();
          try {
            while (!this.messageProcessor.isMessageAvailable()) {
              this.messageProcessor.addReceivedLine(raw);
              raw = reader.readLine();
            }

            message = this.messageProcessor.pull();
          } catch (Exception e) {
            logger.info("Error while handling response from MAX! Cube lan gateway!");
            logger.debug(Utils.getStackTrace(e));
            this.messageProcessor.reset();
          }

          if (message != null) {
            if (message.getType() == MessageType.S) {
              sMessageProcessing((S_Message) message);
            }
          }
          if (!exclusive) {
            socket.close();
            socket = null;
          }
        } catch (UnknownHostException e) {
          logger.info(
              "Host error occurred while connecting to MAX! Cube lan gateway '{}': {}",
              ip,
              e.getMessage());
          socketClose();
        } catch (IOException e) {
          logger.info(
              "IO error occurred while writing to MAX! Cube lan gateway '{}': {}",
              ip,
              e.getMessage());
          socketClose(); // reconnect on next execution
        } catch (Exception e) {
          logger.info(
              "Error occurred while writing to MAX! Cube lan gateway '{}': {}", ip, e.getMessage());
          logger.info(Utils.getStackTrace(e));
          socketClose(); // reconnect on next execution
        }
        logger.debug("Command Sent to {}", ip);
      } else {
        logger.debug("Null Command not sent to {}", ip);
      }
    }
  }