-
Notifications
You must be signed in to change notification settings - Fork 3
/
MMM-DigitalAlarmClock.js
371 lines (328 loc) · 10 KB
/
MMM-DigitalAlarmClock.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* MagicMIrror Module - MMM-DigitalAlarmClock
*
* This is a module for the MagicMirror² By Michael Teeuw http://michaelteeuw.nl
* (https://github.com/MichMich/MagicMirror/).
*
* The digital only part of the default MM2 clock
* written by Michael Teeuw http://michaelteeuw.nl]
*
* Integrated with MMM-AlarmClock by @fewieden
*
* I was shooting for this to have the look of the
* "old school" or vintage digital alarm clocks!
*
* Most of the ones I remember came in red, blue, white, or green
* Of course, you are welcome to choose whatever color or
* color combination you want.
*
* NOT tested with Raspberry Pi
* It DOES work with Windows 10 AND Ubuntu!!!
*
* version: 1.0.0
*
* Modified module by Jim Hallock (justjim1220@gmail.com)
*
* Licensed with a crapload of good ole' Southern Sweet Tea
* and a lot of Cheyenne Extreme Menthol cigars!!!
*/
// jshint esversion:6
Module.register("MMM-DigitalAlarmClock", {
next: null,
alarmFired: false,
timer: null,
fadeInterval: null,
defaults: {
showDate: true,
dateFormat: "ddd ll",
alarmSet: null,
alarms: [{
time: "09:30",
days: [1, 2, 3, 4, 5]
}],
sound: "alarm.mp3",
touch: false,
popup: true,
volume: 1.0,
timer: 60 * 1000,
format: "ddd h:mm a",
fade: false,
fadeTimer: 60 * 1000,
fadeStep: 0.005,
snooze: false,
snoozeTimer: 5,
snoozeTimerUnit: "minutes" // other option is seconds
},
requiresVersion: "2.1.0",
// Define required scripts.
getScripts: function() {
return ["moment.js", "moment-timezone.js"];
},
// Define styles.
getStyles: function() {
return ["MMM-DigitalAlarmClock.css", "font-awesome.css"];
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
"use strict",
// Set locale.
moment.locale(config.language);
// Schedule update interval.
var self = this;
setInterval(function() {
self.updateDom();
}, 1000);
this.setNextAlarm();
setInterval(() => {
this.checkAlarm();
}, 1000);
},
notificationReceived(notification) {
if (notification === "STOP_ALARM") {
this.resetAlarmClock()
} else if(notification === "SNOOZE_ALARM") {
this.snoozeAlarmClock()
}
},
checkAlarm() {
if (!this.alarmFired && this.next && moment().diff(this.next.moment) >= 0) {
var 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);
if (this.config.touch && this.config.popup) {
MM.getModules().enumerate(module => {
if (module.name === "alert") {
if (this.config.snooze && this.next.snooze !== false || this.next.snooze) {
module.alerts["MMM-DigitalAlarmClock"].ntf.addEventListener("click", () => {
this.snoozeAlarmClock();
});
module.alerts["MMM-DigitalAlarmClock"].ntf.addEventListener("dblclick", () => {
this.resetAlarmClock();
});
} else {
module.alerts["MMM-DigitalAlarmClock"].ntf.addEventListener("click", () => {
this.resetAlarmClock();
});
}
}
});
}
setTimeout(() => {
var player = document.getElementById("MMM-AlarmClock-Player");
player.volume = this.config.fade ? 0 : this.config.volume;
this.fadeAlarm();
}, 100);
}
},
fadeAlarm() {
let volume = 0;
let counter = 0;
this.fadeInterval = setInterval(() => {
var player = document.getElementById("MMM-DigitalAlarmClock-Player");
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);
},
setNextAlarm() {
this.next = null;
for (let i = 0; i < this.config.alarms.length; i += 1) {
var 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;
}
}
},
snoozeAlarmClock() {
clearTimeout(this.timer);
clearTimeout(this.fadeInterval);
this.alarmFired = false;
if (this.config.touch && this.config.popup) {
this.sendNotification("HIDE_ALERT");
}
if (this.next.snoozeTimerUnit) {
snoozeTimerUnit = this.next.snoozeTimerUnit;
} else {
snoozeTimerUnit = this.config.snoozeTimerUnit;
}
if (this.next.snoozeTimer) {
this.next.moment.add(this.next.snoozeTimer,snoozeTimerUnit);
} else if (this.config.snoozeTimer) {
this.next.moment.add(this.config.snoozeTimer,snoozeTimerUnit);
} else {
this.setNextAlarm();
}
this.updateDom(300);
},
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);
},
getMoment(alarm) {
var now = moment();
let difference = Math.min();
var hour = parseInt(alarm.time.split(":")[0]);
var 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
});
},
button(onclick) {
var audio = document.getElementsByTagName("audio");
if (!audio.paused) {
audio.pause(onclick);
audio.currentTime = 0;
}
},
getDom: function () {
var wrapper = document.createElement("div");
// Date section
var dateWrapper = document.createElement("div");
dateWrapper.classList.add("medium");
var date = document.createElement("span");
var now = moment();
date.className = "date";
date.innerHTML = now.format(this.config.dateFormat);
dateWrapper.appendChild(date);
// Time section
var timeWrapper = document.createElement("div");
timeWrapper.classList.add("large");
var time = document.createElement("span");
time.className = "time";
var hourSymbol = "HH";
if (this.config.timeFormat !== 24) {
hourSymbol = "h";
}
var colonSymbol = ":";
colonSymbol.className = "colon";
var timeString = now.format(hourSymbol + colonSymbol + "mm");
time.innerHTML = timeString;
timeWrapper.appendChild(time);
// Alarm section
var alarmWrapper = document.createElement("div");
alarmWrapper.classList.add("large");
var alarm = document.createElement("tr");
alarm.className = "alarm";
var pwrBtn = document.createElement("button");
pwrBtn.className = "onoff";
var BtnImg=document.createElement("img");
BtnImg.width="40";
//BtnImg.style.valign="middle";
BtnImg.id="onoff";
if (this.config.alarmSet === true) {
BtnImg.src = "modules/" + this.name + "/on.png";
} else {
BtnImg.src = "modules/" + this.name + "/off.png";
}
pwrBtn.appendChild(BtnImg);
pwrBtn.addEventListener("click", (button) => {
Log.log("in event handler for click");
var b = document.getElementById("onoff");
if(b.src.indexOf("on.png") > 0) {
b.src = "modules/" + this.name + "/off.png";
this.config.alarmSet = false;
this.resetAlarmClock();
}
else {
b.src = "modules/"+this.name+"/on.png";
this.config.alarmSet = true;
}
});
alarmWrapper.appendChild(pwrBtn);
if (this.config.alarmSet === true) {
alarm.classList.add("fa", "fa-bell-o", "bell");
} else {
alarm.classList.add("fa", "fa-bell-slash-o", "bell");
}
alarmWrapper.appendChild(alarm);
var alarmSet = document.createElement("span");
alarmSet.className = "set";
if (this.config.alarmSet === true) {
alarmSet.classList.add("medium");
alarmSet.innerHTML = ` ${this.next.moment.format(this.config.format)}  `;
} else {
alarmSet.innerHTML = " ";
}
alarmWrapper.appendChild(alarmSet);
var setButton = document.createElement("span");
setButton.className = "button";
if (this.config.alarmSet === true) {
setButton.innerHTML = "Alarm Set";
} else {
setButton.innerHTML = "Alarm NOT Set";
}
alarmWrapper.appendChild(setButton);
if (this.alarmFired) {
var sound = document.createElement("audio");
sound.className = "alarmSound";
let srcSound = this.config.sound;
if (this.next.sound) {
srcSound = this.next.sound;
}
if (!srcSound.match(/^https?:\/\//)) {
srcSound = this.file(`sounds/${srcSound}`);
}
sound.src = srcSound;
sound.volume = this.config.volume;
sound.setAttribute("id", "MMM-DigitalAlarmClock-Player");
sound.volume = this.config.fade ? 0 : this.config.volume;
sound.setAttribute("autoplay", true);
sound.setAttribute("loop", true);
if (this.config.fade === true) {
this.fadeAlarm();
}
alarmWrapper.appendChild(sound);
}
digitalWrapper = document.createElement("div");
digitalWrapper.className = "digital";
digitalWrapper.appendChild(dateWrapper);
digitalWrapper.appendChild(timeWrapper);
digitalWrapper.appendChild(alarmWrapper);
wrapper.appendChild(digitalWrapper);
return wrapper;
}
});