This repository has been archived by the owner on Oct 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-LibrespotJava.js
119 lines (101 loc) · 3.26 KB
/
MMM-LibrespotJava.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
'use strict';
let state = "stopped";
Module.register('MMM-LibrespotJava', {
// default values
defaults: {
// Module misc
name: 'MMM-LibrespotJava',
hidden: false,
// user definable
librespotApiHost: "127.0.0.1", // librespot java API host address (ip)
librespotApiPort: "24879", // librespot java API port, default is 24789
updatesEvery: 1.5, // How often should the table be updated in s?
deviceName: ""
},
start: function () {
Log.info('Starting module: ' + this.name);
this.context = null;
this.connectSocket();
this.startFetchingLoop();
},
getDom: function () {
let domBuilder = new NPOS_DomBuilder(this.config, this.file(''));
return domBuilder.getDom(this.context);
},
getStyles: function () {
return [
this.file('css/styles.css'),
this.file('node_modules/moment-duration-format/lib/moment-duration-format.js'),
'font-awesome.css'
];
},
getScripts: function () {
return [
this.file('core/NPOS_DomBuilder.js'),
'moment.js'
];
},
socketNotificationReceived: function (notification, payload) {
if (this.identifier !== payload.id) return
switch (notification) {
case 'RETRIEVED_SONG_DATA':
this.updateSongDom(payload.data)
break;
case 'UPDATE_STATE':
state = payload.data;
if (state != "stopped") break;
case "FETCH_NEW_SONG_DATA":
console.log("Updating song data because of changes")
if (state == "stopped") return this.updateSongDom(null);
this.fetchSong();
break;
case 'SOCKET_CLOSED':
console.error("Socket is closed. Reconnect will be attempted in 1s.", e.reason);
setTimeout(() => this.connectSocket(), 1000);
break;
}
},
connectSocket() {
this.sendSocketNotification('CONNECT_TO_WEBSOCKET', {
id: this.identifier,
data: `ws://${this.config.librespotApiHost}:${this.config.librespotApiPort}/events`
});
},
fetchSong() {
this.sendSocketNotification("FETCH_SONG", {
id: this.identifier,
data: `http://${this.config.librespotApiHost}:${this.config.librespotApiPort}/player/current`
});
},
startFetchingLoop() {
// ... and then repeat in the given interval
setInterval(() => {
if (state != "stopped") this.fetchSong();
}, this.config.updatesEvery * 1000);
},
updateSongDom: function (songInfo) {
if (state == "stopped") {
this.context = { noSong: true };
} else if (songInfo['track'] != null) {
const payload = {
imgURL: this.getImgURL(songInfo.track.album.coverGroup.image),
artist: songInfo.track.artist.map((artist) => artist.name).join(", "),
album: songInfo.track.album.name,
songTitle: songInfo.track.name,
isPlaying: state === "playing",
titleLength: songInfo.track.duration,
progress: (songInfo.trackTime < 0 ? 0 : songInfo.trackTime),
deviceName: this.config.deviceName
};
//console.log(payload)
this.context = payload;
}
this.updateDom();
},
getImgURL(images) {
let filtered = images.filter((image) => {
return image.width >= 240 && image.width <= 350;
});
return `https://i.scdn.co/image/${filtered[0].fileId.toLowerCase()}` //todo
},
});