finish wiring up change, delete and add routes
This commit is contained in:
parent
f2d9923504
commit
435f85ec3b
4 changed files with 42 additions and 8 deletions
|
@ -31,7 +31,13 @@ RC.prototype.delete = function(id, callback) {
|
|||
|
||||
RC.prototype.add = function(rc, callback) {
|
||||
// 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;
|
30
nodejs/public/javascripts/angularApp.js
vendored
30
nodejs/public/javascripts/angularApp.js
vendored
|
@ -130,17 +130,29 @@ app.factory('arduino', ['$http', 'auth', function($http, auth){
|
|||
headers: {Authorization: 'Bearer '+auth.getToken()}
|
||||
}).success(function (data) {
|
||||
console.log(data);
|
||||
RC.id = data.insertId;
|
||||
RC.switch_id = data.insertId;
|
||||
o.RCs.push(RC);
|
||||
});
|
||||
}
|
||||
|
||||
o.deleteRC = function (RC) {
|
||||
var id = RC.id;
|
||||
var id = RC.switch_id;
|
||||
return $http.post('/deleteRC', {id}, {
|
||||
headers: {Authorization: 'Bearer '+auth.getToken()}
|
||||
}).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(){
|
||||
console.log($scope.newrc);
|
||||
arduino.addRC($scope.newrc);
|
||||
$scope.modal.shown = !$scope.modal.shown;
|
||||
$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);
|
||||
}
|
||||
|
||||
}]);
|
||||
|
|
|
@ -37,7 +37,7 @@ router.post('/sendRC', 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);}
|
||||
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
|
||||
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'));
|
||||
|
|
|
@ -47,7 +47,8 @@
|
|||
</div>
|
||||
</div>
|
||||
<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="toggleModal()" class="button">Cancel</button>
|
||||
</form>
|
||||
|
|
Reference in a new issue