-
Notifications
You must be signed in to change notification settings - Fork 1
/
alarm_factory.js
112 lines (89 loc) · 2.97 KB
/
alarm_factory.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
var Alarm = require('./alarm');
const EventEmitter = require('events');
const util = require('util');
// var moment = require('moment');
var moment = require('moment-timezone');
function AlarmFactory (timezone) {
moment.tz.setDefault(timezone);
// this.lights = lights;
this.timezone = timezone;
this.alarm = {};
this.timer = setTimeout(function(){}, 0);
EventEmitter.call(this);
}
util.inherits(AlarmFactory, EventEmitter);
AlarmFactory.prototype.init = function() {
this.alarm = this.getNextAlarm();
this.getNextTimer();
}
AlarmFactory.prototype.getNextAlarm = function () {
var alarmTime = this.getNextAlarmTime();
console.log('AlarmFactory.getNextAlarm', alarmTime.format());
var tillAlarm = alarmTime - moment();
console.log('Hours until next alarm: ', tillAlarm / 1000/60/60);
return this.newAlarm(alarmTime);
}
// Emit a fade trigger event
AlarmFactory.prototype.triggerFade = function (eventName, duration) {
console.log('AlarmFactory.triggerFade', eventName, duration);
this.emit('fade', {name: eventName, duration: duration});
};
// Time till moment event in milliseconds
AlarmFactory.prototype.msUntilMoment = function (momentObject) {
return momentObject.diff(moment());
};
// Set timer for next event trigger
AlarmFactory.prototype.setTimer = function (eventName, duration, delay) {
clearTimeout(this.timer);
this.timer = setTimeout(function(eventName, duration){
this.triggerFade(eventName, duration);
this.getNextTimer();
}.bind(this), delay, eventName, duration);
};
// Gets next alarm event
// Updates alarm if necessary
AlarmFactory.prototype.getNextTimer = function () {
var now = moment();
var nextEvent = this.alarm.getNextEvent(now);
// TODO Get and set next alarm if no more events for this timer
if(nextEvent == false) {
this.alarm = this.getNextAlarm();
nextEvent = this.alarm.getNextEvent(now);
}
var delay = this.msUntilMoment(nextEvent.time);
this.setTimer(nextEvent['name'], nextEvent['duration'], delay);
};
// Factory method for Alarm
AlarmFactory.prototype.newAlarm = function(alarmTime) {
var alarm = new Alarm(alarmTime);
return alarm;
}
// Returns MomentJS
AlarmFactory.prototype.getNextAlarmTime = function() {
var now = moment();
var todayAlarmTime = this.getAlarmTimeForDayNumber(now.day());
var alarmTime = moment()
.hours(todayAlarmTime.hours)
.minutes(todayAlarmTime.minutes)
.seconds(0);
var alarm = this.newAlarm(alarmTime);
// Get tomorrow's if there are no remaining events for the today's alarm
if (alarm.getNextEvent(now) == false) {
alarmTime.add(1, 'day');
var tomorrowAlarmTime = this.getAlarmTimeForDayNumber(alarmTime.day());
alarmTime.hours(tomorrowAlarmTime.hours)
.minutes(tomorrowAlarmTime.minutes);
}
return alarmTime;
}
// TODO use settings
AlarmFactory.prototype.getAlarmTimeForDayNumber = function(dayNumber) {
if(dayNumber == 0 || dayNumber == 6) {
// Weekends
return {'hours': 9, 'minutes': 0};
} else {
// Weekdays
return {'hours': 7, 'minutes': 0};
}
}
module.exports = AlarmFactory;