-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzonos.js
320 lines (269 loc) · 13.4 KB
/
zonos.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
function updateDevicePlaying(device, playing) {
// Update what device is currently playing
// Update room queue
var roomQueue = $('[id="'+device.UDN+'"] .room-queue li');
roomQueue.removeClass('now-playing');
if (!playing.isQueue) {
$(roomQueue).parent().hide();
} else {
$(roomQueue).parent().show();
// Highlight currently playing track
$(roomQueue[playing.queueTrackId]).addClass('now-playing');
// Update track details. When playing from Library (ex. mp3) Sonos
// doesn't know the track details when populating the queue. The
// details become known when the file is read (played).
$(roomQueue[playing.queueTrackId]).find('.artist-name').text(playing.artistName);
$(roomQueue[playing.queueTrackId]).find('.album-title').text(playing.albumTitle);
$(roomQueue[playing.queueTrackId]).find('.track-title').text(playing.trackTitle);
// Scroll to playing track
$(roomQueue).parent().scrollTo($(roomQueue[playing.queueTrackId]),{offset:-45,duration:500});
}
// Currently playing album/radio stream artwork
var nowPlayingAlbumArt = $('[id="'+device.UDN+'"] .room-queue li.now-playing img.album-art');
if (nowPlayingAlbumArt.attr('src') !== undefined) {
// Load artwork from queue
$('#rooms-list li[id="'+device.UDN+'"] img.album-art').attr('src', nowPlayingAlbumArt.attr('src'));
} else {
// Load album artwork from network
// https://developer.mozilla.org/en-US/docs/Web/API/Blob
// http://www.html5rocks.com/en/tutorials/file/xhr2/
var xhr = new XMLHttpRequest();
xhr.open('GET', playing.albumArtURL, true);
xhr.responseType = 'blob';
xhr.onload = function() {
if (this.status == 200) {
var blob = new Blob([this.response], {type: 'image/jpeg'});
var urlObject = URL.createObjectURL(blob);
$('#rooms-list li[id="'+device.UDN+'"] img.album-art').attr('src', urlObject);
$('[id="'+device.UDN+'"] .room-queue li.now-playing img.album-art').attr('src', urlObject);
} else {
$('#rooms-list li[id="'+device.UDN+'"] img.album-art').removeAttr('src');
}
};
xhr.send();
}
// Artist and track information
if (playing.isRadio) {
if (playing.radioShowTitle) {
$('#rooms-list li[id="'+device.UDN+'"] .artist-name').html([playing.radioName,playing.radioShowTitle].join(' - '));
} else {
// Unknown radio show
$('#rooms-list li[id="'+device.UDN+'"] .artist-name').html(playing.radioName);
}
$('#rooms-list li[id="'+device.UDN+'"] .track-title').html(playing.radioCurrentPlaying);
} else {
$('#rooms-list li[id="'+device.UDN+'"] .artist-name').html(playing.artistName);
$('#rooms-list li[id="'+device.UDN+'"] .track-title').html(playing.trackTitle);
}
// Attach currently playing info to room data
$('[id="'+device.UDN+'"].room-info').data('playing', playing);
// Room control buttons
if (playing.state == 'STOPPED' || playing.state == 'PAUSED_PLAYBACK') {
$('[id="'+device.UDN+'"] .room-control .play-button').show();
$('[id="'+device.UDN+'"] .room-control .pause-button').hide();
$('[id="'+device.UDN+'"] .room-control .stop-button').hide();
$('[id="'+device.UDN+'"] .room-control .prev-button').prop('disabled', true);
$('[id="'+device.UDN+'"] .room-control .next-button').prop('disabled', true);
} else { // PLAYING
$('[id="'+device.UDN+'"] .room-control .play-button').hide();
if (playing.isRadio) {
$('[id="'+device.UDN+'"] .room-control .pause-button').hide();
$('[id="'+device.UDN+'"] .room-control .stop-button').show();
$('[id="'+device.UDN+'"] .room-control .prev-button').prop('disabled', true);
$('[id="'+device.UDN+'"] .room-control .next-button').prop('disabled', true);
} else {
$('[id="'+device.UDN+'"] .room-control .stop-button').hide();
$('[id="'+device.UDN+'"] .room-control .pause-button').show();
$('[id="'+device.UDN+'"] .room-control .prev-button').prop('disabled', false);
$('[id="'+device.UDN+'"] .room-control .next-button').prop('disabled', false);
}
}
}
function handlePlayingEvent(device, event) {
var transportState = $(event).find('TransportState').attr('val');
var trackMetadata = $.parseXML($(event).find('CurrentTrackMetadata').attr('val'));
var transportMetadata = $.parseXML($(event).find('r\\:EnqueuedTransportUriMetadata').attr('val'));
var playing = {isRadio:false,isQueue:false};
if ($(trackMetadata).find('res').text().indexOf('x-sonosapi-stream') === 0) {
playing.isRadio = true;
playing.radioName = decodeURIComponent(escape($(transportMetadata).find('title').text()));
if (transportState !== 'TRANSITIONING') {
playing.radioShowTitle = decodeURIComponent(escape($(trackMetadata).find('radioShowMd').text().replace(/\\/g, '').split(',').slice(0,-1).join(',')));
playing.radioCurrentPlaying = decodeURIComponent(escape($(trackMetadata).find('streamContent').text()));
}
} else {
playing.isQueue = true;
playing.artistName = decodeURIComponent(escape($(trackMetadata).find('creator').text()));
playing.albumTitle = decodeURIComponent(escape($(trackMetadata).find('album').text()));
playing.trackTitle = decodeURIComponent(escape($(trackMetadata).find('title').text()));
playing.trackDuration = $(event).find('CurrentTrackDuration').attr('val');
playing.queueTrackId = parseInt($(event).find('currenttrack').attr('val')) - 1; // zero-indexed
}
if ($(trackMetadata).find('albumArtURI').text().length) {
playing.albumArtURL = device.endpointURI+$(trackMetadata).find('albumArtURI').text();
} else {
// TODO: We should have a default artwork
playing.albumArtURL = undefined;
}
playing.state = $(event).find('TransportState').attr('val');
updateDevicePlaying(device, playing);
}
function handleVolumeEvent(device, event) {
var volume = $(event).find('Volume[channel="Master"]').attr('val');
$('[id="'+device.UDN+'"] .room-control .volume-slider').val(volume);
if (volume == 0) {
$('[id="'+device.UDN+'"] .room-control .volume-down').attr('src', '/glyphicons/mute.png');
} else {
$('[id="'+device.UDN+'"] .room-control .volume-down').attr('src', '/glyphicons/volume-down.png');
}
}
function handleQueueEvent(device, event) {
// Queue has changed
// Reload queue
device.callServiceAction('Queue','Browse', {QueueID:0,StartingIndex:0,RequestedCount:0},function(device, result) {
var queue = $.parseXML(result.Result);
// Clear current queue
$('[id="'+device.UDN+'"] .room-queue').empty();
$(queue).find('item').each(function(index) {
var artistName = $(this).find('creator').text();
var albumTitle = $(this).find('album').text();
var trackTitle = $(this).find('title').text();
var li = $('<li>');
li.attr('title', artistName+"\n"+albumTitle+"\n"+trackTitle);
li.append('<img class="album-art">');
li.append('<span class="artist-name">'+artistName+'</span>');
li.append('<span class="album-title">'+albumTitle+'</span>');
li.append('<span class="track-title">'+trackTitle+'</span>');
$('[id="'+device.UDN+'"] .room-queue').append(li);
li.click(function() {
if ($(window).data('windowFocus') === true) return;
if ($(this).hasClass('now-playing')) {
// Click on now playing to play/pause
if ($('[id="'+device.UDN+'"] .pause-button').is(':visible')) {
$('[id="'+device.UDN+'"] .pause-button').trigger('click');
} else {
$('[id="'+device.UDN+'"] .play-button').trigger('click');
}
} else {
// Click to seek track
device.callServiceAction('AVTransport', 'Seek', {InstanceID:0,Unit:'TRACK_NR',Target:$(this).index()+1}, function(){});
}
});
});
// If room is currently playing something toggle now-playing update
var playing = $('[id="'+device.UDN+'"].room-info').data('playing');
if (playing) {
updateDevicePlaying(device, playing);
}
});
}
function handleGroupCoordinatorEvent(device, event) {
if (event.text() === '1') {
$('#rooms-list [id="'+device.UDN+'"]').show();
} else {
// Hide non-coordinator room
$('#rooms-list [id="'+device.UDN+'"]').hide();
}
// Get group information and update group members and coordinator
device.__proto__ = UpnpDevice.prototype;
device.callServiceAction('ZoneGroupTopology', 'GetZoneGroupAttributes', {}, function(device, result) {
var members = result.CurrentZonePlayerUUIDsInGroup.split(',');
var coordinator = members[0];
// Set group coordinator class if there is more than one members in the group
if (members.length > 1) {
$('[id="uuid:'+coordinator+'"]').addClass('group-coordinator');
}
});
}
function showDeviceDetail(device) {
$('div[id="'+device.UDN+'"]').show();
if ($('[id="'+device.UDN+'"] .room-queue li.now-playing').length) {
// Scroll to currently playing item
$('[id="'+device.UDN+'"] .room-queue').scrollTo($('[id="'+device.UDN+'"] .room-queue li.now-playing'),{offset:-45});
}
$('#rooms-list').hide();
}
function addDiscoveredDevice(device) {
$('#searching').hide();
$('#rooms-list').parent().append('<div id="'+device.UDN+'" class="room-info"><input type="image" src="/glyphicons/close.png" class="close-button"><div class="room-control"><span class="room-name">'+device.roomName+'</span><div class="volume-control"><img src="/glyphicons/volume-down.png" class="volume-down"><input type="range" class="volume-slider" min="0" max="100" value="0"><img src="/glyphicons/volume-up.png" class="volume-up"></div><input type="image" src="/glyphicons/prev.png" class="prev-button"><input type="image" src="/glyphicons/play.png" class="play-button"><input type="image" src="/glyphicons/pause.png" class="pause-button"><input type="image" src="/glyphicons/stop.png" class="stop-button"><input type="image" src="/glyphicons/next.png" class="next-button"></div><ol class="room-queue"></ol></div>');
$('div[id="'+device.UDN+'"] .close-button').click(function(){
$('#rooms-list').show();
$('div[id="'+device.UDN+'"]').hide();
});
$('div[id="'+device.UDN+'"] .pause-button').click(function(){
device.callServiceAction('AVTransport', 'Pause', {InstanceID:0}, function(){});
});
$('div[id="'+device.UDN+'"] .play-button').click(function(){
device.callServiceAction('AVTransport', 'Play', {InstanceID:0,'Speed':1}, function(){});
});
$('div[id="'+device.UDN+'"] .stop-button').click(function(){
device.callServiceAction('AVTransport', 'Stop', {InstanceID:0}, function(){});
});
$('div[id="'+device.UDN+'"] .prev-button').click(function(){
device.callServiceAction('AVTransport', 'Previous', {InstanceID:0}, function(){});
});
$('div[id="'+device.UDN+'"] .next-button').click(function(){
device.callServiceAction('AVTransport', 'Next', {InstanceID:0}, function(){});
});
$('div[id="'+device.UDN+'"] .volume-slider').change(function(){
device.callServiceAction('RenderingControl', 'SetVolume', {InstanceID:0,Channel:'Master',DesiredVolume:this.value}, function(){});
});
$('#rooms-list ul').append('<li id="'+device.UDN+'"><div class="now-playing"><img class="album-art"><span class="room-name">'+device.roomName+'</span><span class="artist-name"/><span class="track-title"/></div>');
$('#rooms-list ul li').last().click(function() {
showDeviceDetail(device);
});
}
// Receive messages from background page
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
switch(request.type) {
case 'deviceDiscovery':
// Discovered a new Sonos device.
request.device.__proto__ = UpnpDevice.prototype;
// Hide devices that are not zone group controlling devices.
// For example, in a stereo pair, only one device is controlling
// the group. That device will return a zone group name when
// being queried for group attributes.
request.device.callServiceAction('ZoneGroupTopology', 'GetZoneGroupAttributes', {}, function(device, result) {
if (result.CurrentZoneGroupName) {
addDiscoveredDevice(device);
}
});
break;
case 'deviceEvent':
request.device.__proto__ = UpnpDevice.prototype;
var event = $(request.event);
switch(event.prop('tagName')) {
case 'LASTCHANGE':
change = $(event.text());
if (change.find('TransportState').length) {
handlePlayingEvent(request.device, change);
} else if (change.find('Volume').length) {
handleVolumeEvent(request.device, change);
} else if (change.attr('xmlns') === 'urn:schemas-sonos-com:metadata-1-0/Queue/') {
handleQueueEvent(request.device, change);
} else {
// TODO: Mute event
throw 'Unexpected Sonos Change event: ' + event;
}
break;
case 'GROUPCOORDINATORISLOCAL':
handleGroupCoordinatorEvent(request.device, event);
break;
}
break;
case 'discoveryTimeout':
if ($('.room-info').length == 0) {
$('#searching').html('<img src="/glyphicons/warning.png"/>Your Sonos components could not be found.');
}
break;
}
}
);
$(window).focus(
function(event){
// Prevent events from triggering action when window just recently got focus
$(window).data('windowFocus', true);
setTimeout(function(){$(window).data('windowFocus', false);}, 200);
}
);