-
Notifications
You must be signed in to change notification settings - Fork 18
/
MMM-AlarmClock.js
337 lines (309 loc) · 9.63 KB
/
MMM-AlarmClock.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* @file MMM-AlarmClock.js
*
* @author fewieden
* @license MIT
*
* @see https://github.com/fewieden/MMM-AlarmClock
*/
/* global Module Log moment config MM */
/**
* @external Module
* @see https://github.com/MichMich/MagicMirror/blob/master/js/module.js
*/
/**
* @external Log
* @see https://github.com/MichMich/MagicMirror/blob/master/js/logger.js
*/
/**
* @external moment
* @see https://www.npmjs.com/package/moment
*/
/**
* @external config
* @see https://github.com/MichMich/MagicMirror/blob/master/config/config.js.sample
*/
/**
* @external MM
* @see https://github.com/MichMich/MagicMirror/blob/master/js/main.js
*/
/**
* @module MMM-AlarmClock
* @description Frontend for the module to display data.
*
* @requires external:Module
* @requires external:Log
* @requires external:moment
* @requires external:config
* @requires external:MM
*/
Module.register('MMM-AlarmClock', {
/** @member {?Object} next - Contains the next alarm object. */
next: null,
/** @member {boolean} alarmFired - Flag that indicates if there is a alarm firing right now. */
alarmFired: false,
/** @member {?Timeout} timer - Fires resetAlarmClock */
timer: null,
/** @member {?Interval} fadeInterval - Fades in the alarm volume. */
fadeInterval: null,
/**
* @member {Object} defaults - Defines the default config values.
* @property {string} sound - Alarm sound file.
* @property {boolean} touch - Flag to enable touch support.
* @property {number} volume - Alarm sound volume.
* @property {string} format - Alarm datetime format to display in UI.
* @property {int} timer - Alarm sound duration.
* @property {boolean} fade - Flag to fade in alarm sound volume.
* @property {int} fadeTimer - Fade in duration.
* @property {number} fadeStep - Fade in volume steps.
* @property {boolean} popup - Flag to show alert popup or not.
*/
defaults: {
sound: 'alarm.mp3',
touch: false,
volume: 1.0,
format: 'ddd, h:mmA',
timer: 60 * 1000,
fade: false,
fadeTimer: 60 * 1000,
fadeStep: 0.005,
popup: true
},
/**
* @function getStyles
* @description Style dependencies for this module.
* @override
*
* @returns {string[]} List of the style dependency filepaths.
*/
getStyles() {
return ['font-awesome.css', 'MMM-AlarmClock.css'];
},
/**
* @function getScripts
* @description Script dependencies for this module.
* @override
*
* @returns {string[]} List of the script dependency filepaths.
*/
getScripts() {
return ['moment.js'];
},
/**
* @function getTranslations
* @description Translations for this module.
* @override
*
* @returns {Object.<string, string>} Available translations for this module (key: language code, value: filepath).
*/
getTranslations() {
return {
en: 'translations/en.json',
de: 'translations/de.json',
fr: 'translations/fr.json',
id: 'translations/id.json',
ko: 'translations/ko.json'
};
},
/**
* @function getTemplate
* @description Nunjuck template.
* @override
*
* @returns {string} Path to nunjuck template.
*/
getTemplate() {
return 'templates/MMM-AlarmClock.njk';
},
/**
* @function getTemplateData
* @description Data that gets rendered in the nunjuck template.
* @override
*
* @returns {string} Data for the nunjuck template.
*/
getTemplateData() {
let src = this.config.sound;
if (this.alarmFired && this.next.sound) {
src = this.next.sound;
}
if (!src.match(/^https?:\/\//)) {
src = this.file(`sounds/${src}`);
}
return {
config: this.config,
next: this.next,
alarmFired: this.alarmFired,
src
};
},
/**
* @function start
* @description Sets first alarm and creates interval to check the alarm event.
* @override
*
* @returns {void}
*/
start() {
Log.info(`Starting module: ${this.name}`);
this.setNextAlarm();
setInterval(() => {
this.checkAlarm();
}, 1000);
moment.locale(config.language);
},
/**
* @function notificationReceived
* @description Handles incoming broadcasts from other modules or the MagicMirror core.
* @override
*
* @param {string} notification - Notification name
*
* @returns {void}
*/
notificationReceived(notification) {
if (notification === 'STOP_ALARM') {
this.resetAlarmClock();
}
},
/**
* @function checkAlarm
* @description Checks the alarm event and triggers the alert.
*
* @returns {void}
*/
checkAlarm() {
if (!this.alarmFired && this.next && moment().diff(this.next.moment) >= 0) {
const alert = {
imageFA: 'bell-o',
title: this.next.sender || this.next.title,
message: this.next.message
};
let timer = this.config.timer;
// If the alarm has specific timer and if MM is not touch, we use the alarm timer.
if (typeof this.next.timer !== 'undefined' && !this.config.touch) {
timer = this.next.timer;
}
if (!this.config.touch) {
alert.timer = timer;
}
if (this.config.popup) {
this.sendNotification('SHOW_ALERT', alert);
}
this.alarmFired = true;
this.updateDom();
this.timer = setTimeout(() => {
this.resetAlarmClock();
}, timer);
setTimeout(() => {
if (this.config.touch && this.config.popup) {
MM.getModules().enumerate(module => {
if (module.name === 'alert') {
module.alerts['MMM-AlarmClock'].ntf.addEventListener('click', () => {
this.resetAlarmClock();
});
}
});
}
const player = document.getElementById('MMM-AlarmClock-Player');
player.volume = this.config.fade ? 0 : this.config.volume;
this.fadeAlarm();
}, 100);
}
},
/**
* @function fadeAlarm
* @description Fades in the alarm sound step by step.
*
* @returns {void}
*/
fadeAlarm() {
let volume = this.config.fade ? 0 : this.config.volume;
let counter = 0;
const player = document.getElementById('MMM-AlarmClock-Player');
this.fadeInterval = setInterval(() => {
player.volume = volume;
volume += this.config.fadeStep;
counter += 1000;
if (volume >= this.config.volume || counter >= this.config.fadeTimer) {
player.volume = this.config.volume;
clearInterval(this.fadeInterval);
}
}, 1000);
},
/**
* @function setNextAlarm
* @description Sets the next occurring alarm event.
*
* @returns {void}
*/
setNextAlarm() {
this.next = null;
for (let i = 0; i < this.config.alarms.length; i += 1) {
const temp = this.getMoment(this.config.alarms[i]);
if (!this.next || temp.diff(this.next.moment) < 0) {
this.next = this.config.alarms[i];
this.next.moment = temp;
}
}
},
/**
* @function resetAlarmClock
* @description Resets the alarm clock.
*
* @returns {void}
*/
resetAlarmClock() {
clearTimeout(this.timer);
clearTimeout(this.fadeInterval);
this.alarmFired = false;
if (this.config.touch && this.config.popup) {
this.sendNotification('HIDE_ALERT');
}
this.setNextAlarm();
this.updateDom(300);
},
/**
* @function getMoment
* @description Creates a moment object based on alarm event.
*
* @param {Object} alarm - Alarm event.
* @param {string} alarm.time - Alarm event time.
* @param {int[]} alarm.days - Weekdays the alarm event occurrs.
*
* @returns {Object} Moment object for the next occurrence of this alarm event.
*
* @example <caption>alarm object</caption>
* {
* time: "18:30",
* days: [2,4],
* title: "Soccer",
* message: "Get ready for soccer training!"
* }
*/
getMoment(alarm) {
const now = moment();
let difference = Math.min();
const hour = parseInt(alarm.time.split(':')[0]);
const minute = parseInt(alarm.time.split(':')[1]);
for (let i = 0; i < alarm.days.length; i += 1) {
if (now.day() < alarm.days[i]) {
difference = Math.min(alarm.days[i] - now.day(), difference);
} else if (now.day() === alarm.days[i] && (parseInt(now.hour()) < hour
|| parseInt(now.hour()) === hour && parseInt(now.minute()) < minute)) {
difference = Math.min(0, difference);
} else if (now.day() === alarm.days[i]) {
difference = Math.min(7, difference);
} else {
difference = Math.min(7 - now.day() + alarm.days[i], difference);
}
}
return moment().add(difference, 'days')
.set({
hour,
minute,
second: 0,
millisecond: 0
});
}
});