-
Notifications
You must be signed in to change notification settings - Fork 5
/
sonospowermate.js
386 lines (341 loc) · 10.2 KB
/
sonospowermate.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* sonospowermate.js
* control a sonos speaker (or group that speaker belongs to)
* and monitor its play status with the led ring
*
*/
'use strict'
var SonosDiscovery = require('sonos-discovery'),
discovery = new SonosDiscovery(),
PowerMate = require('node-powermate'),
powermate = new PowerMate(),
download = require('url-download'),
fs = require('fs'),
path = require('path'),
http = require('http'),
util = require('util'),
os = require('os');
// Get the LED strobing while we're discovering the Sonos topology
powermate.setPulseSpeed(511);
powermate.setPulseAwake(true);
var faves;
var favIndex=-1;
var favCounter=20;
var canDelta=true;
var favServer;
var inFaves=false;
var canDelta=true;
var favTimer;
var favURI;
var favTrack;
var player;
// Get our ip addressand make sure our audio container directory exists
initialize();
// Wait until the Sonos discovery process is done, then grab our player
discovery.on('topology-change', function() {
if (!player)
grabPlayer();
})
// Here we're going to look for favorites changes
discovery.on('favorites', function(favorites) {
faves = [];
for (var i = 0; i < favorites.length; i++) {
faves.push(favorites[i].title);
}
// And go get their tts audio
getFaveAudio(0);
});
discovery.on('transport-state', function(msg) {
if (msg.uuid == player.coordinator.uuid) {
// And if we've paused, turn the LED off
if (msg.state.zoneState == "PAUSED_PLAYBACK" || msg.state.zoneState == "STOPPED") powermate.setBrightness(0);
// Anf if we've played, turn the LED on
else if (msg.state.zoneState == "PLAYING") powermate.setBrightness(255);
}
});
var dblClickTimer;
var pressTimer;
var isDown = false;
// Grab the three events our powermate sends, and turn them into gestures
powermate.on('buttonDown', function() {
isDown = true;
// If we hold the button down for more than 1 second, let's call it a long press....
pressTimer = setTimeout(longClick, 1000);
});
powermate.on('buttonUp', function() {
isDown = false;
// If the timer is still going call it a short click
if (pressTimer._idleNext) {
if (dblClickTimer && dblClickTimer._idleNext) {
clearTimeout(dblClickTimer);
doubleClick();
}
else {
dblClickTimer=setTimeout(singleClick,500);
}
}
clearTimeout(pressTimer);
});
powermate.on('wheelTurn', function(delta) {
clearTimeout(pressTimer);
// This is a right turn
if (delta > 0) {
if (isDown) downRight(); // While down
else right(delta); // while up
}
// Left
if (delta < 0) {
if (isDown) downLeft(); //down
else left(delta); // up
}
});
// Our gesstures section
var commandReady = true;
var commandTimer;
// We got a single click..
function singleClick() {
// If we aren't in favorites mode, toggle the Sonos' play status
if (!inFaves)
togglePlay();
// Otherwise, close the favorites server down, and play the selected favorite
else {
clearTimeout(dblClickTimer);
dblClickTimer = null;
favServer.close(function() {
playFavorite(favIndex);
powermate.setPulseAwake(false);
inFaves=false;
favCounter=20;
favIndex=-1;
favServer=null;
});
}
}
// If we're playing, go to the next track, otherwise, if we're not in fav
// mode, go into it, and if we are in fav mode, exit it
function doubleClick() {
if (isPlaying()) {
player.coordinator.nextTrack();
}
else if (!inFaves) {
enterFavorites();
}
else if (inFaves) {
exitFaves();
}
}
// Previous track
function longClick() {
if (isPlaying())
player.coordinator.previousTrack();
}
// Turn up the group volume
function right(delta) {
if (commandReady && isPlaying() && !inFaves) {
commandReady = false;
player.coordinator.groupSetVolume('+2');
commandTimer = setTimeout(function() {
commandReady = true;
}, 100);
}
else if (inFaves) {
favTurn(delta);
}
}
// Turn down the group volume
function left(delta) {
if (commandReady && isPlaying() && !inFaves) {
commandReady = false;
player.coordinator.groupSetVolume('-2');
commandTimer = setTimeout(function() {
commandReady = true;
}, 100);
}
else if (inFaves) {
favTurn(delta);
}
}
// Turn up zone player volume
function downRight() {
if (commandReady && isPlaying()) {
commandReady = false;
player.setVolume('+2');
commandTimer = setTimeout(function() {
commandReady = true;
}, 100);
}
}
// Turn down zone player volume
function downLeft() {
if (commandReady && isPlaying()) {
commandReady = false;
player.setVolume('-2');
commandTimer = setTimeout(function() {
commandReady = true;
}, 100);
}
}
// Toggle the playing state of the player
function togglePlay() {
clearTimeout(dblClickTimer);
dblClickTimer = null;
if (isPlaying()) {
player.coordinator.pause();
} else {
player.coordinator.play();
}
}
function grabPlayer() {
player = discovery.getPlayer('family room');
if (!player) return;
// grabFavorites();
powermate.setPulseAwake(false);
// Figure out if our player is playing. If so, turn the LED on
if (isPlaying()) {
powermate.setBrightness(255);
// Otherwise turn it off
} else {
powermate.setBrightness(0);
}
faves = [];
player.getFavorites(function(success, favorites) {
if (!success) return;
for (var i = 0; i < favorites.length; i++) {
faves.push(favorites[i].title);
}
// getFaveAudio(0);
});
}
function isPlaying() {
return player.coordinator.state['currentState'] == "PLAYING";
}
// Replace the queue with the currently selected favorite (as determined by the index into the favorites array)
function playFavorite(index) {
clearTimeout(favTimer);
player.coordinator.replaceWithFavorite(faves[index], function(success) {
if (success)
player.coordinator.play();
else {
console.log('didnt find it');
}
});
}
// Grab text-to-speech audio for our favorites from voicerss.com
function getFaveAudio(index) {
if (index == 0) deleteFavesAudio();
if (!faves[index]) return;
var link = voiceRssLink('put your key here', faves[index]);
download(link, getUserHome() + '.sonospowermate/sound/', {outputName: index +'.mp3'})
.on('close', function () {
getFaveAudio(index + 1);
})
.on('invalid', function (e) {
console.log('Bad URL: ' + e );
getFaveAudio(index + 1);
})
.on('error', function (e) {
console.log('Couldn\'t download: ' + e );
getFaveAudio(index + 1);
});
}
function voiceRssLink(key, text) {
return('http://api.voicerss.org/?key=' + encodeURIComponent(key) + '&hl=en-us&f=16khz_8bit_mono&src=' + encodeURIComponent(text));
}
// Let's delete everything in the audio directory, because who know what happened while we weren't running,
// so we should start from scratch
function deleteFavesAudio() {
fs.readdirSync(getUserHome() + ".sonospowermate/sound").forEach(function(fileName) {
fs.unlinkSync(getUserHome() + ".sonospowermate/sound/" + fileName);
});
}
function getUserHome() {
return process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'] + path.sep;
}
function enterFavorites() {
resetFavTimer();
favTurn(1);
inFaves=true;
powermate.setPulseSpeed(511);
powermate.setPulseAwake(true);
favURI=player.avTransportUri;
favTrack=player.state.trackNo;
// Create the server that will listen for call from the Sonos, and server up the appropriate favorites
// audio file
favServer=http.createServer(function(request, response) {
var filePath = getUserHome()+'.sonospowermate/sound/'+request.url.replace('/','');
if (fs.existsSync(filePath)) {
var stat = fs.statSync(filePath);
response.writeHead(200, {
'Content-Type': 'audio/mpeg',
'Content-Length': stat.size
});
var readStream = fs.createReadStream(filePath);
readStream.pipe(response);
}
else {
response.writeHead(200, {
'Content-Type': 'audio/mpeg',
'Content-Length': 0
});
response.end("");
}
})
.listen(2000);
favServer.on('error',function(msg){
console.log(msg);
});
}
function favTurn(delta) {
resetFavTimer();
if (canDelta && faves) favCounter += delta;
if (favCounter > 10) {
favCounter=0;
favIndex++;
if (favIndex > faves.length-1) favIndex=0;
}
else if (favCounter < -10) {
favCounter=0
favIndex--;
if (favIndex < 0) favIndex=faves.length-1;
}
else return;
canDelta=false;
player.coordinator.setAVTransportURI('http://'+discovery.localEndpoint+':2000/'+favIndex+'.mp3','',function(success) {
player.coordinator.play(function() {
canDelta=true;
});
});
}
function resetFavTimer() {
clearTimeout(favTimer);
favTimer=setTimeout(exitFaves,15000);
}
function exitFaves() {
player.setAVTransportURI(favURI,'',function(success) {
player.seek(favTrack,function() {
favServer.close(function() {
clearTimeout(favTimer);
powermate.setPulseAwake(false);
inFaves=false;
favCounter=20;
favIndex=-1;
favServer=null;
});
});
});
}
function initialize() {
// See if our favorites sounds directory exists, and create it if it doesn't
if (!fs.existsSync(getUserHome()+'.sonospowermate/sound')) {
if (!fs.existsSync(getUserHome()+'.sonospowermate')) {
fs.mkdir(getUserHome()+'.sonospowermate',function() {
fs.mkdir(getUserHome()+'.sonospowermate/sound',function() {
});
});
}
else {
fs.mkdir(getUserHome()+'.sonospowermate/sound',function() {
});
}
}
}