Exemple #1
0
  public Properties pair(String applicationName) throws CommunicationException {
    HueMethod method = new HueMethod(this);
    HashMap<String, Object> auth = new HashMap<String, Object>();

    auth.put("username", generateKey());
    auth.put("devicetype", applicationName);
    try {
      JSONObject result = method.post("", new JSONObject(auth));
      Properties properties = new Properties();

      properties.put("accessKey", accessKey);
      if (result != null && result.has("success")) {
        result = result.getJSONObject("success");
        if (result.has("username")) {
          accessKey = result.getString("username");
          endpoint = "http://" + ipAddress + "/api/" + accessKey + "/";
          properties.put("accessKey", accessKey);
          return properties;
        }
      }
      throw new HueException("Failed to receive authentication key");
    } catch (JSONException e) {
      throw new HueException(e);
    }
  }
Exemple #2
0
  @Override
  public @Nonnull Iterable<Lightbulb> listBulbs() throws CommunicationException {
    HueMethod method = new HueMethod(this);

    JSONObject list = method.get("lights");

    if (list == null) {
      return Collections.emptyList();
    }
    ArrayList<Lightbulb> matches = new ArrayList<Lightbulb>();

    for (String id : JSONObject.getNames(list)) {
      try {
        JSONObject item = list.getJSONObject(id);
        String name = (item.has("name") ? item.getString("name") : id);

        matches.add(new HueBulb(this, id, name));
      } catch (JSONException e) {
        throw new HueException(e);
      }
    }
    return matches;
  }