Skip to content

Commit 27f75be

Browse files
committed
Merge pull request #13 from duanemay/master
Improvements to the Configurator
2 parents 57ed40e + 31453c4 commit 27f75be

File tree

5 files changed

+358
-170
lines changed

5 files changed

+358
-170
lines changed

README.md

100644100755
Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,27 @@
55
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!
66
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.
77

8-
Register a device, bind to some sort of on/off (vera style) url
8+
9+
To run using maven
10+
```
11+
mvn spring-boot:run
12+
```
13+
14+
somewhat hacked together for now, please excuse the hard coded values
15+
16+
or build using maven, grab the jar, run like this:
17+
```
18+
mvn install
19+
java -jar target/amazon-echo-bridge-0.1.0.jar --upnp.config.address=192.168.1.240
20+
```
21+
replace the --upnp.config.address value with the server ipv4 address.
22+
23+
Then configure by going to the /configurator.html url
24+
```
25+
http://192.168.1.240:8080/configurator.html
26+
```
27+
28+
or Register a device, via REST by binding some sort of on/off (vera style) url
929
```
1030
POST http://host:8080/api/devices
1131
{
@@ -16,15 +36,8 @@ POST http://host:8080/api/devices
1636
}
1737
```
1838

19-
To run using maven
20-
```
21-
mvn spring-boot:run
22-
```
39+
After this Tell Alexa: "Alexa, discover my devices"
2340

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

26-
grab the jar, run like this:
27-
```
28-
java -jar amazon-echo-bridge-0.1.0.jar --upnp.config.address=192.168.1.240
29-
```
30-
replace the --upnp.config.address value with the server ipv4 address.
43+
To view or remove devices that Alexa knows about, you can use the mobile app Menu / Settings / Connected Home

src/main/java/com/armzilla/ha/devicemanagmeent/DeviceResource.java

100644100755
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ public ResponseEntity<DeviceDescriptor> createDevice(@RequestBody Device device)
4040
return new ResponseEntity<>(deviceEntry, null, HttpStatus.CREATED);
4141
}
4242

