mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-19 08:28:46 +00:00
More testing on HUE passthru.
This commit is contained in:
@@ -4,17 +4,18 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import com.bwssystems.HABridge.api.hue.DeviceResponse;
|
import com.bwssystems.HABridge.api.hue.DeviceResponse;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by arm on 4/14/15.
|
* Created by arm on 4/14/15.
|
||||||
*/
|
*/
|
||||||
public class HueApiResponse {
|
public class HueApiResponse {
|
||||||
private Map<String, DeviceResponse> lights;
|
private Map<String, DeviceResponse> lights;
|
||||||
private Map<String, String> scenes;
|
private Map<String, JsonObject> scenes;
|
||||||
private Map<String, String> groups;
|
private Map<String, JsonObject> groups;
|
||||||
private Map<String, String> schedules;
|
private Map<String, JsonObject> schedules;
|
||||||
private Map<String, String> sensors;
|
private Map<String, JsonObject> sensors;
|
||||||
private Map<String, String> rules;
|
private Map<String, JsonObject> rules;
|
||||||
private HueConfig config;
|
private HueConfig config;
|
||||||
|
|
||||||
public HueApiResponse(String name, String ipaddress, String devicetype, String userid) {
|
public HueApiResponse(String name, String ipaddress, String devicetype, String userid) {
|
||||||
@@ -35,43 +36,43 @@ public class HueApiResponse {
|
|||||||
this.lights = lights;
|
this.lights = lights;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> getScenes() {
|
public Map<String, JsonObject> getScenes() {
|
||||||
return scenes;
|
return scenes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setScenes(Map<String, String> scenes) {
|
public void setScenes(Map<String, JsonObject> scenes) {
|
||||||
this.scenes = scenes;
|
this.scenes = scenes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> getGroups() {
|
public Map<String, JsonObject> getGroups() {
|
||||||
return groups;
|
return groups;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setGroups(Map<String, String> groups) {
|
public void setGroups(Map<String, JsonObject> groups) {
|
||||||
this.groups = groups;
|
this.groups = groups;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> getSchedules() {
|
public Map<String, JsonObject> getSchedules() {
|
||||||
return schedules;
|
return schedules;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSchedules(Map<String, String> schedules) {
|
public void setSchedules(Map<String, JsonObject> schedules) {
|
||||||
this.schedules = schedules;
|
this.schedules = schedules;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> getSensors() {
|
public Map<String, JsonObject> getSensors() {
|
||||||
return sensors;
|
return sensors;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSensors(Map<String, String> sensors) {
|
public void setSensors(Map<String, JsonObject> sensors) {
|
||||||
this.sensors = sensors;
|
this.sensors = sensors;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> getRules() {
|
public Map<String, JsonObject> getRules() {
|
||||||
return rules;
|
return rules;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRules(Map<String, String> rules) {
|
public void setRules(Map<String, JsonObject> rules) {
|
||||||
this.rules = rules;
|
this.rules = rules;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,12 +5,17 @@ import java.nio.charset.Charset;
|
|||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.HttpClient;
|
import org.apache.http.client.HttpClient;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
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.impl.client.HttpClients;
|
||||||
import org.apache.http.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.bwssystems.HABridge.NamedIP;
|
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.bwssystems.HABridge.api.hue.HueApiResponse;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
@@ -18,25 +23,61 @@ import com.google.gson.Gson;
|
|||||||
public class HueInfo {
|
public class HueInfo {
|
||||||
private static final Logger log = LoggerFactory.getLogger(HueInfo.class);
|
private static final Logger log = LoggerFactory.getLogger(HueInfo.class);
|
||||||
private HttpClient httpClient;
|
private HttpClient httpClient;
|
||||||
private static final String HUE_REQUEST = "/api/habridge";
|
private static final String HUE_REQUEST = "/api";
|
||||||
private NamedIP hueAddress;
|
private NamedIP hueAddress;
|
||||||
|
private UserCreateRequest theLogin;
|
||||||
|
private String theUser;
|
||||||
|
|
||||||
public HueInfo(NamedIP addressName) {
|
public HueInfo(NamedIP addressName) {
|
||||||
super();
|
super();
|
||||||
httpClient = HttpClients.createDefault();
|
httpClient = HttpClients.createDefault();
|
||||||
hueAddress = addressName;
|
hueAddress = addressName;
|
||||||
|
theLogin = new UserCreateRequest();
|
||||||
|
theLogin.setDevicetype("HA Bridge");
|
||||||
|
theLogin.setUsername("habridge");
|
||||||
|
theUser = theLogin.getUsername();
|
||||||
}
|
}
|
||||||
|
|
||||||
public HueApiResponse getHueApiResponse() {
|
public HueApiResponse getHueApiResponse() {
|
||||||
HueApiResponse theHueApiResponse = null;
|
HueApiResponse theHueApiResponse = null;
|
||||||
|
|
||||||
String theUrl = "http://" + hueAddress.getIp() + HUE_REQUEST;
|
String theUrl = "http://" + hueAddress.getIp() + HUE_REQUEST + "/" + theLogin.getUsername();
|
||||||
String theData;
|
String theData;
|
||||||
|
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);
|
theData = doHttpGETRequest(theUrl);
|
||||||
if(theData != null) {
|
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);
|
theHueApiResponse = new Gson().fromJson(theData, HueApiResponse.class);
|
||||||
log.debug("GET HueApiResponse - name: " + theHueApiResponse.getConfig().getName() + ", mac addr: " + theHueApiResponse.getConfig().getMac());
|
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;
|
return theHueApiResponse;
|
||||||
}
|
}
|
||||||
@@ -59,6 +100,29 @@ public class HueInfo {
|
|||||||
return theContent;
|
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() {
|
public NamedIP getHueAddress() {
|
||||||
return hueAddress;
|
return hueAddress;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
||||||
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonydevices">Harmony Devices</a></li>
|
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonydevices">Harmony Devices</a></li>
|
||||||
<li ng-if="bridge.showNest" role="presentation"><a href="#/nest">Nest</a></li>
|
<li ng-if="bridge.showNest" role="presentation"><a href="#/nest">Nest</a></li>
|
||||||
|
<li ng-if="bridge.showHue" role="presentation"><a href="#/huedevices">Hue Devices</a></li>
|
||||||
<li role="presentation"><a href="#/editor">Manual Add</a></li>
|
<li role="presentation"><a href="#/editor">Manual Add</a></li>
|
||||||
<li role="presentation" class="active"><a href="#/editdevice">Edit Device</a></li>
|
<li role="presentation" class="active"><a href="#/editdevice">Edit Device</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
||||||
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonydevices">Harmony Devices</a></li>
|
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonydevices">Harmony Devices</a></li>
|
||||||
<li ng-if="bridge.showNest" role="presentation"><a href="#/nest">Nest</a></li>
|
<li ng-if="bridge.showNest" role="presentation"><a href="#/nest">Nest</a></li>
|
||||||
|
<li ng-if="bridge.showHue" role="presentation"><a href="#/huedevices">Hue Devices</a></li>
|
||||||
<li role="presentation" class="active"><a href="#/editor">Manual Add</a></li>
|
<li role="presentation" class="active"><a href="#/editor">Manual Add</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
<li role="presentation" class="active"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
<li role="presentation" class="active"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
||||||
<li role="presentation"><a href="#/harmonydevices">Harmony Devices</a></li>
|
<li role="presentation"><a href="#/harmonydevices">Harmony Devices</a></li>
|
||||||
<li ng-if="bridge.showNest" role="presentation"><a href="#/nest">Nest</a></li>
|
<li ng-if="bridge.showNest" role="presentation"><a href="#/nest">Nest</a></li>
|
||||||
|
<li ng-if="bridge.showHue" role="presentation"><a href="#/huedevices">Hue Devices</a></li>
|
||||||
<li role="presentation"><a href="#/editor">Manual Add</a></li>
|
<li role="presentation"><a href="#/editor">Manual Add</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
<li role="presentation"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
<li role="presentation"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
||||||
<li role="presentation" class="active"><a href="#/harmonydevices">Harmony Devices</a></li>
|
<li role="presentation" class="active"><a href="#/harmonydevices">Harmony Devices</a></li>
|
||||||
<li ng-if="bridge.showNest" role="presentation"><a href="#/nest">Nest</a></li>
|
<li ng-if="bridge.showNest" role="presentation"><a href="#/nest">Nest</a></li>
|
||||||
|
<li ng-if="bridge.showHue" role="presentation"><a href="#/huedevices">Hue Devices</a></li>
|
||||||
<li role="presentation"><a href="#/editor">Manual Add</a></li>
|
<li role="presentation"><a href="#/editor">Manual Add</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
||||||
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonydevices">Harmony Devices</a></li>
|
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonydevices">Harmony Devices</a></li>
|
||||||
<li ng-if="bridge.showNest" role="presentation"><a href="#/nest">Nest</a></li>
|
<li ng-if="bridge.showNest" role="presentation"><a href="#/nest">Nest</a></li>
|
||||||
|
<li ng-if="bridge.showHue" role="presentation"><a href="#/huedevices">Hue Devices</a></li>
|
||||||
<li role="presentation"><a href="#/editor">Manual Add</a></li>
|
<li role="presentation"><a href="#/editor">Manual Add</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
||||||
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonydevices">Harmony Devices</a></li>
|
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonydevices">Harmony Devices</a></li>
|
||||||
<li role="presentation" class="active"><a href="#/nest">Nest</a></li>
|
<li role="presentation" class="active"><a href="#/nest">Nest</a></li>
|
||||||
|
<li ng-if="bridge.showHue" role="presentation"><a href="#/huedevices">Hue Devices</a></li>
|
||||||
<li role="presentation"><a href="#/editor">Manual Add</a></li>
|
<li role="presentation"><a href="#/editor">Manual Add</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
||||||
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonydevices">Harmony Devices</a></li>
|
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonydevices">Harmony Devices</a></li>
|
||||||
<li ng-if="bridge.showNest" role="presentation"><a href="#/nest">Nest</a></li>
|
<li ng-if="bridge.showNest" role="presentation"><a href="#/nest">Nest</a></li>
|
||||||
|
<li ng-if="bridge.showHue" role="presentation"><a href="#/huedevices">Hue Devices</a></li>
|
||||||
<li role="presentation"><a href="#/editor">Manual Add</a></li>
|
<li role="presentation"><a href="#/editor">Manual Add</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
||||||
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonydevices">Harmony Devices</a></li>
|
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonydevices">Harmony Devices</a></li>
|
||||||
<li ng-if="bridge.showNest" role="presentation"><a href="#/nest">Nest</a></li>
|
<li ng-if="bridge.showNest" role="presentation"><a href="#/nest">Nest</a></li>
|
||||||
|
<li ng-if="bridge.showHue" role="presentation"><a href="#/huedevices">Hue Devices</a></li>
|
||||||
<li role="presentation"><a href="#/editor">Manual Add</a></li>
|
<li role="presentation"><a href="#/editor">Manual Add</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonyactivities">Harmony Activities</a></li>
|
||||||
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonydevices">Harmony Devices</a></li>
|
<li ng-if="bridge.showHarmony" role="presentation"><a href="#/harmonydevices">Harmony Devices</a></li>
|
||||||
<li ng-if="bridge.showNest" role="presentation"><a href="#/nest">Nest</a></li>
|
<li ng-if="bridge.showNest" role="presentation"><a href="#/nest">Nest</a></li>
|
||||||
|
<li ng-if="bridge.showHue" role="presentation"><a href="#/huedevices">Hue Devices</a></li>
|
||||||
<li role="presentation"><a href="#/editor">Manual Add</a></li>
|
<li role="presentation"><a href="#/editor">Manual Add</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user