Fixed Hue bulk build and save hue registration

This commit is contained in:
Admin
2017-06-19 15:00:10 -05:00
parent 92b7f4e260
commit 220e337d08
5 changed files with 73 additions and 111 deletions

View File

@@ -440,4 +440,15 @@ public class BridgeSettingsDescriptor {
public Boolean isValidLifx() { public Boolean isValidLifx() {
return this.isLifxconfigured(); return this.isLifxconfigured();
} }
public void updateHue(NamedIP aHue) {
int indexHue = -1;
for( int i = 0; i < hueaddress.getDevices().size(); i++) {
if(hueaddress.getDevices().get(i).getName().equals(aHue.getName()))
indexHue = i;
}
if(indexHue >= 0) {
hueaddress.getDevices().set(indexHue, aHue);
this.setSettingsChanged(true);
}
}
} }

View File

@@ -6,6 +6,7 @@ import java.net.URISyntaxException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.CookieSpecs; import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
@@ -67,73 +68,67 @@ public class HTTPHandler {
if (requestBody != null) if (requestBody != null)
putRequest.setEntity(requestBody); putRequest.setEntity(requestBody);
request = putRequest; request = putRequest;
} } else
else
request = new HttpGet(theURI); request = new HttpGet(theURI);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
log.warn("Error creating outbound http request: IllegalArgumentException in log", e); log.warn("Error creating outbound http request: IllegalArgumentException in log", e);
return null; return null;
} }
log.debug("Making outbound call in doHttpRequest: " + request); log.debug("Making outbound call in doHttpRequest: <<<" + request.toString() + ">>>");
if (headers != null && headers.length > 0) { if (headers != null && headers.length > 0) {
for (int i = 0; i < headers.length; i++) { for (int i = 0; i < headers.length; i++) {
request.setHeader(headers[i].getName(), headers[i].getValue()); request.setHeader(headers[i].getName(), headers[i].getValue());
} }
} }
HttpResponse response; HttpResponse response = null;
try { for (int retryCount = 0; retryCount < 2; retryCount++) {
for(int retryCount = 0; retryCount < 2; retryCount++) { try {
response = httpClient.execute(request); response = httpClient.execute(request);
log.debug((httpVerb == null ? "GET" : httpVerb) + " execute (" + retryCount + ") on URL responded: " } catch (ClientProtocolException e) {
+ response.getStatusLine().getStatusCode()); log.warn("Client Protocol Exception received, retyring....");
if (response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300) { } catch (IOException e) {
if (response.getEntity() != null) { log.warn("Error calling out to HA gateway: IOException in log", e);
try { retryCount = 2;
theContent = EntityUtils.toString(response.getEntity(), Charset.forName("UTF-8")); // read }
// content log.debug((httpVerb == null ? "GET" : httpVerb) + " execute (" + retryCount + ") on URL responded: "
// for + response.getStatusLine().getStatusCode());
// data if (response != null && response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300) {
EntityUtils.consume(response.getEntity()); // close out log.debug("Successfull response - The http response is <<<" + theContent + ">>>");
// inputstream retryCount = 2;
// ignore } else if (response != null) {
// content log.warn("HTTP response code was not an expected successful response of between 200 - 299, the code was: "
} catch (Exception e) { + response.getStatusLine());
log.debug("Error ocurred in handling response entity after successful call, still responding success. " if (response.getStatusLine().getStatusCode() == 504) {
+ e.getMessage(), e); log.warn("HTTP response code was 504, retrying...");
} } else
log.debug("Successfull response - The http response is <<<" + theContent + ">>>");
}
retryCount = 2; retryCount = 2;
} else { }
log.warn("HTTP response code was not an expected successful response of between 200 - 299, the code was: " + response.getStatusLine());
try { if (response != null && response.getEntity() != null) {
String someContent = EntityUtils.toString(response.getEntity(), Charset.forName("UTF-8")); // read try {
// content theContent = EntityUtils.toString(response.getEntity(), Charset.forName("UTF-8")); // read
// for // content
// data // for
EntityUtils.consume(response.getEntity()); // close out // data
// inputstream EntityUtils.consume(response.getEntity()); // close out
// ignore // inputstream
// content // ignore
log.debug("Unsuccessfull response - The http response is <<<" + someContent + ">>>"); // content
} catch (Exception e) { } catch (Exception e) {
//noop log.debug("Error ocurred in handling response entity after successful call, still responding success. "
} + e.getMessage(), e);
if (response.getStatusLine().getStatusCode() == 504) { }
log.warn("HTTP response code was 504, retrying..."); }
try {
Thread.sleep(1000); if(retryCount < 2) {
} catch (InterruptedException e1) { theContent = null;
// noop try {
} Thread.sleep(1000);
} } catch (InterruptedException e1) {
else // noop
retryCount = 2;
} }
} }
} catch (IOException e) {
log.warn("Error calling out to HA gateway: IOException in log", e);
} }
return theContent; return theContent;
} }

View File