43+
@RequestMapping(value = "/{lightId}", method = RequestMethod.PUT, produces = "application/json", headers = "Accept=application/json")
44+
public ResponseEntity<DeviceDescriptor> updateDevice(@PathVariable("lightId") String id, @RequestBody Device device) {
45+
DeviceDescriptor deviceEntry = deviceRepository.findOne(id);
46+
if(deviceEntry == null){
47+
return new ResponseEntity<>(null, null, HttpStatus.NOT_FOUND);
48+
}
49+
50+
deviceEntry.setName(device.getName());
51+
deviceEntry.setDeviceType(device.getDeviceType());
52+
deviceEntry.setOnUrl(device.getOnUrl());
53+
deviceEntry.setOffUrl(device.getOffUrl());
54+
55+
deviceRepository.save(deviceEntry);
56+
57+
return new ResponseEntity<>(deviceEntry, null, HttpStatus.OK);
58+
}
59+
4360
@RequestMapping(method = RequestMethod.GET, produces = "application/json")
4461
public ResponseEntity<List<DeviceDescriptor>> findAllDevices() {
4562
List<DeviceDescriptor> deviceList = deviceRepository.findAll();

src/main/java/com/armzilla/ha/filters/SpringBootCorsFilter.java

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public class SpringBootCorsFilter implements Filter {
2222
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
2323
HttpServletResponse response = (HttpServletResponse) res;
2424
response.setHeader("Access-Control-Allow-Origin", "*");
25-
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
25+
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
2626
response.setHeader("Access-Control-Max-Age", "3600");
27-
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
27+
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
2828
chain.doFilter(req, res);
2929
}
3030

src/main/resources/static/app.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
angular.module('configurator', [])
2+
.service('bridgeService', ["$http", function ($http) {
3+
var self = this;
4+
this.state = {base: window.location.origin + "/api/devices", devices: [], error: ""};
5+
6+
this.viewDevices = function () {
7+
this.state.error = "";
8+
return $http.get(this.state.base).then(
9+
function (response) {
10+
self.state.devices = response.data[0].content;
11+
},
12+
function (error) {
13+
if (error.data) {
14+
self.state.error = error.data.message;
15+
} else {
16+
self.state.error = "If you're not seeing any devices, you may be running into problems with CORS. " +
17+
"You can work around this by running a fresh launch of Chrome with the --disable-web-security flag.";
18+
}
19+
console.log(error);
20+
}
21+
);
22+
};
23+
24+
this.addDevice = function (id, name, type, onUrl, offUrl) {
25+
this.state.error = "";
26+
if (id) {
27+
var putUrl = this.state.base + "/" + id;
28+
return $http.put(putUrl, {
29+
id: id,
30+
name: name,
31+
deviceType: type,
32+
onUrl: onUrl,
33+
offUrl: offUrl
34+
}).then(
35+
function (response) {
36+
self.viewDevices();
37+
},
38+
function (error) {
39+
if (error.data) {
40+
self.state.error = error.data.message;
41+
}
42+
console.log(error);
43+
}
44+
);
45+
} else {
46+
return $http.post(this.state.base, {
47+
name: name,
48+
deviceType: type,
49+
onUrl: onUrl,
50+
offUrl: offUrl
51+
}).then(
52+
function (response) {
53+
self.viewDevices();
54+
},
55+
function (error) {
56+
if (error.data) {
57+
self.state.error = error.data.message;
58+
}
59+
console.log(error);
60+
}
61+
);
62+
}
63+
};
64+
65+
this.deleteDevice = function (id) {
66+
this.state.error = "";
67+
return $http.delete(this.state.base + "/" + id).then(
68+
function (response) {
69+
self.viewDevices();
70+
},
71+
function (error) {
72+
if (error.data) {
73+
self.state.error = error.data.message;
74+
}
75+
console.log(error);
76+
}
77+
);
78+
};
79+
80+
this.editDevice = function (id, name, type, onUrl, offUrl) {
81+
this.device.id = id;
82+
this.device.name = name;
83+
this.device.onUrl = onUrl;
84+
this.device.offUrl = offUrl;
85+
};
86+
}])
87+
88+
.controller('ViewingController', ["$scope", "bridgeService", function ($scope, bridgeService) {
89+
bridgeService.viewDevices();
90+
$scope.bridge = bridgeService.state;
91+
$scope.deleteDevice = function (device) {
92+
bridgeService.deleteDevice(device.id);
93+
};
94+
$scope.testUrl = function (url) {
95+
window.open(url, "_blank");
96+
};
97+
$scope.setBridgeUrl = function (url) {
98+
bridgeService.state.base = url;
99+
bridgeService.viewDevices();
100+
};
101+
$scope.editDevice = function (device) {
102+
bridgeService.editDevice(device.id, device.name, device.type, device.onUrl, device.offUrl);
103+
};
104+
}])
105+
106+
.controller('AddingController', ["$scope", "bridgeService", function ($scope, bridgeService) {
107+
108+
$scope.bridge = bridgeService.state;
109+
$scope.device = {id: "", name: "", type: "switch", onUrl: "", offUrl: ""};
110+
$scope.vera = {base: "", port: "3480", id: ""};
111+
bridgeService.device = $scope.device;
112+
113+
$scope.buildUrls = function () {
114+
if ($scope.vera.base.indexOf("http") < 0) {
115+
$scope.vera.base = "http://" + $scope.vera.base;
116+
}
117+
$scope.device.onUrl = $scope.vera.base + ":" + $scope.vera.port
118+
+ "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum="
119+
+ $scope.vera.id;
120+
$scope.device.offUrl = $scope.vera.base + ":" + $scope.vera.port
121+
+ "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum="
122+
+ $scope.vera.id;
123+
};
124+
125+
$scope.testUrl = function (url) {
126+
window.open(url, "_blank");
127+
};
128+
129+
$scope.addDevice = function () {
130+
bridgeService.addDevice($scope.device.id, $scope.device.name, $scope.device.type, $scope.device.onUrl, $scope.device.offUrl).then(
131+
function () {
132+
$scope.device.id = "";
133+
$scope.device.name = "";
134+
$scope.device.onUrl = "";
135+
$scope.device.offUrl = "";
136+
},
137+
function (error) {
138+
}
139+
);
140+
}
141+
}])
142+
143+
.controller('ErrorsController', ["$scope", "bridgeService", function ($scope, bridgeService) {
144+
$scope.bridge = bridgeService.state;
145+
}]);

0 commit comments

Comments
 (0)