-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEXT-RadioPlayer.js
179 lines (166 loc) · 5.46 KB
/
EXT-RadioPlayer.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
/**
** Module : EXT-RadioPlayer
** ©@bugsounet
** v03-2022
** support: https://forum.bugsounet.fr
**/
logRadio = (...args) => { /* do nothing */ }
Module.register("EXT-RadioPlayer", {
defaults: {
debug: false,
minVolume: 30,
maxVolume: 75,
},
start: function () {
if (this.config.debug) logRadio = (...args) => { console.log("[RADIO]", ...args) }
this.radio = null
this.initializeMusicVolumeVLC()
this.radioPlayer= {
ready: false,
play: false,
img: null,
link: null,
new: false
}
},
getStyles: function () {
return [
"EXT-RadioPlayer.css",
"https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"
]
},
getDom: function() {
var radio = document.createElement("div")
radio.id = "EXT_RADIO"
radio.className = "hidden animate__animated"
radio.style.setProperty('--animate-duration', '1s')
var radioBackground = document.createElement("div")
radioBackground.id = "EXT_RADIO_BACKGROUND"
radio.appendChild(radioBackground)
var radioForeground = document.createElement("div")
radioForeground.id = "EXT_RADIO_FOREGROUND"
radio.appendChild(radioForeground)
var radioImg = document.createElement("img")
radioImg.id = "EXT_RADIO_IMG"
radioForeground.appendChild(radioImg)
return radio
},
notificationReceived: function(noti, payload, sender) {
switch(noti) {
case "DOM_OBJECTS_CREATED":
this.sendSocketNotification("INIT", this.config)
break
case "GAv4_READY":
if (sender.name == "MMM-GoogleAssistant") this.sendNotification("EXT_HELLO", this.name)
break
case "EXT_STOP":
case "EXT_RADIO-STOP":
if (this.radioPlayer.play) {
this.radioPlayer.new = false
this.sendSocketNotification("STOP")
}
break
case "EXT_RADIO-VOLUME_MIN":
if (this.radioPlayer.play) this.sendSocketNotification("VOLUME", this.config.minVolume)
break
case "EXT_RADIO-VOLUME_MAX":
if (this.radioPlayer.play) this.sendSocketNotification("VOLUME", this.config.maxVolume)
break
case "EXT_RADIO-START":
this.radioCommand(payload)
break
}
},
socketNotificationReceived: function(noti, payload) {
switch(noti) {
case "PLAYING":
this.radioPlayer.play = true
this.showRadio()
break
case "FINISH":
this.radioPlayer.play = false
if (this.radioPlayer.new) this.radioPlayer.new = false
else this.showRadio()
break
case "READY":
this.radioPlayer.ready = true
break
}
},
showRadio: function() {
if (this.radioPlayer.img) {
if (this.radioPlayer.play) this.showDivWithAnimatedFlip("EXT_RADIO")
else this.hideDivWithAnimatedFlip("EXT_RADIO")
}
if (this.radioPlayer.new) return
if (this.radioPlayer.play) this.sendNotification("EXT_RADIO-CONNECTED")
else this.sendNotification("EXT_RADIO-DISCONNECTED")
},
hideDivWithAnimatedFlip: function(div) {
var module = document.getElementById(div)
module.classList.remove("animate__flipInX")
module.classList.add("animate__flipOutX")
module.addEventListener('animationend', (e) => {
if (e.animationName == "flipOutX" && e.path[0].id == div) {
module.classList.add("hidden")
}
e.stopPropagation()
}, {once: true})
},
showDivWithAnimatedFlip: function(div) {
var module = document.getElementById(div)
module.classList.remove("hidden")
module.classList.remove("animate__flipOutX")
module.classList.add("animate__flipInX")
},
/** Radio command (for recipe) **/
radioCommand: function(payload) {
if (!this.radioPlayer.ready) return
if (this.radioPlayer.play) this.radioPlayer.new = true
if (payload.link) {
console.log(payload)
if (payload.img) {
var radioImg = document.getElementById("EXT_RADIO_IMG")
var backGround = document.getElementById("EXT_RADIO_BACKGROUND")
this.radioPlayer.img = payload.img
radioImg.src = this.radioPlayer.img
backGround.style.backgroundImage = `url(${payload.img})`
}
this.radioPlayer.link = payload.link
this.sendSocketNotification("PLAY", payload.link)
}
},
/** initialise volume control for VLC **/
initializeMusicVolumeVLC: function() {
/** convert volume **/
try {
let valueMin = null
valueMin = parseInt(this.config.minVolume)
if (typeof valueMin === "number" && valueMin >= 0 && valueMin <= 100) this.config.minVolume = this.convertPercentToValue(valueMin)
else {
console.error("[RADIO] config.minVolume error! Corrected with 30")
this.config.minVolume = 70
}
} catch (e) {
console.error("[RADIO] config.minVolume error!", e)
this.config.minVolume = 70
}
try {
let valueMax = null
valueMax = parseInt(this.config.maxVolume)
if (typeof valueMax === "number" && valueMax >= 0 && valueMax <= 100) this.config.maxVolume = this.convertPercentToValue(valueMax)
else {
console.error("[RADIO] config.maxVolume error! Corrected with 100")
this.config.maxVolume = 255
}
} catch (e) {
console.error("[RADIO] config.maxVolume error!", e)
this.config.maxVolume = 255
}
console.log("[RADIO] VLC Volume Control initialized!")
},
/** Convert percent to cvlc value **/
convertPercentToValue: function(percent) {
return parseInt(((percent*256)/100).toFixed(0))
}
})