From 3313548ec21e1f4576a3e178b6b66a605759f370 Mon Sep 17 00:00:00 2001 From: bsamuels Date: Tue, 30 Jan 2018 16:30:55 -0600 Subject: [PATCH] Updated Broadlink for port binding, added debug for RM2 Device ir call data return, updated Harmony library to be the latest version. --- pom.xml | 14 +++++----- .../com/bwssystems/HABridge/HABridge.java | 1 + .../plugins/broadlink/BroadlinkHome.java | 18 ++++++++++-- .../homewizard/HomeWizzardSmartPlugInfo.java | 28 ++++++++++--------- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/pom.xml b/pom.xml index c329b82..118516f 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.bwssystems.HABridge ha-bridge - 5.2.0RC10 + 5.2.0RC11 jar HA Bridge @@ -33,7 +33,7 @@ com.github.bwssytems harmony-java-client - 1.1.5 + master-SNAPSHOT org.slf4j @@ -84,12 +84,12 @@ org.slf4j slf4j-api - 1.7.5 + 1.7.24 ch.qos.logback logback-classic - 1.1.5 + 1.2.1 com.google.code.gson @@ -104,12 +104,12 @@ com.google.inject guice - 4.0-beta4 + 4.1.0 org.igniterealtime.smack smack-core - 4.0.7 + 4.2.0 org.eclipse.paho @@ -119,7 +119,7 @@ junit junit - 4.11 + 4.12 com.github.bwssytems diff --git a/src/main/java/com/bwssystems/HABridge/HABridge.java b/src/main/java/com/bwssystems/HABridge/HABridge.java index 12bc450..368cc2f 100644 --- a/src/main/java/com/bwssystems/HABridge/HABridge.java +++ b/src/main/java/com/bwssystems/HABridge/HABridge.java @@ -45,6 +45,7 @@ public class HABridge { @SuppressWarnings("unused") HttpClientPool thePool; + log.info("HA Bridge startup sequence..."); theVersion = new Version(); // Singleton initialization thePool = new HttpClientPool(); diff --git a/src/main/java/com/bwssystems/HABridge/plugins/broadlink/BroadlinkHome.java b/src/main/java/com/bwssystems/HABridge/plugins/broadlink/BroadlinkHome.java index a8a4bcf..8916ad0 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/broadlink/BroadlinkHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/broadlink/BroadlinkHome.java @@ -1,6 +1,8 @@ package com.bwssystems.HABridge.plugins.broadlink; import java.io.IOException; +import java.net.BindException; +import java.net.DatagramPacket; import java.net.InetAddress; import java.util.ArrayList; import java.util.HashMap; @@ -62,13 +64,15 @@ public class BroadlinkHome implements Home { log.info("Broadlink Home created." + (validBroadlink ? "" : " No Broadlinks configured.") + (isDevMode ? " DevMode is set." : "")); if(validBroadlink) { broadlinkMap = new HashMap(); + int aDiscoverPort = Configuration.BROADLINK_DISCOVER_PORT; + while(aDiscoverPort > 0) { try { log.info("Broadlink discover...."); if(isDevMode) { - clients = TestBLDevice.discoverDevices(InetAddress.getByName(bridgeSettings.getBridgeSettingsDescriptor().getUpnpConfigAddress()), Configuration.BROADLINK_DISCOVER_PORT, Configuration.BROADLINK_DISCONVER_TIMEOUT); + clients = TestBLDevice.discoverDevices(InetAddress.getByName(bridgeSettings.getBridgeSettingsDescriptor().getUpnpConfigAddress()), aDiscoverPort, Configuration.BROADLINK_DISCONVER_TIMEOUT); } else - clients = BLDevice.discoverDevices(InetAddress.getByName(bridgeSettings.getBridgeSettingsDescriptor().getUpnpConfigAddress()), Configuration.BROADLINK_DISCOVER_PORT, Configuration.BROADLINK_DISCONVER_TIMEOUT); + clients = BLDevice.discoverDevices(InetAddress.getByName(bridgeSettings.getBridgeSettingsDescriptor().getUpnpConfigAddress()), aDiscoverPort, Configuration.BROADLINK_DISCONVER_TIMEOUT); if(clients.length <= 0) { log.warn("Did not discover any Broadlinks, try again with bridge reinitialization"); broadlinkMap = null; @@ -83,12 +87,18 @@ public class BroadlinkHome implements Home { log.debug("Ignoring A1 Device - host: " + clients[i].getHost() + ", device Type: " + clients[i].getDeviceDescription() + ", mac: " + (clients[i].getMac() == null ? "no Mac in client" : clients[i].getMac().getMacString())); } } + } catch (BindException e) { + log.warn("Could not discover Broadlinks, Port in use, increasing by 11"); + aDiscoverPort += 11; + if(aDiscoverPort > Configuration.BROADLINK_DISCOVER_PORT + 110) + aDiscoverPort = 0; } catch (IOException e) { log.warn("Could not discover Broadlinks, with IO Exception", e); broadlinkMap = null; validBroadlink = false; return this; } + } } return this; } @@ -229,7 +239,9 @@ public class BroadlinkHome implements Home { try { if(!isDevMode) theDevice.auth(); - ((RM2Device) theDevice).sendCmdPkt(Configuration.BROADLINK_DISCONVER_TIMEOUT, thePayload); + DatagramPacket thePacket = ((RM2Device) theDevice).sendCmdPkt(Configuration.BROADLINK_DISCONVER_TIMEOUT, thePayload); + String returnData = DatatypeConverter.printHexBinary(thePacket.getData()); + log.debug("RM2 Device data return: <<<" + returnData + ">>>"); } catch (IOException e) { log.error("Call to " + _rm2 + " device failed with exception.", e); theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId 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 4620d64..9443f0d 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizzardSmartPlugInfo.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/homewizard/HomeWizzardSmartPlugInfo.java @@ -19,9 +19,7 @@ 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; +import com.google.gson.JsonParser; /** * Control HomeWizard devices over HomeWizard Cloud @@ -75,10 +73,12 @@ public class HomeWizzardSmartPlugInfo { br.close(); // Get session id from result JSON - JSONObject json = new JSONObject(buffer.toString()); - cloudSessionId = json.get("session").toString(); + JsonParser parser = new JsonParser(); + JsonObject json = parser.parse(buffer.toString()).getAsJsonObject(); + + cloudSessionId = json.get("session").getAsString(); } - catch(IOException | JSONException e) + catch(Exception e) { log.warn("Error while login to cloud service ", e); return false; @@ -191,8 +191,9 @@ public class HomeWizzardSmartPlugInfo { try { String result = requestJson(EMPTY_STRING); - JSONObject resultJson = new JSONObject(result); - cloudPlugId = resultJson.getString("id"); + JsonParser parser = new JsonParser(); + JsonObject resultJson = parser.parse(result).getAsJsonObject(); + cloudPlugId = resultJson.get("id").getAsString(); String all_devices_json = resultJson.get("devices").toString(); Device[] devices = gson.fromJson(all_devices_json, Device[].class); @@ -203,7 +204,7 @@ public class HomeWizzardSmartPlugInfo { homewizardDevices.add(mapDeviceToHomeWizardSmartPlugDevice(device)); } } - catch(JSONException e) { + catch(Exception e) { log.warn("Error while get devices from cloud service ", e); } @@ -211,12 +212,13 @@ public class HomeWizzardSmartPlugInfo { return homewizardDevices; } - public void execApply(String jsonToPost) throws JSONException, IOException { + public void execApply(String jsonToPost) throws Exception { // Extract - JSONObject resultJson = new JSONObject(jsonToPost); - String deviceId = resultJson.getString("deviceid"); - String action = resultJson.getString("action"); + JsonParser parser = new JsonParser(); + JsonObject resultJson = parser.parse(jsonToPost).getAsJsonObject(); + String deviceId = resultJson.get("deviceid").getAsString(); + String action = resultJson.get("action").getAsString(); // Check if we have an plug id stored if (StringUtils.isBlank(cloudPlugId)) {