Updated Broadlink for port binding, added debug for RM2 Device ir call

data return, updated Harmony library to be the latest version.
This commit is contained in:
bsamuels
2018-01-30 16:30:55 -06:00
parent e6db6e11e5
commit 3313548ec2
4 changed files with 38 additions and 23 deletions

View File

@@ -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();

View File

@@ -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<String, BLDevice>();
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

View File

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