From c9d55e26aca23ef5b2160bd2c8ac0890b8251585 Mon Sep 17 00:00:00 2001 From: Admin Date: Wed, 25 Jan 2017 15:19:46 -0600 Subject: [PATCH] Fix immediate issues for JSON decoding Fixes #391 Fixes #392 Fixes #398 --- README.md | 12 +++++++----- pom.xml | 2 +- src/main/java/com/bwssystems/HABridge/NamedIP.java | 7 +++++++ .../java/com/bwssystems/HABridge/hue/HueMulator.java | 11 ++++++----- .../HABridge/plugins/exec/CommandHome.java | 2 +- .../com/bwssystems/HABridge/plugins/hal/HalHome.java | 7 ++++--- .../HABridge/plugins/harmony/HarmonyHome.java | 7 ++++++- .../bwssystems/HABridge/plugins/http/HTTPHome.java | 7 ++++--- .../bwssystems/HABridge/plugins/mqtt/MQTTHome.java | 10 ++++++---- 9 files changed, 42 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index e110671..e16bcf9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # ha-bridge Emulates Philips Hue api to other home automation gateways such as an Amazon Echo or Google Home. The Bridge handles basic commands such as "On", "Off" and "brightness" commands of the hue protocol. This bridge can control most devices that have a distinct API. +**SECURITY RISK: If you are unsure on how this software operates and what it exposes to your netowrk, please make sure you understand that it can allow root access to your sytem. It is best practice to not open this to the itnernet through your router as there are no security protocols in place to protect the system. The Licenese agreement states specifically that you use this at your own risk.** + **ATTENTION: This requires a physical Amazon Echo, Dot or Tap and does not work with prototype devices built using the Alexa Voice Service e.g. Amazon's Alexa AVS Sample App and Sam Machin's AlexaPi. The AVS version does not have any capability for Hue Bridge discovery!** **NOTE: This software does require the user to have knwoledge on how processes run on Linux or Windows with java. Also, an understanding of networking basics will help as well. This system reveives upnp udp multicast packets from devices to be found, so that is some thing to understand. Please make sure you have all your devices use static IP addresses from your router. Most all questions have been answered already. PLEASE USE GOOGLE TO FIND YOUR ANSWERS!** @@ -31,16 +33,16 @@ ATTENTION: This requires JDK 1.8 to run ATTENTION: Due to port 80 being the default, Linux restricts this to super user. Use the instructions below. ``` -java -jar ha-bridge-4.0.2.jar +java -jar ha-bridge-4.0.3.jar ``` ### Automation on Linux systems To have this configured and running automatically there are a few resources to use. One is using Docker and a docker container has been built for this and can be gotten here: https://github.com/aptalca/docker-ha-bridge -Create the directory and make sure that ha-bridge-4.0.2.jar is in your /home/pi/habridge directory. +Create the directory and make sure that ha-bridge-4.0.3.jar is in your /home/pi/habridge directory. ``` pi@raspberrypi:~ $ mkdir habridge pi@raspberrypi:~ $ cd habridge -pi@raspberrypi:~/habridge $ wget https://github.com/bwssytems/ha-bridge/releases/download/v4.0.2/ha-bridge-4.0.2.jar +pi@raspberrypi:~/habridge $ wget https://github.com/bwssytems/ha-bridge/releases/download/v4.0.3/ha-bridge-4.0.3.jar ``` #### System Control Setup on a pi (preferred) For next gen Linux systems (this includes the Raspberry Pi), here is a systemctl unit file that you can install. Here is a link on how to do this: https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units @@ -59,7 +61,7 @@ After=network.target [Service] Type=simple -ExecStart=/usr/bin/java -jar -Dconfig.file=/home/pi/habridge/data/habridge.config /home/pi/habridge/ha-bridge-4.0.2.jar +ExecStart=/usr/bin/java -jar -Dconfig.file=/home/pi/habridge/data/habridge.config /home/pi/habridge/ha-bridge-4.0.3.jar [Install] WantedBy=multi-user.target @@ -94,7 +96,7 @@ Then cut and past this, modify any locations that are not correct ``` cd /home/pi/habridge rm /home/pi/habridge/habridge-log.txt -nohup java -jar -Dconfig.file=/home/pi/habridge/data/habridge.config /home/pi/habridge/ha-bridge-4.0.2.jar > /home/pi/habridge/habridge-log.txt 2>&1 & +nohup java -jar -Dconfig.file=/home/pi/habridge/data/habridge.config /home/pi/habridge/ha-bridge-4.0.3.jar > /home/pi/habridge/habridge-log.txt 2>&1 & chmod 777 /home/pi/habridge/habridge-log.txt ``` Exit and save the file with ctrl-X and follow the prompts and then execute on the command line: diff --git a/pom.xml b/pom.xml index 59ffd55..2def510 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.bwssystems.HABridge ha-bridge - 4.0.2 + 4.0.3 jar HA Bridge diff --git a/src/main/java/com/bwssystems/HABridge/NamedIP.java b/src/main/java/com/bwssystems/HABridge/NamedIP.java index 38a6d7f..145f798 100644 --- a/src/main/java/com/bwssystems/HABridge/NamedIP.java +++ b/src/main/java/com/bwssystems/HABridge/NamedIP.java @@ -6,6 +6,7 @@ public class NamedIP { private String port; private String username; private String password; + private Boolean secure; public String getName() { return name; @@ -37,4 +38,10 @@ public class NamedIP { public void setPassword(String password) { this.password = password; } + public Boolean getSecure() { + return secure; + } + public void setSecure(Boolean secure) { + this.secure = secure; + } } diff --git a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java index 4618abb..ae483aa 100644 --- a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java +++ b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java @@ -524,10 +524,11 @@ public class HueMulator { deviceResponseMap = new HashMap(); for (DeviceDescriptor device : deviceList) { DeviceResponse deviceResponse = null; - if ((device.getMapType() != null && device.getMapType().equalsIgnoreCase(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex]))) { - HueDeviceIdentifier deviceId = aGsonHandler.fromJson(device.getOnUrl(), HueDeviceIdentifier.class); - deviceResponse = myHueHome.getHueDeviceInfo(deviceId, device); - } + // In the multi command context, this is not valid anymore +// if ((device.getMapType() != null && device.getMapType().equalsIgnoreCase(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex]))) { +// HueDeviceIdentifier deviceId = aGsonHandler.fromJson(device.getOnUrl(), HueDeviceIdentifier.class); +// deviceResponse = myHueHome.getHueDeviceInfo(deviceId, device); +// } if (deviceResponse == null) deviceResponse = DeviceResponse.createResponse(device); @@ -730,7 +731,7 @@ public class HueMulator { } // code for backwards compatibility - if(!(device.getMapType() != null && device.getMapType().equalsIgnoreCase(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex]))) { + if(device.getMapType() != null && device.getMapType().equalsIgnoreCase(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex])) { if(url == null) url = device.getOnUrl(); } diff --git a/src/main/java/com/bwssystems/HABridge/plugins/exec/CommandHome.java b/src/main/java/com/bwssystems/HABridge/plugins/exec/CommandHome.java index f87c48b..2e60e9e 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/exec/CommandHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/exec/CommandHome.java @@ -25,7 +25,7 @@ public class CommandHome implements Home { log.debug("Exec Request called with url: " + anItem.getItem().getAsString()); String responseString = null; String intermediate; - if (anItem.getItem().toString().contains("exec://")) + if (anItem.getItem().getAsString().contains("exec://")) intermediate = anItem.getItem().getAsString().substring(anItem.getItem().getAsString().indexOf("://") + 3); else intermediate = anItem.getItem().getAsString(); diff --git a/src/main/java/com/bwssystems/HABridge/plugins/hal/HalHome.java b/src/main/java/com/bwssystems/HABridge/plugins/hal/HalHome.java index a153bf7..f57e44f 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/hal/HalHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/hal/HalHome.java @@ -125,9 +125,10 @@ public class HalHome implements Home { String anUrl = BrightnessDecode.calculateReplaceIntensityValue(anItem.getItem().getAsString(), intensity, targetBri, targetBriInc, false); - String aBody; - aBody = BrightnessDecode.calculateReplaceIntensityValue(anItem.getHttpBody(), - intensity, targetBri, targetBriInc, false); + String aBody = null; + if(anItem.getHttpBody()!= null && !anItem.getHttpBody().isEmpty()) + aBody = BrightnessDecode.calculateReplaceIntensityValue(anItem.getHttpBody(), + intensity, targetBri, targetBriInc, false); // make call if (anHttpHandler.doHttpRequest(anUrl, anItem.getHttpVerb(), anItem.getContentType(), aBody, new Gson().fromJson(anItem.getHttpHeaders(), NameValue[].class)) == null) { diff --git a/src/main/java/com/bwssystems/HABridge/plugins/harmony/HarmonyHome.java b/src/main/java/com/bwssystems/HABridge/plugins/harmony/HarmonyHome.java index 8038a66..592cabb 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/harmony/HarmonyHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/harmony/HarmonyHome.java @@ -152,7 +152,12 @@ public class HarmonyHome implements Home { myHarmony.startActivity(anActivity); } } else if(anItem.getType().trim().equalsIgnoreCase(DeviceMapTypes.HARMONY_BUTTON[DeviceMapTypes.typeIndex])) { - String url = anItem.getItem().toString(); + String url = null; + if(anItem.getItem().isJsonObject()) { + url = aGsonHandler.toJson(anItem.getItem()); + } else + url = anItem.getItem().getAsString(); + if (url.substring(0, 1).equalsIgnoreCase("{")) { url = "[" + url + "]"; } diff --git a/src/main/java/com/bwssystems/HABridge/plugins/http/HTTPHome.java b/src/main/java/com/bwssystems/HABridge/plugins/http/HTTPHome.java index ff561bb..bbc2db9 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/http/HTTPHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/http/HTTPHome.java @@ -46,9 +46,10 @@ public class HTTPHome implements Home { String anUrl = BrightnessDecode.calculateReplaceIntensityValue(anItem.getItem().getAsString(), intensity, targetBri, targetBriInc, false); - String aBody; - aBody = BrightnessDecode.calculateReplaceIntensityValue(anItem.getHttpBody(), - intensity, targetBri, targetBriInc, false); + String aBody = null; + if(anItem.getHttpBody()!= null && !anItem.getHttpBody().isEmpty()) + aBody = BrightnessDecode.calculateReplaceIntensityValue(anItem.getHttpBody(), + intensity, targetBri, targetBriInc, false); // make call if (anHttpHandler.doHttpRequest(anUrl, anItem.getHttpVerb(), anItem.getContentType(), aBody, new Gson().fromJson(anItem.getHttpHeaders(), NameValue[].class)) == null) { diff --git a/src/main/java/com/bwssystems/HABridge/plugins/mqtt/MQTTHome.java b/src/main/java/com/bwssystems/HABridge/plugins/mqtt/MQTTHome.java index 4716199..0831407 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/mqtt/MQTTHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/mqtt/MQTTHome.java @@ -80,11 +80,13 @@ public class MQTTHome implements Home { log.debug("executing HUE api request to send message to MQTT broker: " + anItem.getItem().toString()); if (validMqtt) { String mqttObject = null; - if(anItem.getItem().isJsonObject()) - mqttObject = BrightnessDecode.calculateReplaceIntensityValue(anItem.getItem().getAsString(), - intensity, targetBri, targetBriInc, false); + if(anItem.getItem().isJsonObject() || anItem.getItem().isJsonArray()) { + String theItem = aGsonHandler.toJson(anItem.getItem()); + mqttObject = BrightnessDecode.calculateReplaceIntensityValue(theItem, + intensity, targetBri, targetBriInc, false); + } else - mqttObject = BrightnessDecode.calculateReplaceIntensityValue(anItem.getItem().toString(), + mqttObject = BrightnessDecode.calculateReplaceIntensityValue(anItem.getItem().getAsString(), intensity, targetBri, targetBriInc, false); if (mqttObject.substring(0, 1).equalsIgnoreCase("{")) mqttObject = "[" + mqttObject + "]";