Пример #1
0
  /** @{inheritDoc} */
  @Override
  public void updated(Dictionary<String, ?> config) throws ConfigurationException {
    if (config != null) {
      // to override the mode (gpio or http) one has to add a
      // parameter to openhab.cfg like openSprinkler:mode=[gpio|http]
      String modeString = (String) config.get("mode");
      if (StringUtils.isNotBlank(modeString)) {
        try {
          mode = OpenSprinklerMode.valueOf(modeString.toUpperCase());
        } catch (IllegalArgumentException iae) {
          throw new ConfigurationException(
              "openSprinkler:mode",
              "Unknown OpenSprinkler mode "
                  + mode
                  + "! Valid modes are 'gpio' and 'http'. Please fix your openhab.cfg.");
        }
      }

      // to specify the http url one has to add a
      // parameter to openhab.cfg like openSprinkler:httpUrl=<url>
      url = (String) config.get("httpUrl");

      // to specify the http password one has to add a
      // parameter to openhab.cfg like openSprinkler:httpPassword=<password>
      password = (String) config.get("httpPassword");

      // to override the refresh rate of the rain sensor check one has to add a
      // parameter to openhab.cfg like openSprinkler:rsRefresh
      String rsRefreshRate = (String) config.get("refreshInterval");
      if (StringUtils.isNotBlank(rsRefreshRate)) {
        refreshInterval = Long.parseLong(rsRefreshRate);
      }

      // to override the number of stations one has to add a
      // parameter to openhab.cfg like openSprinkler:numberOfStations=<count>
      String numberOfStationsString = (String) config.get("numberOfStations");
      if (StringUtils.isNotBlank(numberOfStationsString)) {
        numberOfStations = Integer.parseInt(numberOfStationsString);
        openSprinkler.setNumberOfStations(numberOfStations);
      }

      // read further config parameters here ...

      setProperlyConfigured(true);

      // then update the binding
      updateBinding();
    }
  }
Пример #2
0
  private void updateBinding() {
    if (openSprinkler != null) {
      openSprinkler.closeConnection();
    }

    if (mode == null) {
      return;
    }

    switch (mode) {
      case GPIO:
        openSprinkler = OpenSprinklerFactory.newGpioConnection();
        break;
      case HTTP:
        openSprinkler = OpenSprinklerFactory.newHttpConnection(url, password);
        break;
      default:
        throw new IllegalStateException(
            "Unknown OpenSprinkler mode: "
                + mode
                + "! Since it is checked while initialization already, this Exception should never be thrown!");
    }

    openSprinkler.setNumberOfStations(numberOfStations);

    logger.info(
        "OpenSprinkler binding running in "
            + mode
            + " mode"
            + (HTTP.equals(mode) ? " with url " + url : "")
            + ". Running with "
            + numberOfStations
            + " stations enabled and a refresh rate of "
            + refreshInterval
            + ".");
  }