Added the Post Put capabilities for the http eecution. cleaned up css

for list ordering. Cleaned up device management calls.
This commit is contained in:
Admin
2015-09-01 13:40:53 -05:00
parent 6c99358f95
commit 7294dbf175
13 changed files with 196 additions and 102 deletions

View File

@@ -8,6 +8,9 @@ public class Device {
private String deviceType; private String deviceType;
private String offUrl; private String offUrl;
private String onUrl; private String onUrl;
private String httpVerb;
private String contentType;
private String contentBody;
public String getName() { public String getName() {
return name; return name;
@@ -40,4 +43,30 @@ public class Device {
public void setOnUrl(String onUrl) { public void setOnUrl(String onUrl) {
this.onUrl = onUrl; this.onUrl = onUrl;
} }
public String getHttpVerb() {
return httpVerb;
}
public void setHttpVerb(String httpVerb) {
this.httpVerb = httpVerb;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getContentBody() {
return contentBody;
}
public void setContentBody(String contentBody) {
this.contentBody = contentBody;
}
} }

View File

@@ -8,6 +8,9 @@ public class DeviceDescriptor{
private String deviceType; private String deviceType;
private String offUrl; private String offUrl;
private String onUrl; private String onUrl;
private String httpVerb;
private String contentType;
private String contentBody;
public String getName() { public String getName() {
return name; return name;
@@ -48,4 +51,30 @@ public class DeviceDescriptor{
public void setId(String id) { public void setId(String id) {
this.id = id; this.id = id;
} }
public String getHttpVerb() {
return httpVerb;
}
public void setHttpVerb(String httpVerb) {
this.httpVerb = httpVerb;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getContentBody() {
return contentBody;
}
public void setContentBody(String contentBody) {
this.contentBody = contentBody;
}
} }

View File

@@ -179,6 +179,15 @@ public class DeviceRepository {
} else if (name.equals("onUrl")) { } else if (name.equals("onUrl")) {
deviceEntry.setOnUrl(reader.nextString()); deviceEntry.setOnUrl(reader.nextString());
log.debug("Read a Device - device json on URL:" + deviceEntry.getOnUrl()); log.debug("Read a Device - device json on URL:" + deviceEntry.getOnUrl());
} else if (name.equals("httpVerb")) {
deviceEntry.setHttpVerb(reader.nextString());
log.debug("Read a Device - device json httpVerb:" + deviceEntry.getHttpVerb());
} else if (name.equals("contentType")) {
deviceEntry.setContentType(reader.nextString());
log.debug("Read a Device - device json contentType:" + deviceEntry.getContentType());
} else if (name.equals("contentBody")) {
deviceEntry.setContentBody(reader.nextString());
log.debug("Read a Device - device json contentBody:" + deviceEntry.getContentBody());
} else { } else {
reader.skipValue(); reader.skipValue();
} }

View File

@@ -4,9 +4,13 @@ import static spark.Spark.get;
import static spark.Spark.post; import static spark.Spark.post;
import static spark.Spark.put; import static spark.Spark.put;
import static spark.Spark.delete; import static spark.Spark.delete;
import java.util.List;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.http.HttpStatus;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -27,7 +31,7 @@ public class DeviceResource {
private DeviceRepository deviceRepository; private DeviceRepository deviceRepository;
private VeraInfo veraInfo; private VeraInfo veraInfo;
private static final Set<String> supportedVerbs = new HashSet<>(Arrays.asList("get", "put", "post"));
public DeviceResource(BridgeSettings theSettings) { public DeviceResource(BridgeSettings theSettings) {
super(); super();
@@ -45,39 +49,44 @@ public class DeviceResource {
post(API_CONTEXT, "application/json", (request, response) -> { post(API_CONTEXT, "application/json", (request, response) -> {
log.debug("Create a Device - request body: " + request.body()); log.debug("Create a Device - request body: " + request.body());
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class); DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
DeviceDescriptor deviceEntry = new DeviceDescriptor(); if(device.getContentBody() != null ) {
deviceEntry.setName(device.getName()); if (device.getContentType() == null || device.getHttpVerb() == null || !supportedVerbs.contains(device.getHttpVerb().toLowerCase())) {
log.debug("Create a Device - device json name: " + deviceEntry.getName()); device = null;
deviceEntry.setDeviceType(device.getDeviceType()); response.status(HttpStatus.SC_BAD_REQUEST);
log.debug("Create a Device - device json type:" + deviceEntry.getDeviceType()); log.debug("Created a Device: " + request.body());
deviceEntry.setOnUrl(device.getOnUrl()); }
log.debug("Create a Device - device json on URL:" + deviceEntry.getOnUrl()); }
deviceEntry.setOffUrl(device.getOffUrl()); else
log.debug("Create a Device - device json off URL:" + deviceEntry.getOffUrl()); {
deviceRepository.save(device);
deviceRepository.save(deviceEntry); log.debug("Created a Device: " + request.body());
log.debug("Created a Device: " + request.body());
response.status(HttpStatus.SC_OK);
response.status(201); }
return deviceEntry; return device;
}, new JsonTransformer()); }, new JsonTransformer());
put (API_CONTEXT + "/:id", "application/json", (request, response) -> { put (API_CONTEXT + "/:id", "application/json", (request, response) -> {
log.debug("Edit a Device - request body: " + request.body());
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class); DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
DeviceDescriptor deviceEntry = deviceRepository.findOne(request.params(":id")); DeviceDescriptor deviceEntry = deviceRepository.findOne(request.params(":id"));
if(deviceEntry == null){ if(deviceEntry == null){
log.debug("Could not save an edited Device Id: " + request.params(":id")); log.debug("Could not save an edited Device Id: " + request.params(":id"));
return null; response.status(HttpStatus.SC_BAD_REQUEST);
}
else
{
log.debug("Saving an edited Device: " + deviceEntry.getName());
deviceEntry.setName(device.getName());
if (device.getDeviceType() != null)
deviceEntry.setDeviceType(device.getDeviceType());
deviceEntry.setOnUrl(device.getOnUrl());
deviceEntry.setOffUrl(device.getOffUrl());
deviceRepository.save(deviceEntry);
response.status(HttpStatus.SC_OK);
} }
log.debug("Saving an edited Device: " + deviceEntry.getName());
deviceEntry.setName(device.getName());
if(device.getDeviceType() != null)
deviceEntry.setDeviceType(device.getDeviceType());
deviceEntry.setOnUrl(device.getOnUrl());
deviceEntry.setOffUrl(device.getOffUrl());
deviceRepository.save(deviceEntry);
return deviceEntry; return deviceEntry;
}, new JsonTransformer()); }, new JsonTransformer());
@@ -87,25 +96,30 @@ public class DeviceResource {
JsonTransformer aRenderer = new JsonTransformer(); JsonTransformer aRenderer = new JsonTransformer();
String theStream = aRenderer.render(deviceList); String theStream = aRenderer.render(deviceList);
log.debug("The Device List: " + theStream); log.debug("The Device List: " + theStream);
response.status(HttpStatus.SC_OK);
return deviceList; return deviceList;
}, new JsonTransformer()); }, new JsonTransformer());
get (API_CONTEXT + "/:id", "application/json", (request, response) -> { get (API_CONTEXT + "/:id", "application/json", (request, response) -> {
log.debug("Get a device"); log.debug("Get a device");
DeviceDescriptor descriptor = deviceRepository.findOne(request.params(":id")); DeviceDescriptor descriptor = deviceRepository.findOne(request.params(":id"));
if(descriptor == null){ if(descriptor == null)
return null; response.status(HttpStatus.SC_NOT_FOUND);
} else
response.status(HttpStatus.SC_OK);
return descriptor; return descriptor;
}, new JsonTransformer()); }, new JsonTransformer());
delete (API_CONTEXT + "/:id", "application/json", (request, response) -> { delete (API_CONTEXT + "/:id", "application/json", (request, response) -> {
log.debug("Delete a device"); log.debug("Delete a device");
DeviceDescriptor deleted = deviceRepository.findOne(request.params(":id")); DeviceDescriptor deleted = deviceRepository.findOne(request.params(":id"));
if(deleted == null){ if(deleted == null)
return null; response.status(HttpStatus.SC_NOT_FOUND);
else
{
deviceRepository.delete(deleted);
response.status(HttpStatus.SC_OK);
} }
deviceRepository.delete(deleted);
return null; return null;
}, new JsonTransformer()); }, new JsonTransformer());
@@ -113,8 +127,11 @@ public class DeviceResource {
log.debug("Get vera devices"); log.debug("Get vera devices");
Sdata sData = veraInfo.getSdata(); Sdata sData = veraInfo.getSdata();
if(sData == null){ if(sData == null){
return null; response.status(HttpStatus.SC_NOT_FOUND);
return null;
} }
response.status(HttpStatus.SC_OK);
return sData.getDevices(); return sData.getDevices();
}, new JsonTransformer()); }, new JsonTransformer());
@@ -122,10 +139,12 @@ public class DeviceResource {
log.debug("Get vera scenes"); log.debug("Get vera scenes");
Sdata sData = veraInfo.getSdata(); Sdata sData = veraInfo.getSdata();
if(sData == null){ if(sData == null){
response.status(HttpStatus.SC_NOT_FOUND);
return null; return null;
} }
response.status(HttpStatus.SC_OK);
return sData.getScenes(); return sData.getScenes();
}, new JsonTransformer()); }, new JsonTransformer());
} }
} }

