Conitnuation of refactoring

This commit is contained in:
bwssystems
2016-12-26 11:28:06 -06:00
parent 729d548d0e
commit d827605fa5
19 changed files with 251 additions and 355 deletions

View File

@@ -5,7 +5,7 @@
<groupId>com.bwssystems.HABridge</groupId> <groupId>com.bwssystems.HABridge</groupId>
<artifactId>ha-bridge</artifactId> <artifactId>ha-bridge</artifactId>
<version>3.5.1n</version> <version>3.5.1o</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>HA Bridge</name> <name>HA Bridge</name>

View File

@@ -16,16 +16,10 @@ import com.bwssystems.HABridge.api.hue.HuePublicConfig;
import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.api.hue.StateChangeBody;
import com.bwssystems.HABridge.api.hue.WhitelistEntry; import com.bwssystems.HABridge.api.hue.WhitelistEntry;
import com.bwssystems.HABridge.dao.*; import com.bwssystems.HABridge.dao.*;
import com.bwssystems.harmony.ButtonPress; import com.bwssystems.http.HTTPHandler;
import com.bwssystems.harmony.HarmonyHandler;
import com.bwssystems.harmony.HarmonyHome;
import com.bwssystems.harmony.RunActivity;
import com.bwssystems.hue.HueDeviceIdentifier; import com.bwssystems.hue.HueDeviceIdentifier;
import com.bwssystems.hue.HueHome; import com.bwssystems.hue.HueHome;
import com.bwssystems.hue.HueUtil; import com.bwssystems.hue.HueUtil;
import com.bwssystems.mqtt.MQTTHandler;
import com.bwssystems.mqtt.MQTTHome;
import com.bwssystems.mqtt.MQTTMessage;
import com.bwssystems.util.JsonTransformer; import com.bwssystems.util.JsonTransformer;
import com.bwssystems.util.UDPDatagramSender; import com.bwssystems.util.UDPDatagramSender;
import com.google.gson.Gson; import com.google.gson.Gson;
@@ -36,22 +30,8 @@ import static spark.Spark.options;
import static spark.Spark.post; import static spark.Spark.post;
import static spark.Spark.put; import static spark.Spark.put;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
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.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -60,9 +40,6 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@@ -71,7 +48,6 @@ import java.util.Set;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.UUID; import java.util.UUID;
import javax.net.ssl.SSLContext;
import javax.xml.bind.DatatypeConverter; import javax.xml.bind.DatatypeConverter;
/** /**
@@ -85,44 +61,22 @@ public class HueMulator {
private DeviceRepository repository; private DeviceRepository repository;
private HomeManager homeManager; private HomeManager homeManager;
private HueHome myHueHome; private HueHome myHueHome;
private HarmonyHome myHarmonyHome;
private MQTTHome mqttHome;
private HttpClient httpClient;
private CloseableHttpClient httpclientSSL;
private SSLContext sslcontext;
private SSLConnectionSocketFactory sslsf;
private RequestConfig globalConfig;
private BridgeSettingsDescriptor bridgeSettings; private BridgeSettingsDescriptor bridgeSettings;
private UDPDatagramSender theUDPDatagramSender; private UDPDatagramSender theUDPDatagramSender;
private byte[] sendData; private byte[] sendData;
private String hueUser;
private String errorString;
private Gson aGsonHandler; private Gson aGsonHandler;
private HTTPHandler anHttpHandler;
public HueMulator(BridgeSettingsDescriptor theBridgeSettings, DeviceRepository aDeviceRepository, HomeManager aHomeManager, UDPDatagramSender aUdpDatagramSender) { public HueMulator(BridgeSettingsDescriptor theBridgeSettings, DeviceRepository aDeviceRepository, HomeManager aHomeManager, UDPDatagramSender aUdpDatagramSender) {
httpClient = HttpClients.createDefault();
// Trust own CA and all self-signed certs
sslcontext = SSLContexts.createDefault();
// Allow TLSv1 protocol only
sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
httpclientSSL = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultRequestConfig(globalConfig).build();
repository = aDeviceRepository; repository = aDeviceRepository;
bridgeSettings = theBridgeSettings; bridgeSettings = theBridgeSettings;
theUDPDatagramSender = aUdpDatagramSender; theUDPDatagramSender = aUdpDatagramSender;
homeManager= aHomeManager; homeManager= aHomeManager;
myHueHome = (HueHome) homeManager.findHome(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex]); myHueHome = (HueHome) homeManager.findHome(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex]);
myHarmonyHome = (HarmonyHome) homeManager.findHome(DeviceMapTypes.HARMONY_ACTIVITY[DeviceMapTypes.typeIndex]);
mqttHome = (MQTTHome) homeManager.findHome(DeviceMapTypes.MQTT_MESSAGE[DeviceMapTypes.typeIndex]);
hueUser = null;
errorString = null;
aGsonHandler = aGsonHandler =
new GsonBuilder() new GsonBuilder()
// .registerTypeAdapter(CallItem.class, new CallItemDeserializer()) .create();
.create(); anHttpHandler = new HTTPHandler();
} }
// This function sets up the sparkjava rest calls for the hue api // This function sets up the sparkjava rest calls for the hue api
@@ -323,84 +277,6 @@ public class HueMulator {
}); });
} }
// This function executes the url from the device repository against the
// target as http or https as defined
protected String doHttpRequest(String url, String httpVerb, String contentType, String body, NameValue[] headers) {
HttpUriRequest request = null;
String theContent = null;
URI theURI = null;
ContentType parsedContentType = null;
StringEntity requestBody = null;
if (contentType != null && contentType.length() > 0) {
parsedContentType = ContentType.parse(contentType);
if (body != null && body.length() > 0)
requestBody = new StringEntity(body, parsedContentType);
}
try {
theURI = new URI(url);
} catch (URISyntaxException e1) {
log.warn("Error creating URI http request: " + url + " with message: " + e1.getMessage());
return null;
}
try {
if (HttpGet.METHOD_NAME.equalsIgnoreCase(httpVerb) || httpVerb == null) {
request = new HttpGet(theURI);
} else if (HttpPost.METHOD_NAME.equalsIgnoreCase(httpVerb)) {
HttpPost postRequest = new HttpPost(theURI);
if (requestBody != null)
postRequest.setEntity(requestBody);
request = postRequest;
} else if (HttpPut.METHOD_NAME.equalsIgnoreCase(httpVerb)) {
HttpPut putRequest = new HttpPut(theURI);
if (requestBody != null)
putRequest.setEntity(requestBody);
request = putRequest;
}
} catch (IllegalArgumentException e) {
log.warn("Error creating outbound http request: IllegalArgumentException in log", e);
return null;
}
log.debug("Making outbound call in doHttpRequest: " + request);
if (headers != null && headers.length > 0) {
for (int i = 0; i < headers.length; i++) {
request.setHeader(headers[i].getName(), headers[i].getValue());
}
}
try {
HttpResponse response;
if (url.startsWith("https"))
response = httpclientSSL.execute(request);
else
response = httpClient.execute(request);
log.debug((httpVerb == null ? "GET" : httpVerb) + " execute on URL responded: "
+ response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300) {
if (response.getEntity() != null) {
try {
theContent = EntityUtils.toString(response.getEntity(), Charset.forName("UTF-8")); // read
// content
// for
// data
EntityUtils.consume(response.getEntity()); // close out
// inputstream
// ignore
// content
} catch (Exception e) {
log.debug(
"Error ocurred in handling response entity after successful call, still responding success. "
+ e.getMessage(),
e);
}
}
if (theContent == null)
theContent = "";
}
} catch (IOException e) {
log.warn("Error calling out to HA gateway: IOException in log", e);
}
return theContent;
}
private String doExecRequest(String anItem, int intensity, String lightId) { private String doExecRequest(String anItem, int intensity, String lightId) {
log.debug("Executing request: " + anItem); log.debug("Executing request: " + anItem);
String responseString = null; String responseString = null;
@@ -681,7 +557,7 @@ public class HueMulator {
theErrors = validateHueUser(userId, deviceId.getIpAddress(), device.getName()); theErrors = validateHueUser(userId, deviceId.getIpAddress(), device.getName());
if (theErrors == null) { if (theErrors == null) {
// make call // make call
responseString = doHttpRequest( responseString = anHttpHandler.doHttpRequest(
"http://" + deviceId.getIpAddress() + "/api/" + myHueHome.getTheHUERegisteredUser() "http://" + deviceId.getIpAddress() + "/api/" + myHueHome.getTheHUERegisteredUser()
+ "/lights/" + deviceId.getDeviceId(), + "/lights/" + deviceId.getDeviceId(),
HttpGet.METHOD_NAME, device.getContentType(), null, null); HttpGet.METHOD_NAME, device.getContentType(), null, null);
@@ -798,7 +674,7 @@ public class HueMulator {
theErrors = validateHueUser(userId, deviceId.getIpAddress(), device.getName()); theErrors = validateHueUser(userId, deviceId.getIpAddress(), device.getName());
if (theErrors == null) { if (theErrors == null) {
// make call // make call
responseString = doHttpRequest("http://" + deviceId.getIpAddress() + "/api/" responseString = anHttpHandler.doHttpRequest("http://" + deviceId.getIpAddress() + "/api/"
+ myHueHome.getTheHUERegisteredUser() + "/lights/" + deviceId.getDeviceId(), + myHueHome.getTheHUERegisteredUser() + "/lights/" + deviceId.getDeviceId(),
HttpGet.METHOD_NAME, device.getContentType(), null, null); HttpGet.METHOD_NAME, device.getContentType(), null, null);
if (responseString == null) { if (responseString == null) {
@@ -893,7 +769,7 @@ public class HueMulator {
String hueUser; String hueUser;
HueErrorResponse theErrorResp = null; HueErrorResponse theErrorResp = null;
if (myHueHome.getTheHUERegisteredUser() == null) { if (myHueHome.getTheHUERegisteredUser() == null) {
hueUser = HueUtil.registerWithHue(httpClient, ipAddress, aName, hueUser = HueUtil.registerWithHue(anHttpHandler, ipAddress, aName,
myHueHome.getTheHUERegisteredUser()); myHueHome.getTheHUERegisteredUser());
if (hueUser == null) { if (hueUser == null) {
theErrorResp = HueErrorResponse.createResponse("901", "/api/" + userId, "Could not register proxy to other hue hub", null, null, null); theErrorResp = HueErrorResponse.createResponse("901", "/api/" + userId, "Could not register proxy to other hue hub", null, null, null);
@@ -907,7 +783,7 @@ public class HueMulator {
return null; return null;
} }
private String changeState(String userId, String lightId, String body,String ipAddress) { private String changeState(String userId, String lightId, String body, String ipAddress) {
String responseString = null; String responseString = null;
String url = null; String url = null;
NameValue[] theHeaders = null; NameValue[] theHeaders = null;
@@ -1029,203 +905,21 @@ public class HueMulator {
} }
if (callItems[i].getType() != null && callItems[i].getType().trim().equalsIgnoreCase(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex])) { if (callItems[i].getType() != null && callItems[i].getType().trim().equalsIgnoreCase(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex])) {
if (myHueHome != null) { return homeManager.findHome(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex]).deviceHandler(callItems[i], aMultiUtil, lightId, i, state, theStateChanges, stateHasBri, stateHasBriInc, device, body);
HueDeviceIdentifier deviceId = aGsonHandler.fromJson(callItems[i].getItem(), HueDeviceIdentifier.class);
if (myHueHome.getTheHUERegisteredUser() == null) {
hueUser = HueUtil.registerWithHue(httpClient, deviceId.getIpAddress(), device.getName(),
myHueHome.getTheHUERegisteredUser());
if (hueUser == null) {
return errorString;
}
myHueHome.setTheHUERegisteredUser(hueUser);
}
// make call
for (int x = 0; x < aMultiUtil.getSetCount(); x++) {
if (x > 0 || i > 0) {
try {
Thread.sleep(aMultiUtil.getTheDelay());
} catch (InterruptedException e) {
// ignore
}
}
if (callItems[i].getDelay() != null && callItems[i].getDelay() > 0)
aMultiUtil.setTheDelay(callItems[i].getDelay());
else
aMultiUtil.setTheDelay(bridgeSettings.getButtonsleep());
responseString = doHttpRequest(
"http://" + deviceId.getIpAddress() + "/api/" + myHueHome.getTheHUERegisteredUser()
+ "/lights/" + deviceId.getDeviceId() + "/state",
HttpPut.METHOD_NAME, device.getContentType(), body, null);
if (responseString.contains("[{\"error\":"))
x = aMultiUtil.getSetCount();
}
if (responseString == null) {
log.warn("Error on calling url to change device state: " + url);
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Error on calling HUE to change device state\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else if (responseString.contains("[{\"error\":")) {
if(responseString.contains("unauthorized user")) {
myHueHome.setTheHUERegisteredUser(null);
hueUser = HueUtil.registerWithHue(httpClient, deviceId.getIpAddress(), device.getName(),
myHueHome.getTheHUERegisteredUser());
if (hueUser == null) {
return errorString;
}
myHueHome.setTheHUERegisteredUser(hueUser);
}
else
log.warn("Error occurred when calling Hue Passthru: " + responseString);
}
} else {
log.warn("No HUE home configured for HUE device passthru call for deviceID: " + device.getId());
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"No HUE configured\", \"parameter\": \"/lights/" + lightId
+ "state\"}}]";
}
} else if (callItems[i].getType() != null && callItems[i].getType().trim().equalsIgnoreCase(DeviceMapTypes.HARMONY_ACTIVITY[DeviceMapTypes.typeIndex])) { } else if (callItems[i].getType() != null && callItems[i].getType().trim().equalsIgnoreCase(DeviceMapTypes.HARMONY_ACTIVITY[DeviceMapTypes.typeIndex])) {
log.debug("executing HUE api request to change activity to Harmony: " + url); return homeManager.findHome(DeviceMapTypes.HARMONY_ACTIVITY[DeviceMapTypes.typeIndex]).deviceHandler(callItems[i], aMultiUtil, lightId, i, state, theStateChanges, stateHasBri, stateHasBriInc, device, body);
if (myHarmonyHome != null) {
RunActivity anActivity = aGsonHandler.fromJson(url, RunActivity.class);
HarmonyHandler myHarmony = myHarmonyHome.getHarmonyHandler(device.getTargetDevice());
if (myHarmony == null) {
log.warn("Should not get here, no harmony hub available");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no harmony hub available\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else {
for (int x = 0; x < aMultiUtil.getSetCount(); x++) {
if (x > 0 || i > 0) {
try {
Thread.sleep(aMultiUtil.getTheDelay());
} catch (InterruptedException e) {
// ignore
}
}
if (callItems[i].getDelay() != null && callItems[i].getDelay() > 0)
aMultiUtil.setTheDelay(callItems[i].getDelay());
else
aMultiUtil.setTheDelay(bridgeSettings.getButtonsleep());
myHarmony.startActivity(anActivity);
}
}
} else {
log.warn("Should not get here, no harmony configured");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no harmony configured\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
}
} else if (callItems[i].getType() != null && callItems[i].getType().trim().equalsIgnoreCase(DeviceMapTypes.HARMONY_BUTTON[DeviceMapTypes.typeIndex])) { } else if (callItems[i].getType() != null && callItems[i].getType().trim().equalsIgnoreCase(DeviceMapTypes.HARMONY_BUTTON[DeviceMapTypes.typeIndex])) {
log.debug("executing HUE api request to button press(es) to Harmony: " + url); return homeManager.findHome(DeviceMapTypes.HARMONY_BUTTON[DeviceMapTypes.typeIndex]).deviceHandler(callItems[i], aMultiUtil, lightId, i, state, theStateChanges, stateHasBri, stateHasBriInc, device, body);
if (myHarmonyHome != null) {
if (url.substring(0, 1).equalsIgnoreCase("{")) {
url = "[" + url + "]";
}
ButtonPress[] deviceButtons = aGsonHandler.fromJson(url, ButtonPress[].class);
HarmonyHandler myHarmony = myHarmonyHome.getHarmonyHandler(device.getTargetDevice());
if (myHarmony == null) {
log.warn("Should not get here, no harmony hub available");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no harmony hub available\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else {
if(deviceButtons.length > 1) {
Integer theCount = 1;
for(int z = 0; z < deviceButtons.length; z++) {
if(deviceButtons[z].getCount() != null && deviceButtons[z].getCount() > 0)
theCount = deviceButtons[z].getCount();
else
theCount = 1;
for(int y = 0; y < theCount; y++) {
if( y > 0 || z > 0) {
try {
Thread.sleep(aMultiUtil.getTheDelay());
} catch (InterruptedException e) {
// ignore
}
}
if (callItems[i].getDelay() != null && callItems[i].getDelay() > 0)
aMultiUtil.setTheDelay(callItems[i].getDelay());
else
aMultiUtil.setTheDelay(bridgeSettings.getButtonsleep());
log.debug("pressing button: " + deviceButtons[z].getDevice() + " - " + deviceButtons[z].getButton() + " - iteration: " + String.valueOf(z) + " - count: " + String.valueOf(y));
myHarmony.pressButton(deviceButtons[z]);
}
}
}
else {
for (int x = 0; x < aMultiUtil.getSetCount(); x++) {
if (x > 0 || i > 0) {
try {
Thread.sleep(aMultiUtil.getTheDelay());
} catch (InterruptedException e) {
// ignore
}
}
if (callItems[i].getDelay() != null && callItems[i].getDelay() > 0)
aMultiUtil.setTheDelay(callItems[i].getDelay());
else
aMultiUtil.setTheDelay(bridgeSettings.getButtonsleep());
log.debug("pressing button: " + deviceButtons[i].getDevice() + " - "
+ deviceButtons[i].getButton() + " - iteration: " + String.valueOf(i)
+ " - count: " + String.valueOf(x));
myHarmony.pressButton(deviceButtons[i]);
}
}
}
} else {
log.warn("Should not get here, no harmony configured");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no harmony configured\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
}
} else if (callItems[i].getType() != null && callItems[i].getType().trim().equalsIgnoreCase(DeviceMapTypes.NEST_HOMEAWAY[DeviceMapTypes.typeIndex])) { } else if (callItems[i].getType() != null && callItems[i].getType().trim().equalsIgnoreCase(DeviceMapTypes.NEST_HOMEAWAY[DeviceMapTypes.typeIndex])) {
return homeManager.findHome(DeviceMapTypes.NEST_HOMEAWAY[DeviceMapTypes.typeIndex]).deviceHandler(callItems[i], aMultiUtil, lightId, i, state, theStateChanges, stateHasBri, stateHasBriInc); return homeManager.findHome(DeviceMapTypes.NEST_HOMEAWAY[DeviceMapTypes.typeIndex]).deviceHandler(callItems[i], aMultiUtil, lightId, i, state, theStateChanges, stateHasBri, stateHasBriInc, device, body);
} else if (callItems[i].getType() != null && callItems[i].getType().trim().equalsIgnoreCase(DeviceMapTypes.NEST_THERMO_SET[DeviceMapTypes.typeIndex])) { } else if (callItems[i].getType() != null && callItems[i].getType().trim().equalsIgnoreCase(DeviceMapTypes.NEST_THERMO_SET[DeviceMapTypes.typeIndex])) {
return homeManager.findHome(DeviceMapTypes.NEST_THERMO_SET[DeviceMapTypes.typeIndex]).deviceHandler(callItems[i], aMultiUtil, lightId, i, state, theStateChanges, stateHasBri, stateHasBriInc); return homeManager.findHome(DeviceMapTypes.NEST_THERMO_SET[DeviceMapTypes.typeIndex]).deviceHandler(callItems[i], aMultiUtil, lightId, i, state, theStateChanges, stateHasBri, stateHasBriInc, device, body);
} else if (callItems[i].getType() != null && callItems[i].getType().trim().equalsIgnoreCase(DeviceMapTypes.MQTT_MESSAGE[DeviceMapTypes.typeIndex])) { } else if (callItems[i].getType() != null && callItems[i].getType().trim().equalsIgnoreCase(DeviceMapTypes.MQTT_MESSAGE[DeviceMapTypes.typeIndex])) {
log.debug("executing HUE api request to send message to MQTT broker: " + url); return homeManager.findHome(DeviceMapTypes.MQTT_MESSAGE[DeviceMapTypes.typeIndex]).deviceHandler(callItems[i], aMultiUtil, lightId, i, state, theStateChanges, stateHasBri, stateHasBriInc, device, body);
if (mqttHome != null) {
MQTTMessage[] mqttMessages = aGsonHandler.fromJson(BrightnessDecode.replaceIntensityValue(url,
BrightnessDecode.calculateIntensity(state, theStateChanges, stateHasBri, stateHasBriInc), false), MQTTMessage[].class);
MQTTHandler mqttHandler = mqttHome.getMQTTHandler(mqttMessages[i].getClientId());
if (mqttHandler == null) {
log.warn("Should not get here, no mqtt hanlder available");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no mqtt handler available\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
}
for (int x = 0; x < aMultiUtil.getSetCount(); x++) {
if (x > 0 || i > 0) {
try {
Thread.sleep(aMultiUtil.getTheDelay());
} catch (InterruptedException e) {
// ignore
}
}
if (callItems[i].getDelay() != null && callItems[i].getDelay() > 0)
aMultiUtil.setTheDelay(callItems[i].getDelay());
else
aMultiUtil.setTheDelay(bridgeSettings.getButtonsleep());
log.debug("publishing message: " + mqttMessages[i].getClientId() + " - "
+ mqttMessages[i].getTopic() + " - " + mqttMessages[i].getMessage()
+ " - iteration: " + String.valueOf(i) + " - count: " + String.valueOf(x));
mqttHandler.publishMessage(mqttMessages[i].getTopic(), mqttMessages[i].getMessage());
}
} else {
log.warn("Should not get here, no mqtt brokers configured");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no mqtt brokers configured\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
}
} else if (callItems[i].getType() != null && callItems[i].getType().trim().equalsIgnoreCase(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex])) { } else if (callItems[i].getType() != null && callItems[i].getType().trim().equalsIgnoreCase(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex])) {
responseString = homeManager.findHome(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex]).deviceHandler(callItems[i], aMultiUtil, lightId, i, state, theStateChanges, stateHasBri, stateHasBriInc); responseString = homeManager.findHome(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex]).deviceHandler(callItems[i], aMultiUtil, lightId, i, state, theStateChanges, stateHasBri, stateHasBriInc, device, body);
} else if (callItems[i].getType() != null && callItems[i].getType().trim().equalsIgnoreCase(DeviceMapTypes.EXEC_DEVICE[DeviceMapTypes.typeIndex])) { } else if (callItems[i].getType() != null && callItems[i].getType().trim().equalsIgnoreCase(DeviceMapTypes.EXEC_DEVICE[DeviceMapTypes.typeIndex])) {
responseString = homeManager.findHome(DeviceMapTypes.EXEC_DEVICE[DeviceMapTypes.typeIndex]).deviceHandler(callItems[i], aMultiUtil, lightId, i, state, theStateChanges, stateHasBri, stateHasBriInc); responseString = homeManager.findHome(DeviceMapTypes.EXEC_DEVICE[DeviceMapTypes.typeIndex]).deviceHandler(callItems[i], aMultiUtil, lightId, i, state, theStateChanges, stateHasBri, stateHasBriInc, device, body);
} else // This section allows the usage of http/tcp/udp/exec } else // This section allows the usage of http/tcp/udp/exec
// calls in a given set of items // calls in a given set of items
{ {
@@ -1313,7 +1007,7 @@ public class HueMulator {
state, theStateChanges, stateHasBri, stateHasBriInc, state, theStateChanges, stateHasBri, stateHasBriInc,
false); false);
// make call // make call
if (doHttpRequest(anUrl, device.getHttpVerb(), device.getContentType(), aBody, if (anHttpHandler.doHttpRequest(anUrl, device.getHttpVerb(), device.getContentType(), aBody,
theHeaders) == null) { theHeaders) == null) {
log.warn("Error on calling url to change device state: " + anUrl); log.warn("Error on calling url to change device state: " + anUrl);
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId

View File

@@ -3,7 +3,8 @@ package com.bwssystems.HABridge.hue;
import com.bwssystems.HABridge.api.CallItem; import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.api.hue.DeviceState; import com.bwssystems.HABridge.api.hue.DeviceState;
import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.api.hue.StateChangeBody;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
public interface HueMulatorHandler { public interface HueMulatorHandler {
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc); public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body);
} }

View File

@@ -12,6 +12,7 @@ import com.bwssystems.HABridge.DeviceMapTypes;
import com.bwssystems.HABridge.api.CallItem; import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.api.hue.DeviceState; import com.bwssystems.HABridge.api.hue.DeviceState;
import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.api.hue.StateChangeBody;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.BrightnessDecode; import com.bwssystems.HABridge.hue.BrightnessDecode;
import com.bwssystems.HABridge.hue.MultiCommandUtil; import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.bwssystems.nest.controller.Home; import com.bwssystems.nest.controller.Home;
@@ -103,7 +104,7 @@ public class NestHome implements com.bwssystems.HABridge.Home {
@Override @Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount,
DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc) { DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) {
String responseString = null; String responseString = null;
log.debug("executing HUE api request to set away for nest " + anItem.getType() + ": " + anItem.getItem().toString()); log.debug("executing HUE api request to set away for nest " + anItem.getType() + ": " + anItem.getItem().toString());
if(!validNest) { if(!validNest) {

View File

@@ -10,6 +10,7 @@ import com.bwssystems.HABridge.Home;
import com.bwssystems.HABridge.api.CallItem; import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.api.hue.DeviceState; import com.bwssystems.HABridge.api.hue.DeviceState;
import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.api.hue.StateChangeBody;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.BrightnessDecode; import com.bwssystems.HABridge.hue.BrightnessDecode;
import com.bwssystems.HABridge.hue.MultiCommandUtil; import com.bwssystems.HABridge.hue.MultiCommandUtil;
@@ -23,7 +24,7 @@ public class CommandHome implements Home {
@Override @Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount,
DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc) { DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) {
log.debug("Exec Request called with url: " + anItem.getItem().toString()); log.debug("Exec Request called with url: " + anItem.getItem().toString());
String responseString = null; String responseString = null;
String intermediate; String intermediate;

View File

@@ -15,6 +15,7 @@ import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.api.CallItem; import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.api.hue.DeviceState; import com.bwssystems.HABridge.api.hue.DeviceState;
import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.api.hue.StateChangeBody;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.MultiCommandUtil; import com.bwssystems.HABridge.hue.MultiCommandUtil;
public class HalHome implements Home { public class HalHome implements Home {
@@ -107,7 +108,7 @@ public class HalHome implements Home {
@Override @Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount,
DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc) { DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) {
log.info("device handler not implemented"); log.info("device handler not implemented");
return null; return null;
} }

View File

@@ -5,6 +5,7 @@ public class ButtonPress {
private String button; private String button;
private Integer delay; private Integer delay;
private Integer count; private Integer count;
private String hub;
public String getDevice() { public String getDevice() {
return device; return device;
} }
@@ -36,4 +37,10 @@ public class ButtonPress {
} }
return false; return false;
} }
public String getHub() {
return hub;
}
public void setHub(String hub) {
this.hub = hub;
}
} }

View File

@@ -18,7 +18,10 @@ import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.api.CallItem; import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.api.hue.DeviceState; import com.bwssystems.HABridge.api.hue.DeviceState;
import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.api.hue.StateChangeBody;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.MultiCommandUtil; import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.whistlingfish.harmony.config.Activity; import net.whistlingfish.harmony.config.Activity;
import net.whistlingfish.harmony.config.Device; import net.whistlingfish.harmony.config.Device;
@@ -28,6 +31,7 @@ public class HarmonyHome implements Home {
private Map<String, HarmonyServer> hubs; private Map<String, HarmonyServer> hubs;
private Boolean isDevMode; private Boolean isDevMode;
private Boolean validHarmony; private Boolean validHarmony;
private Gson aGsonHandler;
public HarmonyHome(BridgeSettingsDescriptor bridgeSettings) { public HarmonyHome(BridgeSettingsDescriptor bridgeSettings) {
super(); super();
@@ -114,10 +118,79 @@ public class HarmonyHome implements Home {
@Override @Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount,
DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc) { DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) {
// TODO Auto-generated method stub String responseString = null;
log.info("device handler not implemented"); log.debug("executing HUE api request to change " + anItem.getType() + " to Harmony: " + device.getTargetDevice());
return null; if(!validHarmony) {
log.warn("Should not get here, no harmony configured");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no harmony configured\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else {
if(anItem.getType().trim().equalsIgnoreCase(DeviceMapTypes.HARMONY_ACTIVITY[DeviceMapTypes.typeIndex]))
{
RunActivity anActivity = aGsonHandler.fromJson(anItem.getItem().toString(), RunActivity.class);
HarmonyHandler myHarmony = getHarmonyHandler(device.getTargetDevice());
if (myHarmony == null) {
log.warn("Should not get here, no harmony hub available");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no harmony hub available\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else {
for (int x = 0; x < aMultiUtil.getSetCount(); x++) {
if (x > 0 || iterationCount > 0) {
try {
Thread.sleep(aMultiUtil.getTheDelay());
} catch (InterruptedException e) {
// ignore
}
}
if (anItem.getDelay() != null && anItem.getDelay() > 0)
aMultiUtil.setTheDelay(anItem.getDelay());
else
aMultiUtil.setTheDelay(aMultiUtil.getDelayDefault());
myHarmony.startActivity(anActivity);
}
}
} else if(anItem.getType().trim().equalsIgnoreCase(DeviceMapTypes.HARMONY_BUTTON[DeviceMapTypes.typeIndex])) {
String url = anItem.getItem().toString();
if (url.substring(0, 1).equalsIgnoreCase("{")) {
url = "[" + url + "]";
}
ButtonPress[] deviceButtons = aGsonHandler.fromJson(url, ButtonPress[].class);
HarmonyHandler myHarmony = getHarmonyHandler(device.getTargetDevice());
if (myHarmony == null) {
log.warn("Should not get here, no harmony hub available");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no harmony hub available\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else {
Integer theCount = 1;
for(int z = 0; z < deviceButtons.length; z++) {
if(deviceButtons[z].getCount() != null && deviceButtons[z].getCount() > 0)
theCount = deviceButtons[z].getCount();
else
theCount = aMultiUtil.getSetCount();
for(int y = 0; y < theCount; y++) {
if( y > 0 || z > 0) {
try {
Thread.sleep(aMultiUtil.getTheDelay());
} catch (InterruptedException e) {
// ignore
}
}
if (anItem.getDelay() != null && anItem.getDelay() > 0)
aMultiUtil.setTheDelay(anItem.getDelay());
else
aMultiUtil.setTheDelay(aMultiUtil.getDelayDefault());
log.debug("pressing button: " + deviceButtons[z].getDevice() + " - " + deviceButtons[z].getButton() + " - iteration: " + String.valueOf(z) + " - count: " + String.valueOf(y));
myHarmony.pressButton(deviceButtons[z]);
}
}
}
}
}
return responseString;
} }
@Override @Override
@@ -128,6 +201,9 @@ public class HarmonyHome implements Home {
log.debug("No valid Harmony config"); log.debug("No valid Harmony config");
} else { } else {
hubs = new HashMap<String, HarmonyServer>(); hubs = new HashMap<String, HarmonyServer>();
aGsonHandler =
new GsonBuilder()
.create();
if(isDevMode) { if(isDevMode) {
NamedIP devModeIp = new NamedIP(); NamedIP devModeIp = new NamedIP();
devModeIp.setIp("10.10.10.10"); devModeIp.setIp("10.10.10.10");

View File

@@ -2,6 +2,7 @@ package com.bwssystems.harmony;
public class RunActivity { public class RunActivity {
private String name; private String name;
private String hub;
public String getName() { public String getName() {
return name; return name;
@@ -15,4 +16,12 @@ public class RunActivity {
return true; return true;
return false; return false;
} }
public String getHub() {
return hub;
}
public void setHub(String hub) {
this.hub = hub;
}
} }

View File

@@ -15,6 +15,7 @@ import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.api.CallItem; import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.api.hue.DeviceState; import com.bwssystems.HABridge.api.hue.DeviceState;
import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.api.hue.StateChangeBody;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.BrightnessDecode; import com.bwssystems.HABridge.hue.BrightnessDecode;
import com.bwssystems.HABridge.hue.MultiCommandUtil; import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.google.gson.Gson; import com.google.gson.Gson;
@@ -110,7 +111,7 @@ public class HassHome implements Home {
@Override @Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, DeviceState state, public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, DeviceState state,
StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc) { StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) {
String theReturn = null; String theReturn = null;
log.debug("executing HUE api request to send message to HomeAssistant: " + anItem.getItem().toString()); log.debug("executing HUE api request to send message to HomeAssistant: " + anItem.getItem().toString());
if(!validHass) { if(!validHass) {

View File

@@ -126,6 +126,16 @@ public class HTTPHandler {
return theContent; return theContent;
} }
public HttpClient getHttpClient() {
return httpClient;
}
public CloseableHttpClient getHttpclientSSL() {
return httpclientSSL;
}
public void closeHandler() { public void closeHandler() {
httpClient = null; httpClient = null;
try { try {

View File

@@ -9,6 +9,7 @@ import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.api.NameValue; import com.bwssystems.HABridge.api.NameValue;
import com.bwssystems.HABridge.api.hue.DeviceState; import com.bwssystems.HABridge.api.hue.DeviceState;
import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.api.hue.StateChangeBody;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.BrightnessDecode; import com.bwssystems.HABridge.hue.BrightnessDecode;
import com.bwssystems.HABridge.hue.MultiCommandUtil; import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.google.gson.Gson; import com.google.gson.Gson;
@@ -24,7 +25,7 @@ public class HTTPHome implements Home {
@Override @Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount,
DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc) { DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) {
String responseString = null; String responseString = null;
log.debug("executing HUE api request to Http " log.debug("executing HUE api request to Http "
+ (anItem.getHttpVerb() == null ? "GET" : anItem.getHttpVerb()) + ": " + (anItem.getHttpVerb() == null ? "GET" : anItem.getHttpVerb()) + ": "

View File

@@ -5,6 +5,7 @@ import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import org.apache.http.client.methods.HttpPut;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -16,13 +17,19 @@ import com.bwssystems.HABridge.api.hue.DeviceResponse;
import com.bwssystems.HABridge.api.hue.DeviceState; import com.bwssystems.HABridge.api.hue.DeviceState;
import com.bwssystems.HABridge.api.hue.HueApiResponse; import com.bwssystems.HABridge.api.hue.HueApiResponse;
import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.api.hue.StateChangeBody;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.MultiCommandUtil; import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.bwssystems.http.HTTPHandler;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class HueHome implements Home { public class HueHome implements Home {
private static final Logger log = LoggerFactory.getLogger(HueHome.class); private static final Logger log = LoggerFactory.getLogger(HueHome.class);
private Map<String, HueInfo> hues; private Map<String, HueInfo> hues;
private String theHUERegisteredUser; private String theHUERegisteredUser;
private Boolean validHue; private Boolean validHue;
private Gson aGsonHandler;
private HTTPHandler anHttpHandler;
public HueHome(BridgeSettingsDescriptor bridgeSettings) { public HueHome(BridgeSettingsDescriptor bridgeSettings) {
super(); super();
@@ -72,10 +79,58 @@ public class HueHome implements Home {
@Override @Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount,
DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc) { DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) {
// TODO Auto-generated method stub String responseString = null;
log.info("device handler not implemented"); String hueUser;
return null; HueDeviceIdentifier deviceId = aGsonHandler.fromJson(anItem.getItem(), HueDeviceIdentifier.class);
if (getTheHUERegisteredUser() == null) {
hueUser = HueUtil.registerWithHue(anHttpHandler, deviceId.getIpAddress(), device.getName(),
getTheHUERegisteredUser());
if (hueUser == null) {
return responseString;
}
setTheHUERegisteredUser(hueUser);
}
// make call
for (int x = 0; x < aMultiUtil.getSetCount(); x++) {
if (x > 0 || iterationCount > 0) {
try {
Thread.sleep(aMultiUtil.getTheDelay());
} catch (InterruptedException e) {
// ignore
}
}
if (anItem.getDelay() != null && anItem.getDelay() > 0)
aMultiUtil.setTheDelay(anItem.getDelay());
else
aMultiUtil.setTheDelay(aMultiUtil.getDelayDefault());
responseString = anHttpHandler.doHttpRequest(
"http://" + deviceId.getIpAddress() + "/api/" + getTheHUERegisteredUser()
+ "/lights/" + deviceId.getDeviceId() + "/state",
HttpPut.METHOD_NAME, "application/json", body, null);
if (responseString.contains("[{\"error\":"))
x = aMultiUtil.getSetCount();
}
if (responseString == null) {
log.warn("Error on calling Hue passthru to change device state: " + device.getName());
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Error on calling HUE to change device state\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else if (responseString.contains("[{\"error\":")) {
if(responseString.contains("unauthorized user")) {
setTheHUERegisteredUser(null);
hueUser = HueUtil.registerWithHue(anHttpHandler, deviceId.getIpAddress(), device.getName(),
getTheHUERegisteredUser());
if (hueUser == null) {
return responseString;
}
setTheHUERegisteredUser(hueUser);
}
else
log.warn("Error occurred when calling Hue Passthru: " + responseString);
}
return responseString;
} }
@Override @Override
@@ -91,13 +146,16 @@ public class HueHome implements Home {
hues.put(aHue.getName(), new HueInfo(aHue, this)); hues.put(aHue.getName(), new HueInfo(aHue, this));
} }
theHUERegisteredUser = null; theHUERegisteredUser = null;
aGsonHandler =
new GsonBuilder()
// .registerTypeAdapter(CallItem.class, new CallItemDeserializer())
.create();
} }
return this; return this;
} }
@Override @Override
public void closeHome() { public void closeHome() {
// TODO Auto-generated method stub anHttpHandler.closeHandler();
} }
} }

View File

@@ -1,12 +1,5 @@
package com.bwssystems.hue; package com.bwssystems.hue;
import java.io.IOException;
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.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;

View File

@@ -20,7 +20,7 @@ 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(HTTPHandler anHttpClient, String ipAddress, String aName, String theUser) { public static final String registerWithHue(HTTPHandler anHttpHandler, 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);
@@ -28,6 +28,7 @@ public class HueUtil {
StringEntity requestBody = new StringEntity(new Gson().toJson(theLogin), parsedContentType); StringEntity requestBody = new StringEntity(new Gson().toJson(theLogin), parsedContentType);
HttpResponse response = null; HttpResponse response = null;
postRequest.setEntity(requestBody); postRequest.setEntity(requestBody);
HttpClient anHttpClient = anHttpHandler.getHttpClient();
try { try {
response = anHttpClient.execute(postRequest); response = anHttpClient.execute(postRequest);
log.debug("POST execute on URL responded: " + response.getStatusLine().getStatusCode()); log.debug("POST execute on URL responded: " + response.getStatusLine().getStatusCode());

View File

@@ -14,12 +14,17 @@ import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.api.CallItem; import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.api.hue.DeviceState; import com.bwssystems.HABridge.api.hue.DeviceState;
import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.api.hue.StateChangeBody;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.BrightnessDecode;
import com.bwssystems.HABridge.hue.MultiCommandUtil; import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class MQTTHome implements Home { public class MQTTHome implements Home {
private static final Logger log = LoggerFactory.getLogger(MQTTHome.class); private static final Logger log = LoggerFactory.getLogger(MQTTHome.class);
private Map<String, MQTTHandler> handlers; private Map<String, MQTTHandler> handlers;
private Boolean validMqtt; private Boolean validMqtt;
private Gson aGsonHandler;
public MQTTHome(BridgeSettingsDescriptor bridgeSettings) { public MQTTHome(BridgeSettingsDescriptor bridgeSettings) {
super(); super();
@@ -72,10 +77,41 @@ public class MQTTHome implements Home {
@Override @Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount,
DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc) { DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) {
// TODO Auto-generated method stub String responseString = null;
log.info("device handler not implemented"); log.debug("executing HUE api request to send message to MQTT broker: " + anItem.getItem().toString());
return null; if (validMqtt) {
MQTTMessage[] mqttMessages = aGsonHandler.fromJson(BrightnessDecode.replaceIntensityValue(anItem.getItem().toString(),
BrightnessDecode.calculateIntensity(state, theStateChanges, stateHasBri, stateHasBriInc), false), MQTTMessage[].class);
Integer theCount = 1;
for(int z = 0; z < mqttMessages.length; z++) {
if(mqttMessages[z].getCount() != null && mqttMessages[z].getCount() > 0)
theCount = mqttMessages[z].getCount();
else
theCount = aMultiUtil.getSetCount();
for(int y = 0; y < theCount; y++) {
if( y > 0 || z > 0) {
log.debug("publishing message: " + mqttMessages[y].getClientId() + " - "
+ mqttMessages[y].getTopic() + " - " + mqttMessages[y].getMessage()
+ " - iteration: " + String.valueOf(iterationCount) + " - count: " + String.valueOf(z));
MQTTHandler mqttHandler = getMQTTHandler(mqttMessages[y].getClientId());
if (mqttHandler == null) {
log.warn("Should not get here, no mqtt hanlder available");
} else {
mqttHandler.publishMessage(mqttMessages[y].getTopic(), mqttMessages[y].getMessage());
}
}
}
}
} else {
log.warn("Should not get here, no mqtt brokers configured");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no mqtt brokers configured\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
}
return responseString;
} }
@Override @Override
@@ -84,6 +120,9 @@ public class MQTTHome implements Home {
if(!validMqtt) { if(!validMqtt) {
log.debug("No MQTT configuration"); log.debug("No MQTT configuration");
} else { } else {
aGsonHandler =
new GsonBuilder()
.create();
handlers = new HashMap<String, MQTTHandler>(); handlers = new HashMap<String, MQTTHandler>();
Iterator<NamedIP> theList = bridgeSettings.getMqttaddress().getDevices().iterator(); Iterator<NamedIP> theList = bridgeSettings.getMqttaddress().getDevices().iterator();
while(theList.hasNext()) { while(theList.hasNext()) {

View File

@@ -5,6 +5,7 @@ import com.bwssystems.HABridge.Home;
import com.bwssystems.HABridge.api.CallItem; import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.api.hue.DeviceState; import com.bwssystems.HABridge.api.hue.DeviceState;
import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.api.hue.StateChangeBody;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.MultiCommandUtil; import com.bwssystems.HABridge.hue.MultiCommandUtil;
public class TCPHome implements Home { public class TCPHome implements Home {
@@ -15,7 +16,7 @@ public class TCPHome implements Home {
@Override @Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount,
DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc) { DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return null; return null;
} }

View File

@@ -5,6 +5,7 @@ import com.bwssystems.HABridge.Home;
import com.bwssystems.HABridge.api.CallItem; import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.api.hue.DeviceState; import com.bwssystems.HABridge.api.hue.DeviceState;
import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.api.hue.StateChangeBody;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.MultiCommandUtil; import com.bwssystems.HABridge.hue.MultiCommandUtil;
public class UDPHome implements Home { public class UDPHome implements Home {
@@ -15,7 +16,7 @@ public class UDPHome implements Home {
@Override @Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount,
DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc) { DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return null; return null;
} }

View File

@@ -16,6 +16,7 @@ import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.api.CallItem; import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.api.hue.DeviceState; import com.bwssystems.HABridge.api.hue.DeviceState;
import com.bwssystems.HABridge.api.hue.StateChangeBody; import com.bwssystems.HABridge.api.hue.StateChangeBody;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.MultiCommandUtil; import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.bwssystems.luupRequests.Device; import com.bwssystems.luupRequests.Device;
import com.bwssystems.luupRequests.Scene; import com.bwssystems.luupRequests.Scene;
@@ -74,7 +75,7 @@ public class VeraHome implements Home {
@Override @Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount, public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int iterationCount,
DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc) { DeviceState state, StateChangeBody theStateChanges, boolean stateHasBri, boolean stateHasBriInc, DeviceDescriptor device, String body) {
// Not a device handler // Not a device handler
return null; return null;
} }