From 7746938c62c727d5bf9f010eb96f6e6c762d9f5a Mon Sep 17 00:00:00 2001 From: Admin Date: Fri, 21 Aug 2015 16:06:06 -0500 Subject: [PATCH] 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. --- pom.xml | 2 +- .../com/bwssystems/HABridge/hue/HueMulator.java | 12 +++++------- .../bwssystems/HABridge/upnp/UpnpListener.java | 17 ++++++++++++----- .../HABridge/upnp/UpnpSettingsResource.java | 6 +++--- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index 1629cc1..c404d5c 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.bwssystems.HABridge ha-bridge - 0.3.0 + 0.3.1 jar HA Bridge diff --git a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java index d0ed083..966a0cb 100644 --- a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java +++ b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java @@ -58,12 +58,10 @@ public class HueMulator { String userId = request.params(":userid"); log.info("hue lights list requested: " + userId + " from " + request.ip()); List deviceList = repository.findAll(); - JsonTransformer aRenderer = new JsonTransformer(); - String theStream = aRenderer.render(deviceList); - log.debug("The Device List: " + theStream); - Map deviceResponseMap = new HashMap<>(); + Map 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 descriptorList = repository.findAll(); if (descriptorList == null) { response.status(404); diff --git a/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java b/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java index 5af5b8f..96a8912 100644 --- a/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java +++ b/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java @@ -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); diff --git a/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java b/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java index 4696d74..e1d10ef 100644 --- a/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java +++ b/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java @@ -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);