2016-01-12 17:49:13 +01:00
|
|
|
var app = angular.module('autome', ['ui.router']);
|
|
|
|
|
|
|
|
app.config(['$stateProvider', '$urlRouterProvider',function($stateProvider, $urlRouterProvider) {
|
|
|
|
|
|
|
|
$stateProvider
|
|
|
|
.state('home', {
|
|
|
|
url: '/home',
|
|
|
|
templateUrl: '/home.html',
|
|
|
|
controller: 'MainCtrl',
|
2016-02-23 17:48:14 +01:00
|
|
|
onEnter: ['$state', 'auth', function ($state, auth) {
|
|
|
|
if (!auth.isLoggedIn()) {
|
|
|
|
$state.go('login');
|
|
|
|
}
|
2016-02-23 22:57:11 +01:00
|
|
|
}],
|
|
|
|
resolve: {
|
|
|
|
postPromise: ['arduino', function(arduino) {
|
|
|
|
return arduino.getRCs();
|
|
|
|
}]
|
|
|
|
}
|
2016-01-12 17:49:13 +01:00
|
|
|
|
2016-02-23 17:48:14 +01:00
|
|
|
})
|
|
|
|
.state('login', {
|
|
|
|
url: '/login',
|
|
|
|
templateUrl: '/login.html',
|
|
|
|
controller: 'AuthCtrl',
|
|
|
|
onEnter: ['$state', 'auth', function ($state, auth) {
|
|
|
|
if (auth.isLoggedIn()) {
|
|
|
|
$state.go('home');
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.state('register', {
|
|
|
|
url: '/register',
|
|
|
|
templateUrl: '/register.html',
|
|
|
|
controller: 'AuthCtrl',
|
|
|
|
onEnter: ['$state', 'auth', function ($state, auth) {
|
|
|
|
if (!auth.isLoggedIn()) {
|
|
|
|
$state.go('home');
|
|
|
|
}
|
|
|
|
}]
|
2016-01-12 17:49:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$urlRouterProvider.otherwise('home');
|
|
|
|
|
|
|
|
}]);
|
2016-02-23 17:48:14 +01:00
|
|
|
app.factory('auth', ['$http', '$window', function ($http, $window) {
|
|
|
|
var auth = {};
|
|
|
|
|
|
|
|
auth.saveToken = function (token) {
|
|
|
|
$window.localStorage['autohome-token'] = token;
|
|
|
|
};
|
|
|
|
|
|
|
|
auth.getToken = function () {
|
|
|
|
return $window.localStorage['autohome-token'];
|
|
|
|
};
|
|
|
|
|
|
|
|
auth.isLoggedIn = function () {
|
|
|
|
var token = auth.getToken();
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
var payload = JSON.parse($window.atob(token.split('.')[1]));
|
|
|
|
|
|
|
|
return payload.exp > Date.now() / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
auth.currentUser = function () {
|
|
|
|
if (auth.isLoggedIn()) {
|
|
|
|
var token = auth.getToken();
|
|
|
|
var payload = JSON.parse($window.atob(token.split('.')[1]));
|
|
|
|
|
|
|
|
return payload.username;
|
|
|
|
}
|
|
|
|
};
|
2016-01-12 17:49:13 +01:00
|
|
|
|
2016-02-23 17:48:14 +01:00
|
|
|
auth.register = function (user) {
|
|
|
|
return $http.post('/register', user, {
|
|
|
|
headers: {Authorization: 'Bearer '+auth.getToken()}
|
|
|
|
}).success(function (data) {
|
|
|
|
auth.saveToken(data.token);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
auth.logIn = function (user) {
|
|
|
|
return $http.post('/login', user).success(function (data) {
|
|
|
|
auth.saveToken(data.token);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
auth.logOut = function () {
|
|
|
|
$window.localStorage.removeItem('autohome-token');
|
|
|
|
};
|
|
|
|
|
|
|
|
return auth;
|
|
|
|
}]);
|
|
|
|
|
|
|
|
app.factory('arduino', ['$http', 'auth', function($http, auth){
|
2016-02-25 19:41:28 +01:00
|
|
|
var o = {
|
|
|
|
RCs: []
|
|
|
|
};
|
2016-01-12 17:49:13 +01:00
|
|
|
|
2016-02-27 21:11:49 +01:00
|
|
|
o.emptyrc = {
|
|
|
|
name: '',
|
|
|
|
on_value: '',
|
|
|
|
off_value:'',
|
|
|
|
tristate: true
|
|
|
|
};
|
|
|
|
|
2016-02-25 19:41:28 +01:00
|
|
|
o.getRCs = function () {
|
|
|
|
return $http.get('/getRCs',{
|
|
|
|
headers: {Authorization: 'Bearer '+auth.getToken()}
|
|
|
|
}).success(function(data) {
|
|
|
|
angular.copy(data, o.RCs);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
o.sendRC = function (RC) {
|
|
|
|
var code = (RC.state) ? RC.off_value : RC.on_value;
|
|
|
|
return $http.post('/sendRC', {code}, {
|
|
|
|
headers: {Authorization: 'Bearer '+auth.getToken()}
|
|
|
|
}).success(function (data) {
|
2016-02-25 21:03:48 +01:00
|
|
|
RC.state = !RC.state;
|
2016-02-25 19:41:28 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
o.addRC = function (RC) {
|
|
|
|
return $http.post('/addRC', RC, {
|
|
|
|
headers: {Authorization: 'Bearer '+auth.getToken()}
|
|
|
|
}).success(function (data) {
|
|
|
|
console.log(data);
|
2016-02-28 20:47:25 +01:00
|
|
|
RC.switch_id = data.insertId;
|
2016-02-25 19:41:28 +01:00
|
|
|
o.RCs.push(RC);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
o.deleteRC = function (RC) {
|
2016-02-28 20:47:25 +01:00
|
|
|
var id = RC.switch_id;
|
2016-02-25 19:41:28 +01:00
|
|
|
return $http.post('/deleteRC', {id}, {
|
|
|
|
headers: {Authorization: 'Bearer '+auth.getToken()}
|
|
|
|
}).success(function (data) {
|
2016-02-28 20:47:25 +01:00
|
|
|
o.RCs.splice(o.RCs.indexOf(o.RCs.filter(function (obj) {
|
|
|
|
return obj.switch_id == id;
|
|
|
|
})), 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
o.changeRC = function (RC) {
|
|
|
|
return $http.post('/changeRC', RC, {
|
|
|
|
headers: {Authorization: 'Bearer '+auth.getToken()}
|
|
|
|
}).success(function (data) {
|
|
|
|
o.RCs[o.RCs.indexOf(o.RCs.filter(function (obj) {
|
|
|
|
return obj.switch_id == RC.switch_id;
|
|
|
|
}))] = RC;
|
2016-02-25 19:41:28 +01:00
|
|
|
});
|
|
|
|
}
|
2016-02-18 17:41:33 +01:00
|
|
|
|
2016-02-25 19:41:28 +01:00
|
|
|
o.irTest = function () {
|
|
|
|
return $http.get('/irTest',{
|
|
|
|
headers: {Authorization: 'Bearer '+auth.getToken()}
|
|
|
|
});
|
|
|
|
}
|
2016-01-21 22:01:57 +01:00
|
|
|
|
2016-02-25 19:41:28 +01:00
|
|
|
return o;
|
2016-01-12 17:49:13 +01:00
|
|
|
}]);
|
|
|
|
|
2016-02-23 17:48:14 +01:00
|
|
|
app.controller('MainCtrl', [
|
|
|
|
'$scope',
|
|
|
|
'arduino',
|
|
|
|
function ($scope, arduino){
|
2016-02-27 21:11:49 +01:00
|
|
|
|
|
|
|
$scope.RCs = arduino.RCs;
|
2016-02-25 19:41:28 +01:00
|
|
|
$scope.sendRC = arduino.sendRC;
|
|
|
|
$scope.irTest = arduino.irTest;
|
2016-02-27 21:11:49 +01:00
|
|
|
$scope.newrc = angular.copy(arduino.emptyrc);
|
2016-02-18 15:44:58 +01:00
|
|
|
|
2016-02-27 23:14:35 +01:00
|
|
|
$scope.modal = {
|
|
|
|
shown: false,
|
|
|
|
delete: false
|
2016-01-21 22:01:57 +01:00
|
|
|
};
|
|
|
|
|
2016-02-27 23:14:35 +01:00
|
|
|
$scope.toggleModal = function(RC) {
|
|
|
|
if(RC) {
|
|
|
|
$scope.newrc = RC;
|
|
|
|
$scope.newrc.tristate = RC.tristate > 0;
|
|
|
|
$scope.modal.delete = true;
|
|
|
|
} else {
|
|
|
|
$scope.modal.delete = false;
|
|
|
|
$scope.newrc = angular.copy(arduino.emptyrc);
|
|
|
|
}
|
|
|
|
|
2016-02-25 21:03:48 +01:00
|
|
|
$scope.modal.shown = !$scope.modal.shown;
|
|
|
|
};
|
|
|
|
|
2016-02-27 21:11:49 +01:00
|
|
|
$scope.addRC = function(){
|
2016-02-28 20:47:25 +01:00
|
|
|
arduino.addRC($scope.newrc);
|
2016-02-25 21:03:48 +01:00
|
|
|
$scope.modal.shown = !$scope.modal.shown;
|
2016-02-27 21:11:49 +01:00
|
|
|
$scope.newrc = angular.copy(arduino.emptyrc);
|
|
|
|
}
|
|
|
|
|
2016-02-28 20:47:25 +01:00
|
|
|
$scope.deleteRC = function() {
|
|
|
|
arduino.deleteRC($scope.newrc);
|
|
|
|
$scope.modal.shown = !$scope.modal.shown;
|
|
|
|
$scope.newrc = angular.copy(arduino.emptyrc);
|
|
|
|
}
|
2016-02-27 23:14:35 +01:00
|
|
|
|
2016-02-28 20:47:25 +01:00
|
|
|
$scope.changeRC = function() {
|
|
|
|
arduino.changeRC($scope.newrc);
|
|
|
|
$scope.modal.shown = !$scope.modal.shown;
|
|
|
|
$scope.newrc = angular.copy(arduino.emptyrc);
|
2016-02-25 21:03:48 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 17:48:14 +01:00
|
|
|
}]);
|
|
|
|
|
|
|
|
app.controller('AuthCtrl', [
|
|
|
|
'$scope',
|
|
|
|
'$state',
|
|
|
|
'auth',
|
|
|
|
function($scope, $state, auth){
|
|
|
|
$scope.user = {};
|
|
|
|
|
|
|
|
$scope.register = function(){
|
|
|
|
auth.register($scope.user).error(function(error){
|
|
|
|
$scope.error = error;
|
|
|
|
}).then(function(){
|
|
|
|
$state.go('home');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.logIn = function(){
|
|
|
|
auth.logIn($scope.user).error(function(error){
|
|
|
|
$scope.error = error;
|
|
|
|
}).then(function(){
|
|
|
|
$state.go('home');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}]);
|
|
|
|
|
|
|
|
app.controller('NavCtrl', [
|
|
|
|
'$scope',
|
|
|
|
'auth',
|
|
|
|
function($scope, auth){
|
|
|
|
$scope.isLoggedIn = auth.isLoggedIn;
|
|
|
|
$scope.currentUser = auth.currentUser;
|
|
|
|
$scope.logOut = auth.logOut;
|
2016-02-27 21:11:49 +01:00
|
|
|
}]);
|
|
|
|
|
|
|
|
app.directive('modal', function() {
|
|
|
|
return {
|
|
|
|
restrict: 'E',
|
|
|
|
scope: {
|
|
|
|
show: '='
|
|
|
|
},
|
|
|
|
replace: true,
|
|
|
|
transclude: true,
|
|
|
|
link: function($scope, element, attrs) {
|
|
|
|
$scope.dialogStyle = {};
|
|
|
|
if (attrs.width)
|
|
|
|
$scope.dialogStyle.width = attrs.width;
|
|
|
|
if (attrs.height)
|
|
|
|
$scope.dialogStyle.height = attrs.height;
|
|
|
|
$scope.hideModal = function() {
|
|
|
|
$scope.show = false;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
templateUrl: '/modal.html'
|
|
|
|
};
|
|
|
|
});
|