-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwirelessd.mjs
224 lines (192 loc) · 7.09 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
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
/**
* 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 } from "./Libraries/ecma-station/ecma-station.mjs";
import { BandAction, Dial} from "./Libraries/ecma-tunerface/ecma-tunerface.mjs";
import { Dials, Stations, Bands } 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)) {
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) => {
var hostname = request.hostname;
return responder.send(
{"links": {
"stations": "http://"+hostname+":"+apiPort+"/"+"stations",
"play": "http://"+hostname+":"+apiPort+"/"+"play",
"pause": "http://"+hostname+":"+apiPort+"/"+"pause",
"volume": "http://"+hostname+":"+apiPort+"/"+"volume"
}
});
});
apiService.get('/stations', (request, responder) => {
var hostname = request.hostname;
var stationLinks = {};
Object.keys(liveStations).forEach(function (station) {
stationLinks[station] = "http://"+hostname+":"+apiPort+"/"+"stations/"+station
})
return responder.send({"links": stationLinks});
});
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('/volume', async (request, responder) => {
var volume = await mpdInstance.api.playback.getvol();
return responder.send(volume.volume);
});
apiService.put('/volume', (request, responder) => {
var volume = parseInt(request.body.volume);
if (volume >= 0 & volume <= 100) {
console.log("Setting volume to:", volume);
mpdInstance.api.playback.setvol(volume)
return responder.send(true);
}
responder.status(422)
return responder.send(false)
});
/**
* Get the currently playing station, and preferably - the currently playing track from supporting stations.
*/
apiService.get('/play', (request, responder) => {
//responder.status(501)
return responder.send([liveStation]);
});
apiService.put('/play/', (request, responder) => {
if (request.body === undefined) {
request.body = {};
}
if (Object.keys(request.body).includes("station")) {
var station = request.body.station
if (!(station in liveStations)) {
console.log("Station", station, "not currently loaded.");
responder.status(404);
return responder.send(false);
};
if (!(liveStation)) {
liveStation = station;
liveStations[station].play();
};
if (liveStation == station) {
return responder.send([true]);
};
mpdInstance.api.playback.pause();
console.log("Starting playback of:", station)
liveStation = station;
return setTimeout((stationObject, responderObject) => {
stationObject.play();
responderObject.send([true]);
}, 50, liveStations[station], responder);
} else {
if (liveStation) {
console.log("Resuming Playback");
return responder.send([mpdInstance.api.playback.play()]);
}
responder.status(404);
return responder.send([false]);
};
});
/**
* Stop playback
*/
apiService.put('/pause', (request, responder) => {
console.log("Stopping Playback");
mpdInstance.api.playback.pause();
return responder.send(false);
});
apiService.listen(apiPort, () =>
console.log(`Wireless control REST API now active on TCP port ${apiPort}!`),
);
function adjustTuner (frequency) {
var station = "static";
for (const stationName of Object.keys(liveStations)) {
if ((frequency <= Stations[stationName].frequencyMax) && (frequency >= Stations[stationName].frequencyMin)) {
station = stationName;
break;
};
};
if (liveStation != station) {
mpdInstance.api.playback.pause();
console.log("Starting playback of:", station)
liveStation = station;
return setTimeout((stationObject) => {
stationObject.play();
}, 50, liveStations[station]);
}
}
var bandActions = {}
bandActions[BandAction.RadioPlay] = function () {mpdInstance.api.playback.play()};
bandActions[BandAction.RadioPause] = function () {mpdInstance.api.playback.pause()};
function adjustBand (frequency) {
var task = function () {}; // Do nothing!
for (const bandName of Object.keys(Bands)) {
if ((frequency <= Bands[bandName].frequencyMax) && (frequency >= Bands[bandName].frequencyMin)) {
task = bandActions[Bands[bandName].type]
break;
};
};
task()
}
var activeDials = {};
var readingTests = {};
var readingIntervals = {};
const dialCallbacks = {
band: adjustBand,
volume: mpdInstance.api.playback.setvol,
tuner: adjustTuner
}
for (const dialName of Object.keys(Dials)) {
readingTests[dialName] = false;
activeDials[dialName] = Dial.from(Dials[dialName], dialCallbacks[dialName]);
readingIntervals[dialName] = setInterval(_ => {
readingTests[dialName] = activeDials[dialName].monitor();
if (readingTests[dialName] == true) {
console.log("Monitoring dial:", dialName);
clearInterval(readingIntervals[dialName]);
}
}, 50);
};