forked from bencevans/soyes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
80 lines (65 loc) · 1.97 KB
/
server.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
/**
* Set your Sonos Zone Player IP Address here
*/
var sonosIp = '192.168.1.68'
/**
* Dependencies
*/
var express = require('express'),
app = express(),
sonos = require('sonos'),
request = require('request'),
_ = require('underscore');
/**
* Express Config
*/
app.use(express.static(__dirname + '/public'));
var devices = {};
sonos.search(function(device) {
device.deviceDescription(function(err, desc) {
devices[desc.UDN] = device;
devices[desc.UDN].description = desc;
device.currentTrack(function(err, track) {
devices[desc.UDN].track = track;
});
});
});
app.get('/devices.json', function(req, res) {
res.send(devices);
});
app.get('/topology.json', function(req, res, next) {
var s = new sonos.Sonos(sonosIp);
s.getTopology(function(err, top) {
res.send(top);
});
});
app.get('/device/:UDN/image.png', function(req, res, next) {
if(!devices[req.params.UDN]) return next();
request('http://' + devices[req.params.UDN].host + ':' + devices[req.params.UDN].port + devices[req.params.UDN].description.iconList.icon[0].url).pipe(res);
});
app.get('/device/:UDN/proxy/*', function(req, res, next) {
var device = devices[req.params.UDN];
if(!device) return next();
request('http://' + device.host + ':' + device.port + '/' + req.params[0] + '?' + require('querystring').stringify(req.query)).pipe(res);
});
app.get('/zone/:UDN', function(req, res, next) {
res.render('index', {
zone: {
devices: [devices[req.params.UDN]],
currentTrack: devices[req.params.UDN].track,
playState: 'playing'
},
devices: devices,
zones: _.map(devices, function(device) {
return {
devices: [ device ],
currentTrack: device.track,
playState: 'playing'
};
})
});
});
app.listen(process.env.PORT || 3000, function() {
console.log('Soyes listening on port ' + (process.env.PORT || 3000));
console.log('Open http://localhost:' + (process.env.PORT || 3000) + ' in your browser');
});