finish wiring up change, delete and add routes

This commit is contained in:
Stefan Sterz 2016-02-28 20:47:25 +01:00
parent f2d9923504
commit 435f85ec3b
4 changed files with 42 additions and 8 deletions

View File

@ -31,7 +31,13 @@ RC.prototype.delete = function(id, callback) {
RC.prototype.add = function(rc, callback) { RC.prototype.add = function(rc, callback) {
// var rc = {tristate:bool, on_value:"FF00...", off_value:"FF00...", name:"Switch name"}; // var rc = {tristate:bool, on_value:"FF00...", off_value:"FF00...", name:"Switch name"};
var query = connection.query('INSERT INTO rc_switches SET ?', rc, callback); connection.query('INSERT INTO rc_switches SET ?', rc, callback);
}
RC.prototype.change = function(rc, callback) {
// var rc = {tristate:bool, on_value:"FF00...", off_value:"FF00...", name:"Switch name"};
console.log(rc);
connection.query('UPDATE rc_switches SET ? WHERE `switch_id` = ?', [rc, rc.switch_id], callback);
} }
module.exports = RC; module.exports = RC;

View File

@ -130,17 +130,29 @@ app.factory('arduino', ['$http', 'auth', function($http, auth){
headers: {Authorization: 'Bearer '+auth.getToken()} headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function (data) { }).success(function (data) {
console.log(data); console.log(data);
RC.id = data.insertId; RC.switch_id = data.insertId;
o.RCs.push(RC); o.RCs.push(RC);
}); });
} }
o.deleteRC = function (RC) { o.deleteRC = function (RC) {
var id = RC.id; var id = RC.switch_id;
return $http.post('/deleteRC', {id}, { return $http.post('/deleteRC', {id}, {
headers: {Authorization: 'Bearer '+auth.getToken()} headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function (data) { }).success(function (data) {
o.RCs.splice(o.RCs.indexOf(RC), 1); 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;
}); });
} }
@ -182,13 +194,21 @@ app.controller('MainCtrl', [
}; };
$scope.addRC = function(){ $scope.addRC = function(){
console.log($scope.newrc); arduino.addRC($scope.newrc);
$scope.modal.shown = !$scope.modal.shown; $scope.modal.shown = !$scope.modal.shown;
$scope.newrc = angular.copy(arduino.emptyrc); $scope.newrc = angular.copy(arduino.emptyrc);
} }
$scope.deleteRC = function(RC) { $scope.deleteRC = function() {
arduino.deleteRC($scope.newrc);
$scope.modal.shown = !$scope.modal.shown;
$scope.newrc = angular.copy(arduino.emptyrc);
}
$scope.changeRC = function() {
arduino.changeRC($scope.newrc);
$scope.modal.shown = !$scope.modal.shown;
$scope.newrc = angular.copy(arduino.emptyrc);
} }
}]); }]);

View File

@ -37,7 +37,7 @@ router.post('/sendRC', auth, function (req, res, next){
}); });
router.post('/addRC', auth, function (req, res, next){ router.post('/addRC', auth, function (req, res, next){
rcdb.add(req.body.rc, function (error, result) { rcdb.add(req.body, function (error, result) {
if(error) {console.log(error); return(next);} if(error) {console.log(error); return(next);}
res.json(result); res.json(result);
}); });
@ -50,6 +50,13 @@ router.post('/deleteRC', auth, function (req, res, next){
}); });
}); });
router.post('/changeRC', auth, function (req, res, next){
rcdb.change(req.body, function (error, result) {
if(error) {console.log(error); return(next);}
res.json(result);
});
});
// IR Routes // IR Routes
router.get('/irTest', auth, function (req, res) { router.get('/irTest', auth, function (req, res) {
arduino.write(new Buffer('X0' + String.fromCharCode(4) + String.fromCharCode(12) + String.fromCharCode(0)+String.fromCharCode(0)+String.fromCharCode(2689 / 256) + String.fromCharCode(2689 % 256),'ascii')); arduino.write(new Buffer('X0' + String.fromCharCode(4) + String.fromCharCode(12) + String.fromCharCode(0)+String.fromCharCode(0)+String.fromCharCode(2689 / 256) + String.fromCharCode(2689 % 256),'ascii'));

View File

@ -47,7 +47,8 @@
</div> </div>
</div> </div>
<label>Is this a tristate switch? <input ng-model="newrc.tristate" type="checkbox" ></input></label> <label>Is this a tristate switch? <input ng-model="newrc.tristate" type="checkbox" ></input></label>
<button type="submit" class="button-primary">Add</button> <button type="submit" ng-hide="modal.delete" class="button-primary">Add</button>
<button type="button" ng-click="changeRC()" ng-show="modal.delete" class="button-primary">Change</button>
<button type="button" ng-click="deleteRC()" ng-show="modal.delete" class="button-primary red">Delete</button> <button type="button" ng-click="deleteRC()" ng-show="modal.delete" class="button-primary red">Delete</button>
<button type="button" ng-click="toggleModal()" class="button">Cancel</button> <button type="button" ng-click="toggleModal()" class="button">Cancel</button>
</form> </form>