-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
89 lines (81 loc) · 3.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var moment = require('moment');
var app = require('./app/index.js')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var Task = require('./model/Task.model');
var Switch = require('./model/Switch.model');
app.use(require('express').static(__dirname+'/public'));
server.listen(app.get('port'),app.get('ip'),function(){
console.log('Listening on '+app.get('ip')+" : "+app.get('port'))
});
var t = setInterval(function(){
Task.find({},function(err,allTasks){
if(err) throw err;
else
{
if(allTasks.length>0)
{
for(var k=0; k<allTasks.length; k++)
{
console.log("k="+k+" compare val="+allTasks.length);
var d1 = new Date().toUTCString();
var d2 = new Date(allTasks[k].taskTimeDate).toUTCString();
console.log("System DateTime"+d1);
console.log("Task DateTime"+d2);
console.log(d1>=d2);
if (d1 >= d2 && !allTasks[k].Completed)
{
console.log("K="+k);
Switch.findOneAndUpdate({'_id':allTasks[k].Switch},{$set:{'status':allTasks[k].status}},
function(err,upSw){
if(err) throw err;
console.log(upSw);
});
/*Task.findByIdAndRemove(allTasks[k]._id,function(err,rmTsk){
if(err) throw err;
console.log(rmTsk.name+" Task Completed");
});*/
if (allTasks[k].repeat) {
if (allTasks[k].Repeat = 'hourly') {
var x = moment(allTasks[k].taskTimeDate);
x.add(1, 'hours');
allTasks[k].taskTimeDate = x.toDate();
allTasks[k].save(function (err, tk) {
if (err) console.log(err);
else console.log("Task " + tk.name + " Updated");
})
}
else if (allTasks[k].Repeat = 'daily') {
var x = moment(allTasks[k].taskTimeDate);
x.add(1, 'days');
allTasks[k].taskTimeDate = x.toDate();
allTasks[k].save(function (err, tk) {
if (err) console.log(err);
else console.log("Task " + tk.name + " Updated");
})
}
else if (allTasks[k].Repeat = 'weekly') {
var x = moment(allTasks[k].taskTimeDate);
x.add(1, 'weeks');
allTasks[k].taskTimeDate = x.toDate();
allTasks[k].save(function (err, tk) {
if (err) console.log(err);
else console.log("Task " + tk.name + " Updated");
})
}
}
else {
allTasks[k].Completed = true;
allTasks[k].save(function (err, tk) {
if (err) console.log(err);
else console.log(tk.name + " Completed");
})
}
}
}
}
}
});
},10000);
/************************** Socket.io ********************************/
require("./sockets/mySocket")(io);