Skip to content

Commit

Permalink
Merge pull request #13 from duanemay/master
Browse files Browse the repository at this point in the history
Improvements to the Configurator
  • Loading branch information
armzilla committed Jul 13, 2015
2 parents 57ed40e + 31453c4 commit 27f75be
Show file tree
Hide file tree
Showing 5 changed files with 358 additions and 170 deletions.
35 changes: 24 additions & 11 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,27 @@
emulates philips hue api to other home automation gateways. The Amazon echo now supports wemo and philip hue... great news if you own any of those devices!
My house is pretty heavily invested in the z-wave using the Vera as the gateway and thought it would be nice bridge the Amazon Echo to it.

Register a device, bind to some sort of on/off (vera style) url

To run using maven
```
mvn spring-boot:run
```

somewhat hacked together for now, please excuse the hard coded values

or build using maven, grab the jar, run like this:
```
mvn install
java -jar target/amazon-echo-bridge-0.1.0.jar --upnp.config.address=192.168.1.240
```
replace the --upnp.config.address value with the server ipv4 address.

Then configure by going to the /configurator.html url
```
http://192.168.1.240:8080/configurator.html
```

or Register a device, via REST by binding some sort of on/off (vera style) url
```
POST http://host:8080/api/devices
{
Expand All @@ -16,15 +36,8 @@ POST http://host:8080/api/devices
}
```

To run using maven
```
mvn spring-boot:run
```
After this Tell Alexa: "Alexa, discover my devices"

somewhat hacked together for now, please excuse the hard coded values
Then you can say "Alexa, Turn on the office light" or whatever name you have given your configured devices.

grab the jar, run like this:
```
java -jar amazon-echo-bridge-0.1.0.jar --upnp.config.address=192.168.1.240
```
replace the --upnp.config.address value with the server ipv4 address.
To view or remove devices that Alexa knows about, you can use the mobile app Menu / Settings / Connected Home
17 changes: 17 additions & 0 deletions src/main/java/com/armzilla/ha/devicemanagmeent/DeviceResource.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ public ResponseEntity<DeviceDescriptor> createDevice(@RequestBody Device device)
return new ResponseEntity<>(deviceEntry, null, HttpStatus.CREATED);
}

@RequestMapping(value = "/{lightId}", method = RequestMethod.PUT, produces = "application/json", headers = "Accept=application/json")
public ResponseEntity<DeviceDescriptor> updateDevice(@PathVariable("lightId") String id, @RequestBody Device device) {
DeviceDescriptor deviceEntry = deviceRepository.findOne(id);
if(deviceEntry == null){
return new ResponseEntity<>(null, null, HttpStatus.NOT_FOUND);
}

deviceEntry.setName(device.getName());
deviceEntry.setDeviceType(device.getDeviceType());
deviceEntry.setOnUrl(device.getOnUrl());
deviceEntry.setOffUrl(device.getOffUrl());

deviceRepository.save(deviceEntry);

return new ResponseEntity<>(deviceEntry, null, HttpStatus.OK);
}

@RequestMapping(method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<List<DeviceDescriptor>> findAllDevices() {
List<DeviceDescriptor> deviceList = deviceRepository.findAll();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/armzilla/ha/filters/SpringBootCorsFilter.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public class SpringBootCorsFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
chain.doFilter(req, res);
}

Expand Down
145 changes: 145 additions & 0 deletions src/main/resources/static/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
angular.module('configurator', [])
.service('bridgeService', ["$http", function ($http) {
var self = this;
this.state = {base: window.location.origin + "/api/devices", devices: [], error: ""};

this.viewDevices = function () {
this.state.error = "";
return $http.get(this.state.base).then(
function (response) {
self.state.devices = response.data[0].content;
},
function (error) {
if (error.data) {
self.state.error = error.data.message;
} else {
self.state.error = "If you're not seeing any devices, you may be running into problems with CORS. " +
"You can work around this by running a fresh launch of Chrome with the --disable-web-security flag.";
}
console.log(error);
}
);
};

this.addDevice = function (id, name, type, onUrl, offUrl) {
this.state.error = "";
if (id) {
var putUrl = this.state.base + "/" + id;
return $http.put(putUrl, {
id: id,
name: name,
deviceType: type,
onUrl: onUrl,
offUrl: offUrl
}).then(
function (response) {
self.viewDevices();
},
function (error) {
if (error.data) {
self.state.error = error.data.message;
}
console.log(error);
}
);
} else {
return $http.post(this.state.base, {
name: name,
deviceType: type,
onUrl: onUrl,
offUrl: offUrl
}).then(
function (response) {
self.viewDevices();
},
function (error) {
if (error.data) {
self.state.error = error.data.message;
}
console.log(error);
}
);
}
};

this.deleteDevice = function (id) {
this.state.error = "";
return $http.delete(this.state.base + "/" + id).then(
function (response) {
self.viewDevices();
},
function (error) {
if (error.data) {
self.state.error = error.data.message;
}
console.log(error);
}
);
};

this.editDevice = function (id, name, type, onUrl, offUrl) {
this.device.id = id;
this.device.name = name;
this.device.onUrl = onUrl;
this.device.offUrl = offUrl;
};
}])

.controller('ViewingController', ["$scope", "bridgeService", function ($scope, bridgeService) {
bridgeService.viewDevices();
$scope.bridge = bridgeService.state;
$scope.deleteDevice = function (device) {
bridgeService.deleteDevice(device.id);
};
$scope.testUrl = function (url) {
window.open(url, "_blank");
};
$scope.setBridgeUrl = function (url) {
bridgeService.state.base = url;
bridgeService.viewDevices();
};
$scope.editDevice = function (device) {
bridgeService.editDevice(device.id, device.name, device.type, device.onUrl, device.offUrl);
};
}])

.controller('AddingController', ["$scope", "bridgeService", function ($scope, bridgeService) {

$scope.bridge = bridgeService.state;
$scope.device = {id: "", name: "", type: "switch", onUrl: "", offUrl: ""};
$scope.vera = {base: "", port: "3480", id: ""};
bridgeService.device = $scope.device;

$scope.buildUrls = function () {
if ($scope.vera.base.indexOf("http") < 0) {
$scope.vera.base = "http://" + $scope.vera.base;
}
$scope.device.onUrl = $scope.vera.base + ":" + $scope.vera.port
+ "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum="
+ $scope.vera.id;
$scope.device.offUrl = $scope.vera.base + ":" + $scope.vera.port
+ "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum="
+ $scope.vera.id;
};

$scope.testUrl = function (url) {
window.open(url, "_blank");
};

$scope.addDevice = function () {
bridgeService.addDevice($scope.device.id, $scope.device.name, $scope.device.type, $scope.device.onUrl, $scope.device.offUrl).then(
function () {
$scope.device.id = "";
$scope.device.name = "";
$scope.device.onUrl = "";
$scope.device.offUrl = "";
},
function (error) {
}
);
}
}])

.controller('ErrorsController', ["$scope", "bridgeService", function ($scope, bridgeService) {
$scope.bridge = bridgeService.state;
}]);
Loading

0 comments on commit 27f75be

Please sign in to comment.