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>
<artifactId>ha-bridge</artifactId>
<version>0.4.5</version>
<version>0.4.6</version>
<packaging>jar</packaging>
<name>HA Bridge</name>

View File

@@ -11,6 +11,7 @@ public class DeviceDescriptor{
private String httpVerb;
private String contentType;
private String contentBody;
private String contentBodyOff;
public String getName() {
return name;
@@ -76,5 +77,13 @@ public class DeviceDescriptor{
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")) {
deviceEntry.setContentBody(reader.nextString());
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 {
reader.skipValue();
}

View File

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

View File

@@ -204,8 +204,12 @@ public class HueMulator {
}
//quick template
String body;
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
if(!doHttpRequest(url, device.getHttpVerb(), device.getContentType(), body)){
response.status(HttpStatus.SC_SERVICE_UNAVAILABLE);

View File

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

View File

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

View File

@@ -35,7 +35,7 @@
</div>
<div class="clearfix visible-xs"></div>
<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 class="form-group">
@@ -49,7 +49,7 @@
</div>
<div class="clearfix visible-xs"></div>
<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 class="form-group">
@@ -58,30 +58,60 @@
</label>
<div class="col-xs-8 col-sm-7">
<input type="text" class="form-control" id="device-http-verb"
ng-model="device.httpVerb" placeholder="Http Verb, i.e. GET/PUT/POST">
<select name="device-http-verb" id="device-http-verb" ng-model="device.httpVerb">
<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 class="form-group">
<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>
<div class="col-xs-8 col-sm-7">
<input type="text" class="form-control" id="device-content-type"
ng-model="device.contentType" placeholder="Content type, i.e. application/json">
<select name="device-content-type" id="device-content-type" ng-model="device.contentType">
<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 class="form-group">
<div class="row">
<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">
<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 class="clearfix visible-xs"></div>
</div>

View File

@@ -86,7 +86,7 @@
</div>
<div class="clearfix visible-xs"></div>
<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 class="form-group">
@@ -100,7 +100,7 @@
</div>
<div class="clearfix visible-xs"></div>
<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 class="form-group">
@@ -109,30 +109,60 @@
</label>
<div class="col-xs-8 col-sm-7">
<input type="text" class="form-control" id="device-http-verb"
ng-model="device.httpVerb" placeholder="Http Verb, i.e. GET/PUT/POST">
</div>
<select name="device-http-verb" id="device-http-verb" ng-model="device.httpVerb">
<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 class="form-group">
<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>
<div class="col-xs-8 col-sm-7">
<input type="text" class="form-control" id="device-content-type"
ng-model="device.contentType" placeholder="Content type, i.e. application/json">
<select name="device-content-type" id="device-content-type" ng-model="device.contentType">
<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 class="form-group">
<div class="row">
<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">
<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 class="clearfix visible-xs"></div>
</div>

View File

@@ -80,7 +80,7 @@
</div>
<div class="clearfix visible-xs"></div>
<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 class="form-group">
@@ -94,7 +94,7 @@
</div>
<div class="clearfix visible-xs"></div>
<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>
</form>

View File

@@ -75,7 +75,7 @@
</div>
<div class="clearfix visible-xs"></div>
<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 class="form-group">
@@ -89,7 +89,7 @@
</div>
<div class="clearfix visible-xs"></div>
<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>
</form>