View File

@@ -17,6 +17,11 @@ import static spark.Spark.put;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
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.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
@@ -162,26 +167,11 @@ public class HueMulator {
url = device.getOffUrl(); url = device.getOffUrl();
} }
/* light weight templating here, was going to use free marker but it was a bit too //quick template
* heavy for what we were trying to do. url = replaceIntensityValue(url, state.getBri());
* String body = replaceIntensityValue(device.getContentBody(), state.getBri());
* currently provides only two variables:
* intensity.byte : 0-255 brightness. this is raw from the echo
* intensity.percent : 0-100, adjusted for the vera
*/
if(url.contains(INTENSITY_BYTE)){
String intensityByte = String.valueOf(state.getBri());
url = url.replace(INTENSITY_BYTE, intensityByte);
responseString = "[{\"success\":{\"/lights/" + lightId + "/state/bri\":"+ String.valueOf(state.getBri()) + "}}]";
}else if(url.contains(INTENSITY_PERCENT)){
int percentBrightness = (int) Math.round(state.getBri()/255.0*100);
String intensityPercent = String.valueOf(percentBrightness);
url = url.replace(INTENSITY_PERCENT, intensityPercent);
responseString = "[{\"success\":{\"/lights/" + lightId + "/state/bri\":"+ String.valueOf(state.getBri()) + "}}]";
}
//make call //make call
if(!doHttpGETRequest(url)){ if(!doHttpRequest(url, device.getHttpVerb(), device.getContentType(), body)){
response.status(503); response.status(503);
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;
@@ -193,14 +183,52 @@ public class HueMulator {
}); });
} }
/* light weight templating here, was going to use free marker but it was a bit too
* heavy for what we were trying to do.
*
* currently provides only two variables:
* intensity.byte : 0-255 brightness. this is raw from the echo
* intensity.percent : 0-100, adjusted for the vera
*/
protected String replaceIntensityValue(String request, int intensity){
if(request == null){
return "";
}
if(request.contains(INTENSITY_BYTE)){
String intensityByte = String.valueOf(intensity);
request = request.replace(INTENSITY_BYTE, intensityByte);
}else if(request.contains(INTENSITY_PERCENT)){
int percentBrightness = (int) Math.round(intensity/255.0*100);
String intensityPercent = String.valueOf(percentBrightness);
request = request.replace(INTENSITY_PERCENT, intensityPercent);
}
return request;
}
// This function executes the url from the device repository against the vera // This function executes the url from the device repository against the vera
protected boolean doHttpGETRequest(String url) { protected boolean doHttpRequest(String url, String httpVerb, String contentType, String body) {
log.debug("calling GET on URL: " + url); HttpUriRequest request = null;
HttpGet httpGet = new HttpGet(url); if(HttpGet.METHOD_NAME.equalsIgnoreCase(httpVerb) || httpVerb == null) {
request = new HttpGet(url);
}else if(HttpPost.METHOD_NAME.equalsIgnoreCase(httpVerb)){
HttpPost postRequest = new HttpPost(url);
ContentType parsedContentType = ContentType.parse(contentType);
StringEntity requestBody = new StringEntity(body, parsedContentType);
postRequest.setEntity(requestBody);
request = postRequest;
}else if(HttpPut.METHOD_NAME.equalsIgnoreCase(httpVerb)){
HttpPut putRequest = new HttpPut(url);
ContentType parsedContentType = ContentType.parse(contentType);
StringEntity requestBody = new StringEntity(body, parsedContentType);
putRequest.setEntity(requestBody);
request = putRequest;
}
log.debug("Making outbound call in doHttpRequest: " + request);
try { try {
HttpResponse response = httpClient.execute(httpGet); HttpResponse response = httpClient.execute(request);
EntityUtils.consume(response.getEntity()); //close out inputstream ignore content EntityUtils.consume(response.getEntity()); //close out inputstream ignore content
log.debug("GET on URL responded: " + response.getStatusLine().getStatusCode()); log.debug("Execute on URL responded: " + response.getStatusLine().getStatusCode());
if(response.getStatusLine().getStatusCode() == 200){ if(response.getStatusLine().getStatusCode() == 200){
return true; return true;
} }

View File

@@ -0,0 +1,11 @@
body {
padding-top: 60px;
padding-bottom: 20px;
}
.sortorder:after {
content: '\25b2';
}
.sortorder.reverse:after {
content: '\25bc';
}

View File

@@ -5,12 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>HA Bridge</title> <title>HA Bridge</title>
<style> <link href="css/main.css" rel="stylesheet">
body {
padding-top: 60px;
padding-bottom: 20px;
}
</style>
<link href="css/bootstrap.min.css" rel="stylesheet"> <link href="css/bootstrap.min.css" rel="stylesheet">
<!--[if lt IE 9]> <!--[if lt IE 9]>

View File

@@ -207,7 +207,7 @@ app.controller('ViewingController', function ($scope, $location, bridgeService,
$scope.BridgeSettings = bridgeService.BridgeSettings; $scope.BridgeSettings = bridgeService.BridgeSettings;
bridgeService.viewDevices(); bridgeService.viewDevices();
$scope.bridge = bridgeService.state; $scope.bridge = bridgeService.state;
$scope.predicate = 'name'; $scope.predicate = '';
$scope.reverse = true; $scope.reverse = true;
$scope.order = function(predicate) { $scope.order = function(predicate) {
$scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false; $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false;
@@ -239,7 +239,7 @@ app.controller('AddingController', function ($scope, bridgeService, BridgeSettin
bridgeService.viewVeraScenes(); bridgeService.viewVeraScenes();
$scope.bridge = bridgeService.state; $scope.bridge = bridgeService.state;
$scope.device = bridgeService.state.device; $scope.device = bridgeService.state.device;
$scope.predicate = 'name'; $scope.predicate = '';
$scope.reverse = true; $scope.reverse = true;
$scope.order = function(predicate) { $scope.order = function(predicate) {
$scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false; $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false;

View File

@@ -70,16 +70,6 @@
<div ng-show='bridge.error != ""'>{{bridge.error}}</div> <div ng-show='bridge.error != ""'>{{bridge.error}}</div>
</div> </div>
</div> </div>
<style type="text/css">
.sortorder:after {
content: '\25b2';
}
.sortorder.reverse:after {
content: '\25bc';
}
</style>
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading"> <div class="panel-heading">
<h2 class="panel-title">Current devices</h2> <h2 class="panel-title">Current devices</h2>

View File

@@ -30,7 +30,7 @@
URL </label> URL </label>
<div class="col-xs-8 col-sm-7"> <div class="col-xs-8 col-sm-7">
<textarea class="form-control" id="device-on-url" <textarea rows="3" class="form-control" id="device-on-url"
ng-model="device.onUrl" placeholder="URL to turn device on"></textarea> ng-model="device.onUrl" placeholder="URL to turn device on"></textarea>
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>
@@ -44,7 +44,7 @@
for="device-off-url">Off URL </label> for="device-off-url">Off URL </label>
<div class="col-xs-8 col-sm-7"> <div class="col-xs-8 col-sm-7">
<textarea class="form-control" id="device-off-url" <textarea rows="3" class="form-control" id="device-off-url"
ng-model="device.offUrl" placeholder="URL to turn device off"></textarea> ng-model="device.offUrl" placeholder="URL to turn device off"></textarea>
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>

View File

@@ -81,7 +81,7 @@
URL </label> URL </label>
<div class="col-xs-8 col-sm-7"> <div class="col-xs-8 col-sm-7">
<textarea class="form-control" id="device-on-url" <textarea rows="3" class="form-control" id="device-on-url"
ng-model="device.onUrl" placeholder="URL to turn device on"></textarea> ng-model="device.onUrl" placeholder="URL to turn device on"></textarea>
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>
@@ -95,7 +95,7 @@
for="device-off-url">Off URL </label> for="device-off-url">Off URL </label>
<div class="col-xs-8 col-sm-7"> <div class="col-xs-8 col-sm-7">
<textarea class="form-control" id="device-off-url" <textarea rows="3" class="form-control" id="device-off-url"
ng-model="device.offUrl" placeholder="URL to turn device off"></textarea> ng-model="device.offUrl" placeholder="URL to turn device off"></textarea>
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>

View File

@@ -9,14 +9,6 @@
<div class="panel-heading"> <div class="panel-heading">
<h2 class="panel-title">Vera Device List</h2> <h2 class="panel-title">Vera Device List</h2>
</div> </div>
<style type="text/css">
.sortorder:after {
content: '\25b2';
}
.sortorder.reverse:after {
content: '\25bc';
}
</style>
<ul class="list-group"> <ul class="list-group">
<li class="list-group-item"> <li class="list-group-item">
<p class="text-muted">You can select a Vera device and generate <p class="text-muted">You can select a Vera device and generate
@@ -83,7 +75,7 @@
URL </label> URL </label>
<div class="col-xs-8 col-sm-7"> <div class="col-xs-8 col-sm-7">
<textarea class="form-control" id="device-on-url" <textarea rows="3" class="form-control" id="device-on-url"
ng-model="device.onUrl" placeholder="URL to turn device on"></textarea> ng-model="device.onUrl" placeholder="URL to turn device on"></textarea>
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>
@@ -97,7 +89,7 @@
for="device-off-url">Off URL </label> for="device-off-url">Off URL </label>
<div class="col-xs-8 col-sm-7"> <div class="col-xs-8 col-sm-7">
<textarea class="form-control" id="device-off-url" <textarea rows="3" class="form-control" id="device-off-url"
ng-model="device.offUrl" placeholder="URL to turn device off"></textarea> ng-model="device.offUrl" placeholder="URL to turn device off"></textarea>
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>

View File

@@ -9,14 +9,6 @@
<div class="panel-heading"> <div class="panel-heading">
<h2 class="panel-title">Vera Scene List</h2> <h2 class="panel-title">Vera Scene List</h2>
</div> </div>
<style type="text/css">
.sortorder:after {
content: '\25b2';
}
.sortorder.reverse:after {
content: '\25bc';
}
</style>
<ul class="list-group"> <ul class="list-group">
<li class="list-group-item"> <li class="list-group-item">
<p class="text-muted">You can select a Vera scene and generate <p class="text-muted">You can select a Vera scene and generate
@@ -78,7 +70,7 @@
URL </label> URL </label>
<div class="col-xs-8 col-sm-7"> <div class="col-xs-8 col-sm-7">
<textarea class="form-control" id="device-on-url" <textarea rows="3" class="form-control" id="device-on-url"
ng-model="device.onUrl" placeholder="URL to turn device on"></textarea> ng-model="device.onUrl" placeholder="URL to turn device on"></textarea>
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>
@@ -92,7 +84,7 @@
for="device-off-url">Off URL </label> for="device-off-url">Off URL </label>
<div class="col-xs-8 col-sm-7"> <div class="col-xs-8 col-sm-7">
<textarea class="form-control" id="device-off-url" <textarea rows="3" class="form-control" id="device-off-url"
ng-model="device.offUrl" placeholder="URL to turn device off"></textarea> ng-model="device.offUrl" placeholder="URL to turn device off"></textarea>
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>