Updated passthru after more testing

This commit is contained in:
Admin
2016-04-21 15:41:06 -05:00
parent a717fd7c68
commit 05418fdda1
4 changed files with 99 additions and 57 deletions

View File

@@ -5,17 +5,12 @@ 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;
@@ -23,25 +18,21 @@ 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";
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();
theUser = "habridge";
}
public HueApiResponse getHueApiResponse() {
HueApiResponse theHueApiResponse = null;
String errorString = null;
String theUrl = "http://" + hueAddress.getIp() + HUE_REQUEST + "/" + theLogin.getUsername();
String theUrl = "http://" + hueAddress.getIp() + HueUtil.HUE_REQUEST + "/" + theUser;
String theData;
boolean loopControl = true;
int retryCount = 0;
@@ -51,14 +42,14 @@ public class HueInfo {
loopControl = false;
break;
}
theUrl = "http://" + hueAddress.getIp() + HUE_REQUEST + "/" + theUser;
theUrl = "http://" + hueAddress.getIp() + HueUtil.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.");
if((theUser = HueUtil.registerWithHue(httpClient, hueAddress.getIp(), hueAddress.getName(), errorString)) == null) {
log.warn("Register to Hue for " + hueAddress.getName() + " returned error: " + errorString);
return null;
}
retryCount++;
@@ -100,29 +91,6 @@ 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;
}