diff --git a/pom.xml b/pom.xml
index a55b325..f9fa477 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
com.bwssystems.HABridge
ha-bridge
- 5.1.0
+ 5.1.0a
jar
HA Bridge
diff --git a/src/main/java/com/bwssystems/HABridge/BridgeSettingsDescriptor.java b/src/main/java/com/bwssystems/HABridge/BridgeSettingsDescriptor.java
index 7da1551..3c1f5ca 100644
--- a/src/main/java/com/bwssystems/HABridge/BridgeSettingsDescriptor.java
+++ b/src/main/java/com/bwssystems/HABridge/BridgeSettingsDescriptor.java
@@ -87,6 +87,9 @@ public class BridgeSettingsDescriptor {
@SerializedName("somfyaddress")
@Expose
private IpList somfyaddress;
+ @SerializedName("openhabaddress")
+ @Expose
+ private IpList openhabaddress;
@SerializedName("hubversion")
@Expose
private String hubversion;
@@ -113,6 +116,7 @@ public class BridgeSettingsDescriptor {
private boolean somfyconfigured;
private boolean lifxconfigured;
private boolean homewizardconfigured;
+ private boolean openhabconfigured;
// Deprecated settings
private String haltoken;
@@ -136,6 +140,7 @@ public class BridgeSettingsDescriptor {
this.domoticzconfigured = false;
this.homewizardconfigured = false;
this.lifxconfigured = false;
+ this.openhabconfigured = false;
this.farenheit = true;
this.securityData = null;
this.settingsChanged = false;
@@ -384,6 +389,18 @@ public class BridgeSettingsDescriptor {
public void setHassconfigured(boolean hassconfigured) {
this.hassconfigured = hassconfigured;
}
+ public IpList getOpenhabaddress() {
+ return openhabaddress;
+ }
+ public void setOpenhabaddress(IpList openhabaddress) {
+ this.openhabaddress = openhabaddress;
+ }
+ public boolean isOpenhabconfigured() {
+ return openhabconfigured;
+ }
+ public void setOpenhabconfigured(boolean openhabconfigured) {
+ this.openhabconfigured = openhabconfigured;
+ }
public String getHubversion() {
return hubversion;
}
@@ -527,4 +544,14 @@ public class BridgeSettingsDescriptor {
return true;
}
+ public Boolean isValidOpenhab() {
+ if(this.getOpenhabaddress() == null || this.getOpenhabaddress().getDevices().size() <= 0)
+ return false;
+
+ List devicesList = this.getOpenhabaddress().getDevices();
+ if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS))
+ return false;
+
+ return true;
+ }
}
diff --git a/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABHome.java b/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABHome.java
new file mode 100644
index 0000000..3057fb8
--- /dev/null
+++ b/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABHome.java
@@ -0,0 +1,82 @@
+package com.bwssystems.HABridge.plugins.openhab;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.bwssystems.HABridge.BridgeSettings;
+import com.bwssystems.HABridge.Home;
+import com.bwssystems.HABridge.NamedIP;
+import com.bwssystems.HABridge.api.CallItem;
+import com.bwssystems.HABridge.dao.DeviceDescriptor;
+import com.bwssystems.HABridge.hue.ColorData;
+import com.bwssystems.HABridge.hue.MultiCommandUtil;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+public class OpenHABHome implements Home {
+ private static final Logger log = LoggerFactory.getLogger(OpenHABHome.class);
+ private Map openhabMap;
+ private Boolean validOpenhab;
+ private Gson aGsonHandler;
+ private boolean closed;
+
+ public OpenHABHome(BridgeSettings bridgeSettings) {
+ super();
+ closed = true;
+ createHome(bridgeSettings);
+ closed = false;
+ }
+
+
+ @Override
+ public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity,
+ Integer targetBri, Integer targetBriInc, ColorData colorData, DeviceDescriptor device, String body) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public Object getItems(String type) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public Home createHome(BridgeSettings bridgeSettings) {
+ openhabMap = null;
+ aGsonHandler = null;
+ validOpenhab = bridgeSettings.getBridgeSettingsDescriptor().isValidOpenhab();
+ log.info("OpenHAB Home created." + (validOpenhab ? "" : " No OpenHABs configured."));
+ if(validOpenhab) {
+ openhabMap = new HashMap();
+ aGsonHandler =
+ new GsonBuilder()
+ .create();
+ Iterator theList = bridgeSettings.getBridgeSettingsDescriptor().getHassaddress().getDevices().iterator();
+ while(theList.hasNext() && validOpenhab) {
+ NamedIP anOpenhab = theList.next();
+ try {
+ openhabMap.put(anOpenhab.getName(), new OpenHABInstance(anOpenhab));
+ } catch (Exception e) {
+ log.error("Cannot get hass (" + anOpenhab.getName() + ") setup, Exiting with message: " + e.getMessage(), e);
+ validOpenhab = false;
+ }
+ }
+ }
+ return this;
+ }
+
+ @Override
+ public void closeHome() {
+ if(!closed) {
+
+ }
+
+ }
+
+
+}
diff --git a/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABInstance.java b/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABInstance.java
new file mode 100644
index 0000000..f81306d
--- /dev/null
+++ b/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABInstance.java
@@ -0,0 +1,116 @@
+package com.bwssystems.HABridge.plugins.openhab;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+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.bwssystems.HABridge.plugins.http.HTTPHome;
+import com.google.gson.Gson;
+
+public class OpenHABInstance {
+ private static final Logger log = LoggerFactory.getLogger(OpenHABInstance.class);
+ private NamedIP theOpenHAB;
+ private HTTPHandler anHttpHandler;
+
+ public OpenHABInstance(NamedIP openhabLocation) {
+ super();
+ anHttpHandler = HTTPHome.getHandler();
+ theOpenHAB = openhabLocation;
+ }
+
+ public NamedIP getOpenHABAddress() {
+ return theOpenHAB;
+ }
+
+ public void setOpenHABAddress(NamedIP openhabAddress) {
+ this.theOpenHAB = openhabAddress;
+ }
+
+ public Boolean callCommand(String aCommand) {
+ log.debug("calling HomeAssistant: " + aCommand);
+ String aUrl = null;
+ if(theOpenHAB.getSecure() != null && theOpenHAB.getSecure())
+ aUrl = "https";
+ else
+ aUrl = "http";
+/* String domain = aCommand.getEntityId().substring(0, aCommand.getEntityId().indexOf("."));
+ aUrl = aUrl + "://" + theOpenHAB.getIp() + ":" + theOpenHAB.getPort() + "/api/services/";
+ if(domain.equals("group"))
+ aUrl = aUrl + "homeassistant";
+ else
+ aUrl = aUrl + domain;
+ String aBody = "{\"entity_id\":\"" + aCommand.getEntityId() + "\"";
+ NameValue[] headers = null;
+ if(theOpenHAB.getPassword() != null && !theOpenHAB.getPassword().isEmpty()) {
+ NameValue password = new NameValue();
+ password.setName("x-ha-access");
+ password.setValue(theOpenHAB.getPassword());
+ headers = new NameValue[1];
+ headers[0] = password;
+ }
+ if(aCommand.getState().equalsIgnoreCase("on")) {
+ aUrl = aUrl + "/turn_on";
+ if(aCommand.getBri() != null)
+ aBody = aBody + ",\"brightness\":" + aCommand.getBri() + "}";
+ else
+ aBody = aBody + "}";
+ }
+ else {
+ aUrl = aUrl + "/turn_off";
+ aBody = aBody + "}";
+ }
+ log.debug("Calling HomeAssistant with url: " + aUrl);
+ String theData = anHttpHandler.doHttpRequest(aUrl, HttpPost.METHOD_NAME, "application/json", aBody, headers);
+ log.debug("call Command return is: <" + theData + ">");
+ */
+ return true;
+ }
+
+ public List getDevices() {
+ List theDeviceStates = null;
+ OpenHABItem[] theOpenhabStates;
+ String theUrl = null;
+ String theData;
+ NameValue[] headers = null;
+ if(theOpenHAB.getPassword() != null && !theOpenHAB.getPassword().isEmpty()) {
+ NameValue password = new NameValue();
+ password.setName("x-ha-access");
+ password.setValue(theOpenHAB.getPassword());
+ headers = new NameValue[1];
+ headers[0] = password;
+ }
+ if(theOpenHAB.getSecure() != null && theOpenHAB.getSecure())
+ theUrl = "https";
+ else
+ theUrl = "http";
+ theUrl = theUrl + "://" + theOpenHAB.getIp() + ":" + theOpenHAB.getPort() + "/rest/items?recursive=false";
+ theData = anHttpHandler.doHttpRequest(theUrl, HttpGet.METHOD_NAME, "application/json", null, headers);
+ if(theData != null) {
+ log.debug("GET OpenHAB States - data: " + theData);
+ theOpenhabStates = new Gson().fromJson(theData, OpenHABItem[].class);
+ if(theOpenhabStates == null) {
+ log.warn("Cannot get an devices for OpenHAB " + theOpenHAB.getName() + " as response is not parsable.");
+ }
+ else {
+ theDeviceStates = new ArrayList(Arrays.asList(theOpenhabStates));
+ }
+ }
+ else
+ log.warn("Cannot get an devices for OpenHAB " + theOpenHAB.getName() + " http call failed.");
+ return theDeviceStates;
+ }
+
+
+ protected void closeClient() {
+ anHttpHandler.closeHandler();
+ anHttpHandler = null;
+ }
+}
diff --git a/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABItem.java b/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABItem.java
new file mode 100644
index 0000000..82efa72
--- /dev/null
+++ b/src/main/java/com/bwssystems/HABridge/plugins/openhab/OpenHABItem.java
@@ -0,0 +1,110 @@
+
+package com.bwssystems.HABridge.plugins.openhab;
+
+import java.util.List;
+import com.google.gson.annotations.Expose;
+import com.google.gson.annotations.SerializedName;
+
+public class OpenHABItem {
+
+ @SerializedName("link")
+ @Expose
+ private String link;
+ @SerializedName("state")
+ @Expose
+ private String state;
+ @SerializedName("type")
+ @Expose
+ private String type;
+ @SerializedName("name")
+ @Expose
+ private String name;
+ @SerializedName("label")
+ @Expose
+ private String label;
+ @SerializedName("category")
+ @Expose
+ private String category;
+ @SerializedName("tags")
+ @Expose
+ private List