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

Commit

Permalink
add error handling for individual devices, prevent crash if one devic…
Browse files Browse the repository at this point in the history
…e throws error

* individual setups should run in a domain, so we can catch errors individually
* add feature to README.md
  • Loading branch information
iona5 committed May 23, 2015
1 parent f90c76d commit b08132c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ When this option is set, airsonos skips teh autodiscovery part and tries to conn
specified in <device_list>. A Sonos device is specified by its IP.
Optionally you can set also its TCP port if the device is set to not use the default port (1400).

As long as airsonos is able to connect to at least one device, it will not end itself.

This is the syntax:
```
airsonos --devices SONOS1_IP[:SONOS1_PORT][,SONOS2_IP[:SONOS2_PORT][,...]]
Expand Down
28 changes: 23 additions & 5 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,35 @@ if (flags.get('version')) {
diag();

} else if (flags.get('devices').length > 0) {
var useDevices = flags.get('devices');
for (var i = 0; i < useDevices.length; i++) {
var ipPortSplit = useDevices[i].split(':');

var deviceArguments = flags.get('devices');
for(var i = 0; i < deviceArguments.length; i++) {

var deviceArg = deviceArguments[i];

// set up a domain to catch errors from setting up individual
// devices. For example it catches the timeout when non-existing IPs
// are entered.
var setupDomain = require('domain').create();
setupDomain.on('error', function(err) {
console.error('Error on setting up device "' + deviceArg + '": ' + err);
});

var sonosDevice;

// Check if the argument contains a port specifier
var ipPortSplit = deviceArg.split(':');
if (ipPortSplit.length > 1) {
sonosDevice = new sonos.Sonos(ipPortSplit[0], ipPortSplit[1]);
} else {
sonosDevice = new sonos.Sonos(useDevices[i]);
sonosDevice = new sonos.Sonos(deviceArg);
}

setupSonos(sonosDevice, 'Sonos' + i);
// run the setup in a domain so we can catch errors for each device
// individually:
setupDomain.run(function() {
setupSonos(sonosDevice);
});
}

} else {
Expand Down

0 comments on commit b08132c

Please sign in to comment.