Updating how controllers work together with shared data.

This commit is contained in:
Admin
2015-08-14 16:19:51 -05:00
parent 4a80ce4230
commit b9076d1f2e
6 changed files with 74 additions and 44 deletions

View File

@@ -15,15 +15,46 @@ app.config(function ($routeProvider) {
})
});
app.factory('BridgeSettings'), function() {
return {
name : 'anonymous'
};
app.run( function (bridgeService) {
bridgeService.viewBridgeSettings();
});
app.service('bridgeService', function ($http) {
app.factory('BridgeSettings', function() {
var BridgeSettings = {};
BridgeSettings.upnpconfigaddress = "";
BridgeSettings.serverport = "";
BridgeSettings.upnpdevicedb = "";
BridgeSettings.upnpresponseport = "";
BridgeSettings.veraaddress = "";
BridgeSettings.setupnpconfigaddress = function(aconfigaddress){
BridgeSettings.upnpconfigaddress = aconfigaddress;
};
BridgeSettings.setserverport = function(aserverport){
BridgeSettings.serverport = aserverport;
};
BridgeSettings.setupnpdevicedb = function(aupnpdevicedb){
BridgeSettings.upnpdevicedb = aupnpdevicedb;
};
BridgeSettings.setupnpresponseport = function(aupnpresponseport){
BridgeSettings.upnpresponseport = aupnpresponseport;
};
BridgeSettings.setveraaddress = function(averaaddress){
BridgeSettings.veraaddress = averaaddress;
};
return BridgeSettings;
});
app.service('bridgeService', function ($http, BridgeSettings) {
var self = this;
this.state = {base: window.location.origin + "/api/devices", upnpbase: window.location.origin + "/upnp/settings", devices: [], error: ""};
self.BridgeSettings = BridgeSettings;
this.state = {base: window.location.origin + "/api/devices", upnpbase: window.location.origin + "/upnp/settings", devices: [], device: [], error: ""};
this.viewDevices = function () {
this.state.error = "";
@@ -47,11 +78,11 @@ app.service('bridgeService', function ($http) {
this.state.error = "";
return $http.get(this.state.upnpbase).then(
function (response) {
self.state.upnpconfigaddress = response.data.upnpconfigaddress;
self.state.serverport = response.data.serverport;
self.state.upnpdevicedb = response.data.upnpdevicedb;
self.state.upnpresponseport = response.data.upnpresponseport;
self.state.veraaddress = response.data.veraaddress;
self.BridgeSettings.setupnpconfigaddress(response.data.upnpconfigaddress);
self.BridgeSettings.setserverport(response.data.serverport);
self.BridgeSettings.setupnpdevicedb(response.data.upnpdevicedb);
self.BridgeSettings.setupnpresponseport(response.data.upnpresponseport);
self.BridgeSettings.setveraaddress(response.data.veraaddress);
},
function (error) {
if (error.data) {
@@ -158,18 +189,14 @@ app.service('bridgeService', function ($http) {
};
this.editDevice = function (id, name, onUrl, offUrl) {
this.device.id = id;
this.device.name = name;
this.device.onUrl = onUrl;
this.device.offUrl = offUrl;
self.state.device = {id: id, name: name, onUrl: onUrl, offUrl: offUrl};
};
});
app.controller('ViewingController', function ($scope, bridgeService) {
app.controller('ViewingController', function ($scope, $location, bridgeService, BridgeSettings) {
$scope.BridgeSettings = bridgeService.BridgeSettings;
bridgeService.viewDevices();
bridgeService.viewBridgeSettings();
bridgeService.viewVeraDevices();
bridgeService.viewVeraScenes();
$scope.bridge = bridgeService.state;
$scope.deleteDevice = function (device) {
bridgeService.deleteDevice(device.id);
@@ -183,14 +210,21 @@ app.controller('ViewingController', function ($scope, bridgeService) {
};
$scope.editDevice = function (device) {
bridgeService.editDevice(device.id, device.name, device.onUrl, device.offUrl);
$location.path('/editor');
};
});
app.controller('AddingController', function ($scope, bridgeService) {
app.controller('AddingController', function ($scope, bridgeService, BridgeSettings) {
$scope.device = {id: "", name: "", type: "switch", onUrl: "", offUrl: ""};
$scope.vera = {base: "", port: "3480", id: ""};
$scope.vera.base = "http://" + BridgeSettings.veraaddress;
bridgeService.device = $scope.device;
bridgeService.viewVeraDevices();
bridgeService.viewVeraScenes();
$scope.bridge = bridgeService.state;
$scope.device = bridgeService.state.device;
$scope.buildUrls = function () {
if ($scope.vera.base.indexOf("http") < 0) {
$scope.vera.base = "http://" + $scope.vera.base;
@@ -203,9 +237,9 @@ app.controller('AddingController', function ($scope, bridgeService) {
+ $scope.vera.id;
};
$scope.buildDeviceUrls = function (veradevice, veraaddr) {
$scope.buildDeviceUrls = function (veradevice) {
if ($scope.vera.base.indexOf("http") < 0) {
$scope.vera.base = "http://" + veraaddr;
$scope.vera.base = "http://" + $scope.vera.base;
}
$scope.device.name = veradevice.name;
$scope.device.onUrl = $scope.vera.base + ":" + $scope.vera.port
@@ -216,9 +250,9 @@ app.controller('AddingController', function ($scope, bridgeService) {
+ veradevice.id;
};
$scope.buildSceneUrls = function (verascene, veraaddr) {
$scope.buildSceneUrls = function (verascene) {
if ($scope.vera.base.indexOf("http") < 0) {
$scope.vera.base = "http://" + veraaddr;
$scope.vera.base = "http://" + $scope.vera.base;
}
$scope.device.name = verascene.name;
$scope.device.onUrl = $scope.vera.base + ":" + $scope.vera.port
@@ -229,10 +263,6 @@ app.controller('AddingController', function ($scope, bridgeService) {
+ verascene.id;
};
$scope.setVeraAddress = function (anAddress) {
$scope.vera.base = "http://" + anAddress;
};
$scope.testUrl = function (url) {
window.open(url, "_blank");
};