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.
This commit is contained in:
Admin
2015-09-03 11:44:26 -05:00
parent eecf0f9875
commit 74d4548beb
5 changed files with 52 additions and 25 deletions

View File

@@ -23,7 +23,7 @@ The default location for the db to contain the devices as they are added is "dat
### -Dupnp.resonse.port=`<port>` ### -Dupnp.resonse.port=`<port>`
The upnp response port that will be used. The default is 50000. The upnp response port that will be used. The default is 50000.
### -Dupnp.strict=`<true|false>` ### -Dupnp.strict=`<true|false>`
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=`<true|false>` 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=`<true|false>` 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=`<true|false>` ### -Dtrace.upnp=`<true|false>`
Turn on tracing for upnp discovery messages. The default is false. Turn on tracing for upnp discovery messages. The default is false.
### -Dvtwo.compatibility=`<true|false>` ### -Dvtwo.compatibility=`<true|false>`

View File

@@ -54,9 +54,9 @@ public class HABridge {
bridgeSettings.setUpnpDeviceDb(System.getProperty("upnp.device.db", "data/device.db")); bridgeSettings.setUpnpDeviceDb(System.getProperty("upnp.device.db", "data/device.db"));
bridgeSettings.setUpnpResponsePort(System.getProperty("upnp.response.port", "50000")); bridgeSettings.setUpnpResponsePort(System.getProperty("upnp.response.port", "50000"));
bridgeSettings.setVeraAddress(System.getProperty("vera.address", "192.168.1.100")); 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("upnp.strict", "false")));
bridgeSettings.setUpnpStrict(Boolean.parseBoolean(System.getProperty("trace.upnp", "false"))); bridgeSettings.setTraceupnp(Boolean.parseBoolean(System.getProperty("trace.upnp", "false")));
bridgeSettings.setUpnpStrict(Boolean.parseBoolean(System.getProperty("vtwo.compatibility", "true"))); bridgeSettings.setVtwocompatibility(Boolean.parseBoolean(System.getProperty("vtwo.compatibility", "true")));
// sparkjava config directive to set ip address for the web server to listen on // sparkjava config directive to set ip address for the web server to listen on
// ipAddress("0.0.0.0"); // not used // ipAddress("0.0.0.0"); // not used

View File

