Updated bridge to be robust on put/post calls and testing. HAd to add a

body for off types.
This commit is contained in:
Admin
2015-10-02 16:35:17 -05:00
parent 2789d8c180
commit 7514e36edb
12 changed files with 142 additions and 40 deletions

View File

@@ -5,7 +5,7 @@
<groupId>com.bwssystems.HABridge</groupId> <groupId>com.bwssystems.HABridge</groupId>
<artifactId>ha-bridge</artifactId> <artifactId>ha-bridge</artifactId>
<version>0.4.5</version> <version>0.4.6</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>HA Bridge</name> <name>HA Bridge</name>

View File

@@ -11,6 +11,7 @@ public class DeviceDescriptor{
private String httpVerb; private String httpVerb;
private String contentType; private String contentType;
private String contentBody; private String contentBody;
private String contentBodyOff;
public String getName() { public String getName() {
return name; return name;
@@ -75,6 +76,14 @@ public class DeviceDescriptor{
public void setContentBody(String contentBody) { public void setContentBody(String contentBody) {
this.contentBody = contentBody; this.contentBody = contentBody;
} }
public String getContentBodyOff() {
return contentBodyOff;
}
public void setContentBodyOff(String contentBodyOff) {
this.contentBodyOff = contentBodyOff;
}
} }

View File

@@ -188,6 +188,9 @@ public class DeviceRepository {
} else if (name.equals("contentBody")) { } else if (name.equals("contentBody")) {
deviceEntry.setContentBody(reader.nextString()); deviceEntry.setContentBody(reader.nextString());
log.debug("Read a Device - device json contentBody:" + deviceEntry.getContentBody()); log.debug("Read a Device - device json contentBody:" + deviceEntry.getContentBody());
} else if (name.equals("contentBodyOff")) {
deviceEntry.setContentBodyOff(reader.nextString());
log.debug("Read a Device - device json contentBodyOff:" + deviceEntry.getContentBodyOff());
} else { } else {
reader.skipValue(); reader.skipValue();
} }

View File

@@ -86,6 +86,7 @@ public class DeviceResource {
deviceEntry.setHttpVerb(device.getHttpVerb()); deviceEntry.setHttpVerb(device.getHttpVerb());
deviceEntry.setContentType(device.getContentType()); deviceEntry.setContentType(device.getContentType());
deviceEntry.setContentBody(device.getContentBody()); deviceEntry.setContentBody(device.getContentBody());
deviceEntry.setContentBodyOff(device.getContentBodyOff());
deviceRepository.save(deviceEntry); deviceRepository.save(deviceEntry);
response.status(HttpStatus.SC_OK); response.status(HttpStatus.SC_OK);

View File

@@ -204,8 +204,12 @@ public class HueMulator {
} }
//quick template //quick template
String body;
url = replaceIntensityValue(url, state.getBri()); url = replaceIntensityValue(url, state.getBri());
String body = replaceIntensityValue(device.getContentBody(), state.getBri()); if (state.isOn())
body = replaceIntensityValue(device.getContentBody(), state.getBri());
else
body = replaceIntensityValue(device.getContentBodyOff(), state.getBri());
//make call //make call
if(!doHttpRequest(url, device.getHttpVerb(), device.getContentType(), body)){ if(!doHttpRequest(url, device.getHttpVerb(), device.getContentType(), body)){
response.status(HttpStatus.SC_SERVICE_UNAVAILABLE); response.status(HttpStatus.SC_SERVICE_UNAVAILABLE);

View File

@@ -36,7 +36,7 @@
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1"> <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li><a href="http://www.bwssystems.com" target="_blank">Developed by BWS Systems</a></li> <li><a href="http://www.bwssystems.com" target="_blank">Developed by BWS Systems</a></li>
<li><a href="http://www.amazon.com/echo" target="_blank">Amazon Echo</a></li> <li><a href="http://www.amazon.com/echo" target="_blank">Amazon Echo</a></li>
<li><a href="">HA Bridge Version 0.4.5</a></li> <li><a href="">HA Bridge Version 0.4.6</a></li>
</ul> </ul>
</li> </li>
</ul> </ul>

View File

@@ -156,7 +156,7 @@ app.service('bridgeService', function ($http, BridgeSettings) {
); );
}; };
this.addDevice = function (id, name, type, onUrl, offUrl, httpVerb, contentType, contentBody) { this.addDevice = function (id, name, type, onUrl, offUrl, httpVerb, contentType, contentBody, contentBodyOff) {
this.state.error = ""; this.state.error = "";
if (id) { if (id) {
var putUrl = this.state.base + "/" + id; var putUrl = this.state.base + "/" + id;
@@ -168,7 +168,8 @@ app.service('bridgeService', function ($http, BridgeSettings) {
offUrl: offUrl, offUrl: offUrl,
httpVerb: httpVerb, httpVerb: httpVerb,
contentType: contentType, contentType: contentType,
contentBody: contentBody contentBody: contentBody,
contentBodyOff: contentBodyOff
}).then( }).then(
function (response) { function (response) {
self.viewDevices(); self.viewDevices();
@@ -188,7 +189,8 @@ app.service('bridgeService', function ($http, BridgeSettings) {
offUrl: offUrl, offUrl: offUrl,
httpVerb: httpVerb, httpVerb: httpVerb,
contentType: contentType, contentType: contentType,
contentBody: contentBody contentBody: contentBody,
contentBodyOff: contentBodyOff
}).then( }).then(
function (response) { function (response) {
self.viewDevices(); self.viewDevices();
@@ -218,12 +220,12 @@ app.service('bridgeService', function ($http, BridgeSettings) {
); );
}; };
this.editDevice = function (id, name, onUrl, offUrl, httpVerb, contentType, contentBody) { this.editDevice = function (id, name, onUrl, offUrl, httpVerb, contentType, contentBody, contentBodyOff) {
self.state.device = {id: id, name: name, onUrl: onUrl, offUrl: offUrl, httpVerb: httpVerb, contentType: contentType, contentBody: contentBody}; self.state.device = {id: id, name: name, onUrl: onUrl, offUrl: offUrl, httpVerb: httpVerb, contentType: contentType, contentBody: contentBody, contentBodyOff: contentBodyOff};
}; };
}); });
app.controller('ViewingController', function ($scope, $location, bridgeService, BridgeSettings) { app.controller('ViewingController', function ($scope, $location, $http, bridgeService, BridgeSettings) {
$scope.BridgeSettings = bridgeService.BridgeSettings; $scope.BridgeSettings = bridgeService.BridgeSettings;
bridgeService.viewDevices(); bridgeService.viewDevices();
@@ -237,20 +239,31 @@ app.controller('ViewingController', function ($scope, $location, bridgeService,
$scope.deleteDevice = function (device) { $scope.deleteDevice = function (device) {
bridgeService.deleteDevice(device.id); bridgeService.deleteDevice(device.id);
}; };
$scope.testUrl = function (url) { $scope.testUrl = function (device, type) {
window.open(url, "_blank"); if(type == "on") {
if(device.httpVerb == "PUT" || device.httpVerb == "POST")
$http.put(device.onUrl, device.contentBody);
else
window.open(device.onUrl, "_blank");
}
else {
if(device.httpVerb == "PUT" || device.httpVerb == "POST")
$http.put(device.offUrl, device.contentBodyOff);
else
window.open(device.offUrl, "_blank");
}
}; };
$scope.setBridgeUrl = function (url) { $scope.setBridgeUrl = function (url) {
bridgeService.state.base = url; bridgeService.state.base = url;
bridgeService.viewDevices(); bridgeService.viewDevices();
}; };
$scope.editDevice = function (device) { $scope.editDevice = function (device) {
bridgeService.editDevice(device.id, device.name, device.onUrl, device.offUrl, device.httpVerb, device.contentType, device.contentBody); bridgeService.editDevice(device.id, device.name, device.onUrl, device.offUrl, device.httpVerb, device.contentType, device.contentBody, device.contentBodyOff);
$location.path('/editdevice'); $location.path('/editdevice');
}; };
}); });
app.controller('AddingController', function ($scope, $location, bridgeService, BridgeSettings) { app.controller('AddingController', function ($scope, $location, $http, bridgeService, BridgeSettings) {
$scope.device = {id: "", name: "", deviceType: "switch", onUrl: "", offUrl: ""}; $scope.device = {id: "", name: "", deviceType: "switch", onUrl: "", offUrl: ""};
$scope.vera = {base: "", port: "3480", id: ""}; $scope.vera = {base: "", port: "3480", id: ""};
@@ -321,11 +334,22 @@ app.controller('AddingController', function ($scope, $location, bridgeService, B
}; };
$scope.testUrl = function (url) { $scope.testUrl = function (url) {
window.open(url, "_blank"); if(type == "on") {
if(device.httpVerb == "PUT" || device.httpVerb == "POST")
$http.put(device.onUrl, device.contentBody);
else
window.open(device.onUrl, "_blank");
}
else {
if(device.httpVerb == "PUT" || device.httpVerb == "POST")
$http.put(device.offUrl, device.contentBodyOff);
else
window.open(device.offUrl, "_blank");
}
}; };
$scope.addDevice = function () { $scope.addDevice = function () {
bridgeService.addDevice($scope.device.id, $scope.device.name, $scope.device.deviceType, $scope.device.onUrl, $scope.device.offUrl, $scope.device.httpVerb, $scope.device.contentType, $scope.device.contentBody).then( bridgeService.addDevice($scope.device.id, $scope.device.name, $scope.device.deviceType, $scope.device.onUrl, $scope.device.offUrl, $scope.device.httpVerb, $scope.device.contentType, $scope.device.contentBody, $scope.device.contentBodyOff).then(
function () { function () {
$scope.device.id = ""; $scope.device.id = "";
$scope.device.name = ""; $scope.device.name = "";
@@ -335,6 +359,7 @@ app.controller('AddingController', function ($scope, $location, bridgeService, B
$scope.device.httpVerb = null; $scope.device.httpVerb = null;
$scope.device.contentType = null; $scope.device.contentType = null;
$scope.device.contentBody = null; $scope.device.contentBody = null;
$scope.device.contentBodyOff = null;
$location.path('/#'); $location.path('/#');
}, },
function (error) { function (error) {

View File

@@ -107,9 +107,9 @@
<td>{{device.deviceType}}</td> <td>{{device.deviceType}}</td>
<td> <td>
<button class="btn btn-info" type="submit" <button class="btn btn-info" type="submit"
ng-click="testUrl(device.onUrl)">Test ON</button> ng-click="testUrl(device, 'on')">Test ON</button>
<button class="btn btn-info" type="submit" <button class="btn btn-info" type="submit"
ng-click="testUrl(device.offUrl)">Test OFF</button> ng-click="testUrl(device, 'off')">Test OFF</button>
<button class="btn btn-warning" type="submit" <button class="btn btn-warning" type="submit"
ng-click="editDevice(device)">Edit</button> ng-click="editDevice(device)">Edit</button>
<button class="btn btn-danger" type="submit" <button class="btn btn-danger" type="submit"

View File

@@ -35,7 +35,7 @@
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>
<button class="col-xs-4 col-sm-2 btn btn-success" type="button" <button class="col-xs-4 col-sm-2 btn btn-success" type="button"
ng-click="testUrl(device.onUrl)">Test</button> ng-click="testUrl(device, 'on')">Test</button>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
@@ -49,7 +49,7 @@
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>
<button class="col-xs-4 col-sm-2 btn btn-success" type="button" <button class="col-xs-4 col-sm-2 btn btn-success" type="button"
ng-click="testUrl(device.offUrl)">Test</button> ng-click="testUrl(device, 'off')">Test</button>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
@@ -58,30 +58,60 @@
</label> </label>
<div class="col-xs-8 col-sm-7"> <div class="col-xs-8 col-sm-7">
<input type="text" class="form-control" id="device-http-verb" <select name="device-http-verb" id="device-http-verb" ng-model="device.httpVerb">
ng-model="device.httpVerb" placeholder="Http Verb, i.e. GET/PUT/POST"> <option value="">---Please select---</option> <!-- not selected / blank option -->
<option value="GET">GET</option>
<option value="PUT">PUT</option>
<option value="POST">POST</option>
</select>
</div> </div>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<div class="row"> <div class="row">
<label class="col-xs-12 col-sm-2 control-label" for="device-content-type">Name <label class="col-xs-12 col-sm-2 control-label" for="device-content-type">Content Type
</label> </label>
<div class="col-xs-8 col-sm-7"> <div class="col-xs-8 col-sm-7">
<input type="text" class="form-control" id="device-content-type" <select name="device-content-type" id="device-content-type" ng-model="device.contentType">
ng-model="device.contentType" placeholder="Content type, i.e. application/json"> <option value="">---Please select---</option> <!-- not selected / blank option -->
<option value="application/atom+xml">application/atom+xml</option>
<option value="application/x-www-form-urlencoded">application/x-www-form-urlencoded</option>
<option value="application/json">application/json</option>
<option value="application/octet-stream">application/octet-stream</option>
<option value="application/svg+xml">application/svg+xml</option>
<option value="application/xhtml+xml">application/xhtml+xml</option>
<option value="application/xml">application/xml</option>
<option value="*">*</option>
<option value="multipart/form-data">multipart/form-data</option>
<option value="text/html">text/html</option>
<option value="text/plain">text/plain</option>
<option value="text/xml">text/xml</option>
<option value="*/*">*/*</option>
</select>
</div> </div>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<div class="row"> <div class="row">
<label class="col-xs-12 col-sm-2 control-label" <label class="col-xs-12 col-sm-2 control-label"
for="device-content-body">Content Body </label> for="device-content-body">Content Body On</label>
<div class="col-xs-8 col-sm-7"> <div class="col-xs-8 col-sm-7">
<textarea rows="3" class="form-control" id="device-content-body" <textarea rows="3" class="form-control" id="device-content-body"
ng-model="device.contentBody" placeholder="Content Body for specific GET/PUT/POST type"></textarea> ng-model="device.contentBody" placeholder="Content Body On for specific GET/PUT/POST type"></textarea>
</div>
<div class="clearfix visible-xs"></div>
</div>
</div>
<div class="form-group">
<div class="row">
<label class="col-xs-12 col-sm-2 control-label"
for="device-content-body-off">Content Body Off</label>
<div class="col-xs-8 col-sm-7">
<textarea rows="3" class="form-control" id="device-content-body-off"
ng-model="device.contentBodyOff" placeholder="Content Body Off for specific GET/PUT/POST type"></textarea>
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>
</div> </div>

View File

@@ -86,7 +86,7 @@
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>
<button class="col-xs-4 col-sm-2 btn btn-success" type="button" <button class="col-xs-4 col-sm-2 btn btn-success" type="button"
ng-click="testUrl(device.onUrl)">Test</button> ng-click="testUrl(device, 'on')">Test</button>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
@@ -100,7 +100,7 @@
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>
<button class="col-xs-4 col-sm-2 btn btn-success" type="button" <button class="col-xs-4 col-sm-2 btn btn-success" type="button"
ng-click="testUrl(device.offUrl)">Test</button> ng-click="testUrl(device, 'off')">Test</button>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
@@ -109,30 +109,60 @@
</label> </label>
<div class="col-xs-8 col-sm-7"> <div class="col-xs-8 col-sm-7">
<input type="text" class="form-control" id="device-http-verb" <select name="device-http-verb" id="device-http-verb" ng-model="device.httpVerb">
ng-model="device.httpVerb" placeholder="Http Verb, i.e. GET/PUT/POST"> <option value="">---Please select---</option> <!-- not selected / blank option -->
</div> <option value="GET">GET</option>
<option value="PUT">PUT</option>
<option value="POST">POST</option>
</select>
</div>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<div class="row"> <div class="row">
<label class="col-xs-12 col-sm-2 control-label" for="device-content-type">Name <label class="col-xs-12 col-sm-2 control-label" for="device-content-type">Content Type
</label> </label>
<div class="col-xs-8 col-sm-7"> <div class="col-xs-8 col-sm-7">
<input type="text" class="form-control" id="device-content-type" <select name="device-content-type" id="device-content-type" ng-model="device.contentType">
ng-model="device.contentType" placeholder="Content type, i.e. application/json"> <option value="">---Please select---</option> <!-- not selected / blank option -->
<option value="application/atom+xml">application/atom+xml</option>
<option value="application/x-www-form-urlencoded">application/x-www-form-urlencoded</option>
<option value="application/json">application/json</option>
<option value="application/octet-stream">application/octet-stream</option>
<option value="application/svg+xml">application/svg+xml</option>
<option value="application/xhtml+xml">application/xhtml+xml</option>
<option value="application/xml">application/xml</option>
<option value="*">*</option>
<option value="multipart/form-data">multipart/form-data</option>
<option value="text/html">text/html</option>
<option value="text/plain">text/plain</option>
<option value="text/xml">text/xml</option>
<option value="*/*">*/*</option>
</select>
</div> </div>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<div class="row"> <div class="row">
<label class="col-xs-12 col-sm-2 control-label" <label class="col-xs-12 col-sm-2 control-label"
for="device-content-body">Content Body </label> for="device-content-body">Content Body On</label>
<div class="col-xs-8 col-sm-7"> <div class="col-xs-8 col-sm-7">
<textarea rows="3" class="form-control" id="device-content-body" <textarea rows="3" class="form-control" id="device-content-body"
ng-model="device.contentBody" placeholder="Content Body for specific GET/PUT/POST type"></textarea> ng-model="device.contentBody" placeholder="Content Body On for specific GET/PUT/POST type"></textarea>
</div>
<div class="clearfix visible-xs"></div>
</div>
</div>
<div class="form-group">
<div class="row">
<label class="col-xs-12 col-sm-2 control-label"
for="device-content-body-off">Content Body Off</label>
<div class="col-xs-8 col-sm-7">
<textarea rows="3" class="form-control" id="device-content-body-off"
ng-model="device.contentBodyOff" placeholder="Content Body Off for specific GET/PUT/POST type"></textarea>
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>
</div> </div>

View File

@@ -80,7 +80,7 @@
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>
<button class="col-xs-4 col-sm-2 btn btn-success" type="button" <button class="col-xs-4 col-sm-2 btn btn-success" type="button"
ng-click="testUrl(device.onUrl)">Test</button> ng-click="testUrl(device, 'on')">Test</button>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
@@ -94,7 +94,7 @@
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>
<button class="col-xs-4 col-sm-2 btn btn-success" type="button" <button class="col-xs-4 col-sm-2 btn btn-success" type="button"
ng-click="testUrl(device.offUrl)">Test</button> ng-click="testUrl(device, 'off')">Test</button>
</div> </div>
</div> </div>
</form> </form>

View File

@@ -75,7 +75,7 @@
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>
<button class="col-xs-4 col-sm-2 btn btn-success" type="button" <button class="col-xs-4 col-sm-2 btn btn-success" type="button"
ng-click="testUrl(device.onUrl)">Test</button> ng-click="testUrl(device, 'on')">Test</button>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
@@ -89,7 +89,7 @@
</div> </div>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>
<button class="col-xs-4 col-sm-2 btn btn-success" type="button" <button class="col-xs-4 col-sm-2 btn btn-success" type="button"
ng-click="testUrl(device.offUrl)">Test</button> ng-click="testUrl(device, 'off')">Test</button>
</div> </div>
</div> </div>
</form> </form>