Skip to content
This repository has been archived by the owner on Jun 4, 2023. It is now read-only.

Improve Title Metadata with actual Client name #286

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/airsonos.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
let Promise = require('bluebird');
let sonos = require('sonos');
let DeviceTunnel = require('./tunnel');
let LogicalDevice = require('./logicalDevice');

class AirSonos {

Expand All @@ -10,7 +11,7 @@ class AirSonos {
}

get searchForDevices() {
return Promise.promisify(sonos.LogicalDevice.search);
return Promise.promisify(LogicalDevice.search);
}

start() {
Expand Down
118 changes: 118 additions & 0 deletions lib/logicalDevice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
var async = require('async');
var Sonos = require('sonos').Sonos;
var { DeviceDiscovery } = require('sonos');
var util = require('util');
var url = require('url');

function LogicalDevice(devices, coordinator, groupId) {
if (devices.length === 0) {
throw new Error('Logical device must be initialized with at least one device (' + devices.length + ' given)');
}

var coordinatorDevice = coordinator || devices[0];

Sonos.call(this, coordinatorDevice.host, coordinatorDevice.port);
var sonosDevices = devices.map(function(device) {
return new Sonos(device.host, device.post);
});

this.devices = sonosDevices;
this.groupId = groupId;
}

util.inherits(LogicalDevice, Sonos);

LogicalDevice.prototype.initialize = function(cb) {
async.forEach(this.devices, function(device, done) {
device.on('Volume', volume => device.currentVolume = volume);
done();
}, cb);
};

LogicalDevice.prototype.destroy = function(cb) {
async.forEach(this.devices, function(device, done) {
device.destroy(done);
}, cb);
};

LogicalDevice.prototype.setVolume = function(volume, cb) {
this.getVolume(function(oldVolume) {

var diff = volume - oldVolume;

async.forEach(this.devices, function(device, done) {
var oldDeviceVolume = device.currentVolume;
var newDeviceVolume = oldDeviceVolume + diff;

newDeviceVolume = Math.max(newDeviceVolume, 0);
newDeviceVolume = Math.min(newDeviceVolume, 100);

device.setVolume(newDeviceVolume, done);

}, cb);

}.bind(this));
};

LogicalDevice.prototype.getVolume = function(cb) {
var sum = 0;

this.devices.forEach(function(device) {
sum += device.currentVolume || 0;
});

cb(sum / this.devices.length);
};

/**
* Create a Search Instance (emits 'DeviceAvailable' with a found Logical Sonos Component)
* @param {Function} Optional 'DeviceAvailable' listener (sonos)
* @return {Search/EventEmitter Instance}
*/
var search = function(callback) {
let search = DeviceDiscovery();
search.once('DeviceAvailable', function(device) {
device.getAllGroups().then(groups => {
var logicalDevices = groups.map(group => {
let groupId = group.ID;
let coordinatorId = group.Coordinator;
let coordinator = undefined;

let devices = group.ZoneGroupMember.map(member => {
let parsedLocation = url.parse(member.Location);
let sonos = new Sonos(parsedLocation.hostname, parsedLocation.port);
if (member.UUID === coordinatorId) {
coordinator = sonos;
}

return sonos;
});

return new LogicalDevice(devices, coordinator, groupId);
});

async.forEach(logicalDevices, function(device, done) {
device.initialize(function(err) {
if (err) done(err);
else {
done(null);
}
});
}, function(err) {

if (err) callback(err);
else {
callback(null, logicalDevices);
}
});
}).catch(err => {

callback(err);
});
});

return search;
};

module.exports = LogicalDevice;
module.exports.search = search;
11 changes: 4 additions & 7 deletions lib/tunnel.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ const EMPTY_CALLBACK = () => {};
class DeviceTunnel extends events.EventEmitter {

static createFor(device, options={}) {

const getZoneAttrs = Promise.promisify(device.getZoneAttrs.bind(device));

return getZoneAttrs().then((zoneAttrs) => {
return device.getZoneAttrs().then((zoneAttrs) => {
return new DeviceTunnel(device, zoneAttrs.CurrentZoneName, options);
});
}
Expand All @@ -26,6 +23,7 @@ class DeviceTunnel extends events.EventEmitter {
this.device = device;
this.deviceName = deviceName;
this.options = options;
this.clientName = 'AirSonos';

this.bindAirplayServer();
}
Expand All @@ -38,9 +36,8 @@ class DeviceTunnel extends events.EventEmitter {

this.airplayServer.on('error', this.emit.bind(this, 'error'));

let clientName = 'AirSonos';
this.airplayServer.on('clientNameChange', (name) => {
clientName = `AirSonos @ ${ name }`;
this.clientName = `AirSonos @ ${ name }`;
});

this.airplayServer.on('clientConnected', this.handleClientConnected.bind(this));
Expand Down Expand Up @@ -75,7 +72,7 @@ class DeviceTunnel extends events.EventEmitter {
this.icecastServer.start(0, (port) => {
this.device.play({
uri: `x-rincon-mp3radio://${ ip.address() }:${ port }/listen.m3u`,
metadata: this.generateSonosMetadata(this.deviceName),
metadata: this.generateSonosMetadata(this.clientName),
});
});
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
"babel": "^5.8.21",
"bluebird": "^2.9.34",
"flags": "~0.1.2",
"ip": "1.1.0",
"ip": "1.1.5",
"nicercast": "0.1.0",
"nodetunes": "^0.3.0",
"sonos": "git://github.com/stephen/node-sonos.git#stephen-1.0.0"
"sonos": "1.11.0"
},
"license": "MIT",
"devDependencies": {
Expand Down