From b9437d42e85a371c19b1306f60cf16d3c9400398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Rennfanz?= Date: Sat, 9 Dec 2017 21:15:02 +0100 Subject: [PATCH 1/4] Update README.md file --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 56d5179..23b69ca 100644 --- a/README.md +++ b/README.md @@ -358,7 +358,7 @@ The Add/Edit tab will show you the fields to fill in for the above in a form, wh The format of the item can be the default HTTP request which executes the URLs formatted as `http://` as a GET. Other options to this are to select the HTTP Verb and add the data type and add a body that is passed with the request. Secure https is supported as well, just use `https://`. When using POST and PUT, you have the ability to specify the body that will be sent with the request as well as the application type for the http call. The valid device types are: "custom", "veraDevice", "veraScene", "harmonyActivity", "harmonyButton", "nestHomeAway", "nestThermoSet", "hueDevice", "halDevice", - "halButton", "halHome", "halThermoSet", "mqttMessage", "cmdDevice", "hassDevice", "tcpDevice", "udpDevice", "httpDevice", "domoticzDevice", "somfyDevice" + "halButton", "halHome", "halThermoSet", "mqttMessage", "cmdDevice", "hassDevice", "homewizardDevice", "tcpDevice", "udpDevice", "httpDevice", "domoticzDevice", "somfyDevice" Filter Ip example: ``` From d337546da70ab27e5acc92d08f4d03acbedf0fc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Rennfanz?= Date: Sat, 9 Dec 2017 21:15:18 +0100 Subject: [PATCH 2/4] Add java part of plug-in for HomeWizard SmartPlug support --- .../bwssystems/HABridge/BridgeSettings.java | 1 + .../HABridge/BridgeSettingsDescriptor.java | 31 ++- .../bwssystems/HABridge/DeviceMapTypes.java | 2 + .../com/bwssystems/HABridge/HomeManager.java | 7 +- .../devicemanagmeent/DeviceResource.java | 6 + .../plugins/homewizard/HomeWizardHome.java | 151 ++++++++++++ .../homewizard/HomeWizardSmartPlugDevice.java | 47 ++++ .../homewizard/HomeWizzardSmartPlugInfo.java | 215 ++++++++++++++++++ .../plugins/homewizard/json/Device.java | 49 ++++ 9 files changed, 505 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizardHome.java create mode 100644 src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizardSmartPlugDevice.java create mode 100644 src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizzardSmartPlugInfo.java create mode 100644 src/main/java/com/bwssystems/HABridge/plugins/homewizard/json/Device.java diff --git a/src/main/java/com/bwssystems/HABridge/BridgeSettings.java b/src/main/java/com/bwssystems/HABridge/BridgeSettings.java index 611ec04..b8eb403 100644 --- a/src/main/java/com/bwssystems/HABridge/BridgeSettings.java +++ b/src/main/java/com/bwssystems/HABridge/BridgeSettings.java @@ -209,6 +209,7 @@ public class BridgeSettings extends BackupHandler { theBridgeSettings.setHassconfigured(theBridgeSettings.isValidHass()); theBridgeSettings.setDomoticzconfigured(theBridgeSettings.isValidDomoticz()); theBridgeSettings.setSomfyconfigured(theBridgeSettings.isValidSomfy()); + theBridgeSettings.setHomeWizardConfigured(theBridgeSettings.isValidHomeWizard()); // Lifx is either configured or not, so it does not need an update. if(serverPortOverride != null) theBridgeSettings.setServerPort(serverPortOverride); diff --git a/src/main/java/com/bwssystems/HABridge/BridgeSettingsDescriptor.java b/src/main/java/com/bwssystems/HABridge/BridgeSettingsDescriptor.java index 4160d2f..6bb1108 100644 --- a/src/main/java/com/bwssystems/HABridge/BridgeSettingsDescriptor.java +++ b/src/main/java/com/bwssystems/HABridge/BridgeSettingsDescriptor.java @@ -93,7 +93,9 @@ public class BridgeSettingsDescriptor { @SerializedName("securityData") @Expose private String securityData; - + @SerializedName("homewizardaddress") + @Expose + private IpList homewizardaddress; private boolean settingsChanged; private boolean veraconfigured; @@ -107,7 +109,8 @@ public class BridgeSettingsDescriptor { private boolean domoticzconfigured; private boolean somfyconfigured; private boolean lifxconfigured; - + private boolean homewizardconfigured; + // Deprecated settings private String haltoken; private boolean upnpstrict; @@ -127,7 +130,7 @@ public class BridgeSettingsDescriptor { this.mqttconfigured = false; this.hassconfigured = false; this.domoticzconfigured = false; - this.somfyconfigured = false; + this.homewizardconfigured = false; this.lifxconfigured = false; this.farenheit = true; this.securityData = null; @@ -188,6 +191,9 @@ public class BridgeSettingsDescriptor { public IpList getSomfyAddress() { return somfyaddress; } + public IpList getHomeWizardAddress() { + return homewizardaddress; + } public void setVeraAddress(IpList veraAddress) { this.veraaddress = veraAddress; } @@ -197,6 +203,9 @@ public class BridgeSettingsDescriptor { public void setSomfyAddress(IpList somfyAddress) { this.somfyaddress = somfyAddress; } + public void setHomeWizardAddress(IpList homewizardaddress) { + this.homewizardaddress = homewizardaddress; + } public IpList getHarmonyAddress() { return harmonyaddress; } @@ -236,6 +245,9 @@ public class BridgeSettingsDescriptor { public boolean isSomfyconfigured() { return somfyconfigured; } + public boolean isHomeWizardConfigured() { + return homewizardconfigured; + } public void setVeraconfigured(boolean veraconfigured) { this.veraconfigured = veraconfigured; } @@ -245,6 +257,9 @@ public class BridgeSettingsDescriptor { public void setSomfyconfigured(boolean somfyconfigured) { this.somfyconfigured = somfyconfigured; } + public void setHomeWizardConfigured(boolean homewizardconfigured) { + this.homewizardconfigured = homewizardconfigured; + } public boolean isHarmonyconfigured() { return harmonyconfigured; } @@ -492,4 +507,14 @@ public class BridgeSettingsDescriptor { this.setSettingsChanged(true); } } + public Boolean isValidHomeWizard() { + if(this.getHomeWizardAddress() == null || this.getHomeWizardAddress().getDevices().size() <= 0) + return false; + + List devicesList = this.getHomeWizardAddress().getDevices(); + if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS)) + return false; + + return true; + } } diff --git a/src/main/java/com/bwssystems/HABridge/DeviceMapTypes.java b/src/main/java/com/bwssystems/HABridge/DeviceMapTypes.java index 9c13d71..b32e9d9 100644 --- a/src/main/java/com/bwssystems/HABridge/DeviceMapTypes.java +++ b/src/main/java/com/bwssystems/HABridge/DeviceMapTypes.java @@ -22,6 +22,7 @@ public class DeviceMapTypes { public final static String[] EXEC_DEVICE_COMPAT = { "exec", "Execute Script/Program"}; public final static String[] CMD_DEVICE = { "cmdDevice", "Execute Command/Script/Program"}; public final static String[] HASS_DEVICE = { "hassDevice", "HomeAssistant Device"}; + public final static String[] HOMEWIZARD_DEVICE = { "homewizardDevice", "HomeWizard Device"}; public final static String[] TCP_DEVICE = { "tcpDevice", "TCP Device"}; public final static String[] TCP_DEVICE_COMPAT = { "TCP", "TCP Device"}; public final static String[] UDP_DEVICE = { "udpDevice", "UDP Device"}; @@ -48,6 +49,7 @@ public class DeviceMapTypes { deviceMapTypes.add(HARMONY_ACTIVITY); deviceMapTypes.add(HARMONY_BUTTON); deviceMapTypes.add(HASS_DEVICE); + deviceMapTypes.add(HOMEWIZARD_DEVICE); deviceMapTypes.add(HTTP_DEVICE); deviceMapTypes.add(HUE_DEVICE); deviceMapTypes.add(LIFX_DEVICE); diff --git a/src/main/java/com/bwssystems/HABridge/HomeManager.java b/src/main/java/com/bwssystems/HABridge/HomeManager.java index 0d6824c..f488885 100644 --- a/src/main/java/com/bwssystems/HABridge/HomeManager.java +++ b/src/main/java/com/bwssystems/HABridge/HomeManager.java @@ -14,6 +14,7 @@ import com.bwssystems.HABridge.plugins.exec.CommandHome; import com.bwssystems.HABridge.plugins.hal.HalHome; import com.bwssystems.HABridge.plugins.harmony.HarmonyHome; import com.bwssystems.HABridge.plugins.hass.HassHome; +import com.bwssystems.HABridge.plugins.homewizard.HomeWizardHome; import com.bwssystems.HABridge.plugins.http.HTTPHome; import com.bwssystems.HABridge.plugins.hue.HueHome; import com.bwssystems.HABridge.plugins.lifx.LifxHome; @@ -68,6 +69,10 @@ public class HomeManager { aHome = new HassHome(bridgeSettings); resourceList.put(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex], aHome); homeList.put(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex], aHome); + // Setup the HomeWizard configuration if available + aHome = new HomeWizardHome(bridgeSettings); + resourceList.put(DeviceMapTypes.HOMEWIZARD_DEVICE[DeviceMapTypes.typeIndex], aHome); + homeList.put(DeviceMapTypes.HOMEWIZARD_DEVICE[DeviceMapTypes.typeIndex], aHome); //setup the command execution Home aHome = new CommandHome(bridgeSettings); homeList.put(DeviceMapTypes.EXEC_DEVICE_COMPAT[DeviceMapTypes.typeIndex], aHome); @@ -96,7 +101,7 @@ public class HomeManager { aHome = new FibaroHome(bridgeSettings); resourceList.put(DeviceMapTypes.FIBARO_DEVICE[DeviceMapTypes.typeIndex], aHome); resourceList.put(DeviceMapTypes.FIBARO_SCENE[DeviceMapTypes.typeIndex], aHome); - //setup the Domoticz 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); diff --git a/src/main/java/com/bwssystems/HABridge/devicemanagmeent/DeviceResource.java b/src/main/java/com/bwssystems/HABridge/devicemanagmeent/DeviceResource.java index c3067b1..849ec21 100644 --- a/src/main/java/com/bwssystems/HABridge/devicemanagmeent/DeviceResource.java +++ b/src/main/java/com/bwssystems/HABridge/devicemanagmeent/DeviceResource.java @@ -291,6 +291,12 @@ public class DeviceResource { return homeManager.findResource(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex]); }, new JsonTransformer()); + get (API_CONTEXT + "/homewizard/devices", "application/json", (request, response) -> { + log.debug("Get HomeWizard Clients"); + response.status(HttpStatus.SC_OK); + return homeManager.findResource(DeviceMapTypes.HOMEWIZARD_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.HOMEWIZARD_DEVICE[DeviceMapTypes.typeIndex]); + }, new JsonTransformer()); + get (API_CONTEXT + "/domoticz/devices", "application/json", (request, response) -> { log.debug("Get Domoticz Clients"); response.status(HttpStatus.SC_OK); diff --git a/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizardHome.java b/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizardHome.java new file mode 100644 index 0000000..02d7278 --- /dev/null +++ b/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizardHome.java @@ -0,0 +1,151 @@ +package com.bwssystems.HABridge.plugins.homewizard; + +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.BridgeSettings; +import com.bwssystems.HABridge.DeviceMapTypes; +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; + +/** + * Control HomeWizard devices over HomeWizard Cloud + * + * @author Björn Rennfanz (bjoern@fam-rennfanz.de) + * + */ +public class HomeWizardHome implements Home { + + private static final Logger log = LoggerFactory.getLogger(HomeWizardHome.class); + + private Map plugGateways; + private Boolean validHomeWizard; + private boolean closed; + + public HomeWizardHome(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) { + + String responseString = null; + if (!validHomeWizard) { + + log.warn("Should not get here, no HomeWizard smart plug available"); + responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId + + "\",\"description\": \"Should not get here, no HomeWizard smart plug available\", \"parameter\": \"/lights/" + + lightId + "state\"}}]"; + } else { + + if (anItem.getType() != null && anItem.getType().trim().equalsIgnoreCase(DeviceMapTypes.HOMEWIZARD_DEVICE[DeviceMapTypes.typeIndex])) { + + log.debug("Executing HUE api request to change activity to HomeWizard smart plug: " + anItem.getItem().toString()); + String jsonToPost = anItem.getItem().toString(); + + HomeWizzardSmartPlugInfo homeWizzardHandler = getHomeWizzardHandler(device.getTargetDevice()); + if(homeWizzardHandler == null) { + log.warn("Should not get here, no HomeWizard smart plug configured"); + responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId + + "\",\"description\": \"Should not get here, no HomeWizard smart plug configured\", \"parameter\": \"/lights/" + + lightId + "state\"}}]"; + } else { + try { + + homeWizzardHandler.execApply(jsonToPost); + } catch (Exception e) { + + log.warn("Error posting request to HomeWizard smart plug"); + responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId + + "\",\"description\": \"Error posting request to HomeWizard smart plug\", \"parameter\": \"/lights/" + lightId + "state\"}}]"; + } + } + } + } + + return responseString; + } + + public HomeWizzardSmartPlugInfo getHomeWizzardHandler(String plugName) { + return plugGateways.get(plugName); + } + + public List getDevices() { + + log.debug("consolidating devices for plug gateways"); + Iterator keys = plugGateways.keySet().iterator(); + ArrayList deviceList = new ArrayList<>(); + + while(keys.hasNext()) + { + String key = keys.next(); + for(HomeWizardSmartPlugDevice device : plugGateways.get(key).getDevices()) + deviceList.add(device); + } + + return deviceList; + } + + @Override + public Object getItems(String type) { + + if (validHomeWizard) + { + if (type.equalsIgnoreCase(DeviceMapTypes.HOMEWIZARD_DEVICE[DeviceMapTypes.typeIndex])) + { + return getDevices(); + } + } + + return null; + } + + @Override + public Home createHome(BridgeSettings bridgeSettings) { + + validHomeWizard = bridgeSettings.getBridgeSettingsDescriptor().isValidHomeWizard(); + log.info("HomeWizard Home created. " + (validHomeWizard ? "" : "No HomeWizard gateways configured.")); + + if (validHomeWizard) + { + plugGateways = new HashMap<>(); + Iterator gatewaysList = bridgeSettings.getBridgeSettingsDescriptor().getHomeWizardAddress().getDevices().iterator(); + + while(gatewaysList.hasNext()) { + + NamedIP gateway = gatewaysList.next(); + plugGateways.put(gateway.getName(), new HomeWizzardSmartPlugInfo(gateway, gateway.getName())); + } + } + + return this; + } + + @Override + public void closeHome() { + + log.debug("Closing Home."); + if(closed) { + + log.debug("Home is already closed...."); + return; + } + + plugGateways = null; + closed = true; + } +} diff --git a/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizardSmartPlugDevice.java b/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizardSmartPlugDevice.java new file mode 100644 index 0000000..83b4b0a --- /dev/null +++ b/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizardSmartPlugDevice.java @@ -0,0 +1,47 @@ +package com.bwssystems.HABridge.plugins.homewizard; + +/** + * Control HomeWizard devices over HomeWizard Cloud + * + * @author Björn Rennfanz (bjoern@fam-rennfanz.de) + * + */ +public class HomeWizardSmartPlugDevice { + + private String name; + private String gateway; + private String id; + private String typeName; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getGateway() { + return gateway; + } + + public void setGateway(String gateway) { + this.gateway = gateway; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTypeName() { + return this.typeName; + } + + public void setTypeName(String typeName) { + this.typeName = typeName; + } +} diff --git a/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizzardSmartPlugInfo.java b/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizzardSmartPlugInfo.java new file mode 100644 index 0000000..e27b2a2 --- /dev/null +++ b/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizzardSmartPlugInfo.java @@ -0,0 +1,215 @@ +package com.bwssystems.HABridge.plugins.homewizard; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.bwssystems.HABridge.NamedIP; +import com.bwssystems.HABridge.plugins.homewizard.json.Device; +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +import us.monoid.json.JSONException; +import us.monoid.json.JSONObject; + +/** + * Control HomeWizard devices over HomeWizard Cloud + * + * @author Björn Rennfanz (bjoern@fam-rennfanz.de) + * + */ +public class HomeWizzardSmartPlugInfo { + + private static final Logger log = LoggerFactory.getLogger(HomeWizardHome.class); + + private static final String HOMEWIZARD_LOGIN_URL = "https://cloud.homewizard.com/account/login"; + private static final String HOMEWIZARD_API_URL = "https://plug.homewizard.com/plugs"; + private static final String EMPTY_STRING = ""; + + private final String cloudAuth; + private final Gson gson; + + private String cloudSessionId; + private String cloudPlugName; + private String cloudPlugId; + + public HomeWizzardSmartPlugInfo(NamedIP gateway, String name) { + + super(); + + cloudAuth = "Basic " + new String(Base64.encodeBase64((gateway.getUsername() + ":" + DigestUtils.sha1Hex(gateway.getPassword())).getBytes())); + cloudPlugName = name; + gson = new Gson(); + } + + public boolean login() + { + try + { + URL url = new URL(HOMEWIZARD_LOGIN_URL); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setRequestProperty("Authorization", cloudAuth); + connection.setRequestProperty("Content-Type", "application/json;charset=utf-8"); + connection.connect(); + + BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); + StringBuilder buffer = new StringBuilder(); + String line; + + while((line = br.readLine()) != null) + { + buffer.append(line).append("\n"); + } + br.close(); + + // Get session id from result JSON + JSONObject json = new JSONObject(buffer.toString()); + cloudSessionId = json.get("session").toString(); + } + catch(IOException | JSONException e) + { + log.warn("Error while login to cloud service ", e); + return false; + } + + return true; + } + + private String requestJson(String request) + { + String result = null; + + // Check login was successful + if (login()) { + + // Request JSON from Cloud service + try + { + URL url = new URL(HOMEWIZARD_API_URL + request); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + + connection.setRequestMethod("GET"); + connection.setRequestProperty("X-Session-Token", cloudSessionId); + connection.setRequestProperty("Content-Type", "application/json;charset=utf-8"); + connection.connect(); + + BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); + StringBuilder buffer = new StringBuilder(); + String line; + + while((line = br.readLine()) != null) + { + buffer.append(line).append("\n"); + } + + br.close(); + + result = buffer.toString(); + result = StringUtils.strip(result, "[]"); + } + catch(IOException e) + { + log.warn("Error while get json request: {} ", request, e); + } + } + + return result; + } + + private boolean sendAction(String request, String action) + { + // Check login was successful + if (login()) { + + // Post action into Cloud service + try + { + URL url = new URL(HOMEWIZARD_API_URL + request); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("X-Session-Token", cloudSessionId); + connection.setRequestProperty("Content-Type", "application/json;charset=utf-8"); + + JsonObject actionJson = new JsonObject(); + actionJson.addProperty("action", action); + + OutputStream os = connection.getOutputStream(); + os.write(actionJson.toString().getBytes("UTF-8")); + os.close(); + } + catch(IOException e) + { + log.warn("Error while post json action: {} ", request, e); + return false; + } + } + else + { + return false; + } + + return true; + } + + public List getDevices() + { + List homewizardDevices = new ArrayList<>(); + try { + + String result = requestJson(EMPTY_STRING); + JSONObject resultJson = new JSONObject(result); + cloudPlugId = resultJson.getString("id"); + + String all_devices_json = resultJson.get("devices").toString(); + Device[] devices = gson.fromJson(all_devices_json, Device[].class); + + // Fix names from JSON + for (Device device : devices) { + device.setTypeName(StringUtils.capitalize(device.getTypeName().replace("_", " "))); + homewizardDevices.add(mapDeviceToHomeWizardSmartPlugDevice(device)); + } + } + catch(JSONException e) { + log.warn("Error while get devices from cloud service ", e); + } + + log.info("Found: " + homewizardDevices.size() + " devices"); + return homewizardDevices; + } + + public void execApply(String jsonToPost) { + try + { + JSONObject resultJson = new JSONObject(jsonToPost); + String deviceId = resultJson.getString("deviceid"); + String action = resultJson.getString("action"); + + sendAction("/" + cloudPlugId + "/devices/" + deviceId + "/action", action); + } + catch(JSONException e) { + log.warn("Error while get devices from cloud service ", e); + } + } + + protected HomeWizardSmartPlugDevice mapDeviceToHomeWizardSmartPlugDevice(Device device) { + HomeWizardSmartPlugDevice homewizardDevice = new HomeWizardSmartPlugDevice(); + homewizardDevice.setId(device.getId()); + homewizardDevice.setGateway(cloudPlugName); + homewizardDevice.setName(device.getName()); + homewizardDevice.setTypeName(device.getTypeName()); + + return homewizardDevice; + } +} diff --git a/src/main/java/com/bwssystems/HABridge/plugins/homewizard/json/Device.java b/src/main/java/com/bwssystems/HABridge/plugins/homewizard/json/Device.java new file mode 100644 index 0000000..01052e7 --- /dev/null +++ b/src/main/java/com/bwssystems/HABridge/plugins/homewizard/json/Device.java @@ -0,0 +1,49 @@ +package com.bwssystems.HABridge.plugins.homewizard.json; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +/** + * Control HomeWizard devices over HomeWizard Cloud + * + * @author Björn Rennfanz (bjoern@fam-rennfanz.de) + * + */ +public class Device { + + @SerializedName("id") + @Expose + private String id; + + @SerializedName("name") + @Expose + private String name; + + @SerializedName("typeName") + @Expose + private String typeName; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getTypeName() { + return this.typeName; + } + + public void setTypeName(String typeName) { + this.typeName = typeName; + } +} From a2136723415d9d9da6c8e33161fb8ce904c269cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Rennfanz?= Date: Sat, 9 Dec 2017 21:17:05 +0100 Subject: [PATCH 3/4] Add web part of plug-in for HomeWizard SmartPlug support --- src/main/resources/public/scripts/app.js | 196 +++++++++++++++++- .../resources/public/views/configuration.html | 1 + .../public/views/domoticzdevice.html | 1 + .../resources/public/views/editdevice.html | 1 + .../resources/public/views/fibarodevice.html | 1 + .../resources/public/views/fibaroscene.html | 1 + .../resources/public/views/haldevice.html | 1 + .../public/views/harmonyactivity.html | 1 + .../resources/public/views/harmonydevice.html | 1 + .../resources/public/views/hassdevice.html | 1 + .../public/views/homewizarddevice.html | 127 ++++++++++++ .../resources/public/views/huedevice.html | 1 + .../resources/public/views/lifxdevice.html | 1 + src/main/resources/public/views/logs.html | 1 + .../resources/public/views/mqttpublish.html | 1 + .../resources/public/views/nestactions.html | 1 + .../resources/public/views/somfydevice.html | 1 + src/main/resources/public/views/system.html | 48 +++++ .../resources/public/views/veradevice.html | 1 + .../resources/public/views/verascene.html | 1 + 20 files changed, 382 insertions(+), 6 deletions(-) create mode 100644 src/main/resources/public/views/homewizarddevice.html diff --git a/src/main/resources/public/scripts/app.js b/src/main/resources/public/scripts/app.js index 0b8ae5d..f29c6d1 100644 --- a/src/main/resources/public/scripts/app.js +++ b/src/main/resources/public/scripts/app.js @@ -62,7 +62,11 @@ app.config (function ($locationProvider, $routeProvider) { }).when ('/hassdevices', { templateUrl: 'views/hassdevice.html', controller: 'HassController', - requiresAuthentication: true + requiresAuthentication: true + }).when ('/homewizarddevices', { + templateUrl: 'views/homewizarddevice.html', + controller: 'HomeWizardController', + requiresAuthentication: true }).when ('/domoticzdevices', { templateUrl: 'views/domoticzdevice.html', controller: 'DomoticzController', @@ -143,7 +147,7 @@ app.service ('bridgeService', function ($rootScope, $http, $base64, $location, n this.state = {base: "./api/devices", bridgelocation: ".", systemsbase: "./system", huebase: "./api", configs: [], backups: [], devices: [], device: {}, mapandid: [], type: "", settings: [], myToastMsg: [], logMsgs: [], loggerInfo: [], mapTypes: [], olddevicename: "", logShowAll: false, isInControl: false, showVera: false, showFibaro: false, showHarmony: false, showNest: false, showHue: false, showHal: false, showMqtt: false, showHass: false, - showDomoticz: false, showSomfy: false, showLifx: false, habridgeversion: {}, viewDevId: "", queueDevId: "", securityInfo: {}, filterDevicesByIpAddress: null, + showHomeWizard: false, showDomoticz: false, showSomfy: false, showLifx: false, habridgeversion: {}, viewDevId: "", queueDevId: "", securityInfo: {}, filterDevicesByIpAddress: null, filterDevicesOnlyFiltered: false, filterDeviceType: null}; this.displayWarn = function(errorTitle, error) { @@ -504,11 +508,16 @@ app.service ('bridgeService', function ($rootScope, $http, $base64, $location, n return; } - this.updateShowHass = function () { - this.state.showHass = self.state.settings.hassconfigured; + this.updateShowHomeWizard = function () { + this.state.showHomeWizard = self.state.settings.homewizardconfigured; return; } + this.updateShowHass = function () { + this.state.showHass = self.state.settings.hassconfigured; + return; + } + this.updateShowDomoticz = function () { this.state.showDomoticz = self.state.settings.domoticzconfigured; return; @@ -536,6 +545,7 @@ app.service ('bridgeService', function ($rootScope, $http, $base64, $location, n self.updateShowHal(); self.updateShowMqtt(); self.updateShowHass(); + self.updateShowHomeWizard(); self.updateShowDomoticz(); self.updateShowSomfy(); self.updateShowLifx(); @@ -781,6 +791,22 @@ app.service ('bridgeService', function ($rootScope, $http, $base64, $location, n ); }; + this.viewHomeWizardDevices = function () { + if (!this.state.showHomeWizard) + return; + return $http.get(this.state.base + "/homewizard/devices").then( + function (response) { + self.state.homewizarddevices = response.data; + }, + function (error) { + if (error.status === 401) + $rootScope.$broadcast('securityReinit', 'done'); + else + self.displayWarn("Get HomeWizard Devices Error: ", error); + } + ); + }; + this.viewDomoticzDevices = function () { if (!this.state.showDomoticz) return; @@ -1468,6 +1494,24 @@ app.controller ('SystemController', function ($scope, $location, bridgeService, } } }; + $scope.addHomeWizardtoSettings = function (newhomewizardname, newhomewizardip, newhomewizardusername, newhomewizardpassword) { + if($scope.bridge.settings.homewizardaddress === undefined || $scope.bridge.settings.homewizardaddress === null) { + $scope.bridge.settings.homewizardaddress = { devices: [] }; + } + var newhomewizard = { name: newhomewizardname, ip: newhomewizardip, username: newhomewizardusername, password: newhomewizardpassword } + $scope.bridge.settings.homewizardaddress.devices.push(newhomewizard); + $scope.newhomewizardname = null; + $scope.newhomewizardip = null; + $scope.newhomewizardusername = null; + $scope.newhomewizardpassword = null; + }; + $scope.removeHomeWizardtoSettings = function (homewizardname, homewizardip) { + for(var i = $scope.bridge.settings.homewizardaddress.devices.length - 1; i >= 0; i--) { + if($scope.bridge.settings.homewizardaddress.devices[i].name === homewizardname && $scope.bridge.settings.homewizardaddress.devices[i].ip === homewizardip) { + $scope.bridge.settings.homewizardaddress.devices.splice(i, 1); + } + } + }; $scope.addDomoticztoSettings = function (newdomoticzname, newdomoticzip, newdomoticzport, newdomoticzusername, newdomoticzpassword) { if($scope.bridge.settings.domoticzaddress === undefined || $scope.bridge.settings.domoticzaddress === null) { $scope.bridge.settings.domoticzaddress = { devices: [] }; @@ -2782,7 +2826,7 @@ app.controller('HassController', function ($scope, $location, bridgeService, ngD $scope.device = bridgeService.state.device; }; - $scope.buildDeviceUrls = function (hassdevice, dim_control, buildonly) { + $scope.buildDeviceUrls = function (hassdevice, buildonly) { onpayload = "{\"entityId\":\"" + hassdevice.deviceState.entity_id + "\",\"hassName\":\"" + hassdevice.hassname + "\",\"state\":\"on\"}"; if((dim_control.indexOf("byte") >= 0 || dim_control.indexOf("percent") >= 0 || dim_control.indexOf("math") >= 0)) dimpayload = "{\"entityId\":\"" + hassdevice.deviceState.entity_id + "\",\"hassName\":\"" + hassdevice.hassname + "\",\"state\":\"on\",\"bri\":\"" + dim_control + "\"}"; @@ -2798,7 +2842,7 @@ app.controller('HassController', function ($scope, $location, bridgeService, ngD } }; - $scope.bulkAddDevices = function(dim_control) { + $scope.bulkAddDevices = function() { var devicesList = []; $scope.clearDevice(); for(var i = 0; i < $scope.bulk.devices.length; i++) { @@ -2894,6 +2938,132 @@ app.controller('HassController', function ($scope, $location, bridgeService, ngD }; }); +app.controller('HomeWizardController', function ($scope, $location, bridgeService, ngDialog) { + $scope.bridge = bridgeService.state; + $scope.device = bridgeService.state.device; + $scope.device_dim_control = ""; + $scope.bulk = { devices: [] }; + $scope.selectAll = false; + bridgeService.viewHomeWizardDevices(); + $scope.imgButtonsUrl = "glyphicon glyphicon-plus"; + $scope.buttonsVisible = false; + + $scope.clearDevice = function () { + bridgeService.clearDevice(); + $scope.device = bridgeService.state.device; + }; + + $scope.buildDeviceUrls = function (homewizarddevice, buildonly) { + + dimpayload = "{\"deviceid\":\"" + homewizarddevice.id + "\",\"action\":\"on\"}"; + onpayload = "{\"deviceid\":\"" + homewizarddevice.id + "\",\"action\":\"on\"}"; + offpayload = "{\"deviceid\":\"" + homewizarddevice.id + "\",\"action\":\"off\"}"; + + bridgeService.buildUrls(onpayload, dimpayload, offpayload, null, true, homewizarddevice.id ,homewizarddevice.name, homewizarddevice.gateway, null, "homewizardDevice", null, null); + $scope.device = bridgeService.state.device; + + if (!buildonly) { + bridgeService.editNewDevice($scope.device); + $location.path('/editdevice'); + } + }; + + $scope.bulkAddDevices = function() { + var devicesList = []; + $scope.clearDevice(); + for(var i = 0; i < $scope.bulk.devices.length; i++) { + for(var x = 0; x < bridgeService.state.homewizarddevices.length; x++) { + if(bridgeService.state.homewizarddevices[x].id === $scope.bulk.devices[i]) { + $scope.buildDeviceUrls(bridgeService.state.homewizarddevices[x],true); + devicesList[i] = { + name: $scope.device.name, + mapId: $scope.device.mapId, + mapType: $scope.device.mapType, + deviceType: $scope.device.deviceType, + targetDevice: $scope.device.targetDevice, + onUrl: $scope.device.onUrl, + dimUrl: $scope.device.dimUrl, + offUrl: $scope.device.offUrl, + colorUrl: $scope.device.colorUrl, + headers: $scope.device.headers, + httpVerb: $scope.device.httpVerb, + contentType: $scope.device.contentType, + contentBody: $scope.device.contentBody, + contentBodyDim: $scope.device.contentBodyDim, + contentBodyOff: $scope.device.contentBodyOff + }; + $scope.clearDevice(); + } + } + } + bridgeService.bulkAddDevice(devicesList).then( + function () { + $scope.clearDevice(); + bridgeService.viewDevices(); + bridgeService.viewHalDevices(); + }, + function (error) { + bridgeService.displayWarn("Error adding HomeWizard devices in bulk.", error) + } + ); + $scope.bulk = { devices: [] }; + $scope.selectAll = false; + }; + + $scope.toggleSelection = function toggleSelection(deviceId) { + var idx = $scope.bulk.devices.indexOf(deviceId); + + // is currently selected + if (idx > -1) { + $scope.bulk.devices.splice(idx, 1); + if($scope.bulk.devices.length === 0 && $scope.selectAll) + $scope.selectAll = false; + } + + // is newly selected + else { + $scope.bulk.devices.push(deviceId); + $scope.selectAll = true; + } + }; + + $scope.toggleSelectAll = function toggleSelectAll() { + if($scope.selectAll) { + $scope.selectAll = false; + $scope.bulk = { devices: [] }; + } + else { + $scope.selectAll = true; + for(var x = 0; x < bridgeService.state.homewizarddevices.length; x++) { + if($scope.bulk.devices.indexOf(bridgeService.state.homewizarddevices[x]) < 0) + $scope.bulk.devices.push(bridgeService.state.homewizarddevices[x].devicename); + } + } + }; + + $scope.toggleButtons = function () { + $scope.buttonsVisible = !$scope.buttonsVisible; + if($scope.buttonsVisible) + $scope.imgButtonsUrl = "glyphicon glyphicon-minus"; + else + $scope.imgButtonsUrl = "glyphicon glyphicon-plus"; + }; + + $scope.deleteDevice = function (device) { + $scope.bridge.device = device; + ngDialog.open({ + template: 'deleteDialog', + controller: 'DeleteDialogCtrl', + className: 'ngdialog-theme-default' + }); + }; + + $scope.editDevice = function (device) { + bridgeService.editDevice(device); + $location.path('/editdevice'); + }; +}); + app.controller('DomoticzController', function ($scope, $location, bridgeService, ngDialog) { $scope.bridge = bridgeService.state; $scope.device = bridgeService.state.device; @@ -3705,6 +3875,20 @@ app.filter('configuredSomfyDevices', function (bridgeService) { } }); +app.filter('configuredHomeWizardDevices', function (bridgeService) { + return function(input) { + var out = []; + if(input === undefined || input === null || input.length === undefined) + return out; + for (var i = 0; i < input.length; i++) { + if(bridgeService.deviceContainsType(input[i], "homewizardDevice")){ + out.push(input[i]); + } + } + return out; + } +}); + app.filter('filterDevicesByRequester', function () { return function(input,search,mustContain,deviceType) { var out = []; diff --git a/src/main/resources/public/views/configuration.html b/src/main/resources/public/views/configuration.html index eebbbdd..96b73a6 100644 --- a/src/main/resources/public/views/configuration.html +++ b/src/main/resources/public/views/configuration.html @@ -23,6 +23,7 @@ href="#!/haldevices">HAL Devices
  • MQTT Messages
  • HomeAssistant Devices
  • +
  • HomeWizard Devices
  • Domoticz Devices
  • Somfy Devices
  • LIFX Devices
  • diff --git a/src/main/resources/public/views/domoticzdevice.html b/src/main/resources/public/views/domoticzdevice.html index 0d32594..4dfb914 100644 --- a/src/main/resources/public/views/domoticzdevice.html +++ b/src/main/resources/public/views/domoticzdevice.html @@ -21,6 +21,7 @@ Devices
  • MQTT Messages
  • HomeAssistant Devices
  • +
  • HomeWizard Devices
  • Somfy Devices
  • LIFX Devices
  • diff --git a/src/main/resources/public/views/editdevice.html b/src/main/resources/public/views/editdevice.html index 457806c..a28e5bc 100644 --- a/src/main/resources/public/views/editdevice.html +++ b/src/main/resources/public/views/editdevice.html @@ -23,6 +23,7 @@ href="#!/mqttmessages">MQTT Messages
  • HomeAssistant Devices
  • +
  • HomeWizard Devices
  • Domoticz Devices
  • Somfy Devices
  • LIFX Devices
  • diff --git a/src/main/resources/public/views/fibarodevice.html b/src/main/resources/public/views/fibarodevice.html index 95e428c..eefe91d 100644 --- a/src/main/resources/public/views/fibarodevice.html +++ b/src/main/resources/public/views/fibarodevice.html @@ -17,6 +17,7 @@ href="#!/haldevices">HAL Devices
  • MQTT Messages
  • HomeAssistant Devices
  • +
  • HomeWizard Devices
  • Domoticz Devices
  • Somfy Devices
  • LIFX Devices
  • diff --git a/src/main/resources/public/views/fibaroscene.html b/src/main/resources/public/views/fibaroscene.html index c5c35fa..f23e9f5 100644 --- a/src/main/resources/public/views/fibaroscene.html +++ b/src/main/resources/public/views/fibaroscene.html @@ -17,6 +17,7 @@ href="#!/haldevices">HAL Devices
  • MQTT Messages
  • HomeAssistant Devices
  • +
  • HomeWizard Devices
  • Domoticz Devices
  • Somfy Devices
  • LIFX Devices
  • diff --git a/src/main/resources/public/views/haldevice.html b/src/main/resources/public/views/haldevice.html index 37f6bc7..88baa9f 100644 --- a/src/main/resources/public/views/haldevice.html +++ b/src/main/resources/public/views/haldevice.html @@ -20,6 +20,7 @@ Devices
  • MQTT Messages
  • HomeAssistant Devices
  • +
  • HomeWizard Devices
  • Domoticz Devices
  • Somfy Devices
  • LIFX Devices
  • diff --git a/src/main/resources/public/views/harmonyactivity.html b/src/main/resources/public/views/harmonyactivity.html index 3b63c91..093b4db 100644 --- a/src/main/resources/public/views/harmonyactivity.html +++ b/src/main/resources/public/views/harmonyactivity.html @@ -21,6 +21,7 @@ href="#!/haldevices">HAL Devices
  • MQTT Messages
  • HomeAssistant Devices
  • +
  • HomeWizard Devices
  • Domoticz Devices
  • Somfy Devices
  • LIFX Devices
  • diff --git a/src/main/resources/public/views/harmonydevice.html b/src/main/resources/public/views/harmonydevice.html index a62adf0..9cd2f5b 100644 --- a/src/main/resources/public/views/harmonydevice.html +++ b/src/main/resources/public/views/harmonydevice.html @@ -21,6 +21,7 @@ href="#!/haldevices">HAL Devices
  • MQTT Messages
  • HomeAssistant Devices
  • +
  • HomeWizard Devices
  • Domoticz Devices
  • Somfy Devices
  • LIFX Devices
  • diff --git a/src/main/resources/public/views/hassdevice.html b/src/main/resources/public/views/hassdevice.html index 71d7525..66e81ec 100644 --- a/src/main/resources/public/views/hassdevice.html +++ b/src/main/resources/public/views/hassdevice.html @@ -19,6 +19,7 @@ href="#!/huedevices">Hue Devices
  • HAL Devices
  • +
  • HomeWizard Devices
  • MQTT Messages
  • Domoticz Devices
  • diff --git a/src/main/resources/public/views/homewizarddevice.html b/src/main/resources/public/views/homewizarddevice.html new file mode 100644 index 0000000..40c8727 --- /dev/null +++ b/src/main/resources/public/views/homewizarddevice.html @@ -0,0 +1,127 @@ + + +
    +
    +

    HomeWizard Device List + ({{bridge.homewizarddevices.length}})

    +
    +
    +

    For any HomeWizard Device, use the build action buttons + to generate the item addition information into the ha-bridge device + and this will put you into the edit screen. Then + you can modify the name to anything you want that will be the keyword + for the Echo or Google Home. Also, you can go back to any helper tab and click a build + action button to add another item for a multi-command. After you are + done in the edit tab, click the 'Add Bridge Device' to finish that selection + setup. The 'Already Configured HomeWizard Devices' list below will show + what is already setup for your HomeWizard Gateway.

    +

    Use the check boxes by the names to use the bulk addition + feature. Select your items, then click + bulk add below. Your items will be added with the name of the device from the HomeWizard Cloud.

    +
    + + + + + + + + + + + + + + + + + + +
    Row NameIdTypeBuild Actions
    {{$index+1}} + {{homewizarddevice.name}}{{homewizarddevice.id}}{{homewizarddevice.typeName}} + + +
    +
    + +
    + +
    +
    +

    + Already Configured HomeWizard Devices +

    +
    +
    + + + + + + + + + + + + + + + + + + +
    RowNameHomeWizard GatewayMap IdActions
    {{$index+1}}{{device.name}}{{device.targetDevice}}{{device.mapId}} +

    + + +

    +
    +
    +
    +
    + diff --git a/src/main/resources/public/views/huedevice.html b/src/main/resources/public/views/huedevice.html index 6855894..b02f0e0 100644 --- a/src/main/resources/public/views/huedevice.html +++ b/src/main/resources/public/views/huedevice.html @@ -21,6 +21,7 @@ href="#!/haldevices">HAL Devices
  • MQTT Messages
  • HomeAssistant Devices
  • +
  • HomeWizard Devices
  • Domoticz Devices
  • Somfy Devices
  • LIFX Devices
  • diff --git a/src/main/resources/public/views/lifxdevice.html b/src/main/resources/public/views/lifxdevice.html index c220e3b..4f8bdf6 100644 --- a/src/main/resources/public/views/lifxdevice.html +++ b/src/main/resources/public/views/lifxdevice.html @@ -13,6 +13,7 @@
  • HAL Devices
  • MQTT Messages
  • HomeAssistant Devices
  • +
  • HomeWizard Devices
  • Domoticz Devices
  • Somfy Devices
  • diff --git a/src/main/resources/public/views/logs.html b/src/main/resources/public/views/logs.html index fca1aee..f31d666 100644 --- a/src/main/resources/public/views/logs.html +++ b/src/main/resources/public/views/logs.html @@ -21,6 +21,7 @@ href="#!/haldevices">HAL Devices
  • MQTT Messages
  • HomeAssistant Devices
  • +
  • HomeWizard Devices
  • Domoticz Devices
  • Somfy Devices
  • LIFX Devices
  • diff --git a/src/main/resources/public/views/mqttpublish.html b/src/main/resources/public/views/mqttpublish.html index 940b211..593a4af 100644 --- a/src/main/resources/public/views/mqttpublish.html +++ b/src/main/resources/public/views/mqttpublish.html @@ -13,6 +13,7 @@
  • HAL Devices
  • HomeAssistant Devices
  • +
  • HomeWizard Devices
  • Domoticz Devices
  • Somfy Devices
  • LIFX Devices
  • diff --git a/src/main/resources/public/views/nestactions.html b/src/main/resources/public/views/nestactions.html index 34875b9..1563e47 100644 --- a/src/main/resources/public/views/nestactions.html +++ b/src/main/resources/public/views/nestactions.html @@ -21,6 +21,7 @@ href="#!/haldevices">HAL Devices
  • MQTT Messages
  • HomeAssistant Devices
  • +
  • HomeWizard Devices
  • Domoticz Devices
  • Somfy Devices
  • LIFX Devices
  • diff --git a/src/main/resources/public/views/somfydevice.html b/src/main/resources/public/views/somfydevice.html index 8bd5000..5f5dd19 100644 --- a/src/main/resources/public/views/somfydevice.html +++ b/src/main/resources/public/views/somfydevice.html @@ -17,6 +17,7 @@ href="#!/haldevices">HAL Devices
  • MQTT Messages
  • HomeAssistant Devices
  • +
  • HomeWizard Devices
  • Domoticz Devices
  • Add/Edit
  • diff --git a/src/main/resources/public/views/system.html b/src/main/resources/public/views/system.html index 603c4c7..8e08304 100644 --- a/src/main/resources/public/views/system.html +++ b/src/main/resources/public/views/system.html @@ -22,6 +22,7 @@ href="#!/haldevices">HAL Devices
  • MQTT Messages
  • HomeAssistant Devices
  • +
  • HomeWizard Devices
  • Domoticz Devices
  • Somfy Devices
  • LIFX Devices
  • @@ -417,6 +418,53 @@ + + HomeWizard Gateways + + + + + + + + + + + + + + + + + + + + + + + + +
    NameIPUsernamePasswordManage
    + Domoticz Names and IP Addresses HAL Devices
  • MQTT Messages
  • HomeAssistant Devices
  • +
  • HomeWizard Devices
  • Domoticz Devices
  • Somfy Devices
  • LIFX Devices
  • diff --git a/src/main/resources/public/views/verascene.html b/src/main/resources/public/views/verascene.html index 0357754..c793a89 100644 --- a/src/main/resources/public/views/verascene.html +++ b/src/main/resources/public/views/verascene.html @@ -17,6 +17,7 @@ href="#!/haldevices">HAL Devices
  • MQTT Messages
  • HomeAssistant Devices
  • +
  • HomeWizard Devices
  • Domoticz Devices
  • Somfy Devices
  • LIFX Devices
  • From bdf5770ba00ba4d8e515903a491433ccec9bf330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Rennfanz?= Date: Sat, 9 Dec 2017 14:27:27 +0100 Subject: [PATCH 4/4] Some corrections after rebasing code of HomeWizard SmartPlug plug-in --- .../plugins/homewizard/HomeWizardHome.java | 1 - .../homewizard/HomeWizzardSmartPlugInfo.java | 63 +++++++++++++------ src/main/resources/public/scripts/app.js | 4 +- 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizardHome.java b/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizardHome.java index 02d7278..b80fd1e 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizardHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizardHome.java @@ -65,7 +65,6 @@ public class HomeWizardHome implements Home { + lightId + "state\"}}]"; } else { try { - homeWizzardHandler.execApply(jsonToPost); } catch (Exception e) { diff --git a/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizzardSmartPlugInfo.java b/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizzardSmartPlugInfo.java index e27b2a2..4620d64 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizzardSmartPlugInfo.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizzardSmartPlugInfo.java @@ -130,37 +130,59 @@ public class HomeWizzardSmartPlugInfo { private boolean sendAction(String request, String action) { + boolean result = true; + // Check login was successful if (login()) { - + // Post action into Cloud service try { URL url = new URL(HOMEWIZARD_API_URL + request); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("POST"); - connection.setRequestProperty("X-Session-Token", cloudSessionId); - connection.setRequestProperty("Content-Type", "application/json;charset=utf-8"); - + JsonObject actionJson = new JsonObject(); - actionJson.addProperty("action", action); + actionJson.addProperty("action", StringUtils.capitalize(action)); + + connection.setRequestMethod("POST"); + connection.setDoOutput(true); + connection.setRequestProperty("X-Session-Token", cloudSessionId); + connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); OutputStream os = connection.getOutputStream(); os.write(actionJson.toString().getBytes("UTF-8")); os.close(); + + BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); + StringBuilder buffer = new StringBuilder(); + String line; + + while((line = br.readLine()) != null) + { + buffer.append(line).append("\n"); + } + + br.close(); + connection.disconnect(); + + // Check if request was Ok + if (!buffer.toString().contains("Success")) + { + result = false; + } } catch(IOException e) { log.warn("Error while post json action: {} ", request, e); - return false; + result = false; } } else { - return false; + result = false; } - return true; + return result; } public List getDevices() @@ -189,17 +211,22 @@ public class HomeWizzardSmartPlugInfo { return homewizardDevices; } - public void execApply(String jsonToPost) { - try - { - JSONObject resultJson = new JSONObject(jsonToPost); - String deviceId = resultJson.getString("deviceid"); - String action = resultJson.getString("action"); + public void execApply(String jsonToPost) throws JSONException, IOException { - sendAction("/" + cloudPlugId + "/devices/" + deviceId + "/action", action); + // Extract + JSONObject resultJson = new JSONObject(jsonToPost); + String deviceId = resultJson.getString("deviceid"); + String action = resultJson.getString("action"); + + // Check if we have an plug id stored + if (StringUtils.isBlank(cloudPlugId)) { + getDevices(); } - catch(JSONException e) { - log.warn("Error while get devices from cloud service ", e); + + // Send request to HomeWizard cloud + if (!sendAction("/" + cloudPlugId + "/devices/" + deviceId + "/action", action)) + { + throw new IOException("Send action to HomeWizard Cloud failed."); } } diff --git a/src/main/resources/public/scripts/app.js b/src/main/resources/public/scripts/app.js index f29c6d1..9ec0790 100644 --- a/src/main/resources/public/scripts/app.js +++ b/src/main/resources/public/scripts/app.js @@ -2826,7 +2826,7 @@ app.controller('HassController', function ($scope, $location, bridgeService, ngD $scope.device = bridgeService.state.device; }; - $scope.buildDeviceUrls = function (hassdevice, buildonly) { + $scope.buildDeviceUrls = function (hassdevice, dim_control, buildonly) { onpayload = "{\"entityId\":\"" + hassdevice.deviceState.entity_id + "\",\"hassName\":\"" + hassdevice.hassname + "\",\"state\":\"on\"}"; if((dim_control.indexOf("byte") >= 0 || dim_control.indexOf("percent") >= 0 || dim_control.indexOf("math") >= 0)) dimpayload = "{\"entityId\":\"" + hassdevice.deviceState.entity_id + "\",\"hassName\":\"" + hassdevice.hassname + "\",\"state\":\"on\",\"bri\":\"" + dim_control + "\"}"; @@ -2842,7 +2842,7 @@ app.controller('HassController', function ($scope, $location, bridgeService, ngD } }; - $scope.bulkAddDevices = function() { + $scope.bulkAddDevices = function(dim_control) { var devicesList = []; $scope.clearDevice(); for(var i = 0; i < $scope.bulk.devices.length; i++) {