Skip to content

Commit

Permalink
Merge pull request #499 from bwssytems/NewConnectors1
Browse files Browse the repository at this point in the history
New connectors and fixes

Fixes #124 
Fixes #396 
Fixes #466 
Fixes #483 
Fixes #488
  • Loading branch information
bwssytems authored Feb 25, 2017
2 parents 2f456aa + 54e9303 commit c7cf48b
Show file tree
Hide file tree
Showing 34 changed files with 1,063 additions and 319 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ 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.1.4.jar
java -jar ha-bridge-4.2.0.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.1.4.jar is in your /home/pi/habridge directory.
Create the directory and make sure that ha-bridge-4.2.0.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.1.4/ha-bridge-4.1.4.jar
pi@raspberrypi:~/habridge $ wget https://github.com/bwssytems/ha-bridge/releases/download/v4.2.0/ha-bridge-4.2.0.jar
```
Create the directory and make sure that ha-bridge-4.1.4.jar is in your /home/pi/habridge directory.
Create the directory and make sure that ha-bridge-4.2.0.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.1.4/ha-bridge-4.1.4.jar
pi@raspberrypi:~/habridge $ wget https://github.com/bwssytems/ha-bridge/releases/download/v4.2.0/ha-bridge-4.2.0.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
Expand All @@ -69,7 +69,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.1.4.jar
ExecStart=/usr/bin/java -jar -Dconfig.file=/home/pi/habridge/data/habridge.config /home/pi/habridge/ha-bridge-4.2.0.jar
[Install]
WantedBy=multi-user.target
Expand Down Expand Up @@ -104,7 +104,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.1.4.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.2.0.jar > /home/pi/habridge/habridge-log.txt 2>&1 &
chmod 777 /home/pi/habridge/habridge-log.txt
```
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.bwssystems.HABridge</groupId>
<artifactId>ha-bridge</artifactId>
<version>4.1.4</version>
<version>4.2.0</version>
<packaging>jar</packaging>

<name>HA Bridge</name>
Expand Down Expand Up @@ -121,6 +121,11 @@
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>com.github.bwssytems</groupId>
<artifactId>lifx-sdk-java</artifactId>
<version>2.1.6</version>
</dependency>
</dependencies>

