-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from duanemay/master
Improvements to the Configurator
- Loading branch information
Showing
5 changed files
with
358 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}]); |
Oops, something went wrong.