-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (79 loc) · 2.87 KB
/
index.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
const five = require("johnny-five");
const config = require("./config");
const board = new five.Board();
let nowPlaying = null;
board.on("ready", async function () {
const button = new five.Button(config.fiveButton);
button.on("down", function () {
Navidrome.star();
});
l = new five.LCD(config.LCD);
l.useChar("note");
scroller(l, 0, "NAVIDROME");
Navidrome.currentlyPlaying(l);
});
function scroller(l, row, str) {
let counter = 0;
setInterval(() => {
let fixedStr = str;
if (counter === 19) {
counter = 0;
}
if (fixedStr.length + counter > 19) {
fixedStr = fixedStr.slice(
0,
fixedStr.length - (fixedStr.length + counter - 19)
);
}
l.cursor(0, 0).print(" ");
l.cursor(row, counter++).print(fixedStr);
}, 500);
}
const Navidrome = {
currentlyPlaying: currentlyPlaying,
star: star,
};
async function currentlyPlaying(l) {
l.useChar("heart");
setInterval(async () => {
try {
const resp = await fetch(
`${config.navidrome.url}/rest/getNowPlaying?u=${config.navidrome.username}&t=${config.navidrome.token}&s=${config.navidrome.salt}&v=${config.navidrome.version}&c=${config.navidrome.appName}&f=json`
);
const result = await resp.json();
if (result?.["subsonic-response"]?.error) {
return;
}
if (!result?.["subsonic-response"]?.nowPlaying?.entry) {
return;
}
const tempNowPlaying =
result?.["subsonic-response"]?.nowPlaying?.entry[0]; //if more than one client is using navidrome, we will just display the most recently used client song playing
const { title, album, artist, year, starred, id } = tempNowPlaying;
if (
// if the id has changed, refresh the whole screen. If the star status has changed, only refresh the star line
(nowPlaying?.id !== id && l.clear()) ||
(nowPlaying?.starred !== starred &&
l.cursor(3, 0).print(` `))
) {
//song or starred status has changed
nowPlaying = tempNowPlaying;
}
l.cursor(1, 0).print(title.length > 19 ? title.slice(0, 19) : title);
l.cursor(2, 0).print(`${artist}`);
l.cursor(3, 0).print(`${year} ${starred ? ":heart:" : ""}`);
} catch (error) {
console.error("ERROR: ", error);
}
}, config.navidrome.interval);
}
async function star() {
if (!nowPlaying) {
return;
}
const resp = await fetch(
nowPlaying.starred
? `http://music.home.io/rest/unstar?id=${nowPlaying.id}&u=${config.navidrome.username}&t=${config.navidrome.token}&s=${config.navidrome.salt}&v=${config.navidrome.version}&c=${config.navidrome.appName}&f=json`
: `http://music.home.io/rest/star?id=${nowPlaying.id}&u=${config.navidrome.username}&t=${config.navidrome.token}&s=${config.navidrome.salt}&v=${config.navidrome.version}&c=${config.navidrome.appName}&f=json`
);
}