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

Apply Airsonos Update #168

Closed
sjbiss opened this issue May 16, 2015 · 3 comments
Closed

Apply Airsonos Update #168

sjbiss opened this issue May 16, 2015 · 3 comments

Comments

@sjbiss
Copy link

sjbiss commented May 16, 2015

I just installed Airsonos and it works great. The only issue I have found thus far is that my speakers show up twice, obviously very minor but I see a code fix has been posted (fix 114) but I have no idea how to apply the code to my machine. Can someone please help me out? Below is the code:

"use strict";

var sonos = require('sonos');
var portastic = require('portastic');
var ip = require('ip');
var NodeTunes = require('nodetunes');
var Nicercast = require('nicercast');
var flags = require('flags');

flags.defineBoolean('diagnostics', false, 'run diagnostics utility');
flags.defineBoolean('version', false, 'return version number');
flags.defineInteger('timeout', 5, 'disconnect timeout (in seconds)');
flags.defineBoolean('verbose', false, 'show verbose output');
flags.parse();

var airplayEnabledDevices = [];

var uniqueDeviceIdFromDevice = function(device) {
return device.host+':'+device.port;
}

var isDeviceAirplayEnabled = function(device){
return (airplayEnabledDevices.indexOf(uniqueDeviceIdFromDevice(device)) >= 0);
}

var addDeviceToEnabledList = function(device){
if (!isDeviceAirplayEnabled(uniqueDeviceIdFromDevice(device))) {
airplayEnabledDevices.push(uniqueDeviceIdFromDevice(device));
}
}

// Commented out while not in use until some event for Sonos device disappeared are available
/var removeDeviceFromEnabledList = function(device){
if (isDeviceAirplayEnabled(uniqueDeviceIdFromDevice(device))) {
airplayEnabledDevices.splice(airplayEnabledDevices.indexOf(uniqueDeviceIdFromDevice(device)),1);
}
}
/

if (flags.get('version')) {

var pjson = require('../package.json');
console.log(pjson.version);

} else if (flags.get('diagnostics')) {

var diag = require('./diagnostics');
diag();

} else {

console.log('Searching for Sonos devices on network...');
sonos.LogicalDevice.search(function(err, devices) {
devices.forEach(function(device) {

  // Avoid duplicates
  if (isDeviceAirplayEnabled(device)) {
    if (flags.get('verbose')) 
      console.log('Found Sonos device:', '{' + device.host + ':' + device.port + '}', 'seen before - skipping it');
    return;
  } else {
    addDeviceToEnabledList(device);
  }

  device.getZoneAttrs(function(err, zoneAttrs) {
    if (err) throw err;

    var deviceName = zoneAttrs.CurrentZoneName;

    console.log('Setting up AirSonos for', deviceName, '{' + device.host + ':' + device.port + '}');

    var airplayServer = new NodeTunes({
      serverName: deviceName + ' (AirSonos)',
      verbose: flags.get('verbose'),
      controlTimeout: flags.get('timeout')
    });

    var clientName = 'AirSonos';
    airplayServer.on('clientNameChange', function(name) {
      clientName = 'AirSonos @ ' + name;
    });

    airplayServer.on('error', function(err) {
      if (err.code === 415) {
        console.error('Warning!', err.message);
        console.error('AirSonos currently does not support codecs used by applications such as iTunes or AirFoil.');
        console.error('Progress on this issue: https://github.com/stephen/nodetunes/issues/1');
      } else {
        console.error('Unknown error:');
        console.error(err);
      }
    })

    airplayServer.on('clientConnected', function(audioStream) {

      portastic.find({
        min : 8000,
        max : 8050,
        retrieve: 1
      }, function(err, port) {
        if (err) throw err;

        var icecastServer = new Nicercast(audioStream, {
          name: 'AirSonos @ ' + deviceName
        });

        airplayServer.on('metadataChange', function(metadata) {
          if (metadata.minm)
            icecastServer.setMetadata(metadata.minm + (metadata.asar ? ' - ' + metadata.asar : '') + (metadata.asal ? ' (' + metadata.asal +  ')' : ''));
        });

        airplayServer.on('clientDisconnected', function() {
          icecastServer.stop();
        });

        icecastServer.start(port);

        device.play({
          uri: 'x-rincon-mp3radio://' + ip.address() + ':' + port + '/listen.m3u',
          metadata: '<?xml version="1.0"?>' +
            '<DIDL-Lite xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:r="urn:schemas-rinconnetworks-com:metadata-1-0/" xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/">' +
            '<item id="R:0/0/49" parentID="R:0/0" restricted="true">' +
            '<dc:title>' + clientName + '</dc:title>' +
            '<upnp:class>object.item.audioItem.audioBroadcast</upnp:class>' +
            '<desc id="cdudn" nameSpace="urn:schemas-rinconnetworks-com:metadata-1-0/">SA_RINCON65031_</desc>' +
            '</item>' +
            '</DIDL-Lite>'
        });

      });
    });

    airplayServer.on('clientDisconnected', function() {
      device.stop(function() {});
    });

    airplayServer.on('volumeChange', function(vol) {
      vol = 100 - Math.floor(-1 * (Math.max(vol, -30) / 30) * 100);
      device.setVolume(vol, function() {
        // ?
      });
    });

    airplayServer.start();

  });
});

});
}

Thanks

@awolfend
Copy link

Mine would show up 3 times.

@jeeftor
Copy link

jeeftor commented May 30, 2015

You have the following options:

  1. When somebody makes a code request they create a "pull request" these requests are basically a different version of the code that the developer is requesting the maintainer "pull" into the archive - you can check out one of these branches and build it you self (https://help.github.com/articles/checking-out-pull-requests-locally/)

  2. You can check out the source yourself and apply the fix manually

  3. Wait for the change to be added to the repo

@stephen
Copy link
Owner

stephen commented Aug 9, 2015

This should be resolved now.

@stephen stephen closed this as completed Aug 9, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants