Skip to content

Commit

Permalink
Added features for applyPreset and some minor bug changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jishi committed Jan 10, 2015
1 parent 4de0748 commit 893b49c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 145 deletions.
2 changes: 1 addition & 1 deletion lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function parseMetaDataXML(xml) {
nodeValue = uq(str);
});

saxParser.on('startNode', function(elem, attr){
saxParser.on('startNode', function(elem, attr) {
if(elem == "container"){
var attributes = attr();
for(var key in attributes){
Expand Down
179 changes: 36 additions & 143 deletions lib/sonos.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function Discovery(settings) {
"ST: " + SONOS_PLAYER_UPNP_URN].join("\r\n"));

// use random port to allow for multiple instances
var port = 1902 + Math.round(Math.random(Date.now()) * 800);
var port = 1901;
this.log.info("binding SSDP to port", port);

var interfaces = os.networkInterfaces();
Expand Down Expand Up @@ -262,28 +262,6 @@ function Discovery(settings) {
} else {
_this.emit('notify', notifyState);
}
// else if (elem == "FavoritesUpdateID") {
// this.log.info("FavoritesUpdateID", notifyState);
// var player;
// for (var i in _this.players) {
// player = _this.players[i];
// break;
// }

// if (!player) return;

// player.getFavorites(function (success, favorites) {
// if (!success) return;
// _this.emit('favorites', favorites);
// });
// } else if (elem == "ContainerUpdateIDs" && notifyState.body.indexOf('Q:0') > -1) {
// this.log.info("ContainerUpdateIDs", notifyState);
// return;
// if (/uuid:(.+)_sub/.test(notifyState.sid))
// _this.emit('queue-changed', {uuid: RegExp.$1});
// } else {
// notifyState.type = elem;
// }
});

saxParser.on('textNode', function (s, uq) {
Expand All @@ -292,32 +270,6 @@ function Discovery(settings) {

saxParser.parse(buffer.join(""));

// if (notifyState.type == "ZoneGroupState") {

// } else if (notifyState.type == "FavoritesUpdateID") {
// this.log.info("FavoritesUpdateID", notifyState);
// var player;
// for (var i in _this.players) {
// player = _this.players[i];
// break;
// }

// if (!player) return;

// player.getFavorites(function (success, favorites) {
// if (!success) return;
// _this.emit('favorites', favorites);
// });
// } else if (notifyState.type == "ContainerUpdateIDs" && notifyState.body.indexOf('Q:0') > -1) {
// this.log.info("ContainerUpdateIDs", notifyState);
// return;
// if (/uuid:(.+)_sub/.test(notifyState.sid))
// _this.emit('queue-changed', {uuid: RegExp.$1});
// } else {
// // if (notifyState.type == "LastChange")
// // this.log.info(notifyState);
// _this.emit('notify', notifyState);
// }
});
}

Expand Down Expand Up @@ -441,87 +393,6 @@ function Discovery(settings) {

}

// function subscribeToContentDirectory(callback) {
// // TEST
// // ContentDirectory
// var headers = {
// 'TIMEOUT': 'Second-' + subscriptionTimeout
// };

// if (contentDirectorySid) {
// headers['SID'] = contentDirectorySid;
// } else {
// headers['CALLBACK'] = '<http://'+ _this.localEndpoint +':' + _this.notificationPort + '/>';
// headers['NT'] = 'upnp:event';
// }

// var client = http.request({
// host: subscribedTo,
// port: 1400,
// path: '/MediaServer/ContentDirectory/Event',
// method: 'SUBSCRIBE',
// headers: headers
// }, function (res) {

// // Store some relevant headers?


// });

// client.on('error', function (e) {
// _this.log.error(e);
// });

// client.end();
// }

// function subscribeToZoneTopology(callback) {

// var headers = {
// 'TIMEOUT': 'Second-' + subscriptionTimeout
// };

// if (topologySid) {
// headers['SID'] = topologySid;
// } else {
// headers['CALLBACK'] = '<http://'+ _this.localEndpoint +':' + _this.notificationPort + '/>';
// headers['NT'] = 'upnp:event';
// }

// var client = http.request({
// host: subscribedTo,
// port: 1400,
// path: '/ZoneGroupTopology/Event',
// method: 'SUBSCRIBE',
// headers: headers
// }, function (res) {
// _this.log.info(res.headers);
// // Store some relevant headers?
// if (!callback) {
// return;
// }
// if (res.statusCode == 200) {
// setTimeout(subscribeToZoneTopology, subscriptionTimeout * 900);
// callback(true);

// } else {
// topologySid = null;
// // try again in 30 seconds
// setTimeout(subscribeToZoneTopology, 30000);
// callback(false);
// }

// });

// client.on('error', function (e) {
// // If this fails, this player has fallen of the grid
// _this.log.error(e);
// callback && callback(false);
// });

// client.end();
// }

function subscribe(path) {
var headers = {
'TIMEOUT': 'Second-' + subscriptionTimeout
Expand Down Expand Up @@ -703,7 +574,7 @@ Discovery.prototype.applyPreset = function (preset, callback) {
this.zones.forEach(function (i) {
asyncSeries.push(function (callback) {
i.coordinator.pause(function (success) {
callback(null, success);
callback(null, 'pausing ' + i.coordinator.roomName);
});
});
});
Expand Down Expand Up @@ -734,7 +605,7 @@ Discovery.prototype.applyPreset = function (preset, callback) {
// not part of group, should be detached
asyncSeries.push(function (callback) {
player.becomeCoordinatorOfStandaloneGroup(function (success) {
callback(null, success);
callback(null, 'breakout');
});
});
}
Expand All @@ -745,36 +616,36 @@ Discovery.prototype.applyPreset = function (preset, callback) {
this.log.info("ungrouping", coordinator.roomName, "to prepare for grouping");
asyncSeries.push(function (callback) {
coordinator.becomeCoordinatorOfStandaloneGroup(function (success) {
callback(null, success);
callback(null, 'ungrouping');
});
});
}

// Create a callback chain based on the preset
asyncSeries.push(function (callback) {
coordinator.setVolume(coordinatorVolume, function (success) {
callback(null, success);
callback(null, 'set volume, coordinator');
});
});

if (preset.favorite) {
asyncSeries.push(function (callback) {
coordinator.replaceWithFavorite(preset.favorite, function (success) {
callback(null, success);
callback(null, 'applying favorite');
});
});
} else if (preset.uri) {
asyncSeries.push(function (callback) {
coordinator.setAVTransportURI(preset.uri, null, function (success) {
callback(null, success);
callback(null, 'setting uri');
});
});
}

if (preset.playMode) {
asyncSeries.push(function (callback) {
coordinator.setPlayMode(preset.playMode, function (success) {
callback(null, success);
callback(null, 'set playmode');
});
});
}
Expand All @@ -787,11 +658,12 @@ Discovery.prototype.applyPreset = function (preset, callback) {
continue;
}
var streamUrl = "x-rincon:" + coordinator.uuid;
if (player.avTransportUri != streamUrl) {

if (player.uuid != coordinator.uuid && player.avTransportUri != streamUrl) {
asyncSeries.push(function (player, streamUrl) {
return function (callback) {
player.setAVTransportURI(streamUrl, null, function (success) {
callback(success ? null : "error", success);
callback(success ? null : 'error in setAVTransportURI', 'AVTransportURI');
});
}
}(player, streamUrl));
Expand All @@ -800,17 +672,38 @@ Discovery.prototype.applyPreset = function (preset, callback) {
asyncSeries.push(function (player, volume) {
return function (callback) {
player.setVolume(volume, function (success) {
callback(null, success);
callback(null, 'volume for ' + player.roomName);
});
}
}(player, playerInfo.volume));
}

if (preset.trackNo) {
asyncSeries.push( function (callback) {
coordinator.seek(preset.trackNo, function (success) {
// we don't care if this breaks or not.
callback(null, 'seek');
});
});
}

if (preset.elapsedTime) {
asyncSeries.push( function (callback) {
coordinator.trackSeek(preset.elapsedTime, function (success) {
// we don't care if this breaks or not.
callback(null, 'trackSeek');
});
});
}

async.series(asyncSeries, function (err, result) {
if (preset.state != "stopped")
coordinator.play(callback);
if (preset.state && preset.state.toLowerCase() == "playing")
coordinator.play(function (success) {
if (callback instanceof Function)
callback(success ? null : 'error on play', result);
});
else if (callback instanceof Function)
callback(err);
callback(err, result);
});
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sonos-discovery",
"version": "0.14.0",
"version": "0.15.0",
"description": "A simple node module for finding players and controlling them",
"author": "Jimmy Shimizu <[email protected]>",
"repository": {
Expand Down

0 comments on commit 893b49c

Please sign in to comment.