-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.js
102 lines (80 loc) · 2.68 KB
/
button.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
//Mit WebsocketServer verbinden
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost:8080');
//GPIO Bibliothek laden
const Gpio = require('onoff').Gpio;
//PreviousPlaylist-Button
const buttonPreviousPlaylist = new Gpio(10, 'in', 'rising', { debounceTimeout: 10 });
//NextPlaylist-Button
const buttonNextPlaylist = new Gpio(25, 'in', 'rising', { debounceTimeout: 10 });
//PreviousTrack-Button
const buttonPreviousTrack = new Gpio(8, 'in', 'rising', { debounceTimeout: 10 });
//NextTrack-Button
const buttonNextTrack = new Gpio(7, 'in', 'rising', { debounceTimeout: 10 });
//SeekMinus-Button
const buttonSeekMinus = new Gpio(12, 'in', 'rising', { debounceTimeout: 10 });
//SeekPlus-Button
const buttonSeekPlus = new Gpio(16, 'in', 'rising', { debounceTimeout: 10 });
//ResetCountdown-Button
const buttonResetCountdown = new Gpio(20, 'in', 'rising', { debounceTimeout: 10 });
//Pause-Button
const buttonPause = new Gpio(21, 'in', 'rising', { debounceTimeout: 10 });
//Wenn Verbindung mit WSS hergestellt wird
ws.on('open', function open() {
console.log("connected to wss");
//zur vorherigen Playlist wechseln
buttonPreviousPlaylist.watch(() => {
//Play-Befehl (pico file) killen, falls vorhanden
try {
execSync("(! pidof play) || sudo kill -9 $(pidof play)");
}
catch (e) {
}
//Befehl an WSS schicken
send("change-playlist", -1);
});
//zur naechsten Playlist wechseln
buttonNextPlaylist.watch(() => {
//Play-Befehl (pico file) killen, falls vorhanden
try {
execSync("(! pidof play) || sudo kill -9 $(pidof play)");
}
catch (e) {
}
//Befehl an WSS schicken
send("change-playlist", 1);
});
//zur vorherigen Track wechseln
buttonPreviousTrack.watch(() => {
send("change-track", false);
});
//zur naechsten Track wechseln
buttonNextTrack.watch(() => {
send("change-track", true);
});
//seek -
buttonSeekMinus.watch(() => {
send("seek", -10);
});
//seek +
buttonSeekPlus.watch(() => {
send("seek", 10);
});
//reset countdown
buttonResetCountdown.watch(() => {
send("reset-countdown", "");
});
//Pause / Unpuase / Playlist wieder von vorne starten
buttonPause.watch(() => {
send("toggle-paused-restart", "");
});
});
//Nachricht an WSS schicken
function send(type, value) {
console.log(type + ": " + value);
//Nachricht per JSON schicken
ws.send(JSON.stringify({
type: type,
value: value
}));
}