Some corrections after rebasing code of HomeWizard SmartPlug plug-in

This commit is contained in:
Björn Rennfanz
2017-12-09 14:27:27 +01:00
parent a213672341
commit bdf5770ba0
3 changed files with 47 additions and 21 deletions

View File

@@ -65,7 +65,6 @@ public class HomeWizardHome implements Home {
+ lightId + "state\"}}]"; + lightId + "state\"}}]";
} else { } else {
try { try {
homeWizzardHandler.execApply(jsonToPost); homeWizzardHandler.execApply(jsonToPost);
} catch (Exception e) { } catch (Exception e) {

View File

@@ -130,37 +130,59 @@ public class HomeWizzardSmartPlugInfo {
private boolean sendAction(String request, String action) private boolean sendAction(String request, String action)
{ {
boolean result = true;
// Check login was successful // Check login was successful
if (login()) { if (login()) {
// Post action into Cloud service // Post action into Cloud service
try try
{ {
URL url = new URL(HOMEWIZARD_API_URL + request); URL url = new URL(HOMEWIZARD_API_URL + request);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 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(); 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(); OutputStream os = connection.getOutputStream();
os.write(actionJson.toString().getBytes("UTF-8")); os.write(actionJson.toString().getBytes("UTF-8"));
os.close(); 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) catch(IOException e)
{ {
log.warn("Error while post json action: {} ", request, e); log.warn("Error while post json action: {} ", request, e);
return false; result = false;
} }
} }
else else
{ {
return false; result = false;
} }
return true; return result;
} }
public List<HomeWizardSmartPlugDevice> getDevices() public List<HomeWizardSmartPlugDevice> getDevices()
@@ -189,17 +211,22 @@ public class HomeWizzardSmartPlugInfo {
return homewizardDevices; return homewizardDevices;
} }
public void execApply(String jsonToPost) { public void execApply(String jsonToPost) throws JSONException, IOException {
try
{
JSONObject resultJson = new JSONObject(jsonToPost);
String deviceId = resultJson.getString("deviceid");
String action = resultJson.getString("action");
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.");
} }
} }

View File

@@ -2826,7 +2826,7 @@ app.controller('HassController', function ($scope, $location, bridgeService, ngD
$scope.device = bridgeService.state.device; $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\"}"; 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)) 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 + "\"}"; 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 = []; var devicesList = [];
$scope.clearDevice(); $scope.clearDevice();
for(var i = 0; i < $scope.bulk.devices.length; i++) { for(var i = 0; i < $scope.bulk.devices.length; i++) {