From 74d4548bebd0aa11c48ec65834c03de1517f4192 Mon Sep 17 00:00:00 2001 From: Admin Date: Thu, 3 Sep 2015 11:44:26 -0500 Subject: [PATCH] Fixed saving of updated device context when special commands are used. Updated create user in hue emulator to handle no device tpye or usernmae given. Fixed parameter settings as they were incorrect. Updated config display to show new parameters. --- README.md | 2 +- .../com/bwssystems/HABridge/HABridge.java | 6 +-- .../devicemanagmeent/DeviceResource.java | 17 ++++---- .../bwssystems/HABridge/hue/HueMulator.java | 40 ++++++++++++------- .../resources/public/views/configuration.html | 12 ++++++ 5 files changed, 52 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 074e957..f9eb1d0 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ The default location for the db to contain the devices as they are added is "dat ### -Dupnp.resonse.port=`` The upnp response port that will be used. The default is 50000. ### -Dupnp.strict=`` -Upnp has been very closed on this platform to try and respond as a hue and there is now a setting to control if it is more open or strict, Add -Dupnp.strict=`` to your command line to have the emulator respond to what it thinks is an echo to a hue or any other device. The default is upnp.strict=true. +Upnp has been very closed on this platform to try and respond as a hue and there is now a setting to control if it is more open or strict, Add -Dupnp.strict=`` to your command line to have the emulator respond to what it thinks is an echo to a hue or any other device. The default is upnp.strict=false. ### -Dtrace.upnp=`` Turn on tracing for upnp discovery messages. The default is false. ### -Dvtwo.compatibility=`` diff --git a/src/main/java/com/bwssystems/HABridge/HABridge.java b/src/main/java/com/bwssystems/HABridge/HABridge.java index adb494d..98b41b1 100644 --- a/src/main/java/com/bwssystems/HABridge/HABridge.java +++ b/src/main/java/com/bwssystems/HABridge/HABridge.java @@ -54,9 +54,9 @@ public class HABridge { bridgeSettings.setUpnpDeviceDb(System.getProperty("upnp.device.db", "data/device.db")); bridgeSettings.setUpnpResponsePort(System.getProperty("upnp.response.port", "50000")); bridgeSettings.setVeraAddress(System.getProperty("vera.address", "192.168.1.100")); - bridgeSettings.setUpnpStrict(Boolean.parseBoolean(System.getProperty("upnp.strict", "true"))); - bridgeSettings.setUpnpStrict(Boolean.parseBoolean(System.getProperty("trace.upnp", "false"))); - bridgeSettings.setUpnpStrict(Boolean.parseBoolean(System.getProperty("vtwo.compatibility", "true"))); + bridgeSettings.setUpnpStrict(Boolean.parseBoolean(System.getProperty("upnp.strict", "false"))); + bridgeSettings.setTraceupnp(Boolean.parseBoolean(System.getProperty("trace.upnp", "false"))); + bridgeSettings.setVtwocompatibility(Boolean.parseBoolean(System.getProperty("vtwo.compatibility", "true"))); // sparkjava config directive to set ip address for the web server to listen on // ipAddress("0.0.0.0"); // not used diff --git a/src/main/java/com/bwssystems/HABridge/devicemanagmeent/DeviceResource.java b/src/main/java/com/bwssystems/HABridge/devicemanagmeent/DeviceResource.java index 7b144c5..c97cda5 100644 --- a/src/main/java/com/bwssystems/HABridge/devicemanagmeent/DeviceResource.java +++ b/src/main/java/com/bwssystems/HABridge/devicemanagmeent/DeviceResource.java @@ -53,16 +53,16 @@ public class DeviceResource { if (device.getContentType() == null || device.getHttpVerb() == null || !supportedVerbs.contains(device.getHttpVerb().toLowerCase())) { device = null; response.status(HttpStatus.SC_BAD_REQUEST); - log.debug("Created a Device: " + request.body()); + log.debug("Bad http verb in create a Device: " + request.body()); + return device; } } - else - { - deviceRepository.save(device); - log.debug("Created a Device: " + request.body()); - response.status(HttpStatus.SC_CREATED); - } + deviceRepository.save(device); + log.debug("Created a Device: " + request.body()); + + response.status(HttpStatus.SC_CREATED); + return device; }, new JsonTransformer()); @@ -83,6 +83,9 @@ public class DeviceResource { deviceEntry.setDeviceType(device.getDeviceType()); deviceEntry.setOnUrl(device.getOnUrl()); deviceEntry.setOffUrl(device.getOffUrl()); + deviceEntry.setHttpVerb(device.getHttpVerb()); + deviceEntry.setContentType(device.getContentType()); + deviceEntry.setContentBody(device.getContentBody()); deviceRepository.save(deviceEntry); response.status(HttpStatus.SC_OK); diff --git a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java index 34275a9..025aeb6 100644 --- a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java +++ b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java @@ -15,6 +15,7 @@ import static spark.Spark.post; import static spark.Spark.put; import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; @@ -70,21 +71,32 @@ public class HueMulator { deviceResponseMap.put(device.getId(), deviceResponse); } response.type("application/json; charset=utf-8"); - response.status(200); + response.status(HttpStatus.SC_OK); return deviceResponseMap; } , new JsonTransformer()); // http://ip_address:port/api with body of user request returns json object for a success of user add post(HUE_CONTEXT, "application/json", (request, response) -> { - log.debug("hue api user create requested: " + request.body() + " from " + request.ip()); - UserCreateRequest aNewUser = new Gson().fromJson(request.body(), UserCreateRequest.class); - String newUser = aNewUser.getUsername(); + UserCreateRequest aNewUser = null; + String newUser = null; + String aDeviceType = null; + + log.debug("hue api user create requested: " + request.body() + " from " + request.ip()); + + if(request.body() != null && !request.body().isEmpty()) { + aNewUser = new Gson().fromJson(request.body(), UserCreateRequest.class); + newUser = aNewUser.getUsername(); + aDeviceType = aNewUser.getDevicetype(); + } if(newUser == null) newUser = "lightssystem"; - log.debug("hue api user create requested for device type: " + aNewUser.getDevicetype() + " and username: " + newUser); + + if(aDeviceType == null) + aDeviceType = ""; + log.debug("hue api user create requested for device type: " + aDeviceType + " and username: " + newUser); response.type("application/json; charset=utf-8"); - response.status(200); + response.status(HttpStatus.SC_OK); return "[{\"success\":{\"username\":\"" + newUser + "\"}}]"; } ); @@ -94,7 +106,7 @@ public class HueMulator { log.debug("hue api full state requested: " + userId + " from " + request.ip()); List descriptorList = repository.findAll(); if (descriptorList == null) { - response.status(404); + response.status(HttpStatus.SC_NOT_FOUND); return null; } Map deviceList = new HashMap<>(); @@ -108,7 +120,7 @@ public class HueMulator { apiResponse.setLights(deviceList); response.type("application/json; charset=utf-8"); - response.status(200); + response.status(HttpStatus.SC_OK); return apiResponse; }, new JsonTransformer()); @@ -119,7 +131,7 @@ public class HueMulator { log.debug("hue light requested: " + lightId + " for user: " + userId + " from " + request.ip()); DeviceDescriptor device = repository.findOne(lightId); if (device == null) { - response.status(404); + response.status(HttpStatus.SC_NOT_FOUND); return null; } else { log.debug("found device named: " + device.getName()); @@ -127,7 +139,7 @@ public class HueMulator { DeviceResponse lightResponse = DeviceResponse.createResponse(device.getName(), device.getId()); response.type("application/json; charset=utf-8"); - response.status(200); + response.status(HttpStatus.SC_OK); return lightResponse; }, new JsonTransformer()); @@ -146,13 +158,13 @@ public class HueMulator { state = mapper.readValue(request.body(), DeviceState.class); } catch (IOException e) { log.error("Object mapper barfed on input of body.", e); - response.status(400); + response.status(HttpStatus.SC_BAD_REQUEST); return null; } DeviceDescriptor device = repository.findOne(lightId); if (device == null) { - response.status(404); + response.status(HttpStatus.SC_NOT_FOUND); log.error("Could not find devcie: " + lightId + " for hue state change request: " + userId + " from " + request.ip() + " body: " + request.body()); return null; } @@ -172,13 +184,13 @@ public class HueMulator { String body = replaceIntensityValue(device.getContentBody(), state.getBri()); //make call if(!doHttpRequest(url, device.getHttpVerb(), device.getContentType(), body)){ - response.status(503); + response.status(HttpStatus.SC_SERVICE_UNAVAILABLE); log.error("Error on calling url to change device state: " + url); return null; } response.type("application/json; charset=utf-8"); - response.status(200); + response.status(HttpStatus.SC_OK); return responseString; }); } diff --git a/src/main/resources/public/views/configuration.html b/src/main/resources/public/views/configuration.html index 1ac0cbe..be7d237 100644 --- a/src/main/resources/public/views/configuration.html +++ b/src/main/resources/public/views/configuration.html @@ -53,6 +53,18 @@ vera.address {{BridgeSettings.veraaddress}} + + upnp.strict + {{BridgeSettings.upnpstrict}} + + + trace.upnp + {{BridgeSettings.traceupnp}} + + + vtwo.compatibility + {{BridgeSettings.vtwocompatibility}} +