From 8408d7350edda13dda5c0ba99824c5d102e38c40 Mon Sep 17 00:00:00 2001 From: Admin Date: Wed, 20 Apr 2016 16:44:02 -0500 Subject: [PATCH] More testing on HUE passthru. --- .../HABridge/api/hue/HueApiResponse.java | 31 ++++---- src/main/java/com/bwssystems/hue/HueInfo.java | 78 +++++++++++++++++-- .../resources/public/views/editdevice.html | 1 + src/main/resources/public/views/editor.html | 1 + .../public/views/harmonyactivity.html | 1 + .../resources/public/views/harmonydevice.html | 1 + src/main/resources/public/views/logs.html | 1 + .../resources/public/views/nestactions.html | 1 + src/main/resources/public/views/system.html | 1 + .../resources/public/views/veradevice.html | 1 + .../resources/public/views/verascene.html | 1 + 11 files changed, 96 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/bwssystems/HABridge/api/hue/HueApiResponse.java b/src/main/java/com/bwssystems/HABridge/api/hue/HueApiResponse.java index 6259d61..f9c9533 100644 --- a/src/main/java/com/bwssystems/HABridge/api/hue/HueApiResponse.java +++ b/src/main/java/com/bwssystems/HABridge/api/hue/HueApiResponse.java @@ -4,17 +4,18 @@ import java.util.HashMap; import java.util.Map; import com.bwssystems.HABridge.api.hue.DeviceResponse; +import com.google.gson.JsonObject; /** * Created by arm on 4/14/15. */ public class HueApiResponse { private Map lights; - private Map scenes; - private Map groups; - private Map schedules; - private Map sensors; - private Map rules; + private Map scenes; + private Map groups; + private Map schedules; + private Map sensors; + private Map rules; private HueConfig config; public HueApiResponse(String name, String ipaddress, String devicetype, String userid) { @@ -35,43 +36,43 @@ public class HueApiResponse { this.lights = lights; } - public Map getScenes() { + public Map getScenes() { return scenes; } - public void setScenes(Map scenes) { + public void setScenes(Map scenes) { this.scenes = scenes; } - public Map getGroups() { + public Map getGroups() { return groups; } - public void setGroups(Map groups) { + public void setGroups(Map groups) { this.groups = groups; } - public Map getSchedules() { + public Map getSchedules() { return schedules; } - public void setSchedules(Map schedules) { + public void setSchedules(Map schedules) { this.schedules = schedules; } - public Map getSensors() { + public Map getSensors() { return sensors; } - public void setSensors(Map sensors) { + public void setSensors(Map sensors) { this.sensors = sensors; } - public Map getRules() { + public Map getRules() { return rules; } - public void setRules(Map rules) { + public void setRules(Map rules) { this.rules = rules; } diff --git a/src/main/java/com/bwssystems/hue/HueInfo.java b/src/main/java/com/bwssystems/hue/HueInfo.java index 50f6d33..4ce8c9f 100644 --- a/src/main/java/com/bwssystems/hue/HueInfo.java +++ b/src/main/java/com/bwssystems/hue/HueInfo.java @@ -5,12 +5,17 @@ import java.nio.charset.Charset; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.bwssystems.HABridge.NamedIP; +import com.bwssystems.HABridge.api.SuccessUserResponse; +import com.bwssystems.HABridge.api.UserCreateRequest; import com.bwssystems.HABridge.api.hue.HueApiResponse; import com.google.gson.Gson; @@ -18,25 +23,61 @@ import com.google.gson.Gson; public class HueInfo { private static final Logger log = LoggerFactory.getLogger(HueInfo.class); private HttpClient httpClient; - private static final String HUE_REQUEST = "/api/habridge"; + private static final String HUE_REQUEST = "/api"; private NamedIP hueAddress; + private UserCreateRequest theLogin; + private String theUser; public HueInfo(NamedIP addressName) { super(); httpClient = HttpClients.createDefault(); hueAddress = addressName; + theLogin = new UserCreateRequest(); + theLogin.setDevicetype("HA Bridge"); + theLogin.setUsername("habridge"); + theUser = theLogin.getUsername(); } public HueApiResponse getHueApiResponse() { HueApiResponse theHueApiResponse = null; - String theUrl = "http://" + hueAddress.getIp() + HUE_REQUEST; + String theUrl = "http://" + hueAddress.getIp() + HUE_REQUEST + "/" + theLogin.getUsername(); String theData; - - theData = doHttpGETRequest(theUrl); - if(theData != null) { - theHueApiResponse = new Gson().fromJson(theData, HueApiResponse.class); - log.debug("GET HueApiResponse - name: " + theHueApiResponse.getConfig().getName() + ", mac addr: " + theHueApiResponse.getConfig().getMac()); + boolean loopControl = true; + int retryCount = 0; + while(loopControl) { + if(retryCount > 3) { + log.warn("Max Retry reached to get Hue data from " + hueAddress.getName()); + loopControl = false; + break; + } + theUrl = "http://" + hueAddress.getIp() + HUE_REQUEST + "/" + theUser; + theData = doHttpGETRequest(theUrl); + if(theData != null) { + log.debug("GET HueApiResponse - data: " + theData); + if(theData.contains("[{\"error\":")) { + if(theData.contains("unauthorized user")) { + if(!registerWithHue()) { + log.warn("Register to Hue for " + hueAddress.getName() + " - returned error."); + return null; + } + retryCount++; + } + else { + log.warn("GET HueApiResponse for " + hueAddress.getName() + " - returned error: " + theData); + return null; + } + } + else { + theHueApiResponse = new Gson().fromJson(theData, HueApiResponse.class); + log.debug("GET HueApiResponse for " + hueAddress.getName() + " - Gson parse - name: " + theHueApiResponse.getConfig().getName() + ", mac addr: " + theHueApiResponse.getConfig().getMac()); + loopControl = false; + } + } + else { + log.warn("GET HueApiResponse for " + hueAddress.getName() + " - returned null, no data."); + loopControl = false; + } } return theHueApiResponse; } @@ -59,6 +100,29 @@ public class HueInfo { return theContent; } + private boolean registerWithHue() { + boolean responseValue = false; + HttpPost postRequest = new HttpPost("http://" + hueAddress.getIp() + HUE_REQUEST); + ContentType parsedContentType = ContentType.parse("application/json"); + StringEntity requestBody = new StringEntity(new Gson().toJson(theLogin), parsedContentType); + HttpResponse response = null; + postRequest.setEntity(requestBody); + try { + response = httpClient.execute(postRequest); + log.debug("POST execute on URL responded: " + response.getStatusLine().getStatusCode()); + if(response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300){ + String theBody = EntityUtils.toString(response.getEntity()); + log.debug("registerWithHue response data: " + theBody); + SuccessUserResponse[] theResponses = new Gson().fromJson(theBody, SuccessUserResponse[].class); //read content for data, SuccessUserResponse[].class); + theUser = theResponses[0].getSuccess().getUsername(); + responseValue = true; + } + EntityUtils.consume(response.getEntity()); //close out inputstream ignore content + } catch (IOException e) { + log.warn("Error loggin into HUE: IOException in log", e); + } + return responseValue; + } public NamedIP getHueAddress() { return hueAddress; } diff --git a/src/main/resources/public/views/editdevice.html b/src/main/resources/public/views/editdevice.html index 6d39fe2..d657276 100644 --- a/src/main/resources/public/views/editdevice.html +++ b/src/main/resources/public/views/editdevice.html @@ -7,6 +7,7 @@
  • Harmony Activities
  • Harmony Devices
  • Nest
  • +
  • Hue Devices
  • Manual Add
  • diff --git a/src/main/resources/public/views/editor.html b/src/main/resources/public/views/editor.html index 0d5d1e4..256643d 100644 --- a/src/main/resources/public/views/editor.html +++ b/src/main/resources/public/views/editor.html @@ -7,6 +7,7 @@
  • Harmony Activities
  • Harmony Devices
  • Nest
  • +
  • Hue Devices
  • diff --git a/src/main/resources/public/views/harmonyactivity.html b/src/main/resources/public/views/harmonyactivity.html index 9418552..8687166 100644 --- a/src/main/resources/public/views/harmonyactivity.html +++ b/src/main/resources/public/views/harmonyactivity.html @@ -7,6 +7,7 @@
  • Harmony Devices
  • Nest
  • +
  • Hue Devices
  • Manual Add
  • diff --git a/src/main/resources/public/views/harmonydevice.html b/src/main/resources/public/views/harmonydevice.html index 2bfe929..5f43ad9 100644 --- a/src/main/resources/public/views/harmonydevice.html +++ b/src/main/resources/public/views/harmonydevice.html @@ -7,6 +7,7 @@
  • Harmony Activities
  • Nest
  • +
  • Hue Devices
  • Manual Add
  • diff --git a/src/main/resources/public/views/logs.html b/src/main/resources/public/views/logs.html index cdc94cc..d009d06 100644 --- a/src/main/resources/public/views/logs.html +++ b/src/main/resources/public/views/logs.html @@ -7,6 +7,7 @@
  • Harmony Activities
  • Harmony Devices
  • Nest
  • +
  • Hue Devices
  • Manual Add
  • diff --git a/src/main/resources/public/views/nestactions.html b/src/main/resources/public/views/nestactions.html index 47de20d..b737280 100644 --- a/src/main/resources/public/views/nestactions.html +++ b/src/main/resources/public/views/nestactions.html @@ -7,6 +7,7 @@
  • Harmony Activities
  • Harmony Devices
  • +
  • Hue Devices
  • Manual Add
  • diff --git a/src/main/resources/public/views/system.html b/src/main/resources/public/views/system.html index faa0e59..cb1e302 100644 --- a/src/main/resources/public/views/system.html +++ b/src/main/resources/public/views/system.html @@ -7,6 +7,7 @@
  • Harmony Activities
  • Harmony Devices
  • Nest
  • +
  • Hue Devices
  • Manual Add
  • diff --git a/src/main/resources/public/views/veradevice.html b/src/main/resources/public/views/veradevice.html index 36d54e1..46c5b90 100644 --- a/src/main/resources/public/views/veradevice.html +++ b/src/main/resources/public/views/veradevice.html @@ -7,6 +7,7 @@
  • Harmony Activities
  • Harmony Devices
  • Nest
  • +
  • Hue Devices
  • Manual Add
  • diff --git a/src/main/resources/public/views/verascene.html b/src/main/resources/public/views/verascene.html index 9728de3..fb676d6 100644 --- a/src/main/resources/public/views/verascene.html +++ b/src/main/resources/public/views/verascene.html @@ -7,6 +7,7 @@
  • Harmony Activities
  • Harmony Devices
  • Nest
  • +
  • Hue Devices
  • Manual Add