-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMMM-SmartWebDisplay.js
438 lines (320 loc) · 12.5 KB
/
MMM-SmartWebDisplay.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* Magic Mirror
* Module: MMM-SmartWebDisplay
*
* By AgP42
* First Version : 09/02/2019
*
* MIT Licensed.
*/
//For the PIR sensor
var UserPresence = true; //by default : user present. (No PIR-Sensor)
var startTimePir = 0;
Module.register("MMM-SmartWebDisplay",{
// Default module config.
defaults: {
logDebug: false, //set to true to get detailed debug logs. To see them : "Ctrl+Shift+i"
height:"100%", //hauteur du cadre en pixel ou %
width:"100%", //largeur
updateInterval: 0, //in min. Set it to 0 for no refresh (for videos)
NextURLInterval: 0.5, //in min, set it to 0 not to have automatic URL change. If only 1 URL given, it will be updated
displayStateInfos: false, //to display extra debug info directly on the module
displayLastUpdate: true, //to display the last update of the URL
displayLastUpdateFormat: 'ddd - HH:mm:ss', //format of the date and time to display
url: ["http://magicmirror.builders/", "https://www.youtube.com/embed/Qwc2Eq6YXTQ?autoplay=1"], //source of the URL to be displayed
scrolling: "no", // allow scrolling or not. html 4 only
shutoffDelay: 10000
},
start: function () {
Log.info("Starting module: " + this.name + " with identifier: " + this.identifier);
self = this;
this.ModuleiFrameHidden = false; //displayed by default
this.updateIntervalID = 0;
this.NextURLIntervalID = 0;
this.URLposition = 0;
this.ActualState = "Starting...";
//local variables, copie the config at startup and then will follow its life...
this.url_list = this.config.url;
this.updateInt = this.config.updateInterval;
this.nextURLInt = this.config.NextURLInterval;
this.shutoffDelay = this.config.shutoffDelay;
//run !!
this.StartDisplay();
}, //end start function
//call only once at start-up
iframeLoad: function() {
if(this.config.logDebug){
Log.log ("iframeLoad à : " + moment.unix(Date.now() / 1000).format('dd - HH:mm:ss') + "url : " + this.urlToDisplay);
}
//Init of the iFrame
iframe = document.createElement("IFRAME");
iframe.width = this.config.width;
iframe.height = this.config.height;
iframe.scrolling = this.config.scrolling;
iframe.src = this.urlToDisplay;
return iframe;
},
//allow to select the URL to be displayed
selectURL: function(direction) {
self = this;
if(this.config.logDebug){
Log.log("Fct selectURL - URLposition = " + self.URLposition);
}
if(direction === "next"){
self.URLposition++;
if(self.URLposition === self.url_list.length) //quand on a atteint la fin de la liste, on repart au debut
{
self.URLposition = 0;
}
}else if(direction === "prev"){
self.URLposition--;
if(self.URLposition < 0) //si negatif, on revient a partir de la fin de la liste
{
self.URLposition = self.url_list.length + self.URLposition;
}
}
return self.url_list[self.URLposition];
},
suspend: function() { //function called when module is hidden
this.ModuleiFrameHidden = true;
if(this.config.logDebug){
Log.log("Fct suspend - ModuleHidden = " + this.ModuleiFrameHidden);
}
this.GestionUpdateIntervalSWD(); //call the function that manage all cases
},
resume: function() { //function called when module is displayed again
this.ModuleiFrameHidden = false;
if(this.config.logDebug){
Log.log("Fct resume - ModuleHidden = " + this.ModuleiFrameHidden);
}
this.GestionUpdateIntervalSWD();
},
notificationReceived: function(notification, payload) {
if (notification === "USER_PRESENCE") { // notification sent by the module MMM-PIR-Sensor or others
if(this.config.logDebug){
Log.log("Fct notificationReceived USER_PRESENCE - payload = " + payload);
}
UserPresence = payload;
this.GestionUpdateIntervalSWD();
}
if (notification === "SWD_URL") {
if(this.config.logDebug){
Log.log("Fct notificationReceived SWD_URL - payload = " + payload.url + payload.update + payload.NextURL);
}
this.URLposition = 0; //on reinitialise notre boucle de lecture des URL pour recommencer par la 1ere
if (payload.url){
this.url_list = payload.url;
if(this.config.logDebug){
Log.log("URL présent et le voilà = " + this.url_list);
}
}
if (payload.update){
this.updateInt = payload.update;
if(this.config.logDebug){
Log.log("update présent et le voilà = " + this.updateInt);
}
}
if (payload.NextURL){
this.nextURLInt = payload.NextURL;
if(this.config.logDebug){
Log.log("NextURL présent et le voilà = " + this.nextURLInt);
}
}
this.StopDisplay(); //on arrete tout ce qui cours
this.StartDisplay(); //on relance avec les nouvelles infos
}
if (notification === "SWD_NEXT") {
if(this.config.logDebug){
Log.log("Fct notificationReceived SWD_NEXT");
}
this.StopDisplay(); //on arrete tout ce qui cours
this.StartDisplay("next"); //demande la prochaine video
//arreter puis relancer permet de relancer les timers de mise à jour
}
if (notification === "SWD_PREV") {
if(this.config.logDebug){
Log.log("Fct notificationReceived SWD_PREV");
}
this.StopDisplay(); //on arrete tout ce qui cours
this.StartDisplay("prev");
//arreter puis relancer permet de relancer les timers de mise à jour
}
if (notification === "SWD_PLAY") {
if(this.config.logDebug){
Log.log("Fct notificationReceived SWD_PLAY");
}
this.StopDisplay(); //on arrete tout ce qui cours
this.StartDisplay(); //on relance avec l'URL en cours
//arreter puis relancer permet de relancer les timers de mise à jour
}
if (notification === "SWD_STOP") {
if(this.config.logDebug){
Log.log("Fct notificationReceived SWD_STOP");
}
this.StopDisplay(); //on arrete tout ce qui cours et on coupe la video
}
if (notification === "SWD_PAUSE") {
if(this.config.logDebug){
Log.log("Fct notificationReceived SWD_PAUSE");
}
clearInterval(this.updateIntervalID); // on arrete l'intervalle d'update en cours
this.updateIntervalID=0; //on reset la variable
clearInterval(this.NextURLIntervalID); // on arrete l'intervalle pour passer d'une URL à l'autre en cours
this.NextURLIntervalID=0; //on reset la variable
this.ActualState = "Pause";
//but the actual video continu to run, or the actual display will remains, only timers are stopped
}
},
getCommands: function(commander) {
commander.add({
command: 'swd_next',
callback: 'command_swd_next'
});
commander.add({
command: 'swd_prev',
callback: 'command_swd_prev'
});
commander.add({
command: 'swd_play',
callback: 'command_swd_play'
});
commander.add({
command: 'swd_stop',
callback: 'command_swd_stop'
});
commander.add({
command: 'swd_pause',
callback: 'command_swd_pause'
});
commander.add({
command: 'swd_url',
callback: 'command_swd_url'
});
},
command_swd_next: function(command, handler) {
this.StopDisplay(); //on arrete tout ce qui cours
this.StartDisplay("next"); //demande la prochaine video
//arreter puis relancer permet de relancer les timers de mise à jour
handler.reply("TEXT", "Next URL requested");
},
command_swd_prev: function(command, handler) {
this.StopDisplay(); //on arrete tout ce qui cours
this.StartDisplay("prev");
//arreter puis relancer permet de relancer les timers de mise à jour
handler.reply("TEXT", "Previous URL requested");
},
command_swd_play: function(command, handler) {
this.StopDisplay(); //on arrete tout ce qui cours
this.StartDisplay(); //on relance avec l'URL en cours
//arreter puis relancer permet de relancer les timers de mise à jour
handler.reply("TEXT", "Play requested");
},
command_swd_stop: function(command, handler) {
this.StopDisplay(); //on arrete tout ce qui cours et on coupe la video
handler.reply("TEXT", "Stop requested");
},
command_swd_pause: function(command, handler) {
clearInterval(this.updateIntervalID); // on arrete l'intervalle d'update en cours
this.updateIntervalID=0; //on reset la variable
clearInterval(this.NextURLIntervalID); // on arrete l'intervalle pour passer d'une URL à l'autre en cours
this.NextURLIntervalID=0; //on reset la variable
this.ActualState = "Pause";
//but the actual video continu to run, or the actual display will remains, only timers are stopped
handler.reply("TEXT", "Pause requested");
},
command_swd_url: function(command, handler) {
this.URLposition = 0; //on reinitialise notre boucle de lecture des URL pour recommencer par la 1ere
if (handler.args){
this.url_list = [handler.args];
}
this.StopDisplay(); //on arrete tout ce qui cours
this.StartDisplay(); //on relance avec les nouvelles infos
handler.reply("TEXT", "New URL received");
},
GestionUpdateIntervalSWD: function() {
if (UserPresence === true && this.ModuleiFrameHidden === false){ //user is present and module is displayed
this.StartDisplay(); //on relance au dernier URL
this.ModuleiFrameHidden = true;
}else if (UserPresence === false && this.ModuleiFrameHidden === false && new Date().getTime() > this.startTimePir + this.shutoffDelay){ //user is not present AND module is displayed AND shutoffDelay is ended already : stop updating and stop video playing
this.StopDisplay();
}
},
StopDisplay: function() {
if(this.config.logDebug){
Log.log("Stop Display ! updateIntervalID : " + this.updateIntervalID + " NextURLIntervalID : " + this.NextURLIntervalID);
}
clearInterval(this.updateIntervalID); // on arrete l'intervalle d'update en cours
this.updateIntervalID=0; //on reset la variable
clearInterval(this.NextURLIntervalID); // on arrete l'intervalle pour passer d'une URL à l'autre en cours
this.NextURLIntervalID=0; //on reset la variable
//stop the video
this.urlToDisplay = "";
this.ActualState = "STOPPED";
this.updateDom(1000);
},
StartDisplay: function(direction) {
var self = this;
if(this.config.logDebug){
Log.log("StartDisplay, direction : " + direction);
}
this.urlToDisplay = this.selectURL(direction);
this.ActualState = "Playing";
self.updateDom(1000);
//set autoupdate of the DOM
if(this.updateIntervalID === 0 && this.updateInt > 0){
this.updateIntervalID = setInterval( function () {
self.updateDom(1000);
}, this.updateInt * 60 * 1000);
}
//set auto next url
if(this.NextURLIntervalID === 0 && this.nextURLInt > 0){
this.NextURLIntervalID = setInterval( function () {
self.urlToDisplay = self.selectURL("next");
self.updateDom(1000);
}, this.nextURLInt * 60 * 1000);
}
},
getStyles: function() {
return ["MMM-SmartWebDisplay.css"];
},
// Override dom generator.
getDom: function() {
var self = this;
if(this.config.logDebug){
Log.log ("update SWD DOM at : " + moment.unix(Date.now() / 1000).format('dd - HH:mm:ss') + ", url : " + this.urlToDisplay);
}
var wrapper = document.createElement("div");// main Wrapper that containts the others
wrapper.className = "mainWrapper"; //for CSS customization
//Init of the iFrame
iframe = document.createElement("IFRAME");
iframe.width = this.config.width;
iframe.height = this.config.height;
iframe.scrolling = this.config.scrolling;
iframe.src = this.urlToDisplay;
wrapper.appendChild(iframe);//request the iFrame to be displayed
/* var html = `
<iframe
src="${this.urlToDisplay}"
width="${this.config.width}"
height="${this.config.height}"
scrolling="${this.config.scrolling}"
></iframe>
`;
wrapper.insertAdjacentHTML("afterbegin", html); //*/
//to display last update at the end
if(this.config.displayLastUpdate){
this.lastUpdate = Date.now() / 1000 ;
var updateinfo = document.createElement("div"); //le div qui donne la date, si configuré pour etre affichée
updateinfo.className = "updateinfo"; // align-left
updateinfo.innerHTML = "Update requested at : " + moment.unix(this.lastUpdate).format(this.config.displayLastUpdateFormat);
wrapper.appendChild(updateinfo);
}
//to display state info at the end
if(this.config.displayStateInfos){
var stateinfo = document.createElement("div");
stateinfo.className = "stateinfo"; // align-left
stateinfo.innerHTML = /*"Update display interval : " + this.updateInt + "min, Delay between each URL : " + this.nextURLInt + "min, actual status : " + this.ActualState +*/ ", URL : " + this.url_list;
wrapper.appendChild(stateinfo);
}//*/
return wrapper;
}//fin getDom
}); //fin module register