mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-16 18:24:36 +00:00
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:
@@ -186,9 +186,7 @@ public class DeviceRepository extends BackupHandler {
|
|||||||
nextId++;
|
nextId++;
|
||||||
}
|
}
|
||||||
if (descriptors[i].getUniqueid() == null || descriptors[i].getUniqueid().length() == 0) {
|
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:" + hueUniqueId(Integer.valueOf(descriptors[i].getId())));
|
||||||
|
|
||||||
descriptors[i].setUniqueid("00:17:88:5E:D3:" + hexValue + "-" + hexValue);
|
|
||||||
}
|
}
|
||||||
put(descriptors[i].getId(), descriptors[i]);
|
put(descriptors[i].getId(), descriptors[i]);
|
||||||
theNames = theNames + " " + descriptors[i].getName() + ", ";
|
theNames = theNames + " " + descriptors[i].getName() + ", ";
|
||||||
@@ -203,8 +201,6 @@ public class DeviceRepository extends BackupHandler {
|
|||||||
Iterator<DeviceDescriptor> deviceIterator = list.iterator();
|
Iterator<DeviceDescriptor> deviceIterator = list.iterator();
|
||||||
Map<String, DeviceDescriptor> newdevices = new HashMap<String, DeviceDescriptor>();
|
Map<String, DeviceDescriptor> newdevices = new HashMap<String, DeviceDescriptor>();
|
||||||
List<String> lockedIds = new ArrayList<String>();
|
List<String> lockedIds = new ArrayList<String>();
|
||||||
String hexValue;
|
|
||||||
Integer newValue;
|
|
||||||
DeviceDescriptor theDevice;
|
DeviceDescriptor theDevice;
|
||||||
boolean findNext = true;
|
boolean findNext = true;
|
||||||
|
|
||||||
@@ -230,14 +226,7 @@ public class DeviceRepository extends BackupHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
theDevice.setId(String.valueOf(nextId));
|
theDevice.setId(String.valueOf(nextId));
|
||||||
newValue = nextId % 256;
|
theDevice.setUniqueid("00:17:88:5E:D3:" + hueUniqueId(nextId));
|
||||||
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);
|
|
||||||
nextId++;
|
nextId++;
|
||||||
}
|
}
|
||||||
newdevices.put(theDevice.getId(), theDevice);
|
newdevices.put(theDevice.getId(), theDevice);
|
||||||
@@ -304,4 +293,29 @@ public class DeviceRepository extends BackupHandler {
|
|||||||
|
|
||||||
return content;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -19,15 +19,20 @@ public class MozIotInstance {
|
|||||||
private JWT moziotToken;
|
private JWT moziotToken;
|
||||||
private NamedIP mozIotIP;
|
private NamedIP mozIotIP;
|
||||||
private NameValue[] headers;
|
private NameValue[] headers;
|
||||||
|
private HTTPHandler anHttpClient;
|
||||||
|
|
||||||
public MozIotInstance(NamedIP theNamedIp, HTTPHandler httpClient) {
|
public MozIotInstance(NamedIP theNamedIp, HTTPHandler httpClient) {
|
||||||
mozIotIP = theNamedIp;
|
mozIotIP = theNamedIp;
|
||||||
headers = null;
|
headers = null;
|
||||||
gatewayLogin(httpClient);
|
moziotToken = null;
|
||||||
|
anHttpClient = httpClient;
|
||||||
|
gatewayLogin();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean callCommand(String deviceId, String aCommand, MozIotCommandDetail commandData, HTTPHandler httpClient) {
|
public Boolean callCommand(String deviceId, String aCommand, MozIotCommandDetail commandData,
|
||||||
log.debug("calling Mozilla IOT: {}:{}{}{}", mozIotIP.getIp(), mozIotIP.getPort(), aCommand, commandData.getBody());
|
HTTPHandler httpClient) {
|
||||||
|
log.debug("calling Mozilla IOT: {}:{}{}{}", mozIotIP.getIp(), mozIotIP.getPort(), aCommand,
|
||||||
|
commandData.getBody());
|
||||||
String aUrl = null;
|
String aUrl = null;
|
||||||
|
|
||||||
if (mozIotIP.getSecure() != null && mozIotIP.getSecure())
|
if (mozIotIP.getSecure() != null && mozIotIP.getSecure())
|
||||||
@@ -36,8 +41,9 @@ public class MozIotInstance {
|
|||||||
aUrl = "http://";
|
aUrl = "http://";
|
||||||
headers = getAuthHeader();
|
headers = getAuthHeader();
|
||||||
|
|
||||||
aUrl = aUrl + mozIotIP.getIp() + ":" + mozIotIP.getPort() + "/things/" + deviceId + "/" + aCommand;
|
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);
|
log.debug("call Command return is: <<<{}>>>", theData);
|
||||||
if (theData.contains("error") || theData.contains("ERROR") || theData.contains("Error"))
|
if (theData.contains("error") || theData.contains("ERROR") || theData.contains("Error"))
|
||||||
return false;
|
return false;
|
||||||
@@ -77,7 +83,10 @@ public class MozIotInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private NameValue[] getAuthHeader() {
|
private NameValue[] getAuthHeader() {
|
||||||
if (headers == null) {
|
if (moziotToken == null)
|
||||||
|
gatewayLogin();
|
||||||
|
|
||||||
|
if (headers == null && moziotToken != null) {
|
||||||
headers = new NameValue[3];
|
headers = new NameValue[3];
|
||||||
headers[0] = new NameValue();
|
headers[0] = new NameValue();
|
||||||
headers[0].setName("Authorization");
|
headers[0].setName("Authorization");
|
||||||
@@ -88,13 +97,14 @@ public class MozIotInstance {
|
|||||||
headers[2] = new NameValue();
|
headers[2] = new NameValue();
|
||||||
headers[2].setName("Accept");
|
headers[2].setName("Accept");
|
||||||
headers[2].setValue("application/json");
|
headers[2].setValue("application/json");
|
||||||
}
|
}
|
||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void gatewayLogin(HTTPHandler httpClient) {
|
private void gatewayLogin() {
|
||||||
String aUrl = null;
|
String aUrl = null;
|
||||||
|
|
||||||
|
moziotToken = null;
|
||||||
if (mozIotIP.getSecure() != null && mozIotIP.getSecure())
|
if (mozIotIP.getSecure() != null && mozIotIP.getSecure())
|
||||||
aUrl = "https://";
|
aUrl = "https://";
|
||||||
else
|
else
|
||||||
@@ -112,13 +122,15 @@ public class MozIotInstance {
|
|||||||
String commandData = "{\"email\": \"" + mozIotIP.getUsername() + "\", \"password\":\"" + mozIotIP.getPassword()
|
String commandData = "{\"email\": \"" + mozIotIP.getUsername() + "\", \"password\":\"" + mozIotIP.getPassword()
|
||||||
+ "\"}";
|
+ "\"}";
|
||||||
log.debug("The login body: {}", commandData);
|
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) {
|
if (theData != null) {
|
||||||
log.debug("GET Mozilla login - data: {}", theData);
|
log.debug("GET Mozilla login - data: {}", theData);
|
||||||
try {
|
try {
|
||||||
moziotToken = new Gson().fromJson(theData, JWT.class);
|
moziotToken = new Gson().fromJson(theData, JWT.class);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("Cannot get login for Mozilla IOT {} Gson Parse Error.", mozIotIP.getName());
|
log.warn("Cannot get login for Mozilla IOT {} Gson Parse Error.", mozIotIP.getName());
|
||||||
|
moziotToken = null;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.warn("Could not login {} error: <<<{}>>>", mozIotIP.getName(), theData);
|
log.warn("Could not login {} error: <<<{}>>>", mozIotIP.getName(), theData);
|
||||||
|
|||||||
@@ -37,30 +37,30 @@ public class UpnpListener {
|
|||||||
+ HueConstants.UUID_PREFIX + "%s::urn:schemas-upnp-org:device:basic:1\r\n\r\n";
|
+ 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"
|
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/"
|
+ "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";
|
+ 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"
|
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/"
|
+ "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";
|
+ "%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"
|
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/"
|
+ "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";
|
+ "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"
|
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/"
|
+ "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";
|
+ 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"
|
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/"
|
+ "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";
|
+ "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"
|
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/"
|
+ "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";
|
+ "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,
|
public UpnpListener(BridgeSettingsDescriptor theSettings, BridgeControlDescriptor theControl,
|
||||||
|
|||||||
@@ -699,7 +699,7 @@
|
|||||||
<td><input type="checkbox" ng-model="newmoziotsecure" ng-true-value=true
|
<td><input type="checkbox" ng-model="newmoziotsecure" ng-true-value=true
|
||||||
ng-false-value=false></td>
|
ng-false-value=false></td>
|
||||||
<td><button class="btn btn-success" type="submit"
|
<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>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
Reference in New Issue
Block a user