-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
297 lines (276 loc) · 10.9 KB
/
app.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Initialize Roon APIs
var RoonApi = require("node-roon-api"),
RoonApiBrowse = require("node-roon-api-browse"),
RoonApiTransport = require("node-roon-api-transport"),
RoonApiStatus = require("node-roon-api-status"),
RoonApiSettings = require("node-roon-api-settings"),
transport,
browser,
zones = [],
station_list = [];
const NO_PRESET_VALUE = "None (disabled)";
// Initialize Express.js
var express = require('express'),
app = express();
var roon = new RoonApi({
extension_id: 'me.iangrant.radio-presets-api',
display_name: "Radio Presets API Trigger",
display_version: "1.0.0",
publisher: 'Ian Grant',
email: '[email protected]',
website: 'https://github.com/imgrant/roon-extension-radio-presets-api',
// Handler: on connection with Roon core
core_paired: function(core) {
// Connect to transport service
transport = core.services.RoonApiTransport;
transport.subscribe_zones(function(cmd, data) {
// On first connection to core, populate zones list and log zone names and ids
if (cmd == "Subscribed") {
zones = data.zones;
// On zone change, update the zone list
} else if (cmd == "Changed") {
if ("zones_added" in data) {
for (let item in data.zones_added) {
if (! get_zone(data.zones_added[item].display_name)) {
zones.push(data.zones_added[item]);
}
}
} else if ("zones_removed" in data) {
for (let item in data.zones_removed) {
zones.splice(zones.indexOf(transport.zone_by_zone_id(data.zones_removed[item])), 1);
}
} else if ("zones_changed" in data) {
data.zones_changed.forEach(changed_zone => {
zones.forEach( (existing_zone, index) => {
if (existing_zone.zone_id == changed_zone.zone_id) {
zones[index] = changed_zone;
}
});
});
}
// Should not fire...
} else {
console.log("Error: unhandled command...");
}
});
// Connect to browser service
browser = core.services.RoonApiBrowse;
},
// Handler: on disconnection from Roon core
core_unpaired: function(core) {
console.log("Lost connection to Roon core")
}
});
// Status handler
var svc_status = new RoonApiStatus(roon);
// Extension settings
var presets = roon.load_config("settings") || {
preset_1: NO_PRESET_VALUE,
preset_2: NO_PRESET_VALUE,
preset_3: NO_PRESET_VALUE,
preset_4: NO_PRESET_VALUE,
preset_5: NO_PRESET_VALUE,
preset_6: NO_PRESET_VALUE
};
var svc_settings = new RoonApiSettings(roon, {
get_settings: function(cb) {
fetch_station_list( () => {
cb(makelayout(presets));
});
},
save_settings: function(req, isdryrun, settings) {
let l = makelayout(settings.values);
req.send_complete(l.has_error ? "NotValid" : "Success", { settings: l });
if (!isdryrun && !l.has_error) {
presets = l.values;
svc_settings.update_settings(l);
roon.save_config("settings", presets);
}
}
});
function makelayout(settings) {
let l = {
values: settings,
layout: [],
has_error: false
};
for (i=1; i<=6; i++) {
l.layout.push({
type: "dropdown",
title: "Preset " + i,
values: station_list,
setting: "preset_" + i
});
}
return l;
}
// Populate list of user's available stations, transform for settings dropdowns
function fetch_station_list(cb) {
let opts = {
hierarchy: "internet_radio",
pop_all: true
};
load_browse_result(opts, (result) => {
// Using null for the value of the 'None' entry doesn't seem to work...
station_list = [{ title: "None (disabled)", value: NO_PRESET_VALUE }].concat(
result.map(item => {
return { title: item.title, value: item.title }
})
);
cb && cb();
});
}
// Helper function for fetching stations (or other)
function load_browse_result(opts, cb) {
browser.browse(opts, (err,r) => {
if (err) { console.log(err, r); return; }
browser.load(opts, (err, r) => {
if (err) {
console.log(err, r);
return;
} else if (cb) {
cb(r.items);
}
});
});
}
// Helper function to look up zone object from ID string or display name
function get_zone(id_or_display_name) {
for (item in zones) {
if (id_or_display_name == zones[item].zone_id || id_or_display_name == zones[item].display_name) {
return zones[item];
}
}
// Zone not found
return null;
}
// Radio preset request handler
function radio_preset(zone, preset_number, api_result) {
let opts = {
hierarchy: "internet_radio",
pop_all: true
};
let preset = "preset_" + preset_number;
let preset_title = presets[preset];
if (!(preset in presets)) {
msg = "Preset " + preset_number + " is not valid";
console.log("Error: " + msg);
api_result.status(409);
api_result.send(msg);
} else if (preset_title == NO_PRESET_VALUE) {
msg = "Preset " + preset_number + " is not set";
console.log("Error: " + msg);
api_result.status(500);
api_result.send(msg);
} else {
// This is an asynchronous method, so the API endpoint will
// return 200 OK, regardless of eventual success (e.g. if
// the station cannot be found)
load_browse_result(opts, (stations) => {
found = false;
stations.forEach(radio => {
if (radio["title"] == preset_title) {
msg = "Preset station found, playing " + radio["title"] + " on zone " + zone.display_name;
console.log("Success: " + msg);
opts["zone_or_output_id"] = zone.zone_id;
opts["item_key"] = radio["item_key"];
opts["pop_all"] = false;
browser.browse(opts);
found = true;
return true;
}
});
// If the preset station wasn't found, we end up here
// ... but also if the station was found ¯\_(ツ)_/¯
// so rely on the 'found' variable instead
if (!found) {
msg = "Preset " + preset_number + " station not found (" + preset_title + ")";
console.log("Error: " + msg);
}
});
}
}
// Expose and consume the following services to/from Roon core:
roon.init_services({
// Provide status of extension
provided_services: [ svc_status, svc_settings ],
// Require access to transport control and browse interface
required_services: [ RoonApiTransport, RoonApiBrowse ]
});
// Set status on connection with core (can be viewed in Roon: Settings -> Extensions)
svc_status.set_status("Extension starting ...", false);
// Start discovery to find Roon core (this is for pairing with a single core only)
roon.start_discovery();
// API endpoint handler, which accepts incoming HTTP GET requests
// formatted as, e.g.: http://<host>:<port>/api?preset=<number>&zone=<zone>
// -> preset: Numbered preset corresponding to the ratio station to play
// -> zone: Zone display name, such as Living Room (must be URL-encoded, e.g 'Living Room' = 'Living%20Room'), or zone ID (e.g. 16010ca1ea807d48b5531c73e2e1326c4932)
// -> host: Hostname or IP address of where the extension is running (may or may not be the same as the Roon core in your setup!)
// -> port: The listening port is defined below
app.get("/api", function(req, res) {
// Response can be customized as Express.js allows
if (req.query.preset && req.query.zone) {
console.log("Preset requested: " + req.query.preset + ", zone: " + req.query.zone);
let preset = req.query.preset,
zone = get_zone(req.query.zone);
if (zone) {
console.log("Zone matched: " + zone.zone_id);
// If the zone is found (i.e. not null) pass to radio preset function
radio_preset(zone, preset, res);
} else {
msg = "Zone not found";
console.log("Error: " + msg);
res.status(404);
res.send(msg);
}
} else if (req.query.get_presets) {
res.send(presets);
} else if (req.query.get_zones) {
let content = [];
zones.forEach(zone => {
content.push({ name: zone.display_name, id: zone.zone_id });
});
res.send(content);
} else {
msg = "Unsupported query";
console.log("Error: " + msg);
res.status(400);
res.send(msg);
}
res.end();
});
// Start the server
let server_opts = {
"host": "0.0.0.0", // for any/all interfaces, use :: or 0.0.0.0
"port": 33161,
"ipv6Only": false // for IPv4 only, use 0.0.0.0 for host to listen on any/all interfaces; use :: when this is set to true
}
var server = app.listen(server_opts, function() {
let listeners = [];
let status = [];
if (server.address().address == "0.0.0.0" || server.address().address == "::") {
var os = require('os');
var ifaces = os.networkInterfaces();
Object.keys(ifaces).forEach(function (ifname) {
ifaces[ifname].forEach(function (iface) {
if ( (server.address().address == "0.0.0.0" && 'IPv4' !== iface.family) || iface.internal !== false ) {
// Skip localhost and, if listening to IPv4 only, any IPv6 addresses
return;
}
if ( server.address().address == "::" && server_opts.ipv6Only == true && 'IPv6' !== iface.family ) {
// If listening to IPv6 only, skip any IPv4 addresses
return;
}
console.log("Extension server listening on " + iface.address + ":" + server.address().port);
listeners.push(iface.address + ":" + server.address().port);
});
});
} else {
console.log("Extension server listening on " + server.address().address + ":" + server.address().port);
listeners.push(server.address().address + ":" + server.address().port);
}
listeners.forEach(function (listener) {
status.push("API endpoint available at http://"+ listener + "/api");
});
svc_status.set_status(status.join("\n"), false);
});