forked from temmihoo/plushie-badname
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calendar.js
140 lines (122 loc) · 4.23 KB
/
Calendar.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
** Filename - 'calendar.js'
** Author - 'Sayantan Hore'
** Created on - '07.12.2015'
** Description - 'This file holds the calendar events to listen to and calls CoAP client with required params'
*/
module.exports.getCalendar = function(__DEBUG__){
'use strict';
var moment = require('moment');
var PeriodicTask = require('periodic-task');
// Delay should be less than a second, because our events are separated by minimum a second
var delay = 100;
var eventsToken = null;
var periodicEventGenerator = null;
/*
** Checks if a scheduled event has occured by comparing the moment of the event with a new moment created just now
** Parameters checked : second, minute, hour, day, month, year
*/
function checkForEqualMoment(now, recordedMoment){
var isEqualMoment =
now.isSame(recordedMoment, 'second')
&& now.isSame(recordedMoment, 'minute')
&& now.isSame(recordedMoment, 'hour')
&& now.isSame(recordedMoment, 'day')
&& now.isSame(recordedMoment, 'month')
&& now.isSame(recordedMoment, 'year');
return isEqualMoment;
};
function createPeriodicTask(callback, args, taskDelay){
var obj = {};
if (typeof taskDelay === 'undefined'){
taskDelay = delay;
}
var task = new PeriodicTask(taskDelay, callback, obj, args);
obj.stop = task.stop.bind(task);
return task;
};
function getEvents(maxDelay){
var maxDelay = maxDelay || 8000;
var event = require('./Event');
var events = event.emitEvents(moment(), (parseInt(maxDelay) / 1000));
return events;
};
function scanRunningEvents(args){
var events = args[0];
events.map(function(event, index){
if (checkForEqualMoment(moment(), moment(event.end.time))){
if (!__DEBUG__){
event.end.perform();
}
events.splice(index, 1);
}
});
if (events.length === 0){
this.stop();
}
};
function scanEvents(args){
var events = args[0];
var running_events = args[1];
var __FLAG__Running = args[2];
events.map(function(event, index){
if (checkForEqualMoment(moment(), moment(event.begin.time))){
running_events.push(event);
if (!__DEBUG__){
event.begin.perform();
}
events.splice(index, 1);
if ((__FLAG__Running === true) && (running_events.length === 1)){
var task = createPeriodicTask(scanRunningEvents, [running_events], delay);
task.run();
}
}
});
if (events.length === 0){
this.stop();
}
};
function monitorEvents(events){
var running_events = [];
var task = createPeriodicTask(scanEvents, [events, running_events, true], delay);
task.run();
};
function publishEvents(args){
var pubsub = args[0];
var events = getEvents();
pubsub.publish("generateEvents", events);
};
function subscribeEvents(pubsub){
return pubsub.subscribe("generateEvents", monitorEvents);
};
function stop(pubsub){
periodicEventGenerator.stop();
pubsub.unsubscribe(eventsToken);
};
function start(pubsub){
eventsToken = subscribeEvents(pubsub);
var delay = 10000;
periodicEventGenerator = createPeriodicTask(publishEvents, [pubsub], delay);
periodicEventGenerator.run();
};
var rtrn_obj = null;
if (__DEBUG__){
rtrn_obj = {
checkForEqualMoment: checkForEqualMoment,
getEvents: getEvents,
createPeriodicTask: createPeriodicTask,
scanEvents: scanEvents,
scanRunningEvents: scanRunningEvents,
monitorEvents: monitorEvents
}
}
else{
rtrn_obj = {
start: start,
stop: stop,
getEvents: getEvents,
monitorEvents: monitorEvents
}
}
return rtrn_obj;
}