-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwirelessd.mjs
171 lines (141 loc) · 5.2 KB
/
wirelessd.mjs
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
/**
* thewireless, an internet radio client for upcycling with Pi.
* Copyright (C) 2023 mo-g
*
* thewireless is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* thewireless is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with thewireless If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Note - this program will initially contain methods and assumptions that will
* be transferred to more specific models, config and interfaces as development
* progresses. This will start ugly and clean up as we work out the final
* architecture.
*/
//import mcpspi from 'mcp-spi-adc';
//import gpio from 'rpi-gpio';
import express from 'express';
import { Station, InternetStation, StreamProtocol} from "./Libraries/ecma-station/ecma-station.mjs";
import { Dials, Switches, NeedleServo, Stations } from './config.mjs';
import mpdapi from 'mpd-api';
const apiPort = 1932; // Port on which thewireless listens for control commands.
// Settings for the default MPD player. Should be moved to config, and have support for multiple players added (without sync).
const mpdConfig = {
path: "/run/mpd/socket"
};
const mpdInstance = await mpdapi.connect(mpdConfig);
/**
* Load all stations. They don't take much resource loaded, and don't pull any audio from the server till you hit play.
*/
var liveStations = {};
for (const stationName of Object.keys(Stations)) {
console.log(stationName);
liveStations[stationName] = Station.from(Stations[stationName]);
liveStations[stationName].player = mpdInstance;
};
var liveStation = null;
/**
* Set up the API Server.
*/
var apiService = new express();
apiService.use(express.json());
apiService.get('/', (request, responder) => {
return responder.send(["stations", "play"]);
});
apiService.get('/stations', (request, responder) => {
return responder.send(Object.keys(liveStations));
});
apiService.get('/stations/:station', (request, responder) => {
var station = request.params.station
if (station in liveStations) {
return responder.send(liveStations[station]);
};
console.log("Station", station, "not currently loaded.");
responder.status(404);
return responder.send("Station", station, "not known. Load /stations endpoint for current list.");
});
/*apiService.get('/stations/:station/:volume', (request, responder) => {
var station = request.params.station
if (!(station in liveStations)) {
console.log("Station", station, "not currently loaded.");
responder.status(404);
return responder.send("Station", station, "not known. Load /stations endpoint for current list.");
};
var volume = parseFloat(request.params.volume);
liveStations[station].volume = volume;
return responder.send(true);
});*/
apiService.get('/speaker', (request, responder) => {
return responder.send(mpdInstance);
});
apiService.get('/volume', (request, responder) => {
var volume = mpdInstance.api.playback.getvol()
return responder.send({"volume": volume});
});
apiService.get('/volume/:volume', (request, responder) => {
var volume = parseInt(request.params.volume);
if (volume >= 0 & volume <= 100) {
}
mpdInstance.api.playback.setvol(volume)
return responder.send(true);
});
/**
* Get the currently playing station, and preferably - the currently playing track from supporting stations.
*/
apiService.get('/status', (request, responder) => {
//responder.status(501)
return responder.send([liveStation]);
});
/**
* This should actually be a put, without :station, but I'm being lazy.
*/
apiService.get('/play/:station', (request, responder) => {
var station = request.params.station
if (!(station in liveStations)) {
console.log("Station", station, "not currently loaded.");
responder.status(404);
return responder.send("Station " + station + " not known. Load /stations endpoint for current list.");
};
if (!(liveStation)) {
liveStation = station;
liveStations[station].play();
};
if (liveStation == station) {
return responder.send([true]);
};
mpdInstance.api.playback.pause();
liveStation = station;
return setTimeout((stationObject, responderObject) => {
stationObject.play();
responderObject.send([true]);
}, 500, liveStations[station], responder);
});
apiService.get('/play', (request, responder) => {
if (liveStation) {
return responder.send([mpdInstance.api.playback.play()]);
}
responder.status(404);
return responder.send([false]);
});
/**
* Stop playback
*/
apiService.get('/pause', (request, responder) => {
mpdInstance.api.playback.pause();
return responder.send(false);
});
apiService.listen(apiPort, () =>
console.log(`Wireless control REST API now active on TCP port ${apiPort}!`),
);
/**
* Do something useful:
*/
//var interStation = new Static();