-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-MplayerRadio.js
489 lines (441 loc) · 17 KB
/
MMM-MplayerRadio.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/* global Module
/* MagicMirror²
* Module: MplayerRadio
*
* By Tom Hirschberger
* MIT Licensed.
*/
Module.register('MMM-MplayerRadio', {
/**
* By default, we should try to make the configuration match the demo
* implementation. This means 3 pages, and some default enabled styles.
*/
defaults: {
mplayerPath: "/usr/bin/mplayer",
mplayerCache: 512,
changeStationOnProfileChange: true,
showControls: true,
showVolControls: true,
showStreamInfo: true,
showStations: true,
showLogos: true,
showTitles: true,
scrollableStations: false,
stationsBeforeAndAfter: 1,
scrollToActiveStation: true,
missingLogoUrl: "./MMM-MplayerRadio/radio-freepnglogos.png",
previousIcon: "ic-round-skip-previous",
playIcon: "ic-round-play-arrow",
stopIcon: "ic-round-stop",
nextIcon: "ic-round-skip-next",
volUpIcon: "bi-volume-up-fill",
volDownIcon: "bi-volume-down-fill",
animationSpeed: 500,
customCommand: null,
customCommandArgs: [],
autoplay: null,
initStation: null,
stopOnSuspend: false,
},
/**
* Apply any styles, if we have any.
*/
getStyles() {
if (this.config.scrollableStations){
return ['mplayer-radio.css', 'mplayer-radio-scroll.css'];
} else {
return ['mplayer-radio.css'];
}
},
getScripts: function() {
return ['//code.iconify.design/1/1.0.5/iconify.min.js']
},
suspend: function() {
const self = this
if (self.config.stopOnSuspend){
self.sendSocketNotification("RADIO_STOP")
}
},
/**
* Pseudo-constructor for our module. Sets the default current page to 0.
*/
start() {
const self = this
Log.info("Starting module: " + self.name);
for (let i = 0; i < self.config.stations.length; i++){
if (typeof self.config.stations[i].title === "undefined"){
let title = self.config.stations[i].url
title = title.substring(title.lastIndexOf("/")+1)
title = title.substring(0,title.lastIndexOf("."))
self.config.stations[i].title = title
}
}
self.sendSocketNotification('CONFIG', self.config);
self.playing = false;
self.activeStation = null;
self.curStreamInfo = null;
self.streamInfoObj = null;
self.currentProfile = ''
self.currentProfilePattern = new RegExp('.*')
self.instanceCssClass = "mradio"
if(self.config.autoplay !== null){
self.curStationIndex = self.config.autoplay
setTimeout(()=>{
self.sendSocketNotification("RADIO_PLAY",{id: self.config.autoplay})
}, 1000)
} else {
if(self.config.initStation !== null){
self.curStationIndex = self.config.initStation
} else {
if (self.config.stations.length > 2){
self.curStationIndex = self.getNextStationId(1, type=0)
} else {
self.curStationIndex = self.getNextStationId(0, type=0)
}
}
}
},
getStationDomObject: function(id){
const self = this
let curId = id
let stationWrapper = document.createElement("div")
stationWrapper.addEventListener("click", ()=>{self.notificationReceived("RADIO_PLAY", {
id: curId
})})
if(self.curStationIndex === curId){
self.activeStation = stationWrapper
if(self.playing){
stationWrapper.className = "station selected playing"
} else {
stationWrapper.className = "station selected stopped"
}
} else {
stationWrapper.className = "station unselected"
}
let stationInnerWrapper = document.createElement("div")
stationInnerWrapper.className = "innerWrapper"
if(self.config.showLogos){
let stationLogoWrapper = document.createElement("div")
stationLogoWrapper.className = "logoWrapper"
let stationLogo = document.createElement("img")
stationLogo.className = "logo"
if(typeof self.config.stations[curId].logo !== "undefined"){
stationLogo.src = self.config.stations[curId].logo
} else {
stationLogo.src = self.config.missingLogoUrl
}
stationLogoWrapper.appendChild(stationLogo)
stationInnerWrapper.appendChild(stationLogoWrapper)
}
if (self.config.showTitles){
let stationTitleWrapper = document.createElement("div")
stationTitleWrapper.className = "titleWrapper"
stationTitleWrapper.innerHTML = self.config.stations[curId].title
stationInnerWrapper.appendChild(stationTitleWrapper)
}
stationWrapper.appendChild(stationInnerWrapper)
return stationWrapper
},
getStreamInfoDom(){
const self = this
let streamInfoWrapper = document.createElement("div")
streamInfoWrapper.className = "streamInfoWrapper"
if (self.curStreamInfo != null){
streamInfoWrapper.innerHTML = self.curStreamInfo
} else {
streamInfoWrapper.innerHTML = " "
}
return streamInfoWrapper
},
getControlDom(){
const self = this
let controlWrapper = document.createElement("div")
controlWrapper.className = "controlWrapper"
if (self.config.showVolControls == true){
let volDownButtonWrapper = document.createElement("span")
volDownButtonWrapper.className = "volDownButtonWrapper"
volDownButtonWrapper.addEventListener("click", ()=>{self.sendNotification("VOLUME_DOWN")})
let volDownButton = document.createElement("span")
volDownButton.className = "button volDownButton iconify"
volDownButton.setAttribute("data-icon", self.config.volDownIcon)
volDownButton.setAttribute("data-inline", "false")
volDownButtonWrapper.appendChild(volDownButton)
controlWrapper.appendChild(volDownButtonWrapper)
}
let prevButtonWrapper = document.createElement("span")
prevButtonWrapper.className = "previousButtonWrapper"
prevButtonWrapper.addEventListener("click", ()=>{
self.notificationReceived("RADIO_PREVIOUS")
})
let prevButton = document.createElement("span")
prevButton.className = "button previousButton iconify"
prevButton.setAttribute("data-icon", self.config.previousIcon)
prevButton.setAttribute("data-inline", "false")
prevButtonWrapper.appendChild(prevButton)
controlWrapper.appendChild(prevButtonWrapper)
if(this.playing){
let stopButtonWrapper = document.createElement("span")
stopButtonWrapper.className = "stopButtonWrapper"
stopButtonWrapper.addEventListener("click", ()=>{self.notificationReceived("RADIO_STOP")})
let stopButton = document.createElement("span")
stopButton.className = "button stopButton iconify"
stopButton.setAttribute("data-icon", this.config.stopIcon)
stopButton.setAttribute("data-inline", "false")
stopButtonWrapper.appendChild(stopButton)
controlWrapper.appendChild(stopButtonWrapper)
} else {
let playButtonWrapper = document.createElement("span")
playButtonWrapper.className = "playButtonWrapper"
playButtonWrapper.addEventListener("click", ()=>{
self.notificationReceived("RADIO_PLAY",
{
id: self.curStationIndex,
}
)})
let playButton = document.createElement("span")
playButton.className = "button playButton iconify"
playButton.setAttribute("data-icon", this.config.playIcon)
playButton.setAttribute("data-inline", "false")
playButtonWrapper.appendChild(playButton)
controlWrapper.appendChild(playButtonWrapper)
}
let nextButtonWrapper = document.createElement("span")
nextButtonWrapper.className = "nextButtonWrapper"
nextButtonWrapper.addEventListener("click", ()=>{
self.notificationReceived("RADIO_NEXT")
})
let nextButton = document.createElement("span")
nextButton.className = "button nextButton iconify"
nextButton.setAttribute("data-icon", self.config.nextIcon)
nextButton.setAttribute("data-inline", "false")
nextButtonWrapper.appendChild(nextButton)
controlWrapper.appendChild(nextButtonWrapper)
if (self.config.showVolControls == true){
let volUpButtonWrapper = document.createElement("span")
volUpButtonWrapper.className = "volUpButtonWrapper"
volUpButtonWrapper.addEventListener("click", ()=>{self.sendNotification("VOLUME_UP")})
let volUpButton = document.createElement("span")
volUpButton.className = "button volUpButton iconify"
volUpButton.setAttribute("data-icon", self.config.volUpIcon)
volUpButton.setAttribute("data-inline", "false")
volUpButtonWrapper.appendChild(volUpButton)
controlWrapper.appendChild(volUpButtonWrapper)
}
return controlWrapper
},
/**
* Render the cicles for each page, and highlighting the page we're on.
*/
getDom() {
const self = this
let wrapper = document.createElement("div")
wrapper.className = self.instanceCssClass+" wrapper"
if(self.config.showStations){
let stationsWrapper = document.createElement("div")
stationsWrapper.className = "stationsWrapper"
if(self.config.showStations){
console.log("RADIO: Number of stations in this profile: "+self.getNumberOfStationsInCurrentProfile())
if (self.config.scrollableStations || (self.getNumberOfStationsInCurrentProfile() < ((self.config.stationsBeforeAndAfter * 2)+1))){
for (let curId = 0; curId < self.config.stations.length; curId ++){
if(
(typeof self.config.stations[curId].profiles === 'undefined') ||
(self.currentProfilePattern.test(self.config.stations[curId].profiles))
){
stationsWrapper.appendChild(self.getStationDomObject(curId))
}
}
} else {
let stationBeforeIdx = self.curStationIndex
for(let i = 0; i < self.config.stationsBeforeAndAfter; i += 1){
stationBeforeIdx = self.getNextStationId(stationBeforeIdx, 1)
}
stationsWrapper.appendChild(self.getStationDomObject(stationBeforeIdx))
for (let i = 0; i < self.config.stationsBeforeAndAfter-1; i++){
stationBeforeIdx = self.getNextStationId(stationBeforeIdx, type=-1)
stationsWrapper.appendChild(self.getStationDomObject(stationBeforeIdx))
}
stationsWrapper.appendChild(self.getStationDomObject(self.curStationIndex))
let curNextId = self.getNextStationId(self.curStationIndex, type=-1)
for (let stationAfterIdx = 0; stationAfterIdx < self.config.stationsBeforeAndAfter; stationAfterIdx += 1){
stationsWrapper.appendChild(self.getStationDomObject(curNextId))
curNextId = self.getNextStationId(curNextId, type=-1)
}
}
}
wrapper.appendChild(stationsWrapper)
}
if(self.config.showStreamInfo){
console.log("CUR_STREAM_INFO: "+self.curStreamInfo)
self.streamInfoObj = self.getStreamInfoDom()
wrapper.appendChild(self.streamInfoObj)
}
if(self.config.showControls){
wrapper.appendChild(self.getControlDom())
}
self.updateScrollPosition(1000)
return wrapper;
},
getNumberOfStationsInCurrentProfile: function(){
const self = this
if (self.currentProfile === ''){
return self.config.stations.length
} else {
let curCount = 0;
for (let curId = 0; curId < self.config.stations.length; curId ++){
if(
(typeof self.config.stations[curId].profiles === 'undefined') ||
(self.currentProfilePattern.test(self.config.stations[curId].profiles))
){
curCount += 1
}
}
return curCount
}
},
getNextStationId: function(curId, type=1){
const self = this
var retId = null
curId = parseInt(curId)
if (isNaN(curId)){
curId = null
}
if(curId !== null){
if(type > 0){
console.log("Searching for fitting prev station")
var newId = curId
for(var i = 0; i < self.config.stations.length; i++){
newId -= 1
if(newId < 0){
newId = self.config.stations.length - 1
}
if(
(typeof self.config.stations[newId].profiles === 'undefined') ||
((typeof self.currentProfilePattern !== 'undefined') &&
(self.currentProfilePattern.test(self.config.stations[newId].profiles)))
){
retId = newId
break
}
}
} else if(type < 0){
console.log("Searching for fitting next station")
var newId = curId
for(var i = 0; i < self.config.stations.length; i++){
newId += 1
if(newId > self.config.stations.length - 1){
newId = 0
}
if(
(typeof self.config.stations[newId].profiles === 'undefined') ||
((typeof self.currentProfilePattern !== 'undefined') &&
(self.currentProfilePattern.test(self.config.stations[newId].profiles)))
){
retId = newId
break
}
}
} else if(type === 0){
if(
(typeof self.config.stations[curId].profiles === 'undefined') ||
((typeof self.currentProfilePattern !== 'undefined') &&
(self.currentProfilePattern.test(self.config.stations[curId].profiles)))
){
console.log("Station with id: "+curId+" is ok for profile: "+self.currentProfile)
return curId
} else {
console.log("Station with id: "+curId+" is NOT ok for profile: "+self.currentProfile)
return self.getNextStationId(curId, -1)
}
}
} else {
return self.getNextStationId(0,1)
}
return retId
},
updateScrollPosition: function(timeout){
const self = this
if((self.config.scrollableStations)&&(self.config.scrollToActiveStation && (self.activeStation != null))){
setTimeout(()=>{
self.activeStation.parentNode.scrollTop = self.activeStation.offsetTop - self.activeStation.parentNode.offsetTop
}, timeout)
}
},
notificationReceived: function(notification,payload) {
const self = this
if (notification === 'CHANGED_PROFILE'){
if(typeof payload.to !== 'undefined'){
console.log("Updating profile information to: "+payload.to)
self.currentProfile = payload.to
self.currentProfilePattern = new RegExp('\\b'+payload.to+'\\b')
if(self.config.changeStationOnProfileChange){
var newId = self.getNextStationId(self.curStationIndex, 0)
if(newId !== self.curStationIndex){
self.curStationIndex = newId
if(self.playing){
self.sendSocketNotification("RADIO_PLAY", {
id: newId,
})
} else {
self.curStreamInfo = payload.curStreamInfo
}
}
}
self.updateDom(this.config.animationSpeed)
}
} else if (notification === 'RADIO_NEXT'){
self.sendSocketNotification("RADIO_PLAY", {
id: self.getNextStationId(self.curStationIndex, -1),
})
} else if (notification === 'RADIO_PREVIOUS'){
self.sendSocketNotification("RADIO_PLAY", {
id: self.getNextStationId(self.curStationIndex, 1),
})
} else if (notification === "RADIO_TOGGLE") {
if(self.playing){
self.sendSocketNotification("RADIO_STOP")
} else {
self.sendSocketNotification("RADIO_PLAY", {
id: self.curStationIndex,
})
}
} else if( (notification === "RADIO_PLAY") ||
(notification === "RADIO_STOP")
){
console.log("RADIO: Notification->"+notification)
console.log("RADIO: PAYLOAD->"+JSON.stringify(payload))
if ((payload === null) ||
(typeof payload === "undefined")
){
payload = {}
}
if (
(typeof payload['id'] === "undefined") &&
(typeof payload['id'] === "undefined")
){
payload["id"] = self.curStationIndex
}
this.sendSocketNotification(notification,payload)
}
},
socketNotificationReceived: function (notification, payload) {
const self = this
if(notification === "RADIO_PLAYING"){
this.curStationIndex = payload.curStationIndex
this.curStreamInfo = payload.curStreamInfo
this.playing = true
this.updateDom(this.config.animationSpeed)
} else if(notification === "RADIO_STOPPED"){
this.curStreamInfo = null
this.playing = false
this.updateDom(this.config.animationSpeed)
} else if(notification === "RADIO_CURRENT_STREAM_INFO"){
console.log("Updating Stream Info: "+payload.curStreamInfo)
this.curStreamInfo = payload.curStreamInfo
this.playing = true
//this.updateDom(this.config.animationSpeed)
if (this.streamInfoObj !== null){
this.streamInfoObj.innerHTML = this.curStreamInfo
}
}
},
});