From 1df411d916e73b73faea5787c417e1a39f69bd85 Mon Sep 17 00:00:00 2001 From: Raoul Date: Tue, 8 Sep 2015 09:25:44 +0200 Subject: [PATCH] TESTING NEEDED Proposing idea to de-asynchronize Sonos device discovery with a timeout event for push. I have no IDE at hand right now, so please check syntax before merging! I have no Sonos devices, so please check somebody with the hardware at hand! Thanks Raoul --- platforms/Sonos.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/platforms/Sonos.js b/platforms/Sonos.js index 67d086e..1d19c2f 100644 --- a/platforms/Sonos.js +++ b/platforms/Sonos.js @@ -6,6 +6,8 @@ function SonosPlatform(log, config){ this.config = config; this.name = config["name"]; this.playVolume = config["play_volume"]; + // timeout for device discovery + this.discoveryTimeout = (config.deviceDiscoveryTimeout || 10)*1000; // assume 10sec as a default } SonosPlatform.prototype = { @@ -16,6 +18,18 @@ SonosPlatform.prototype = { // track found devices so we don't add duplicates var roomNamesFound = {}; + // collector array for the devices from callbacks + var devicesFound = []; + // tell the sonos callbacks if timeout already occured + var timeout = false; + + // the timeout event will push the accessories back + setTimeout(function(){ + timeout=true; + callback(devicesFound); + }, this.discoveryTimeout); + + sonos.search(function (device) { that.log("Found device at " + device.host); @@ -26,9 +40,13 @@ SonosPlatform.prototype = { if (!roomNamesFound[roomName]) { roomNamesFound[roomName] = true; that.log("Found playable device - " + roomName); + if (timeout) { + that.log("Ignored: Discovered after timeout (Set deviceDiscoveryTimeout parameter in Sonos section of config.json)"); + } // device is an instance of sonos.Sonos var accessory = new SonosAccessory(that.log, that.config, device, description); - callback([accessory]); + // add it to the collector array + devicesFound.push(accessory); } else { that.log("Ignoring playable device with duplicate room name - " + roomName);