diff --git a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java index fd13e85..da00d50 100644 --- a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java +++ b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java @@ -133,7 +133,34 @@ public class HueMulator implements HueErrorStringSet { // This function sets up the sparkjava rest calls for the hue api public void setupServer() { log.info("Hue emulator service started...."); - // http://ip_address:port/api/{userId}/lights returns json objects of all lights configured + // http://ip_address:port/api/{userId}/groups returns json objects of all groups configured + get(HUE_CONTEXT + "/:userid/groups", "application/json", (request, response) -> { + String userId = request.params(":userid"); + log.debug("hue groups list requested: " + userId + " from " + request.ip()); + response.header("Access-Control-Allow-Origin", request.headers("Origin")); + return ""; + } , new JsonTransformer()); + // http://ip_address:port/api/{userId}/groups/0 returns json objects of all groups configured + get(HUE_CONTEXT + "/:userid/groups/0", "application/json", (request, response) -> { + String userId = request.params(":userid"); + log.debug("hue group 0 list requested: " + userId + " from " + request.ip()); + response.header("Access-Control-Allow-Origin", request.headers("Origin")); + return ""; + } , new JsonTransformer()); + // http://ip_address:port/api/{userId}/scenes returns json objects of all scenes configured + get(HUE_CONTEXT + "/:userid/scenes", "application/json", (request, response) -> { + String userId = request.params(":userid"); + log.debug("hue scenes list requested: " + userId + " from " + request.ip()); + response.header("Access-Control-Allow-Origin", request.headers("Origin")); + return ""; + } , new JsonTransformer()); + // http://ip_address:port/api/{userId}/schedules returns json objects of all schedules configured + get(HUE_CONTEXT + "/:userid/schedules", "application/json", (request, response) -> { + String userId = request.params(":userid"); + log.debug("hue schedules list requested: " + userId + " from " + request.ip()); + response.header("Access-Control-Allow-Origin", request.headers("Origin")); + return ""; + } , new JsonTransformer()); get(HUE_CONTEXT + "/:userid/lights", "application/json", (request, response) -> { String userId = request.params(":userid"); if(bridgeSettings.isTraceupnp()) @@ -146,6 +173,7 @@ public class HueMulator implements HueErrorStringSet { deviceResponseMap.put(device.getId(), deviceResponse); } response.type("application/json; charset=utf-8"); + response.header("Access-Control-Allow-Origin", request.headers("Origin")); response.status(HttpStatus.SC_OK); return deviceResponseMap; } , new JsonTransformer()); @@ -219,7 +247,6 @@ public class HueMulator implements HueErrorStringSet { aDeviceType = ""; log.debug("HH trace: hue api user create requested for device type: " + aDeviceType + " and username: " + newUser); - response.header("Access-Control-Allow-Origin", request.headers("Origin")); response.type("application/json; charset=utf-8"); response.status(HttpStatus.SC_OK); return "[{\"success\":{\"username\":\"" + newUser + "\"}}]"; @@ -233,6 +260,7 @@ public class HueMulator implements HueErrorStringSet { HueApiResponse apiResponse = new HueApiResponse("Philips hue", bridgeSettings.getUpnpConfigAddress(), "My App", "none"); response.type("application/json; charset=utf-8"); + response.header("Access-Control-Allow-Origin", request.headers("Origin")); response.status(HttpStatus.SC_OK); // String responseString = null; // responseString = "[{\"swversion\":\"" + apiResponse.getConfig().getSwversion() + "\",\"apiversion\":\"" + apiResponse.getConfig().getApiversion() + "\",\"name\":\"" + apiResponse.getConfig().getName() + "\",\"mac\":\"" + apiResponse.getConfig().getMac() + "\"}]"; @@ -249,6 +277,7 @@ public class HueMulator implements HueErrorStringSet { HueApiResponse apiResponse = new HueApiResponse("Philips hue", bridgeSettings.getUpnpConfigAddress(), "My App", userId); response.type("application/json; charset=utf-8"); + response.header("Access-Control-Allow-Origin", request.headers("Origin")); response.status(HttpStatus.SC_OK); return apiResponse.getConfig(); }, new JsonTransformer()); @@ -258,6 +287,9 @@ public class HueMulator implements HueErrorStringSet { get(HUE_CONTEXT + "/:userid", "application/json", (request, response) -> { String userId = request.params(":userid"); log.debug("hue api full state requested: " + userId + " from " + request.ip()); + response.header("Access-Control-Allow-Origin", request.headers("Origin")); + if(userId.equalsIgnoreCase("undefined")) + return "[{\"error\":{\"address\":\"/\",\"description\":\"unauthorized user\",\"type\":\"1\"}}]"; List descriptorList = repository.findAll(); if (descriptorList == null) { response.status(HttpStatus.SC_NOT_FOUND); @@ -282,6 +314,7 @@ public class HueMulator implements HueErrorStringSet { get(HUE_CONTEXT + "/:userid/lights/:id", "application/json", (request, response) -> { String userId = request.params(":userid"); String lightId = request.params(":id"); + response.header("Access-Control-Allow-Origin", request.headers("Origin")); log.debug("hue light requested: " + lightId + " for user: " + userId + " from " + request.ip()); DeviceDescriptor device = repository.findOne(lightId); if (device == null) { diff --git a/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java b/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java index 5766c9e..0d4c8d2 100644 --- a/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java +++ b/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java @@ -22,6 +22,7 @@ public class UpnpListener { private boolean strict; private boolean traceupnp; private BridgeControlDescriptor bridgeControl; + private boolean discoveryTemplateLatest; private String discoveryTemplate = "HTTP/1.1 200 OK\r\n" + "CACHE-CONTROL: max-age=86400\r\n" + "EXT:\r\n" + @@ -29,15 +30,13 @@ public class UpnpListener { "SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/1.10.0\r\n" + "ST: urn:schemas-upnp-org:device:basic:1\r\n" + "USN: uuid:Socket-1_0-221438K0100073::urn:schemas-upnp-org:device:basic:1\r\n\r\n"; - private String discoveryTemplateNew = "HTTP/1.1 200 OK\r\n" + - "HOST: %s:%s\r\n" + + private String discoveryTemplateOld = "HTTP/1.1 200 OK\r\n" + + "CACHE-CONTROL: max-age=86400\r\n" + "EXT:\r\n" + - "CACHE-CONTROL: max-age=100\r\n" + "LOCATION: http://%s:%s/description.xml\r\n" + - "SERVER: FreeRTOS/7.4.2 UPnP/1.0 IpBridge/1.10.0\r\n" + - "ST: upnp:rootdevice\r\n" + - "hue-bridgeid: 001788FFFE09A206\r\n" + - "USN: uuid:88f6698f-2c83-4393-bd03-cd54a9f8595:upnp:rootdevice\r\n\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:schemas-upnp-org:device:basic:1\r\n\r\n"; public UpnpListener(BridgeSettingsDescriptor theSettings, BridgeControlDescriptor theControl) { super(); @@ -47,6 +46,7 @@ public class UpnpListener { strict = theSettings.isUpnpStrict(); traceupnp = theSettings.isTraceupnp(); bridgeControl = theControl; + discoveryTemplateLatest = true; } @SuppressWarnings("resource") @@ -207,10 +207,10 @@ public class UpnpListener { protected void sendUpnpResponse(DatagramSocket socket, InetAddress requester, int sourcePort) throws IOException { String discoveryResponse = null; - if(true) + if(discoveryTemplateLatest) discoveryResponse = String.format(discoveryTemplate, responseAddress, httpServerPort); else - discoveryResponse = String.format(discoveryTemplateNew, Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT, responseAddress, httpServerPort); + discoveryResponse = String.format(discoveryTemplateOld, Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT, responseAddress, httpServerPort); if(traceupnp) log.info("Traceupnp: sendUpnpResponse discovery template with address: " + responseAddress + " and port: " + httpServerPort); else diff --git a/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java b/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java index f7d241d..fd705a3 100644 --- a/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java +++ b/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java @@ -97,5 +97,17 @@ public class UpnpSettingsResource { return filledTemplate; } ); +// http://ip_adress:port/favicon.ico + get("/favicon.ico", "application/xml; charset=utf-8", (request, response) -> { + return ""; + } ); +// http://ip_adress:port/hue_logo_0.png + get("/hue_logo_0.png", "application/xml; charset=utf-8", (request, response) -> { + return ""; + } ); +// http://ip_adress:port/hue_logo_3.png + get("/hue_logo_3.png", "application/xml; charset=utf-8", (request, response) -> { + return ""; + } ); } }