mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-16 18:24:36 +00:00
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:
@@ -8,6 +8,9 @@ public class Device {
|
||||
private String deviceType;
|
||||
private String offUrl;
|
||||
private String onUrl;
|
||||
private String httpVerb;
|
||||
private String contentType;
|
||||
private String contentBody;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
@@ -40,4 +43,30 @@ public class Device {
|
||||
public void setOnUrl(String 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -8,6 +8,9 @@ public class DeviceDescriptor{
|
||||
private String deviceType;
|
||||
private String offUrl;
|
||||
private String onUrl;
|
||||
private String httpVerb;
|
||||
private String contentType;
|
||||
private String contentBody;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
@@ -48,4 +51,30 @@ public class DeviceDescriptor{
|
||||
public void setId(String 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -179,6 +179,15 @@ public class DeviceRepository {
|
||||
} else if (name.equals("onUrl")) {
|
||||
deviceEntry.setOnUrl(reader.nextString());
|
||||
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 {
|
||||
reader.skipValue();
|
||||
}
|
||||
|
||||
@@ -5,8 +5,12 @@ import static spark.Spark.post;
|
||||
import static spark.Spark.put;
|
||||
import static spark.Spark.delete;
|
||||
|
||||
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.LoggerFactory;
|
||||
|
||||
@@ -27,7 +31,7 @@ public class DeviceResource {
|
||||
|
||||
private DeviceRepository deviceRepository;
|
||||
private VeraInfo veraInfo;
|
||||
|
||||
private static final Set<String> supportedVerbs = new HashSet<>(Arrays.asList("get", "put", "post"));
|
||||
|
||||
public DeviceResource(BridgeSettings theSettings) {
|
||||
super();
|
||||
@@ -45,39 +49,44 @@ public class DeviceResource {
|
||||
post(API_CONTEXT, "application/json", (request, response) -> {
|
||||
log.debug("Create a Device - request body: " + request.body());
|
||||
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
|
||||
DeviceDescriptor deviceEntry = new DeviceDescriptor();
|
||||
deviceEntry.setName(device.getName());
|
||||
log.debug("Create a Device - device json name: " + deviceEntry.getName());
|
||||
deviceEntry.setDeviceType(device.getDeviceType());
|
||||
log.debug("Create a Device - device json type:" + deviceEntry.getDeviceType());
|
||||
deviceEntry.setOnUrl(device.getOnUrl());
|
||||
log.debug("Create a Device - device json on URL:" + deviceEntry.getOnUrl());
|
||||
deviceEntry.setOffUrl(device.getOffUrl());
|
||||
log.debug("Create a Device - device json off URL:" + deviceEntry.getOffUrl());
|
||||
if(device.getContentBody() != null ) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
deviceRepository.save(device);
|
||||
log.debug("Created a Device: " + request.body());
|
||||
|
||||
deviceRepository.save(deviceEntry);
|
||||
log.debug("Created a Device: " + request.body());
|
||||
|
||||
response.status(201);
|
||||
return deviceEntry;
|
||||
response.status(HttpStatus.SC_OK);
|
||||
}
|
||||
return device;
|
||||
}, new JsonTransformer());
|
||||
|
||||
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 deviceEntry = deviceRepository.findOne(request.params(":id"));
|
||||
if(deviceEntry == null){
|
||||
log.debug("Could not save an edited Device Id: " + request.params(":id"));
|
||||
return null;
|
||||
response.status(HttpStatus.SC_BAD_REQUEST);
|
||||
}
|
||||
log.debug("Saving an edited Device: " + deviceEntry.getName());
|
||||
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());
|
||||
deviceEntry.setName(device.getName());
|
||||
if (device.getDeviceType() != null)
|
||||
deviceEntry.setDeviceType(device.getDeviceType());
|
||||
deviceEntry.setOnUrl(device.getOnUrl());
|
||||
deviceEntry.setOffUrl(device.getOffUrl());
|
||||
|
||||
deviceRepository.save(deviceEntry);
|
||||
deviceRepository.save(deviceEntry);
|
||||
response.status(HttpStatus.SC_OK);
|
||||
}
|
||||
return deviceEntry;
|
||||
}, new JsonTransformer());
|
||||
|
||||
@@ -87,25 +96,30 @@ public class DeviceResource {
|
||||
JsonTransformer aRenderer = new JsonTransformer();
|
||||
String theStream = aRenderer.render(deviceList);
|
||||
log.debug("The Device List: " + theStream);
|
||||
response.status(HttpStatus.SC_OK);
|
||||
return deviceList;
|
||||
}, new JsonTransformer());
|
||||
|
||||
get (API_CONTEXT + "/:id", "application/json", (request, response) -> {
|
||||
log.debug("Get a device");
|
||||
DeviceDescriptor descriptor = deviceRepository.findOne(request.params(":id"));
|
||||
if(descriptor == null){
|
||||
return null;
|
||||
}
|
||||
if(descriptor == null)
|
||||
response.status(HttpStatus.SC_NOT_FOUND);
|
||||
else
|
||||
response.status(HttpStatus.SC_OK);
|
||||
return descriptor;
|
||||
}, new JsonTransformer());
|
||||
|
||||
delete (API_CONTEXT + "/:id", "application/json", (request, response) -> {
|
||||
log.debug("Delete a device");
|
||||
DeviceDescriptor deleted = deviceRepository.findOne(request.params(":id"));
|
||||
if(deleted == null){
|
||||
return null;
|
||||
if(deleted == null)
|
||||
response.status(HttpStatus.SC_NOT_FOUND);
|
||||
else
|
||||
{
|
||||
deviceRepository.delete(deleted);
|
||||
response.status(HttpStatus.SC_OK);
|
||||
}
|
||||
deviceRepository.delete(deleted);
|
||||
return null;
|
||||
}, new JsonTransformer());
|
||||
|
||||
@@ -113,8 +127,11 @@ public class DeviceResource {
|
||||
log.debug("Get vera devices");
|
||||
Sdata sData = veraInfo.getSdata();
|
||||
if(sData == null){
|
||||
return null;
|
||||
response.status(HttpStatus.SC_NOT_FOUND);
|
||||
return null;
|
||||
}
|
||||
|
||||
response.status(HttpStatus.SC_OK);
|
||||
return sData.getDevices();
|
||||
}, new JsonTransformer());
|
||||
|
||||
@@ -122,8 +139,10 @@ public class DeviceResource {
|
||||
log.debug("Get vera scenes");
|
||||
Sdata sData = veraInfo.getSdata();
|
||||
if(sData == null){
|
||||
response.status(HttpStatus.SC_NOT_FOUND);
|
||||
return null;
|
||||
}
|
||||
response.status(HttpStatus.SC_OK);
|
||||
return sData.getScenes();
|
||||
}, new JsonTransformer());
|
||||
|
||||
|
||||
@@ -17,6 +17,11 @@ import static spark.Spark.put;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
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.util.EntityUtils;
|
||||
|
||||
@@ -162,26 +167,11 @@ public class HueMulator {
|
||||
url = device.getOffUrl();
|
||||
}
|
||||
|
||||
/* 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
|
||||
*/
|
||||
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()) + "}}]";
|
||||
}
|
||||
|
||||
//quick template
|
||||
url = replaceIntensityValue(url, state.getBri());
|
||||
String body = replaceIntensityValue(device.getContentBody(), state.getBri());
|
||||
//make call
|
||||
if(!doHttpGETRequest(url)){
|
||||
if(!doHttpRequest(url, device.getHttpVerb(), device.getContentType(), body)){
|
||||
response.status(503);
|
||||
log.error("Error on calling url to change device state: " + url);
|
||||
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
|
||||
protected boolean doHttpGETRequest(String url) {
|
||||
log.debug("calling GET on URL: " + url);
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
protected boolean doHttpRequest(String url, String httpVerb, String contentType, String body) {
|
||||
HttpUriRequest request = null;
|
||||
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 {
|
||||
HttpResponse response = httpClient.execute(httpGet);
|
||||
HttpResponse response = httpClient.execute(request);
|
||||
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){
|
||||
return true;
|
||||
}
|
||||
|
||||
11
src/main/resources/public/css/main.css
Normal file
11
src/main/resources/public/css/main.css
Normal file
@@ -0,0 +1,11 @@
|
||||
body {
|
||||
padding-top: 60px;
|
||||
padding-bottom: 20px;
|
||||
}
|
||||
|
||||
.sortorder:after {
|
||||
content: '\25b2';
|
||||
}
|
||||
.sortorder.reverse:after {
|
||||
content: '\25bc';
|
||||
}
|
||||
@@ -5,12 +5,7 @@
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>HA Bridge</title>
|
||||
<style>
|
||||
body {
|
||||
padding-top: 60px;
|
||||
padding-bottom: 20px;
|
||||
}
|
||||
</style>
|
||||
<link href="css/main.css" rel="stylesheet">
|
||||
<link href="css/bootstrap.min.css" rel="stylesheet">
|
||||
|
||||
<!--[if lt IE 9]>
|
||||
|
||||
@@ -207,7 +207,7 @@ app.controller('ViewingController', function ($scope, $location, bridgeService,
|
||||
$scope.BridgeSettings = bridgeService.BridgeSettings;
|
||||
bridgeService.viewDevices();
|
||||
$scope.bridge = bridgeService.state;
|
||||
$scope.predicate = 'name';
|
||||
$scope.predicate = '';
|
||||
$scope.reverse = true;
|
||||
$scope.order = function(predicate) {
|
||||
$scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false;
|
||||
@@ -239,7 +239,7 @@ app.controller('AddingController', function ($scope, bridgeService, BridgeSettin
|
||||
bridgeService.viewVeraScenes();
|
||||
$scope.bridge = bridgeService.state;
|
||||
$scope.device = bridgeService.state.device;
|
||||
$scope.predicate = 'name';
|
||||
$scope.predicate = '';
|
||||
$scope.reverse = true;
|
||||
$scope.order = function(predicate) {
|
||||
$scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false;
|
||||
|
||||
@@ -70,16 +70,6 @@
|
||||
<div ng-show='bridge.error != ""'>{{bridge.error}}</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-heading">
|
||||
<h2 class="panel-title">Current devices</h2>
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
URL </label>
|
||||
|
||||
<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>
|
||||
</div>
|
||||
<div class="clearfix visible-xs"></div>
|
||||
@@ -44,7 +44,7 @@
|
||||
for="device-off-url">Off URL </label>
|
||||
|
||||
<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>
|
||||
</div>
|
||||
<div class="clearfix visible-xs"></div>
|
||||
|
||||
@@ -81,7 +81,7 @@
|
||||
URL </label>
|
||||
|
||||
<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>
|
||||
</div>
|
||||
<div class="clearfix visible-xs"></div>
|
||||
@@ -95,7 +95,7 @@
|
||||
for="device-off-url">Off URL </label>
|
||||
|
||||
<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>
|
||||
</div>
|
||||
<div class="clearfix visible-xs"></div>
|
||||
|
||||
@@ -9,14 +9,6 @@
|
||||
<div class="panel-heading">
|
||||
<h2 class="panel-title">Vera Device List</h2>
|
||||
</div>
|
||||
<style type="text/css">
|
||||
.sortorder:after {
|
||||
content: '\25b2';
|
||||
}
|
||||
.sortorder.reverse:after {
|
||||
content: '\25bc';
|
||||
}
|
||||
</style>
|
||||
<ul class="list-group">
|
||||
<li class="list-group-item">
|
||||
<p class="text-muted">You can select a Vera device and generate
|
||||
@@ -83,7 +75,7 @@
|
||||
URL </label>
|
||||
|
||||
<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>
|
||||
</div>
|
||||
<div class="clearfix visible-xs"></div>
|
||||
@@ -97,7 +89,7 @@
|
||||
for="device-off-url">Off URL </label>
|
||||
|
||||
<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>
|
||||
</div>
|
||||
<div class="clearfix visible-xs"></div>
|
||||
|
||||
@@ -9,14 +9,6 @@
|
||||
<div class="panel-heading">
|
||||
<h2 class="panel-title">Vera Scene List</h2>
|
||||
</div>
|
||||
<style type="text/css">
|
||||
.sortorder:after {
|
||||
content: '\25b2';
|
||||
}
|
||||
.sortorder.reverse:after {
|
||||
content: '\25bc';
|
||||
}
|
||||
</style>
|
||||
<ul class="list-group">
|
||||
<li class="list-group-item">
|
||||
<p class="text-muted">You can select a Vera scene and generate
|
||||
@@ -78,7 +70,7 @@
|
||||
URL </label>
|
||||
|
||||
<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>
|
||||
</div>
|
||||
<div class="clearfix visible-xs"></div>
|
||||
@@ -92,7 +84,7 @@
|
||||
for="device-off-url">Off URL </label>
|
||||
|
||||
<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>
|
||||
</div>
|
||||
<div class="clearfix visible-xs"></div>
|
||||
|
||||
Reference in New Issue
Block a user