Updated upnp discovery logic to only respond to the echo request for a

upnp device and updated hue response when getting lights to be more
accurate with the hue.
This commit is contained in:
Admin
2015-08-21 16:06:06 -05:00
parent 96074628fb
commit 7746938c62
4 changed files with 21 additions and 16 deletions

View File

@@ -5,7 +5,7 @@
<groupId>com.bwssystems.HABridge</groupId>
<artifactId>ha-bridge</artifactId>
<version>0.3.0</version>
<version>0.3.1</version>
<packaging>jar</packaging>
<name>HA Bridge</name>

View File

@@ -58,12 +58,10 @@ public class HueMulator {
String userId = request.params(":userid");
log.info("hue lights list requested: " + userId + " from " + request.ip());
List<DeviceDescriptor> deviceList = repository.findAll();
JsonTransformer aRenderer = new JsonTransformer();
String theStream = aRenderer.render(deviceList);
log.debug("The Device List: " + theStream);
Map<String, String> deviceResponseMap = new HashMap<>();
Map<String, DeviceResponse> deviceResponseMap = new HashMap<>();
for (DeviceDescriptor device : deviceList) {
deviceResponseMap.put(device.getId(), device.getName());
DeviceResponse deviceResponse = DeviceResponse.createResponse(device.getName(), device.getId());
deviceResponseMap.put(device.getId(), deviceResponse);
}
response.status(200);
return deviceResponseMap;
@@ -82,10 +80,10 @@ public class HueMulator {
return "[{\"success\":{\"username\":\"" + newUser + "\"}}]";
} );
// http://ip_address:port/api/{userId} returns json objects for the list of names of lights
// http://ip_address:port/api/{userId} returns json objects for the full state
get(HUE_CONTEXT + "/:userid", "application/json", (request, response) -> {
String userId = request.params(":userid");
log.info("hue api root requested: " + userId + " from " + request.ip());
log.info("hue api full state requested: " + userId + " from " + request.ip());
List<DeviceDescriptor> descriptorList = repository.findAll();
if (descriptorList == null) {
response.status(404);

View File

@@ -64,6 +64,12 @@ public class UpnpListener {
upnpMulticastSocket.receive(packet);
String packetString = new String(packet.getData());
if(isSSDPDiscovery(packetString)){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
log.error("could not sleep");
}
log.debug("Got SSDP Discovery packet from " + packet.getAddress().getHostAddress() + ":" + packet.getPort());
sendUpnpResponse(responseSocket, packet.getAddress(), packet.getPort());
}
@@ -83,7 +89,9 @@ public class UpnpListener {
* @return
*/
protected boolean isSSDPDiscovery(String body){
if(body != null && body.startsWith("M-SEARCH * HTTP/1.1") && body.contains("MAN: \"ssdp:discover\"")){
// log.debug("Check if this is a MAN ssdp-discover packet for a upnp basic device: " + body);
//Only respond to discover request for upnp basic device from echo, the others are for the wemo
if(body != null && body.startsWith("M-SEARCH * HTTP/1.1") && body.contains("MAN: \"ssdp:discover\"")&& body.contains("ST: urn:schemas-upnp-org:device:basic:1")){
return true;
}
return false;
@@ -92,11 +100,10 @@ public class UpnpListener {
String discoveryTemplate = "HTTP/1.1 200 OK\r\n" +
"CACHE-CONTROL: max-age=86400\r\n" +
"EXT:\r\n" +
"LOCATION: http://%s:%s/upnp/ha-bridge/setup.xml\r\n" +
"OPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n" +
"01-NLS: %s\r\n" +
"LOCATION: http://%s:%s/upnp/ha-bridge/description.xml\r\n" +
"SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/0.1\r\n" +
"ST: urn:schemas-upnp-org:device:basic:1\r\n" +
"USN: uuid:Socket-1_0-221438K0100073::urn:Belkin:device:**\r\n\r\n";
"USN: uuid:Socket-1_0-221438K0100073::urn:schemas-upnp-org:device:basic:1\r\n\r\n";
protected void sendUpnpResponse(DatagramSocket socket, InetAddress requester, int sourcePort) throws IOException {
String discoveryResponse = String.format(discoveryTemplate, responseAddress, httpServerPort, getRandomUUIDString());
log.debug("sndUpnpResponse: " + discoveryResponse);

View File

@@ -44,8 +44,8 @@ public class UpnpSettingsResource {
}
private void setupListener (BridgeSettings theSettings) {
// http://ip_address:port/upnp/:id/setup.xml which returns the xml configuration for the location of the hue emulator
get(UPNP_CONTEXT + "/:id/setup.xml", "application/xml", (request, response) -> {
// http://ip_address:port/upnp/:id/description.xml which returns the xml configuration for the location of the hue emulator
get(UPNP_CONTEXT + "/:id/description.xml", "application/xml", (request, response) -> {
log.info("upnp device settings requested: " + request.params(":id") + " from " + request.ip());
String portNumber = Integer.toString(request.port());
String filledTemplate = String.format(hueTemplate, theSettings.getUpnpConfigAddress(), portNumber, theSettings.getUpnpConfigAddress());
@@ -56,7 +56,7 @@ public class UpnpSettingsResource {
} );
// http://ip_address:port/upnp/settings which returns the bridge configuration settings
get(UPNP_CONTEXT + "/settings", "application/xml", (request, response) -> {
get(UPNP_CONTEXT + "/settings", "application/json", (request, response) -> {
log.debug("bridge settings requested from " + request.ip());
response.status(201);