mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-21 01:05:43 +00:00
More refactoring
This commit is contained in:
@@ -1,20 +1,14 @@
|
|||||||
package com.bwssystems.hal;
|
package com.bwssystems.hal;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.http.HttpResponse;
|
|
||||||
import org.apache.http.client.HttpClient;
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
|
||||||
import org.apache.http.impl.client.HttpClients;
|
|
||||||
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.http.HTTPHandler;
|
||||||
import com.bwssystems.util.TextStringFormatter;
|
import com.bwssystems.util.TextStringFormatter;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
@@ -39,13 +33,13 @@ public class HalInfo {
|
|||||||
private static final String MACRO_TYPE = "Macro";
|
private static final String MACRO_TYPE = "Macro";
|
||||||
private static final String SCENE_TYPE = "Scene";
|
private static final String SCENE_TYPE = "Scene";
|
||||||
private static final String IRDATA_TYPE = "IrData";
|
private static final String IRDATA_TYPE = "IrData";
|
||||||
private HttpClient httpClient;
|
private HTTPHandler httpClient;
|
||||||
private NamedIP halAddress;
|
private NamedIP halAddress;
|
||||||
private String theToken;
|
private String theToken;
|
||||||
|
|
||||||
public HalInfo(NamedIP addressName, String aGivenToken) {
|
public HalInfo(NamedIP addressName, String aGivenToken) {
|
||||||
super();
|
super();
|
||||||
httpClient = HttpClients.createDefault();
|
httpClient = new HTTPHandler();
|
||||||
halAddress = addressName;
|
halAddress = addressName;
|
||||||
theToken = aGivenToken;
|
theToken = aGivenToken;
|
||||||
}
|
}
|
||||||
@@ -105,7 +99,7 @@ public class HalInfo {
|
|||||||
String theUrl = null;
|
String theUrl = null;
|
||||||
String theData;
|
String theData;
|
||||||
theUrl = "http://" + halAddress.getIp() + apiType + theToken;
|
theUrl = "http://" + halAddress.getIp() + apiType + theToken;
|
||||||
theData = doHttpGETRequest(theUrl);
|
theData = httpClient.doHttpRequest(theUrl, null, null, null, null);
|
||||||
if(theData != null) {
|
if(theData != null) {
|
||||||
log.debug("GET " + deviceType + " HalApiResponse - data: " + theData);
|
log.debug("GET " + deviceType + " HalApiResponse - data: " + theData);
|
||||||
theHalApiResponse = new Gson().fromJson(theData, DeviceElements.class);
|
theHalApiResponse = new Gson().fromJson(theData, DeviceElements.class);
|
||||||
@@ -150,7 +144,7 @@ public class HalInfo {
|
|||||||
while (theHalDevices.hasNext()) {
|
while (theHalDevices.hasNext()) {
|
||||||
HalDevice theHalDevice = theHalDevices.next();
|
HalDevice theHalDevice = theHalDevices.next();
|
||||||
theUrl = "http://" + halAddress.getIp() + IRBUTTON_REQUEST + TextStringFormatter.forQuerySpaceUrl(theHalDevice.getHaldevicename()) + TOKEN_REQUEST + theToken;
|
theUrl = "http://" + halAddress.getIp() + IRBUTTON_REQUEST + TextStringFormatter.forQuerySpaceUrl(theHalDevice.getHaldevicename()) + TOKEN_REQUEST + theToken;
|
||||||
theData = doHttpGETRequest(theUrl);
|
theData = httpClient.doHttpRequest(theUrl, null, null, null, null);
|
||||||
if (theData != null) {
|
if (theData != null) {
|
||||||
log.debug("GET IrData for IR Device " + theHalDevice.getHaldevicename() + " HalApiResponse - data: " + theData);
|
log.debug("GET IrData for IR Device " + theHalDevice.getHaldevicename() + " HalApiResponse - data: " + theData);
|
||||||
theHalApiResponse = new Gson().fromJson(theData, DeviceElements.class);
|
theHalApiResponse = new Gson().fromJson(theData, DeviceElements.class);
|
||||||
@@ -177,24 +171,6 @@ public class HalInfo {
|
|||||||
return deviceList;
|
return deviceList;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function executes the url against the hal
|
|
||||||
protected String doHttpGETRequest(String url) {
|
|
||||||
String theContent = null;
|
|
||||||
log.debug("calling GET on URL: " + url);
|
|
||||||
HttpGet httpGet = new HttpGet(url);
|
|
||||||
try {
|
|
||||||
HttpResponse response = httpClient.execute(httpGet);
|
|
||||||
log.debug("GET on URL responded: " + response.getStatusLine().getStatusCode());
|
|
||||||
if(response.getStatusLine().getStatusCode() == 200){
|
|
||||||
theContent = EntityUtils.toString(response.getEntity(), Charset.forName("UTF-8")); //read content for data
|
|
||||||
EntityUtils.consume(response.getEntity()); //close out inputstream ignore content
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("doHttpGETRequest: Error calling out to HA gateway: " + e.getMessage());
|
|
||||||
}
|
|
||||||
return theContent;
|
|
||||||
}
|
|
||||||
|
|
||||||
public NamedIP getHalAddress() {
|
public NamedIP getHalAddress() {
|
||||||
return halAddress;
|
return halAddress;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,12 +12,13 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import com.bwssystems.HABridge.NamedIP;
|
import com.bwssystems.HABridge.NamedIP;
|
||||||
import com.bwssystems.HABridge.api.hue.HueApiResponse;
|
import com.bwssystems.HABridge.api.hue.HueApiResponse;
|
||||||
|
import com.bwssystems.http.HTTPHandler;
|
||||||
import com.google.gson.Gson;
|
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 HTTPHandler httpClient;
|
||||||
private NamedIP hueAddress;
|
private NamedIP hueAddress;
|
||||||
private String theUser;
|
private String theUser;
|
||||||
private HueHome theHueHome;
|
private HueHome theHueHome;
|
||||||
@@ -25,7 +26,7 @@ public class HueInfo {
|
|||||||
|
|
||||||
public HueInfo(NamedIP addressName, HueHome aHueHome) {
|
public HueInfo(NamedIP addressName, HueHome aHueHome) {
|
||||||
super();
|
super();
|
||||||
httpClient = HttpClients.createDefault();
|
httpClient = new HTTPHandler();
|
||||||
hueAddress = addressName;
|
hueAddress = addressName;
|
||||||
theUser = "habridge";
|
theUser = "habridge";
|
||||||
theHueHome = aHueHome;
|
theHueHome = aHueHome;
|
||||||
@@ -45,7 +46,7 @@ public class HueInfo {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
theUrl = "http://" + hueAddress.getIp() + HueUtil.HUE_REQUEST + "/" + theUser;
|
theUrl = "http://" + hueAddress.getIp() + HueUtil.HUE_REQUEST + "/" + theUser;
|
||||||
theData = doHttpGETRequest(theUrl);
|
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);
|
||||||
if(theData.contains("[{\"error\":")) {
|
if(theData.contains("[{\"error\":")) {
|
||||||
@@ -78,24 +79,6 @@ public class HueInfo {
|
|||||||
return theHueApiResponse;
|
return theHueApiResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function executes the url against the vera
|
|
||||||
protected String doHttpGETRequest(String url) {
|
|
||||||
String theContent = null;
|
|
||||||
log.debug("calling GET on URL: " + url);
|
|
||||||
HttpGet httpGet = new HttpGet(url);
|
|
||||||
try {
|
|
||||||
HttpResponse response = httpClient.execute(httpGet);
|
|
||||||
log.debug("GET on URL responded: " + response.getStatusLine().getStatusCode());
|
|
||||||
if(response.getStatusLine().getStatusCode() == 200){
|
|
||||||
theContent = EntityUtils.toString(response.getEntity(), Charset.forName("UTF-8")); //read content for data
|
|
||||||
EntityUtils.consume(response.getEntity()); //close out inputstream ignore content
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("doHttpGETRequest: Error calling out to HA gateway: " + e.getMessage());
|
|
||||||
}
|
|
||||||
return theContent;
|
|
||||||
}
|
|
||||||
|
|
||||||
public NamedIP getHueAddress() {
|
public NamedIP getHueAddress() {
|
||||||
return hueAddress;
|
return hueAddress;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,13 +13,14 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import com.bwssystems.HABridge.api.SuccessUserResponse;
|
import com.bwssystems.HABridge.api.SuccessUserResponse;
|
||||||
import com.bwssystems.HABridge.api.UserCreateRequest;
|
import com.bwssystems.HABridge.api.UserCreateRequest;
|
||||||
|
import com.bwssystems.http.HTTPHandler;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
public class HueUtil {
|
public class HueUtil {
|
||||||
private static final Logger log = LoggerFactory.getLogger(HueUtil.class);
|
private static final Logger log = LoggerFactory.getLogger(HueUtil.class);
|
||||||
public static final String HUE_REQUEST = "/api";
|
public static final String HUE_REQUEST = "/api";
|
||||||
|
|
||||||
public static final String registerWithHue(HttpClient anHttpClient, String ipAddress, String aName, String theUser) {
|
public static final String registerWithHue(HTTPHandler anHttpClient, String ipAddress, String aName, String theUser) {
|
||||||
UserCreateRequest theLogin = new UserCreateRequest();
|
UserCreateRequest theLogin = new UserCreateRequest();
|
||||||
theLogin.setDevicetype("HABridge#MyMachine");
|
theLogin.setDevicetype("HABridge#MyMachine");
|
||||||
HttpPost postRequest = new HttpPost("http://" + ipAddress + HUE_REQUEST);
|
HttpPost postRequest = new HttpPost("http://" + ipAddress + HUE_REQUEST);
|
||||||
|
|||||||
Reference in New Issue
Block a user