From 3fe19f5d4e72c516cda5a9fb9146bc8a0e054b1b Mon Sep 17 00:00:00 2001 From: BWS Systems Date: Fri, 28 Jun 2019 15:02:28 -0500 Subject: [PATCH] Updated a few issues Updated the hue uniqueid generation. Updated the moziot login handling. updated upnp response message to have hue-bridgeId in capital letters - HUE-BRIDGEID --- .../HABridge/dao/DeviceRepository.java | 40 +++++++++++++------ .../plugins/moziot/MozIotInstance.java | 30 +++++++++----- .../HABridge/upnp/UpnpListener.java | 12 +++--- src/main/resources/public/views/system.html | 2 +- 4 files changed, 55 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/bwssystems/HABridge/dao/DeviceRepository.java b/src/main/java/com/bwssystems/HABridge/dao/DeviceRepository.java index 59cd5c2..1b4bf99 100644 --- a/src/main/java/com/bwssystems/HABridge/dao/DeviceRepository.java +++ b/src/main/java/com/bwssystems/HABridge/dao/DeviceRepository.java @@ -186,9 +186,7 @@ public class DeviceRepository extends BackupHandler { nextId++; } if (descriptors[i].getUniqueid() == null || descriptors[i].getUniqueid().length() == 0) { - String hexValue = HexLibrary.encodeUsingBigIntegerToString(descriptors[i].getId()); - - descriptors[i].setUniqueid("00:17:88:5E:D3:" + hexValue + "-" + hexValue); + descriptors[i].setUniqueid("00:17:88:5E:D3:" + hueUniqueId(Integer.valueOf(descriptors[i].getId()))); } put(descriptors[i].getId(), descriptors[i]); theNames = theNames + " " + descriptors[i].getName() + ", "; @@ -203,8 +201,6 @@ public class DeviceRepository extends BackupHandler { Iterator deviceIterator = list.iterator(); Map newdevices = new HashMap(); List lockedIds = new ArrayList(); - String hexValue; - Integer newValue; DeviceDescriptor theDevice; boolean findNext = true; @@ -230,14 +226,7 @@ public class DeviceRepository extends BackupHandler { } } theDevice.setId(String.valueOf(nextId)); - newValue = nextId % 256; - if (newValue <= 0) - newValue = 1; - else if (newValue > 255) - newValue = 255; - hexValue = HexLibrary.encodeUsingBigIntegerToString(newValue.toString()); - - theDevice.setUniqueid("00:17:88:5E:D3:" + hexValue + "-" + hexValue); + theDevice.setUniqueid("00:17:88:5E:D3:" + hueUniqueId(nextId)); nextId++; } newdevices.put(theDevice.getId(), theDevice); @@ -304,4 +293,29 @@ public class DeviceRepository extends BackupHandler { return content; } + + private String hueUniqueId(Integer anId) { + String theUniqueId; + Integer newValue; + String hexValueLeft; + String hexValueRight; + + newValue = anId % 256; + if (newValue <= 0) + newValue = 1; + else if (newValue > 255) + newValue = 255; + hexValueLeft = HexLibrary.byteToHex(newValue.byteValue()); + newValue = anId / 256; + newValue = newValue % 256; + if (newValue < 0) + newValue = 0; + else if (newValue > 255) + newValue = 255; + hexValueRight = HexLibrary.byteToHex(newValue.byteValue()); + + theUniqueId = String.format("%s-%s", hexValueLeft, hexValueRight).toUpperCase(); + + return theUniqueId; + } } \ No newline at end of file diff --git a/src/main/java/com/bwssystems/HABridge/plugins/moziot/MozIotInstance.java b/src/main/java/com/bwssystems/HABridge/plugins/moziot/MozIotInstance.java index bd336b6..4c28b9a 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/moziot/MozIotInstance.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/moziot/MozIotInstance.java @@ -19,15 +19,20 @@ public class MozIotInstance { private JWT moziotToken; private NamedIP mozIotIP; private NameValue[] headers; + private HTTPHandler anHttpClient; public MozIotInstance(NamedIP theNamedIp, HTTPHandler httpClient) { mozIotIP = theNamedIp; headers = null; - gatewayLogin(httpClient); + moziotToken = null; + anHttpClient = httpClient; + gatewayLogin(); } - public Boolean callCommand(String deviceId, String aCommand, MozIotCommandDetail commandData, HTTPHandler httpClient) { - log.debug("calling Mozilla IOT: {}:{}{}{}", mozIotIP.getIp(), mozIotIP.getPort(), aCommand, commandData.getBody()); + public Boolean callCommand(String deviceId, String aCommand, MozIotCommandDetail commandData, + HTTPHandler httpClient) { + log.debug("calling Mozilla IOT: {}:{}{}{}", mozIotIP.getIp(), mozIotIP.getPort(), aCommand, + commandData.getBody()); String aUrl = null; if (mozIotIP.getSecure() != null && mozIotIP.getSecure()) @@ -36,8 +41,9 @@ public class MozIotInstance { aUrl = "http://"; headers = getAuthHeader(); - aUrl = aUrl + mozIotIP.getIp() + ":" + mozIotIP.getPort() + "/things/" + deviceId + "/" + aCommand; - String theData = httpClient.doHttpRequest(aUrl, HttpPut.METHOD_NAME, "application/json", commandData.getBody(), headers); + aUrl = aUrl + mozIotIP.getIp() + ":" + mozIotIP.getPort() + "/things/" + deviceId + "/" + aCommand; + String theData = httpClient.doHttpRequest(aUrl, HttpPut.METHOD_NAME, "application/json", commandData.getBody(), + headers); log.debug("call Command return is: <<<{}>>>", theData); if (theData.contains("error") || theData.contains("ERROR") || theData.contains("Error")) return false; @@ -77,7 +83,10 @@ public class MozIotInstance { } private NameValue[] getAuthHeader() { - if (headers == null) { + if (moziotToken == null) + gatewayLogin(); + + if (headers == null && moziotToken != null) { headers = new NameValue[3]; headers[0] = new NameValue(); headers[0].setName("Authorization"); @@ -88,13 +97,14 @@ public class MozIotInstance { headers[2] = new NameValue(); headers[2].setName("Accept"); headers[2].setValue("application/json"); - } + } return headers; } - private void gatewayLogin(HTTPHandler httpClient) { + private void gatewayLogin() { String aUrl = null; + moziotToken = null; if (mozIotIP.getSecure() != null && mozIotIP.getSecure()) aUrl = "https://"; else @@ -112,13 +122,15 @@ public class MozIotInstance { String commandData = "{\"email\": \"" + mozIotIP.getUsername() + "\", \"password\":\"" + mozIotIP.getPassword() + "\"}"; log.debug("The login body: {}", commandData); - String theData = httpClient.doHttpRequest(aUrl, HttpPost.METHOD_NAME, "application/json", commandData, headers); + String theData = anHttpClient.doHttpRequest(aUrl, HttpPost.METHOD_NAME, "application/json", commandData, + headers); if (theData != null) { log.debug("GET Mozilla login - data: {}", theData); try { moziotToken = new Gson().fromJson(theData, JWT.class); } catch (Exception e) { log.warn("Cannot get login for Mozilla IOT {} Gson Parse Error.", mozIotIP.getName()); + moziotToken = null; } } else { log.warn("Could not login {} error: <<<{}>>>", mozIotIP.getName(), theData); diff --git a/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java b/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java index 7b6205d..8167f1c 100644 --- a/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java +++ b/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java @@ -37,30 +37,30 @@ public class UpnpListener { + HueConstants.UUID_PREFIX + "%s::urn:schemas-upnp-org:device:basic:1\r\n\r\n"; private String responseTemplate1 = "HTTP/1.1 200 OK\r\n" + "HOST: %s:%s\r\n" + "CACHE-CONTROL: max-age=100\r\n" + "EXT:\r\n" + "LOCATION: http://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/" - + HueConstants.API_VERSION + "\r\n" + "hue-bridgeid: %s\r\n" + "ST: upnp:rootdevice\r\n" + "USN: uuid:" + + HueConstants.API_VERSION + "\r\n" + "HUE-BRIDGEID: %s\r\n" + "ST: upnp:rootdevice\r\n" + "USN: uuid:" + HueConstants.UUID_PREFIX + "%s::upnp:rootdevice\r\n\r\n"; private String responseTemplate2 = "HTTP/1.1 200 OK\r\n" + "HOST: %s:%s\r\n" + "CACHE-CONTROL: max-age=100\r\n" + "EXT:\r\n" + "LOCATION: http://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/" - + HueConstants.API_VERSION + "\r\n" + "hue-bridgeid: %s\r\n" + "ST: uuid:" + HueConstants.UUID_PREFIX + + HueConstants.API_VERSION + "\r\n" + "HUE-BRIDGEID: %s\r\n" + "ST: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n" + "USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\r\n"; private String responseTemplate3 = "HTTP/1.1 200 OK\r\n" + "HOST: %s:%s\r\n" + "CACHE-CONTROL: max-age=100\r\n" + "EXT:\r\n" + "LOCATION: http://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/" - + HueConstants.API_VERSION + "\r\n" + "hue-bridgeid: %s\r\n" + "ST: urn:schemas-upnp-org:device:basic:1\r\n" + + HueConstants.API_VERSION + "\r\n" + "HUE-BRIDGEID: %s\r\n" + "ST: urn:schemas-upnp-org:device:basic:1\r\n" + "USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\r\n"; private String notifyTemplate = "NOTIFY * HTTP/1.1\r\n" + "HOST: %s:%s\r\n" + "CACHE-CONTROL: max-age=100\r\n" + "LOCATION: http://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/" - + HueConstants.API_VERSION + "\r\n" + "NTS: ssdp:alive\r\n" + "hue-bridgeid: %s\r\n" + "NT: uuid:" + + HueConstants.API_VERSION + "\r\n" + "NTS: ssdp:alive\r\n" + "HUE-BRIDGEID: %s\r\n" + "NT: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n" + "USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\r\n"; private String notifyTemplate2 = "NOTIFY * HTTP/1.1\r\n" + "HOST: %s:%s\r\n" + "CACHE-CONTROL: max-age=100\r\n" + "LOCATION: http://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/" - + HueConstants.API_VERSION + "\r\n" + "NTS: ssdp:alive\r\n" + "hue-bridgeid: %s\r\n" + + HueConstants.API_VERSION + "\r\n" + "NTS: ssdp:alive\r\n" + "HUE-BRIDGEID: %s\r\n" + "NT: upnp:rootdevice\r\n" + "USN: uuid:" + HueConstants.UUID_PREFIX + "%s::upnp:rootdevice\r\n\r\n"; private String notifyTemplate3 = "NOTIFY * HTTP/1.1\r\n" + "HOST: %s:%s\r\n" + "CACHE-CONTROL: max-age=100\r\n" + "LOCATION: http://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/" - + HueConstants.API_VERSION + "\r\n" + "NTS: ssdp:alive\r\n" + "hue-bridgeid: %s\r\n" + + HueConstants.API_VERSION + "\r\n" + "NTS: ssdp:alive\r\n" + "HUE-BRIDGEID: %s\r\n" + "NT: urn:schemas-upnp-org:device:basic:1\r\n" + "USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\r\n"; public UpnpListener(BridgeSettingsDescriptor theSettings, BridgeControlDescriptor theControl, diff --git a/src/main/resources/public/views/system.html b/src/main/resources/public/views/system.html index c55033f..06d5a35 100644 --- a/src/main/resources/public/views/system.html +++ b/src/main/resources/public/views/system.html @@ -699,7 +699,7 @@ + ng-click="addMozIottoSettings(newmoziotname, newmoziotip, newmoziotport, newmoziotusername, newmoziotpassword, newmoziotwebhook, newmoziotsecure)">Add