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++) {