-
Notifications
You must be signed in to change notification settings - Fork 1
/
alarm_mode.js
133 lines (107 loc) · 3.25 KB
/
alarm_mode.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
var util = require('util');
var Moment = require('moment');
var EventEmitter = require('events').EventEmitter;
function AlarmMode() {
var that = this;
this.durationSeconds = 60;
// Halve and convert to milliseconds
this.duration = this.durationSeconds / 2;
this.durationMs = this.duration * 1000;
this.dayDurationSeconds = 60 * 60;
this.dayDurationMs = this.dayDurationSeconds * 1000;
}
util.inherits(AlarmMode, EventEmitter);
AlarmMode.prototype.setAlarm = function(){
var that = this;
var now = new Moment();
var alarmTime = new Moment().add(this.duration + 60, 'seconds');
var predawnTime = alarmTime.clone().subtract(this.durationSeconds + 10, 'seconds');
var dawnTime = alarmTime.clone().subtract(this.durationSeconds, 'seconds');
var sunriseTime = alarmTime.clone().subtract(this.durationSeconds / 2, 'seconds');
console.log('now: '.blue + now.format().blue);
console.log('predawnTime: '.blue + predawnTime.format().blue);
console.log('dawnTime: '.blue + dawnTime.format().blue);
console.log('sunriseTime: '.blue + sunriseTime.format().blue);
console.log('alarmTime: '.blue + alarmTime.format().blue);
// Predawn countdown
that.predawnTimer = setTimeout(function() {
var predawn = that.predawn();
that.emit('fade', predawn);
}, predawnTime.format('x') - now.format('x'));
// Dawn
that.dawnTimer = setTimeout(function() {
that.emit('fade', that.dawn());
}, dawnTime.format('x') - now.format('x'));
// Sunrise
that.sunriseTimer = setTimeout(function() {
that.emit('fade', that.sunrise());
}, sunriseTime.format('x') - now.format('x'));
// Day
that.dayTimer = setTimeout(function() {
that.emit('fade', that.day());
}, nextAlarm.format('x') - now.format('x'));
}
AlarmMode.prototype.getNextAlarm = function() {
var now = new Moment();
var alarm = new Moment();
var todayAlarm = this.settings.getAlarmForDay(now.day());
alarm = this._setAlarmOnMoment(alarm, todayAlarm);
if (now > alarm) {
alarm.add(1, 'days');
var tomorrowAlarm = this.settings.getAlarmForDay(alarm.day());
return this._setAlarmOnMoment(alarm, tomorrowAlarm);
} else {
return alarm;
}
}
AlarmMode.prototype._setAlarmOnMoment = function(moment, alarmString) {
var timeArray = alarmString.split(":");
// Set alarm details
moment
.hour(timeArray[0])
.minute(timeArray[1])
.seconds(0);
return moment;
}
AlarmMode.prototype.predawn = function() {
console.log('predawn: '.blue + new Date());
var fade = {
hue: 0,
saturation: 100,
brightness: 0,
kelvin: 2500,
duration: 1000
}
return fade;
}
AlarmMode.prototype.dawn = function() {
console.log('dawn: '.blue + new Date());
return {
hue: 0,
saturation: 100,
brightness: 100,
kelvin: 2500,
duration: this.durationMs / 2
}
}
AlarmMode.prototype.sunrise = function() {
console.log('sunrise: '.blue + new Date());
return {
hue: 0,
saturation: 0,
brightness: 100,
kelvin: 2500,
duration: this.durationMs / 2
}
}
AlarmMode.prototype.day = function() {
console.log('day: '.blue + new Date());
return {
hue: 0,
saturation: 0,
brightness: 0,
kelvin: 9000,
duration: this.dayDurationMs
}
}
module.exports = AlarmMode;