Skip to content

Commit

Permalink
Enable saving room of a lifeevent. Update controller and services
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles committed Nov 29, 2015
1 parent 1fdbb45 commit dbfbc1a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
18 changes: 16 additions & 2 deletions api/controllers/LifeEventController.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,22 @@ module.exports = {
* @return
*/
create : function (req, res, next){
LifeEventService.saveEvent(req.param('eventtype'),req.session.User.id, req.param('param'), function(err){
if(err) return res.json(err);

var event = {
user: req.session.User.id,
eventtype: req.param('eventtype')
};

if(req.param('room')){
event.room = req.param('room');
}

if(req.param('param')){
event.param = req.param('param');
}

LifeEventService.saveEvent(event, function(err){
if(err) return res.status(500).json(err);

return res.json("ok");
});
Expand Down
25 changes: 11 additions & 14 deletions api/services/LifeEventService.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,22 @@ module.exports = {
* @param {} callback
* @return
*/
saveEvent: function(eventType,userId, param,callback){
callback = callback || function() {};
saveEvent: function(event, cb){
cb = cb || function() {};

EventType.findOne({name:eventType}).exec(function(err, eventtype) {
if(err) return callback(err);
EventType.findOne({name: event.eventtype}).exec(function(err, eventtype) {
if(err) return cb(err);

if(eventtype){
eventtype.lifeevents.add({user:userId, param:param});

eventtype.save(function(err) {
if(err) return callback(err);

// fire the event
ScenarioService.launcher(eventType, param, function(){});
event.eventtype = eventtype.id;
LifeEvent.create(event, function(err, lifeevent){
if(err) return cb(err);

callback(null, true);
});
ScenarioService.launcher(eventtype.name, event.param, function (){});
cb(null, lifeevent);
});
}else{
callback('eventType ' + eventType + ' not found');
cb('eventType ' + event.eventtype + ' not found');
}
});
},
Expand Down
17 changes: 15 additions & 2 deletions api/services/MilightService.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,14 @@ module.exports = {
lastLampId = lampId;
if(callback)
callback(false);

var event = {
user: userId,
eventtype: sails.config.lifeevent.milightOn.name,
param: lampId
};

LifeEventService.saveEvent(sails.config.lifeevent.milightOn.name, userId, lampId, function(err){
LifeEventService.saveEvent(event, function(err){
if(err) sails.log.warn(err);
});
});
Expand All @@ -114,8 +120,15 @@ module.exports = {
lastLampId = lampId;
if(callback)
callback(false);

var event = {
user: userId,
eventtype: sails.config.lifeevent.milightOff.name,
param: lampId
};


LifeEventService.saveEvent(sails.config.lifeevent.milightOff.name, userId, lampId, function(err){
LifeEventService.saveEvent(event, function(err){
if(err) sails.log.warn(err);
});
});
Expand Down
14 changes: 12 additions & 2 deletions api/services/UserService.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ module.exports = {
goToSleep: function(userId, callback){
User.update({id:userId}, {isSleeping : true}, function(err, user){
if(err) return callback(err);

var event = {
user: userId,
eventtype: sails.config.lifeevent.goingToSleep.name,
};

// save goingToSleep Event in LifeEvents database
LifeEventService.saveEvent(sails.config.lifeevent.goingToSleep.name,userId, null,function(err){
LifeEventService.saveEvent(event ,function(err){
if(err) return callback(err);

callback(null);
Expand All @@ -45,9 +50,14 @@ module.exports = {
wakeUp: function(userId, callback){
User.update({id:userId}, {isSleeping : false}, function(err, user){
if(err) return callback(err);

var event = {
user: userId,
eventtype: sails.config.lifeevent.wakeUp.name,
};

// save WakeUp Event in LifeEvents database
LifeEventService.saveEvent(sails.config.lifeevent.wakeUp.name,userId, null,function(err){
LifeEventService.saveEvent(event ,function(err){
if(err) return callback(err);

callback(null);
Expand Down

0 comments on commit dbfbc1a

Please sign in to comment.