-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathMMM-ShairportMetadata.js
230 lines (193 loc) · 6.72 KB
/
MMM-ShairportMetadata.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
/* global Log, Module */
/* Magic Mirror
* Module: MMM-ShairportMetadata
*
* By Prateek Sureka <surekap@gmail.com>
* MIT Licensed.
*
* Forked by ChielChiel
* Who made the progressbar
*
*/
Module.register("MMM-ShairportMetadata",{
// Module config defaults.
defaults: {
metadataPipe: "/tmp/shairport-sync-metadata",
alignment: "center"
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
var self = this;
self.data.header = "Nothing playing";
// Schedule update timer.
var self = this;
var progress = [];
var playing = null;
var lastUpdate = new Date().getTime() / 1000;
this.sendSocketNotification('CONFIG', this.config);
setInterval(() => {
this.updateDom(0);
}, 1000);
},
socketNotificationReceived: function(notification, payload){
if(notification === 'DATA') {
this.lastUpdate = new Date().getTime() / 1000;
if ((Object.keys(payload).length == 0)) {
this.playing = (this.playing == false) ? false : null;
}
if ((Object.keys(payload).length > 0)) {
this.playing = true
}
if (payload.hasOwnProperty('pause')) {
this.playing = !payload['pause'];
}
if (payload.hasOwnProperty('image')){
this.albumart = payload['image'];
} else {
this.metadata = payload;
if (payload.hasOwnProperty('prgr') && payload['prgr'] != 'undefined') {
this.progress = payload['prgr'].split("/");
}
}
this.updateDom(1000);
}
},
//convert RTP timestamps to seconds (assuming music is 44100hz or 44khz)
//As stated in this section https://github.com/mikebrady/shairport-sync#more-information
//"The default is 44,100 samples per second / 16 bits"
getSec: function(timestamp) {
return parseInt(timestamp) / 44100;
},
//convert seconds to normal minute:seconds format like: 201 --> 3:21
secToTime: function(sec) {
let min = Math.floor(sec / 60);
var remain = Math.floor((sec % 60));
remain = (remain.toString().length > 1) ? remain : "0" + (remain);
return (min + ":" + remain);
},
//determines whether the player should hide if there wasn't any update in last 2 minutes
//probably a bug then.
shouldHide: function() {
let now = new Date().getTime() / 1000;
return (now > (this.lastUpdate + 2 * 60)) ? true : false;
},
// Override dom generator.
getDom: function() {
var self = this;
var wrapper = document.createElement("div");
wrapper.className = this.config.classes ? this.config.classes : "small";
alignment = (this.config.alignment == "left") ? "left" : ((this.config.alignment == "right") ? "right" : "center");
wrapper.setAttribute("style", "text-align:" + alignment + ";")
if ((!this.metadata || (Object.keys(this.metadata).length == 0)) && (!this.progress && this.playing == false)){
wrapper.setAttribute("style", "display:none;");
return wrapper;
}
self.data.header = "Somebody is now playing";
metadata = document.createElement("div");
imgtag = document.createElement("img");
if (this.albumart){
imgtag.setAttribute('src', this.albumart);
imgtag.setAttribute('style', "width:100px;height:100px;");
}
imgtag.className = 'albumart';
metadata.appendChild(imgtag);
//create a break below the image
metadata.appendChild(document.createElement('br'));
//create the progressbar
var progressEl = document.createElement('progress');
progressEl.id = "musicProgress";
metadata.appendChild(progressEl);
metadata.appendChild(document.createElement('br'));
//create the label for the progress
var prgrLabel = document.createElement("label");
prgrLabel.setAttribute("for", "musicProgress");
prgrLabel.id = "progressLabel";
prgrLabel.innerHTML = "0:00 - 0:00";
metadata.appendChild(prgrLabel);
if (this.progress && this.progress.length > 0 && this.playing == true) {
//get the progress
let prData = this.progress;
//get the start, current play frame and the end of the song in seconds
let start = this.getSec(prData[0]);
let current = this.getSec(prData[1]);
let end = this.getSec(prData[2]);
let prgrInSec = current - start;
let songLength = end - start;
prData[1] = (parseInt(prData[1]) + 44100).toString(); //adds 1 sec of progress
this.progress = prData;
var progEl = progressEl;
//Make sure that, when there's a bug or something else
//That the 'progress' won't go past the 'end' of the song.
if (prgrInSec >= songLength) {
if (this.shouldHide()) {
//song is already over and it has been 2 minutes without update
wrapper.setAttribute("style", "display:none;");
return wrapper;
}
prgrInSec = songLength;
}
//update the progressbar
progEl.setAttribute("value", prgrInSec);
progEl.setAttribute("max", songLength);
prgrLabel.innerHTML = this.secToTime(prgrInSec) + " - " + this.secToTime(songLength);
} else if (this.playing == null && this.progress) { //song was paused
let prData = this.progress;
let start = this.getSec(prData[0]);
let current = this.getSec(prData[1]);
let end = this.getSec(prData[2]);
let prgrInSec = current - start;
let songLength = end - start;
this.progress = prData;
var progEl = progressEl;
if (prgrInSec >= songLength) {
if (this.shouldHide()) {
//song is already over and it has been 2 minutes without update
wrapper.setAttribute("style", "display:none;");
return wrapper;
}
prgrInSec = songLength;
}
progEl.setAttribute("value", prgrInSec);
progEl.setAttribute("max", songLength);
prgrLabel.innerHTML = this.secToTime(prgrInSec) + " - " + this.secToTime(songLength);
} else { //nothing is playing
wrapper.setAttribute("style", "display:none;");
return wrapper;
}
if (this.metadata['Title'] && this.metadata['Title'].length > 30){
//Because the dom regenerates every second. The marque won't scroll
//I wasn't able to fix that
titletag = document.createElement('div');
titletag.style.fontSize = "10px";
}else{
titletag = document.createElement("div");
}
titletag.innerHTML = (this.metadata['Title']) ? this.metadata['Title'] : "";
titletag.className = "bright";
metadata.appendChild(titletag)
var txt = "";
if (this.metadata['Artist'] || this.metadata['Album Name']){
txt = this.metadata['Artist'] + " - " + this.metadata['Album Name']
}
if (txt.length > 50){
//Because the dom regenerates every second. The marque won't scroll
//I wasn't able to fix that
artisttag = document.createElement('div');
artisttag.style.fontSize = "10px";
}else{
artisttag = document.createElement('div');
}
artisttag.innerHTML = txt;
artisttag.className = "xsmall";
metadata.appendChild(artisttag)
wrapper.appendChild(metadata);
return wrapper;
},
//Added css file to style the progressbar
getStyles: function() {
return [
"MMM-ShairportMetadata.css",
];
},
});