@@ -24,9 +24,11 @@ public class HueHome implements Home {
private Map<String, HueInfo> hues; private Map<String, HueInfo> hues;
private Boolean validHue; private Boolean validHue;
private Gson aGsonHandler; private Gson aGsonHandler;
private BridgeSettings theBridgeSettings;
public HueHome(BridgeSettings bridgeSettings) { public HueHome(BridgeSettings bridgeSettings) {
super(); super();
theBridgeSettings = bridgeSettings;
createHome(bridgeSettings); createHome(bridgeSettings);
} }
@@ -113,12 +115,19 @@ public class HueHome implements Home {
Iterator<NamedIP> theList = bridgeSettings.getBridgeSettingsDescriptor().getHueaddress().getDevices().iterator(); Iterator<NamedIP> theList = bridgeSettings.getBridgeSettingsDescriptor().getHueaddress().getDevices().iterator();
while(theList.hasNext()) { while(theList.hasNext()) {
NamedIP aHue = theList.next(); NamedIP aHue = theList.next();
hues.put(aHue.getName(), new HueInfo(aHue)); hues.put(aHue.getName(), new HueInfo(aHue, this));
} }
aGsonHandler = new GsonBuilder().create(); aGsonHandler = new GsonBuilder().create();
} }
return this; return this;
} }
protected void updateHue(NamedIP aHue) {
theBridgeSettings.getBridgeSettingsDescriptor().updateHue(aHue);
if(theBridgeSettings.getBridgeSettingsDescriptor().isSettingsChanged()) {
theBridgeSettings.updateConfigFile();
}
}
@Override @Override
public void closeHome() { public void closeHome() {

View File

@@ -27,12 +27,14 @@ public class HueInfo {
private static final Logger log = LoggerFactory.getLogger(HueInfo.class); private static final Logger log = LoggerFactory.getLogger(HueInfo.class);
private HTTPHandler httpClient; private HTTPHandler httpClient;
private NamedIP hueAddress; private NamedIP hueAddress;
private HueHome myHome;
public static final String HUE_REQUEST = "/api"; public static final String HUE_REQUEST = "/api";
public HueInfo(NamedIP addressName) { public HueInfo(NamedIP addressName, HueHome theHome) {
super(); super();
httpClient = new HTTPHandler(); httpClient = new HTTPHandler();
hueAddress = addressName; hueAddress = addressName;
myHome = theHome;
} }
public HueApiResponse getHueApiResponse() { public HueApiResponse getHueApiResponse() {
@@ -62,7 +64,7 @@ public class HueInfo {
// ignore // ignore
} }
} }
theUrl = "http://" + hueAddress.getIp() + HueUtil.HUE_REQUEST + "/" + hueAddress.getUsername(); theUrl = "http://" + hueAddress.getIp() + HUE_REQUEST + "/" + hueAddress.getUsername();
theData = httpClient.doHttpRequest(theUrl, null, null, null, null); theData = httpClient.doHttpRequest(theUrl, null, null, null, null);
if(theData != null) { if(theData != null) {
log.debug("GET HueApiResponse - data: " + theData); log.debug("GET HueApiResponse - data: " + theData);
@@ -118,6 +120,7 @@ public class HueInfo {
else { else {
SuccessUserResponse[] theResponses = new Gson().fromJson(theBody, SuccessUserResponse[].class); //read content for data, SuccessUserResponse[].class); SuccessUserResponse[] theResponses = new Gson().fromJson(theBody, SuccessUserResponse[].class); //read content for data, SuccessUserResponse[].class);
hueAddress.setUsername(theResponses[0].getSuccess().getUsername()); hueAddress.setUsername(theResponses[0].getSuccess().getUsername());
myHome.updateHue(hueAddress);
} }
} }
EntityUtils.consume(response.getEntity()); //close out inputstream ignore content EntityUtils.consume(response.getEntity()); //close out inputstream ignore content

View File

@@ -1,56 +0,0 @@
package com.bwssystems.HABridge.plugins.hue;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.api.SuccessUserResponse;
import com.bwssystems.HABridge.api.UserCreateRequest;
import com.bwssystems.HABridge.plugins.http.HTTPHandler;
import com.google.gson.Gson;
public class HueUtil {
private static final Logger log = LoggerFactory.getLogger(HueUtil.class);
public static final String HUE_REQUEST = "/api";
public static final String registerWithHue(HTTPHandler anHttpHandler, String ipAddress, String aName, String theUser) {
UserCreateRequest theLogin = new UserCreateRequest();
theLogin.setDevicetype("HABridge#MyMachine");
HttpPost postRequest = new HttpPost("http://" + ipAddress + HUE_REQUEST);
ContentType parsedContentType = ContentType.parse("application/json");
StringEntity requestBody = new StringEntity(new Gson().toJson(theLogin), parsedContentType);
HttpResponse response = null;
postRequest.setEntity(requestBody);
HttpClient anHttpClient = anHttpHandler.getHttpClient();
try {
response = anHttpClient.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);
if(theBody.contains("[{\"error\":")) {
if(theBody.contains("link button not")) {
log.warn("registerWithHue needs link button pressed on HUE bridge: " + aName);
}
else
log.warn("registerWithHue returned an unexpected error: " + theBody);
}
else {
SuccessUserResponse[] theResponses = new Gson().fromJson(theBody, SuccessUserResponse[].class); //read content for data, SuccessUserResponse[].class);
theUser = theResponses[0].getSuccess().getUsername();
}
}
EntityUtils.consume(response.getEntity()); //close out inputstream ignore content
} catch (IOException e) {
log.warn("Error logging into HUE: IOException in log", e);
}
return theUser;
}
}