Continue on domoticz impl

This commit is contained in:
Admin
2017-01-24 08:29:59 -06:00
parent 104d341864
commit d118dd8523
5 changed files with 391 additions and 1 deletions

View File

@@ -5,7 +5,7 @@
<groupId>com.bwssystems.HABridge</groupId>
<artifactId>ha-bridge</artifactId>
<version>4.1.0beta1</version>
<version>4.1.0beta2</version>
<packaging>jar</packaging>
<name>HA Bridge</name>

View File

@@ -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<NamedIP> devicesList = this.getDomoticzaddress().getDevices();
if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS))
return false;
return true;
}
}

View File

@@ -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;
}
}

View File

@@ -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<DomoticzDevice> getLights() {
return getDomoticzDevices(DEVICE_REQUEST + LIGHT_REQUEST + TOKEN_REQUEST, LIGHT_REQUEST);
}
public List<DomoticzDevice> getAppliances() {
return getDomoticzDevices(DEVICE_REQUEST + APPL_REQUEST + TOKEN_REQUEST, APPL_REQUEST);
}
public List<DomoticzDevice> getTheatre() {
return getDomoticzDevices(DEVICE_REQUEST + THEATRE_REQUEST + TOKEN_REQUEST, THEATRE_REQUEST);
}
public List<DomoticzDevice> getCustom() {
return getDomoticzDevices(DEVICE_REQUEST + CUSTOM_REQUEST + TOKEN_REQUEST, CUSTOM_REQUEST);
}
public List<DomoticzDevice> getHVAC() {
return getDomoticzDevices(HVAC_REQUEST + TOKEN_REQUEST, HVAC_TYPE);
}
public List<DomoticzDevice> getGroups() {
return getDomoticzDevices(GROUP_REQUEST + TOKEN_REQUEST, GROUP_TYPE);
}
public List<DomoticzDevice> getMacros() {
return getDomoticzDevices(MACRO_REQUEST + TOKEN_REQUEST, MACRO_TYPE);
}
public List<DomoticzDevice> getScenes() {
return getDomoticzDevices(SCENE_REQUEST + TOKEN_REQUEST, SCENE_TYPE);
}
public List<DomoticzDevice> getButtons() {
List<DomoticzDevice> irDataDevices = getDomoticzDevices(IRDATA_REQUEST + TOKEN_REQUEST, IRDATA_TYPE);
return getDeviceButtons(irDataDevices);
}
public List<DomoticzDevice> getHome(String theDeviceName) {
List<DomoticzDevice> deviceList = null;
deviceList = new ArrayList<DomoticzDevice>();
DomoticzDevice aNewDomoticzDevice = new DomoticzDevice();
aNewDomoticzDevice.setDomoticzdevicetype(HOME_TYPE);
aNewDomoticzDevice.setDomoticzdevicename(theDeviceName);
deviceList.add(aNewDomoticzDevice);
return deviceList;
}
private List<DomoticzDevice> getDomoticzDevices(String apiType, String deviceType) {
Devices theDomoticzApiResponse = null;
List<DomoticzDevice> 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<DomoticzDevice>();
Iterator<DeviceResult> 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<DomoticzDevice> getDeviceButtons(List<DomoticzDevice> theIrDevices) {
Devices theDomoticzApiResponse = null;
List<DomoticzDevice> deviceList = null;
String theUrl = null;
String theData;
if(theIrDevices == null)
return null;
Iterator<DomoticzDevice> theDomoticzDevices = theIrDevices.iterator();
deviceList = new ArrayList<DomoticzDevice>();
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;
}
}

View File

@@ -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<String, DomoticzHandler> 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<DomoticzDevice> theResponse = null;
Iterator<String> keys = Domoticzs.keySet().iterator();
List<DomoticzDevice> deviceList = new ArrayList<DomoticzDevice>();
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<DomoticzDevice> theDeviceList, List<DomoticzDevice> theSourceList, String theKey) {
if(!validDomoticz)
return null;
Iterator<DomoticzDevice> 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<String, DomoticzHandler>();
Iterator<NamedIP> 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
}
}