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
This commit is contained in:
BWS Systems
2019-06-28 15:02:28 -05:00
parent 5736bb92db
commit 3fe19f5d4e
4 changed files with 55 additions and 29 deletions

View File

@@ -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<DeviceDescriptor> deviceIterator = list.iterator();
Map<String, DeviceDescriptor> newdevices = new HashMap<String, DeviceDescriptor>();
List<String> lockedIds = new ArrayList<String>();
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;
}
}

View File

@@ -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())
@@ -37,7 +42,8 @@ public class MozIotInstance {
headers = getAuthHeader();
aUrl = aUrl + mozIotIP.getIp() + ":" + mozIotIP.getPort() + "/things/" + deviceId + "/" + aCommand;
String theData = httpClient.doHttpRequest(aUrl, HttpPut.METHOD_NAME, "application/json", commandData.getBody(), headers);
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");
@@ -92,9 +101,10 @@ public class MozIotInstance {
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);

View File

@@ -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,

View File

@@ -699,7 +699,7 @@
<td><input type="checkbox" ng-model="newmoziotsecure" ng-true-value=true
ng-false-value=false></td>
<td><button class="btn btn-success" type="submit"
ng-click="addMozIottoSettings(newmoziotname, newmoziotip, newmoziotport, newsmoziotusername, newmoziotpassword, newmoziotwebhook, newmoziotsecure)">Add</button>
ng-click="addMozIottoSettings(newmoziotname, newmoziotip, newmoziotport, newmoziotusername, newmoziotpassword, newmoziotwebhook, newmoziotsecure)">Add</button>
</td>
</tr>
</table>