From 0491f9b71ad8b8bf264f6291e14e5269680cb618 Mon Sep 17 00:00:00 2001 From: madmod Date: Sat, 27 Jun 2015 17:48:47 -0600 Subject: [PATCH 1/4] use the percent functions from node-hue-api --- platforms/PhilipsHue.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/platforms/PhilipsHue.js b/platforms/PhilipsHue.js index 7913f80..9467dd4 100644 --- a/platforms/PhilipsHue.js +++ b/platforms/PhilipsHue.js @@ -60,22 +60,15 @@ var execute = function(api, device, characteristic, value) { } } else if (characteristic === "hue") { - value = value/360; - value = value*65535; + value = value*182.5487; value = Math.round(value); state.hue(value); } else if (characteristic === "brightness") { - value = value/100; - value = value*255; - value = Math.round(value); - state.bri(value); + state.brightness(value); } else if (characteristic === "saturation") { - value = value/100; - value = value*255; - value = Math.round(value); - state.sat(value); + state.saturation(value); } api.setLightState(device.id, state, function(err, lights) { if (!err) { @@ -227,7 +220,7 @@ PhilipsHueAccessory.prototype = { supportBonjour: false, manfDescription: "Adjust Saturation of Light", designedMinValue: 0, - designedMaxValue: 255, + designedMaxValue: 100, designedMinStep: 1, unit: "%" } From 39a7550d673b89436654efbd3fbab5cc64ff4686 Mon Sep 17 00:00:00 2001 From: madmod Date: Sat, 27 Jun 2015 19:17:19 -0600 Subject: [PATCH 2/4] bridge discovery (wip) --- platforms/PhilipsHue.js | 83 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 10 deletions(-) diff --git a/platforms/PhilipsHue.js b/platforms/PhilipsHue.js index 9467dd4..2f3e187 100644 --- a/platforms/PhilipsHue.js +++ b/platforms/PhilipsHue.js @@ -60,7 +60,7 @@ var execute = function(api, device, characteristic, value) { } } else if (characteristic === "hue") { - value = value*182.5487; + value = value * 182.5487; // Convert degrees to 0-65535 range value = Math.round(value); state.hue(value); } @@ -80,6 +80,55 @@ var execute = function(api, device, characteristic, value) { }); }; + +// Get the ip address of the first available bridge with meethue.com or a network scan. +var locateBridge = function (callback) { + // Report the results of the scan to the user + var getIp = function (err, bridges) { + if (!bridges || bridges.length === 0) { + this.log("No Philips Hue bridges found."); + callback(err || new Error("No bridges found")); + return; + } + + if (bridges.length > 1) { + this.log("Warning: Multiple Philips Hue bridges detected. The first bridge will be used automatically. To use a different bridge set ip_address manually in configuration."); + } + + this.log( + "Philips Hue bridges found:", + bridges.map(function (bridge) { + // Bridge name is only returned from meethue.com so use id instead if it isn't there + return '\t' + (bridge.name || bridge.id) + bridge.ipaddress + '\n'; + }) + ); + + callback(null, bridges[0].ipaddress); + }; + + // Try to discover the bridge ip using meethue.com + this.log("Attempting to discover Philips Hue bridge with network scan."); + api.locateBridges(function (locateError, bridges) { + if (locateError) { + this.log("Philips Hue bridge discovery with meethue.com failed. Register your bridge with the meethue.com for more reiable discovery."); + + this.log("Attempting to discover Philips Hue bridge with network scan."); + + api.searchForBridges(function (searchError, bridges) { + if (err) { + this.log("Philips Hue bridge discovery with network scan failed. Check your network connection or set ip_address manually in configuration."); + getIp(new Error("Scan failed")); + } else { + getIp(null, bridges); + } + }); + } else { + getIp(null, bridges); + } + }); +}; + + PhilipsHuePlatform.prototype = { accessories: function(callback) { this.log("Fetching Philips Hue lights..."); @@ -87,17 +136,31 @@ PhilipsHuePlatform.prototype = { var that = this; var foundAccessories = []; - var api = new HueApi(this.ip_address, this.username); + var getLights = function () { + var api = new HueApi(that.ip_address, that.username); - // Connect to the API and loop through lights - api.lights(function(err, response) { - if (err) throw err; - response.lights.map(function(device) { - var accessory = new PhilipsHueAccessory(that.log, device, api); - foundAccessories.push(accessory); + // Connect to the API and loop through lights + api.lights(function(err, response) { + if (err) throw err; + response.lights.map(function(device) { + var accessory = new PhilipsHueAccessory(that.log, device, api); + foundAccessories.push(accessory); + }); + callback(foundAccessories); }); - callback(foundAccessories); - }); + }; + + // Discover the bridge if needed + if (!this.ip_address) { + locateBridge.call(this, function (err, ip_address) { + // TODO: Find a way to persist this + that.ip_address = ip_address; + that.log("Save the Philips Hue bridge ip address "+ ip_address +" to your config to skip discovery."); + getLights(); + }); + } else { + getLights(); + } } }; From 479dfb63295ca823c64818405d3278b48c84a77d Mon Sep 17 00:00:00 2001 From: madmod Date: Sat, 27 Jun 2015 19:27:39 -0600 Subject: [PATCH 3/4] scope and name fixes removed deprecated node-hue-api methods and change this to that. --- platforms/PhilipsHue.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/platforms/PhilipsHue.js b/platforms/PhilipsHue.js index 2f3e187..4ebeafc 100644 --- a/platforms/PhilipsHue.js +++ b/platforms/PhilipsHue.js @@ -83,19 +83,21 @@ var execute = function(api, device, characteristic, value) { // Get the ip address of the first available bridge with meethue.com or a network scan. var locateBridge = function (callback) { + var that = this; + // Report the results of the scan to the user var getIp = function (err, bridges) { if (!bridges || bridges.length === 0) { - this.log("No Philips Hue bridges found."); + that.log("No Philips Hue bridges found."); callback(err || new Error("No bridges found")); return; } if (bridges.length > 1) { - this.log("Warning: Multiple Philips Hue bridges detected. The first bridge will be used automatically. To use a different bridge set ip_address manually in configuration."); + that.log("Warning: Multiple Philips Hue bridges detected. The first bridge will be used automatically. To use a different bridge set ip_address manually in configuration."); } - this.log( + that.log( "Philips Hue bridges found:", bridges.map(function (bridge) { // Bridge name is only returned from meethue.com so use id instead if it isn't there @@ -107,16 +109,16 @@ var locateBridge = function (callback) { }; // Try to discover the bridge ip using meethue.com - this.log("Attempting to discover Philips Hue bridge with network scan."); - api.locateBridges(function (locateError, bridges) { + that.log("Attempting to discover Philips Hue bridge with network scan."); + hue.nupnpSearch(function (locateError, bridges) { if (locateError) { - this.log("Philips Hue bridge discovery with meethue.com failed. Register your bridge with the meethue.com for more reiable discovery."); + that.log("Philips Hue bridge discovery with meethue.com failed. Register your bridge with the meethue.com for more reiable discovery."); - this.log("Attempting to discover Philips Hue bridge with network scan."); + that.log("Attempting to discover Philips Hue bridge with network scan."); - api.searchForBridges(function (searchError, bridges) { + hue.upnpSearch(function (searchError, bridges) { if (err) { - this.log("Philips Hue bridge discovery with network scan failed. Check your network connection or set ip_address manually in configuration."); + that.log("Philips Hue bridge discovery with network scan failed. Check your network connection or set ip_address manually in configuration."); getIp(new Error("Scan failed")); } else { getIp(null, bridges); From 8b71a1c44f7eeeb585ba8f7c0a781058e92dbbb2 Mon Sep 17 00:00:00 2001 From: madmod Date: Sat, 27 Jun 2015 20:23:32 -0600 Subject: [PATCH 4/4] fix network scan and logging --- platforms/PhilipsHue.js | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/platforms/PhilipsHue.js b/platforms/PhilipsHue.js index 4ebeafc..1723725 100644 --- a/platforms/PhilipsHue.js +++ b/platforms/PhilipsHue.js @@ -98,32 +98,36 @@ var locateBridge = function (callback) { } that.log( - "Philips Hue bridges found:", - bridges.map(function (bridge) { + "Philips Hue bridges found:\n" + + (bridges.map(function (bridge) { // Bridge name is only returned from meethue.com so use id instead if it isn't there - return '\t' + (bridge.name || bridge.id) + bridge.ipaddress + '\n'; - }) + return "\t" + bridge.ipaddress + ' - ' + (bridge.name || bridge.id); + })).join("\n") ); callback(null, bridges[0].ipaddress); }; // Try to discover the bridge ip using meethue.com - that.log("Attempting to discover Philips Hue bridge with network scan."); + that.log("Attempting to discover Philips Hue bridge with meethue.com..."); hue.nupnpSearch(function (locateError, bridges) { if (locateError) { that.log("Philips Hue bridge discovery with meethue.com failed. Register your bridge with the meethue.com for more reiable discovery."); - that.log("Attempting to discover Philips Hue bridge with network scan."); + that.log("Attempting to discover Philips Hue bridge with network scan..."); + + // Timeout after one minute + hue.upnpSearch(60000) + .then(function (bridges) { + that.log("Scan complete") - hue.upnpSearch(function (searchError, bridges) { - if (err) { - that.log("Philips Hue bridge discovery with network scan failed. Check your network connection or set ip_address manually in configuration."); - getIp(new Error("Scan failed")); - } else { getIp(null, bridges); - } - }); + }) + .fail(function (scanError) { + that.log("Philips Hue bridge discovery with network scan failed. Check your network connection or set ip_address manually in configuration."); + + getIp(new Error("Scan failed: " + scanError.message)); + }).done(); } else { getIp(null, bridges); } @@ -155,6 +159,8 @@ PhilipsHuePlatform.prototype = { // Discover the bridge if needed if (!this.ip_address) { locateBridge.call(this, function (err, ip_address) { + if (err) throw err; + // TODO: Find a way to persist this that.ip_address = ip_address; that.log("Save the Philips Hue bridge ip address "+ ip_address +" to your config to skip discovery.");