-
Notifications
You must be signed in to change notification settings - Fork 1
/
alarm.js
50 lines (41 loc) · 1.58 KB
/
alarm.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
var moment = require('moment');
/*
* GLOSSARY
* Predawn - Fade to black in preparation
* Dawn is the first appearance of light in the sky before sunrise. The start of the first sequence (black to red)
* Twilight is the period between dawn and sunrise
* Sunrise is the time in the morning when the sun appears. The start of the second sequence (red to white)
* Day occurs once the sunrise sequence is complete
* Daybreak could be the term for the event at the end of sunrise?
*/
function Alarm (alarmTime) {
var sunriseDurationInMinutes = 30;
var dawn = moment(alarmTime);
dawn.subtract(sunriseDurationInMinutes, 'minutes');
var sunrise = moment(alarmTime);
sunrise.subtract(sunriseDurationInMinutes/2, 'minutes');
var predawn = moment(dawn);
predawn.subtract(10, 'seconds');
var shutoff = moment(alarmTime);
shutoff.add(90, 'minutes');
var postshutoff = moment(shutoff);
postshutoff.add(15, 'seconds');
this.stages = {
'predawn': {name: 'predawn', time: predawn, duration: 1000}, // 1 sec
'dawn': {name: 'dawn', time: dawn, duration: 900000}, // 15 mins
'sunrise': {name: 'sunrise', time: sunrise, duration: 900000}, // 15 mins
'shutoff': {name: 'shutoff', time: shutoff, duration: 10000}, // 10 secs
'postshutoff': {name: 'postshutoff', time: postshutoff, duration: 1000} // 1 sec
};
}
Alarm.prototype.getNextEvent = function (time) {
var previous = null;
for (stageName in this.stages) {
var stage = this.stages[stageName];
if (time <= stage.time && (previous == null || previous <= time)) {
return stage;
}
}
return false;
};
module.exports = Alarm;