@@ -53,16 +53,16 @@ public class DeviceResource {
if (device.getContentType() == null || device.getHttpVerb() == null || !supportedVerbs.contains(device.getHttpVerb().toLowerCase())) { if (device.getContentType() == null || device.getHttpVerb() == null || !supportedVerbs.contains(device.getHttpVerb().toLowerCase())) {
device = null; device = null;
response.status(HttpStatus.SC_BAD_REQUEST); 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; return device;
}, new JsonTransformer()); }, new JsonTransformer());
@@ -83,6 +83,9 @@ public class DeviceResource {
deviceEntry.setDeviceType(device.getDeviceType()); deviceEntry.setDeviceType(device.getDeviceType());
deviceEntry.setOnUrl(device.getOnUrl()); deviceEntry.setOnUrl(device.getOnUrl());
deviceEntry.setOffUrl(device.getOffUrl()); deviceEntry.setOffUrl(device.getOffUrl());
deviceEntry.setHttpVerb(device.getHttpVerb());
deviceEntry.setContentType(device.getContentType());
deviceEntry.setContentBody(device.getContentBody());
deviceRepository.save(deviceEntry); deviceRepository.save(deviceEntry);
response.status(HttpStatus.SC_OK); response.status(HttpStatus.SC_OK);

View File

@@ -15,6 +15,7 @@ import static spark.Spark.post;
import static spark.Spark.put; import static spark.Spark.put;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
@@ -70,21 +71,32 @@ public class HueMulator {
deviceResponseMap.put(device.getId(), deviceResponse); deviceResponseMap.put(device.getId(), deviceResponse);
} }
response.type("application/json; charset=utf-8"); response.type("application/json; charset=utf-8");
response.status(200); response.status(HttpStatus.SC_OK);
return deviceResponseMap; return deviceResponseMap;
} , new JsonTransformer()); } , new JsonTransformer());
// http://ip_address:port/api with body of user request returns json object for a success of user add // 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) -> { post(HUE_CONTEXT, "application/json", (request, response) -> {
log.debug("hue api user create requested: " + request.body() + " from " + request.ip()); UserCreateRequest aNewUser = null;
UserCreateRequest aNewUser = new Gson().fromJson(request.body(), UserCreateRequest.class); String newUser = null;
String newUser = aNewUser.getUsername(); 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) if(newUser == null)
newUser = "lightssystem"; newUser = "lightssystem";
log.debug("hue api user create requested for device type: " + aNewUser.getDevicetype() + " and username: " + newUser);
if(aDeviceType == null)
aDeviceType = "<not given>";
log.debug("hue api user create requested for device type: " + aDeviceType + " and username: " + newUser);
response.type("application/json; charset=utf-8"); response.type("application/json; charset=utf-8");
response.status(200); response.status(HttpStatus.SC_OK);
return "[{\"success\":{\"username\":\"" + newUser + "\"}}]"; return "[{\"success\":{\"username\":\"" + newUser + "\"}}]";
} ); } );
@@ -94,7 +106,7 @@ public class HueMulator {
log.debug("hue api full state requested: " + userId + " from " + request.ip()); log.debug("hue api full state requested: " + userId + " from " + request.ip());
List<DeviceDescriptor> descriptorList = repository.findAll(); List<DeviceDescriptor> descriptorList = repository.findAll();
if (descriptorList == null) { if (descriptorList == null) {
response.status(404); response.status(HttpStatus.SC_NOT_FOUND);
return null; return null;
} }
Map<String, DeviceResponse> deviceList = new HashMap<>(); Map<String, DeviceResponse> deviceList = new HashMap<>();
@@ -108,7 +120,7 @@ public class HueMulator {
apiResponse.setLights(deviceList); apiResponse.setLights(deviceList);
response.type("application/json; charset=utf-8"); response.type("application/json; charset=utf-8");
response.status(200); response.status(HttpStatus.SC_OK);
return apiResponse; return apiResponse;
}, new JsonTransformer()); }, new JsonTransformer());
@@ -119,7 +131,7 @@ public class HueMulator {
log.debug("hue light requested: " + lightId + " for user: " + userId + " from " + request.ip()); log.debug("hue light requested: " + lightId + " for user: " + userId + " from " + request.ip());
DeviceDescriptor device = repository.findOne(lightId); DeviceDescriptor device = repository.findOne(lightId);
if (device == null) { if (device == null) {
response.status(404); response.status(HttpStatus.SC_NOT_FOUND);
return null; return null;
} else { } else {
log.debug("found device named: " + device.getName()); log.debug("found device named: " + device.getName());
@@ -127,7 +139,7 @@ public class HueMulator {
DeviceResponse lightResponse = DeviceResponse.createResponse(device.getName(), device.getId()); DeviceResponse lightResponse = DeviceResponse.createResponse(device.getName(), device.getId());
response.type("application/json; charset=utf-8"); response.type("application/json; charset=utf-8");
response.status(200); response.status(HttpStatus.SC_OK);
return lightResponse; return lightResponse;
}, new JsonTransformer()); }, new JsonTransformer());
@@ -146,13 +158,13 @@ public class HueMulator {
state = mapper.readValue(request.body(), DeviceState.class); state = mapper.readValue(request.body(), DeviceState.class);
} catch (IOException e) { } catch (IOException e) {
log.error("Object mapper barfed on input of body.", e); log.error("Object mapper barfed on input of body.", e);
response.status(400); response.status(HttpStatus.SC_BAD_REQUEST);
return null; return null;
} }
DeviceDescriptor device = repository.findOne(lightId); DeviceDescriptor device = repository.findOne(lightId);
if (device == null) { 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()); log.error("Could not find devcie: " + lightId + " for hue state change request: " + userId + " from " + request.ip() + " body: " + request.body());
return null; return null;
} }
@@ -172,13 +184,13 @@ public class HueMulator {
String body = replaceIntensityValue(device.getContentBody(), state.getBri()); String body = replaceIntensityValue(device.getContentBody(), state.getBri());
//make call //make call
if(!doHttpRequest(url, device.getHttpVerb(), device.getContentType(), body)){ 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); log.error("Error on calling url to change device state: " + url);
return null; return null;
} }
response.type("application/json; charset=utf-8"); response.type("application/json; charset=utf-8");
response.status(200); response.status(HttpStatus.SC_OK);
return responseString; return responseString;
}); });
} }

View File

@@ -53,6 +53,18 @@
<td>vera.address</td> <td>vera.address</td>
<td>{{BridgeSettings.veraaddress}}</td> <td>{{BridgeSettings.veraaddress}}</td>
</tr> </tr>
<tr>
<td>upnp.strict</td>
<td>{{BridgeSettings.upnpstrict}}</td>
</tr>
<tr>
<td>trace.upnp</td>
<td>{{BridgeSettings.traceupnp}}</td>
</tr>
<tr>
<td>vtwo.compatibility</td>
<td>{{BridgeSettings.vtwocompatibility}}</td>
</tr>
</table> </table>
</div> </div>
</div> </div>