<build>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/bwssystems/HABridge/BridgeSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public void buildSettings() {
theBridgeSettings.setMqttconfigured(theBridgeSettings.isValidMQTT());
theBridgeSettings.setHassconfigured(theBridgeSettings.isValidHass());
theBridgeSettings.setDomoticzconfigured(theBridgeSettings.isValidDomoticz());
// Lifx is either configured or not, so it does not need an update.
if(serverPortOverride != null)
theBridgeSettings.setServerPort(serverPortOverride);
if(serverIpOverride != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class BridgeSettingsDescriptor {
private String hubversion;
private IpList domoticzaddress;
private boolean domoticzconfigured;
private boolean lifxconfigured;

public BridgeSettingsDescriptor() {
super();
Expand Down Expand Up @@ -263,6 +264,12 @@ public boolean isDomoticzconfigured() {
public void setDomoticzconfigured(boolean domoticzconfigured) {
this.domoticzconfigured = domoticzconfigured;
}
public boolean isLifxconfigured() {
return lifxconfigured;
}
public void setLifxconfigured(boolean lifxconfigured) {
this.lifxconfigured = lifxconfigured;
}
public Boolean isValidVera() {
if(this.getVeraAddress() == null || this.getVeraAddress().getDevices().size() <= 0)
return false;
Expand Down Expand Up @@ -328,4 +335,7 @@ public Boolean isValidDomoticz() {
return false;
return true;
}
public Boolean isValidLifx() {
return this.isLifxconfigured();
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/bwssystems/HABridge/DeviceMapTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class DeviceMapTypes {
public final static String[] UDP_DEVICE_COMPAT = { "UDP", "UDP Device"};
public final static String[] HTTP_DEVICE = { "httpDevice", "HTTP Device"};
public final static String[] DOMOTICZ_DEVICE = { "domoticzDevice", "Domoticz Device"};
public final static String[] LIFX_DEVICE = { "lifxDevice", "LIFX Device"};

public final static int typeIndex = 0;
public final static int displayIndex = 1;
Expand All @@ -46,6 +47,7 @@ public DeviceMapTypes() {
deviceMapTypes.add(HASS_DEVICE);
deviceMapTypes.add(HTTP_DEVICE);
deviceMapTypes.add(HUE_DEVICE);
deviceMapTypes.add(LIFX_DEVICE);
deviceMapTypes.add(MQTT_MESSAGE);
deviceMapTypes.add(NEST_HOMEAWAY);
deviceMapTypes.add(NEST_THERMO_SET);
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/bwssystems/HABridge/HomeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.bwssystems.HABridge.plugins.hass.HassHome;
import com.bwssystems.HABridge.plugins.http.HTTPHome;
import com.bwssystems.HABridge.plugins.hue.HueHome;
import com.bwssystems.HABridge.plugins.lifx.LifxHome;
import com.bwssystems.HABridge.plugins.mqtt.MQTTHome;
import com.bwssystems.HABridge.plugins.tcp.TCPHome;
import com.bwssystems.HABridge.plugins.udp.UDPHome;
Expand Down Expand Up @@ -71,7 +72,6 @@ public void buildHomes(BridgeSettingsDescriptor bridgeSettings, UDPDatagramSende
homeList.put(DeviceMapTypes.CUSTOM_DEVICE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.VERA_DEVICE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.VERA_SCENE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.DOMOTICZ_DEVICE[DeviceMapTypes.typeIndex], aHome);
//setup the tcp handler Home
aHome = new TCPHome(bridgeSettings);
homeList.put(DeviceMapTypes.TCP_DEVICE[DeviceMapTypes.typeIndex], aHome);
Expand All @@ -80,13 +80,18 @@ public void buildHomes(BridgeSettingsDescriptor bridgeSettings, UDPDatagramSende
aHome = new UDPHome(bridgeSettings, aUdpDatagramSender);
homeList.put(DeviceMapTypes.UDP_DEVICE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.UDP_DEVICE_COMPAT[DeviceMapTypes.typeIndex], aHome);

// Setup Vera Home if available
aHome = new VeraHome(bridgeSettings);
resourceList.put(DeviceMapTypes.VERA_DEVICE[DeviceMapTypes.typeIndex], aHome);
resourceList.put(DeviceMapTypes.VERA_SCENE[DeviceMapTypes.typeIndex], aHome);
//setup the HomeAssistant configuration if available
//setup the Domoticz configuration if available
aHome = new DomoticzHome(bridgeSettings);
homeList.put(DeviceMapTypes.DOMOTICZ_DEVICE[DeviceMapTypes.typeIndex], aHome);
resourceList.put(DeviceMapTypes.DOMOTICZ_DEVICE[DeviceMapTypes.typeIndex], aHome);
//setup the Lifx configuration if available
aHome = new LifxHome(bridgeSettings);
resourceList.put(DeviceMapTypes.LIFX_DEVICE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.LIFX_DEVICE[DeviceMapTypes.typeIndex], aHome);
}

public Home findHome(String type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ private void setupEndpoints() {
return homeManager.findResource(DeviceMapTypes.DOMOTICZ_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.DOMOTICZ_DEVICE[DeviceMapTypes.typeIndex]);
}, new JsonTransformer());

get (API_CONTEXT + "/lifx/devices", "application/json", (request, response) -> {
log.debug("Get LIFX devices");
response.status(HttpStatus.SC_OK);
return homeManager.findResource(DeviceMapTypes.LIFX_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.LIFX_DEVICE[DeviceMapTypes.typeIndex]);
}, new JsonTransformer());

get (API_CONTEXT + "/map/types", "application/json", (request, response) -> {
log.debug("Get map types");
return new DeviceMapTypes().getDeviceMapTypes();
Expand Down
17 changes: 3 additions & 14 deletions src/main/java/com/bwssystems/HABridge/hue/BrightnessDecode.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package com.bwssystems.HABridge.hue;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.DatatypeConverter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.java.dev.eval.Expression;

public class BrightnessDecode {
Expand Down Expand Up @@ -49,9 +44,7 @@ public static String replaceIntensityValue(String request, int intensity, boolea
}
if (request.contains(INTENSITY_BYTE)) {
if (isHex) {
BigInteger bigInt = BigInteger.valueOf(intensity);
byte[] theBytes = bigInt.toByteArray();
String hexValue = DatatypeConverter.printHexBinary(theBytes);
String hexValue = Integer.toHexString(intensity);
request = request.replace(INTENSITY_BYTE, hexValue);
} else {
String intensityByte = String.valueOf(intensity);
Expand All @@ -60,9 +53,7 @@ public static String replaceIntensityValue(String request, int intensity, boolea
} else if (request.contains(INTENSITY_PERCENT)) {
int percentBrightness = (int) Math.round(intensity / 255.0 * 100);
if (isHex) {
BigInteger bigInt = BigInteger.valueOf(percentBrightness);
byte[] theBytes = bigInt.toByteArray();
String hexValue = DatatypeConverter.printHexBinary(theBytes);
String hexValue = Integer.toHexString(percentBrightness);
request = request.replace(INTENSITY_PERCENT, hexValue);
} else {
String intensityPercent = String.valueOf(percentBrightness);
Expand All @@ -81,9 +72,7 @@ public static String replaceIntensityValue(String request, int intensity, boolea
BigDecimal result = exp.eval(variables);
Integer endResult = Math.round(result.floatValue());
if (isHex) {
BigInteger bigInt = BigInteger.valueOf(endResult);
byte[] theBytes = bigInt.toByteArray();
String hexValue = DatatypeConverter.printHexBinary(theBytes);
String hexValue = Integer.toHexString(endResult);
request = request.replace(INTENSITY_MATH + mathDescriptor + INTENSITY_MATH_CLOSE, hexValue);
} else {
request = request.replace(INTENSITY_MATH + mathDescriptor + INTENSITY_MATH_CLOSE,
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/bwssystems/HABridge/hue/HueMulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.bwssystems.HABridge.api.hue.StateChangeBody;
import com.bwssystems.HABridge.api.hue.WhitelistEntry;
import com.bwssystems.HABridge.dao.*;
import com.bwssystems.HABridge.plugins.hue.HueDeviceIdentifier;
import com.bwssystems.HABridge.plugins.hue.HueHome;
import com.bwssystems.HABridge.util.JsonTransformer;
import com.google.gson.Gson;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import java.util.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.api.NameValue;
import com.bwssystems.HABridge.plugins.http.HTTPHandler;
import com.google.gson.Gson;

Expand All @@ -16,32 +17,33 @@ public class DomoticzHandler {
private static final String GET_REQUEST = "/json.htm?type=";
private static final String DEVICES_TYPE = "devices";
private static final String SCENES_TYPE = "scenes";
private static final String FILTER_USED = "&used=";
private HTTPHandler httpClient;
private static final String FILTER_USED = "&used=true";
private NamedIP domoticzAddress;

public DomoticzHandler(NamedIP addressName) {
super();
httpClient = new HTTPHandler();
domoticzAddress = addressName;
}

public List<DomoticzDevice> getDevices() {
return getDomoticzDevices(GET_REQUEST, DEVICES_TYPE, FILTER_USED);
public List<DomoticzDevice> getDevices(HTTPHandler httpClient) {
return getDomoticzDevices(GET_REQUEST, DEVICES_TYPE, FILTER_USED, httpClient);
}

public List<DomoticzDevice> getScenes() {
return getDomoticzDevices(GET_REQUEST, SCENES_TYPE, null);
public List<DomoticzDevice> getScenes(HTTPHandler httpClient) {
return getDomoticzDevices(GET_REQUEST, SCENES_TYPE, null, httpClient);
}

private List<DomoticzDevice> getDomoticzDevices(String rootRequest, String type, String postpend) {
private List<DomoticzDevice> getDomoticzDevices(String rootRequest, String type, String postpend, HTTPHandler httpClient) {
Devices theDomoticzApiResponse = null;
List<DomoticzDevice> deviceList = null;

String theUrl = null;
String theData;
theUrl = "http://" + domoticzAddress.getIp() + ":" + domoticzAddress.getPort() + rootRequest + type;
theData = httpClient.doHttpRequest(theUrl, null, null, null, null);
if(postpend != null && !postpend.isEmpty())
theUrl = buildUrl(rootRequest + type + postpend);
else
theUrl = buildUrl(rootRequest + type);
theData = httpClient.doHttpRequest(theUrl, null, null, null, buildHeaders());
if(theData != null) {
log.debug("GET " + type + " DomoticzApiResponse - data: " + theData);
theDomoticzApiResponse = new Gson().fromJson(theData, Devices.class);
Expand Down Expand Up @@ -70,6 +72,44 @@ private List<DomoticzDevice> getDomoticzDevices(String rootRequest, String type,
return deviceList;
}

public String buildUrl(String thePayload) {
String newUrl = null;

if(thePayload != null && !thePayload.isEmpty()) {
if(domoticzAddress.getSecure() != null && domoticzAddress.getSecure())
newUrl = "https://";
else
newUrl = "http://";

newUrl = newUrl + domoticzAddress.getIp();

if(domoticzAddress.getPort() != null && !domoticzAddress.getPort().isEmpty())
newUrl = newUrl + ":" + domoticzAddress.getPort();

if(thePayload.startsWith("/"))
newUrl = newUrl + thePayload;
else
newUrl = newUrl + "/" + thePayload;
}

return newUrl;
}

public NameValue[] buildHeaders() {
NameValue[] headers = null;

if(domoticzAddress.getUsername() != null && !domoticzAddress.getUsername().isEmpty()
&& domoticzAddress.getPassword() != null && !domoticzAddress.getPassword().isEmpty()) {
NameValue theAuth = new NameValue();
theAuth.setName("Authorization");
String encoding = Base64.getEncoder().encodeToString((domoticzAddress.getUsername() + ":" + domoticzAddress.getPassword()).getBytes());
theAuth.setValue("Basic " + encoding);
headers = new NameValue[1];
headers[0] = theAuth;
}

return headers;
}
public NamedIP getDomoticzAddress() {
return domoticzAddress;
}
Expand Down
Loading

0 comments on commit c7cf48b

Please sign in to comment.