From d118dd85233800b3262ab4a172f4b15b42346454 Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 24 Jan 2017 08:29:59 -0600 Subject: [PATCH] Continue on domoticz impl --- pom.xml | 2 +- .../HABridge/BridgeSettingsDescriptor.java | 22 +++ .../plugins/domoticz/DomoticzDevice.java | 32 ++++ .../plugins/domoticz/DomoticzHandler.java | 169 ++++++++++++++++++ .../plugins/domoticz/DomoticzHome.java | 167 +++++++++++++++++ 5 files changed, 391 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/bwssystems/HABridge/plugins/domoticz/DomoticzDevice.java create mode 100644 src/main/java/com/bwssystems/HABridge/plugins/domoticz/DomoticzHandler.java create mode 100644 src/main/java/com/bwssystems/HABridge/plugins/domoticz/DomoticzHome.java diff --git a/pom.xml b/pom.xml index 1dc3c56..e2d70af 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.bwssystems.HABridge ha-bridge - 4.1.0beta1 + 4.1.0beta2 jar HA Bridge diff --git a/src/main/java/com/bwssystems/HABridge/BridgeSettingsDescriptor.java b/src/main/java/com/bwssystems/HABridge/BridgeSettingsDescriptor.java index d13cdcf..442513b 100644 --- a/src/main/java/com/bwssystems/HABridge/BridgeSettingsDescriptor.java +++ b/src/main/java/com/bwssystems/HABridge/BridgeSettingsDescriptor.java @@ -38,6 +38,8 @@ public class BridgeSettingsDescriptor { private IpList hassaddress; private boolean hassconfigured; private String hubversion; + private IpList domoticzaddress; + private boolean domoticzconfigured; public BridgeSettingsDescriptor() { super(); @@ -249,6 +251,18 @@ public class BridgeSettingsDescriptor { public void setHubversion(String hubversion) { this.hubversion = hubversion; } + public IpList getDomoticzaddress() { + return domoticzaddress; + } + public void setDomoticzaddress(IpList domoticzaddress) { + this.domoticzaddress = domoticzaddress; + } + public boolean isDomoticzconfigured() { + return domoticzconfigured; + } + public void setDomoticzconfigured(boolean domoticzconfigured) { + this.domoticzconfigured = domoticzconfigured; + } public Boolean isValidVera() { if(this.getVeraAddress() == null || this.getVeraAddress().getDevices().size() <= 0) return false; @@ -306,4 +320,12 @@ public class BridgeSettingsDescriptor { return false; return true; } + public Boolean isValidDomoticz() { + if(this.getDomoticzaddress() == null || this.getDomoticzaddress().getDevices().size() <= 0) + return false; + List devicesList = this.getDomoticzaddress().getDevices(); + if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS)) + return false; + return true; + } } diff --git a/src/main/java/com/bwssystems/HABridge/plugins/domoticz/DomoticzDevice.java b/src/main/java/com/bwssystems/HABridge/plugins/domoticz/DomoticzDevice.java new file mode 100644 index 0000000..c499bd5 --- /dev/null +++ b/src/main/java/com/bwssystems/HABridge/plugins/domoticz/DomoticzDevice.java @@ -0,0 +1,32 @@ +package com.bwssystems.HABridge.plugins.domoticz; + +public class DomoticzDevice { + private String Domoticzdevicetype; + private String Domoticzdevicename; + private String Domoticzaddress; + private String Domoticzname; + public String getDomoticzdevicetype() { + return Domoticzdevicetype; + } + public void setDomoticzdevicetype(String Domoticzdevicetype) { + this.Domoticzdevicetype = Domoticzdevicetype; + } + public String getDomoticzdevicename() { + return Domoticzdevicename; + } + public void setDomoticzdevicename(String Domoticzdevicename) { + this.Domoticzdevicename = Domoticzdevicename; + } + public String getDomoticzaddress() { + return Domoticzaddress; + } + public void setDomoticzaddress(String Domoticzaddress) { + this.Domoticzaddress = Domoticzaddress; + } + public String getDomoticzname() { + return Domoticzname; + } + public void setDomoticzname(String Domoticzname) { + this.Domoticzname = Domoticzname; + } +} \ No newline at end of file diff --git a/src/main/java/com/bwssystems/HABridge/plugins/domoticz/DomoticzHandler.java b/src/main/java/com/bwssystems/HABridge/plugins/domoticz/DomoticzHandler.java new file mode 100644 index 0000000..0f851aa --- /dev/null +++ b/src/main/java/com/bwssystems/HABridge/plugins/domoticz/DomoticzHandler.java @@ -0,0 +1,169 @@ +package com.bwssystems.HABridge.plugins.domoticz; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.bwssystems.HABridge.NamedIP; +import com.bwssystems.HABridge.plugins.http.HTTPHandler; +import com.bwssystems.HABridge.util.TextStringFormatter; +import com.google.gson.Gson; + +public class DomoticzHandler { + private static final Logger log = LoggerFactory.getLogger(DomoticzHandler.class); + private static final String DEVICE_REQUEST = "/DeviceData!DeviceCmd=GetNames!DeviceType="; + private static final String HVAC_REQUEST = "/HVACData!HVACCmd=GetNames"; + private static final String GROUP_REQUEST = "/GroupData!GroupCmd=GetNames"; + private static final String MACRO_REQUEST = "/MacroData!MacroCmd=GetNames"; + private static final String SCENE_REQUEST = "/SceneData!SceneCmd=GetNames"; + private static final String IRDATA_REQUEST = "/IrData!IRCmd=GetNames"; + private static final String IRBUTTON_REQUEST = "/IrData!IRCmd=GetButtons!IrDevice="; + private static final String TOKEN_REQUEST = "?Token="; + private static final String LIGHT_REQUEST = "Light"; + private static final String APPL_REQUEST = "Appl"; + // private static final String VIDEO_REQUEST = "Video"; + private static final String THEATRE_REQUEST = "Theatre"; + private static final String CUSTOM_REQUEST = "Custom"; + private static final String HVAC_TYPE = "HVAC"; + private static final String HOME_TYPE = "Home"; + private static final String GROUP_TYPE = "Group"; + private static final String MACRO_TYPE = "Macro"; + private static final String SCENE_TYPE = "Scene"; + private static final String IRDATA_TYPE = "IrData"; + private HTTPHandler httpClient; + private NamedIP domoticzAddress; + private String theToken; + + public DomoticzHandler(NamedIP addressName, String aGivenToken) { + super(); + httpClient = new HTTPHandler(); + domoticzAddress = addressName; + theToken = aGivenToken; + } + + public List getLights() { + return getDomoticzDevices(DEVICE_REQUEST + LIGHT_REQUEST + TOKEN_REQUEST, LIGHT_REQUEST); + } + + public List getAppliances() { + return getDomoticzDevices(DEVICE_REQUEST + APPL_REQUEST + TOKEN_REQUEST, APPL_REQUEST); + } + + public List getTheatre() { + return getDomoticzDevices(DEVICE_REQUEST + THEATRE_REQUEST + TOKEN_REQUEST, THEATRE_REQUEST); + } + + public List getCustom() { + return getDomoticzDevices(DEVICE_REQUEST + CUSTOM_REQUEST + TOKEN_REQUEST, CUSTOM_REQUEST); + } + + public List getHVAC() { + return getDomoticzDevices(HVAC_REQUEST + TOKEN_REQUEST, HVAC_TYPE); + } + + public List getGroups() { + return getDomoticzDevices(GROUP_REQUEST + TOKEN_REQUEST, GROUP_TYPE); + } + + public List getMacros() { + return getDomoticzDevices(MACRO_REQUEST + TOKEN_REQUEST, MACRO_TYPE); + } + + public List getScenes() { + return getDomoticzDevices(SCENE_REQUEST + TOKEN_REQUEST, SCENE_TYPE); + } + + public List getButtons() { + List irDataDevices = getDomoticzDevices(IRDATA_REQUEST + TOKEN_REQUEST, IRDATA_TYPE); + + return getDeviceButtons(irDataDevices); + } + + public List getHome(String theDeviceName) { + List deviceList = null; + deviceList = new ArrayList(); + DomoticzDevice aNewDomoticzDevice = new DomoticzDevice(); + aNewDomoticzDevice.setDomoticzdevicetype(HOME_TYPE); + aNewDomoticzDevice.setDomoticzdevicename(theDeviceName); + deviceList.add(aNewDomoticzDevice); + return deviceList; + } + + private List getDomoticzDevices(String apiType, String deviceType) { + Devices theDomoticzApiResponse = null; + List deviceList = null; + + String theUrl = null; + String theData; + theUrl = "http://" + domoticzAddress.getIp() + apiType + theToken; + theData = httpClient.doHttpRequest(theUrl, null, null, null, null); + if(theData != null) { + log.debug("GET " + deviceType + " DomoticzApiResponse - data: " + theData); + theDomoticzApiResponse = new Gson().fromJson(theData, Devices.class); + if(theDomoticzApiResponse.getResult() == null) { + log.warn("Cannot get an devices for type " + deviceType + " for Domoticz " + domoticzAddress.getName() + " as response is not parsable."); + return deviceList; + } + deviceList = new ArrayList(); + + Iterator theDeviceNames = theDomoticzApiResponse.getResult().iterator(); + while(theDeviceNames.hasNext()) { + DeviceResult theDevice = theDeviceNames.next(); + DomoticzDevice aNewDomoticzDevice = new DomoticzDevice(); + aNewDomoticzDevice.setDomoticzdevicetype(deviceType); +// aNewDomoticzDevice.setDomoticzdevicename(theDevice.getDeviceName()); + deviceList.add(aNewDomoticzDevice); + + } + } + else { + log.warn("Get Domoticz device types " + deviceType + " for " + domoticzAddress.getName() + " - returned null, no data."); + } + return deviceList; + } + + private List getDeviceButtons(List theIrDevices) { + Devices theDomoticzApiResponse = null; + List deviceList = null; + + String theUrl = null; + String theData; + if(theIrDevices == null) + return null; + Iterator theDomoticzDevices = theIrDevices.iterator(); + deviceList = new ArrayList(); + while (theDomoticzDevices.hasNext()) { + DomoticzDevice theDomoticzDevice = theDomoticzDevices.next(); + theUrl = "http://" + domoticzAddress.getIp() + IRBUTTON_REQUEST + TextStringFormatter.forQuerySpaceUrl(theDomoticzDevice.getDomoticzdevicename()) + TOKEN_REQUEST + theToken; + theData = httpClient.doHttpRequest(theUrl, null, null, null, null); + if (theData != null) { + log.debug("GET IrData for IR Device " + theDomoticzDevice.getDomoticzdevicename() + " DomoticzApiResponse - data: " + theData); + theDomoticzApiResponse = new Gson().fromJson(theData, Devices.class); + if (theDomoticzApiResponse.getResult() == null) { + log.warn("Cannot get buttons for IR Device " + theDomoticzDevice.getDomoticzdevicename() + " for Domoticz " + + domoticzAddress.getName() + " as response is not parsable."); + return deviceList; + } +// theDomoticzDevice.setButtons(theDomoticzApiResponse); + deviceList.add(theDomoticzDevice); + + } else { + log.warn("Get Domoticz buttons for IR Device " + theDomoticzDevice.getDomoticzdevicename() + " for " + + domoticzAddress.getName() + " - returned null, no data."); + } + } + return deviceList; + } + + public NamedIP getDomoticzAddress() { + return domoticzAddress; + } + + public void setDomoticzAddress(NamedIP DomoticzAddress) { + this.domoticzAddress = DomoticzAddress; + } + +} diff --git a/src/main/java/com/bwssystems/HABridge/plugins/domoticz/DomoticzHome.java b/src/main/java/com/bwssystems/HABridge/plugins/domoticz/DomoticzHome.java new file mode 100644 index 0000000..3266189 --- /dev/null +++ b/src/main/java/com/bwssystems/HABridge/plugins/domoticz/DomoticzHome.java @@ -0,0 +1,167 @@ +package com.bwssystems.HABridge.plugins.domoticz; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.bwssystems.HABridge.BridgeSettingsDescriptor; +import com.bwssystems.HABridge.Home; +import com.bwssystems.HABridge.NamedIP; +import com.bwssystems.HABridge.api.CallItem; +import com.bwssystems.HABridge.api.NameValue; +import com.bwssystems.HABridge.api.hue.HueError; +import com.bwssystems.HABridge.api.hue.HueErrorResponse; +import com.bwssystems.HABridge.dao.DeviceDescriptor; +import com.bwssystems.HABridge.hue.BrightnessDecode; +import com.bwssystems.HABridge.hue.MultiCommandUtil; +import com.bwssystems.HABridge.plugins.http.HTTPHandler; +import com.google.gson.Gson; + +public class DomoticzHome implements Home { + private static final Logger log = LoggerFactory.getLogger(DomoticzHome.class); + private Map Domoticzs; + private Boolean validDomoticz; + private HTTPHandler anHttpHandler; + + public DomoticzHome(BridgeSettingsDescriptor bridgeSettings) { + super(); + createHome(bridgeSettings); + } + + @Override + public Object getItems(String type) { + if(!validDomoticz) + return null; + log.debug("consolidating devices for hues"); + List theResponse = null; + Iterator keys = Domoticzs.keySet().iterator(); + List deviceList = new ArrayList(); + while(keys.hasNext()) { + String key = keys.next(); + theResponse = Domoticzs.get(key).getLights(); + if(theResponse != null) + addDomoticzDevices(deviceList, theResponse, key); + else { + log.warn("Cannot get lights for Domoticz with name: " + key + ", skipping this Domoticz."); + continue; + } + theResponse = Domoticzs.get(key).getAppliances(); + if(theResponse != null) + addDomoticzDevices(deviceList, theResponse, key); + else + log.warn("Cannot get appliances for Domoticz with name: " + key); + theResponse = Domoticzs.get(key).getTheatre(); + if(theResponse != null) + addDomoticzDevices(deviceList, theResponse, key); + else + log.warn("Cannot get theatre for Domoticz with name: " + key); + theResponse = Domoticzs.get(key).getCustom(); + if(theResponse != null) + addDomoticzDevices(deviceList, theResponse, key); + else + log.warn("Cannot get custom for Domoticz with name: " + key); + theResponse = Domoticzs.get(key).getHVAC(); + if(theResponse != null) + addDomoticzDevices(deviceList, theResponse, key); + else + log.warn("Cannot get HVAC for Domoticz with name: " + key); + theResponse = Domoticzs.get(key).getHome(key); + if(theResponse != null) + addDomoticzDevices(deviceList, theResponse, key); + else + log.warn("Cannot get Homes for Domoticz with name: " + key); + theResponse = Domoticzs.get(key).getGroups(); + if(theResponse != null) + addDomoticzDevices(deviceList, theResponse, key); + else + log.warn("Cannot get Groups for Domoticz with name: " + key); + theResponse = Domoticzs.get(key).getMacros(); + if(theResponse != null) + addDomoticzDevices(deviceList, theResponse, key); + else + log.warn("Cannot get Macros for Domoticz with name: " + key); + theResponse = Domoticzs.get(key).getScenes(); + if(theResponse != null) + addDomoticzDevices(deviceList, theResponse, key); + else + log.warn("Cannot get Scenes for Domoticz with name: " + key); + theResponse = Domoticzs.get(key).getButtons(); + if(theResponse != null) + addDomoticzDevices(deviceList, theResponse, key); + else + log.warn("Cannot get Buttons for Domoticz with name: " + key); + } + return deviceList; + } + + private Boolean addDomoticzDevices(List theDeviceList, List theSourceList, String theKey) { + if(!validDomoticz) + return null; + Iterator devices = theSourceList.iterator(); + while(devices.hasNext()) { + DomoticzDevice theDevice = devices.next(); + DomoticzDevice aNewDomoticzDevice = new DomoticzDevice(); + aNewDomoticzDevice.setDomoticzdevicetype(theDevice.getDomoticzdevicetype()); + aNewDomoticzDevice.setDomoticzdevicename(theDevice.getDomoticzdevicename()); +// aNewDomoticzDevice.setButtons(theDevice.getButtons()); + aNewDomoticzDevice.setDomoticzaddress(Domoticzs.get(theKey).getDomoticzAddress().getIp()); + aNewDomoticzDevice.setDomoticzname(theKey); + theDeviceList.add(aNewDomoticzDevice); + } + anHttpHandler = new HTTPHandler(); + return true; + } + + @Override + public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity, + Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) { + log.debug("executing HUE api request to Domoticz Http " + anItem.getItem().getAsString()); + String responseString = null; + + String anUrl = BrightnessDecode.calculateReplaceIntensityValue(anItem.getItem().getAsString(), + intensity, targetBri, targetBriInc, false); + String aBody; + 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) { + log.warn("Error on calling url to change device state: " + anUrl); + responseString = new Gson().toJson(HueErrorResponse.createResponse("6", "/lights/" + lightId, + "Error on calling url to change device state", "/lights/" + + lightId + "state", null, null).getTheErrors(), HueError[].class); + } + return responseString; + } + + @Override + public Home createHome(BridgeSettingsDescriptor bridgeSettings) { +// validDomoticz = bridgeSettings.isValidDomoticz(); + log.info("Domoticz Home created." + (validDomoticz ? "" : " No Domoticz devices configured.")); + if(!validDomoticz) + return null; + Domoticzs = new HashMap(); + Iterator theList = bridgeSettings.getDomoticzaddress().getDevices().iterator(); + while(theList.hasNext()) { + NamedIP aDomoticz = theList.next(); + try { + Domoticzs.put(aDomoticz.getName(), new DomoticzHandler(aDomoticz, "stuff")); + } catch (Exception e) { + log.error("Cannot get Domoticz client (" + aDomoticz.getName() + ") setup, Exiting with message: " + e.getMessage(), e); + return null; + } + } + return this; + } + + @Override + public void closeHome() { + // noop + + } +}