From 04bb156ee7084e6c06055b4f7a2d4ceb3b11f9f6 Mon Sep 17 00:00:00 2001 From: Peter Murray Date: Thu, 18 Dec 2014 20:09:35 +0000 Subject: [PATCH] Adding a configurable timeout for requests to the bridge, fixess #16 --- Changelog.md | 3 +++ README.md | 28 ++++++++++++++++++++++++---- hue-api/httpPromise.js | 6 ++---- hue-api/index.js | 8 +++++--- package.json | 2 +- test/bridge-connect.js | 15 +++++++++++++++ test/support/testValues.js | 4 ++-- 7 files changed, 52 insertions(+), 14 deletions(-) diff --git a/Changelog.md b/Changelog.md index 7ee0792..2cc9061 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,8 @@ # Change Log +## 0.2.4 +- Added ability to configure the timeout when communicating with the Hue Bridge + ## 0.2.3 - Updated endpoint for hue discovery to use https - Swapped out q-io http for request 2.36.0 diff --git a/README.md b/README.md index a634260..7fc53fd 100644 --- a/README.md +++ b/README.md @@ -1460,6 +1460,30 @@ If the deletion was successful, then ``true`` will be returned from the promise, as in the case if the schedule does not exist. +## Advanced Options + +If there are issues with the Bridge not responding in time for a result of error to be delivered, then you +may need to tweak the timeout settings for the API. When this happens you will get an +`ETIMEOUT` error. + +The way to set a maximum timeout when interacting with the bridge is done when you instantiate the ``HueApi``. + +```js +var hue = require("node-hue-api"), + HueApi = hue.HueApi; + +var host = "192.168.2.129", + username = "08a902b95915cdd9b75547cb50892dc4", + timeout = 20000 // timeout in milliseconds + api; + +api = new HueApi(host, password, timeout); +``` + +The default timeout, when onw is not specified will be 10000ms (10 seconds). This is usually enough time for the bridge +to respond unless you are returning a very large result (like the complete state for the bridge in a large installation) + + ## License Copyright 2013. All Rights Reserved. @@ -1468,7 +1492,3 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use You may obtain a copy of the License at . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - - -[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/peter-murray/node-hue-api/trend.png)](https://bitdeli.com/free "Bitdeli Badge") - diff --git a/hue-api/httpPromise.js b/hue-api/httpPromise.js index 4670cad..bea09b6 100644 --- a/hue-api/httpPromise.js +++ b/hue-api/httpPromise.js @@ -40,7 +40,7 @@ function _buildOptions(command, parameters) { body, urlObj = { protocol: parameters.ssl ? "https" : "http", - hostname: parameters.host, + hostname: parameters.host }; // if (parameters.host) { @@ -51,6 +51,7 @@ function _buildOptions(command, parameters) { // throw new Error("A host name must be provided in the parameters"); // } + options.timeout = parameters.timeout || 10000; options.method = command.method || "GET"; if (command.getPath) { @@ -82,11 +83,8 @@ function _buildOptions(command, parameters) { if (parameters.ssl) { options.ssl = parameters.ssl; -// options.strictSSL = false; } - options.timeout = 10000; //TODO make adjustable - return options; } diff --git a/hue-api/index.js b/hue-api/index.js index 7eff62e..4fb7a7f 100644 --- a/hue-api/index.js +++ b/hue-api/index.js @@ -12,9 +12,10 @@ var Q = require("q"), bridgeDiscovery = require("./bridge-discovery"); -function HueApi(host, username) { +function HueApi(host, username, timeout) { this.host = host; this.username = username; + this.timeout = timeout || 10000; } module.exports = HueApi; @@ -842,13 +843,14 @@ function _errorPromise(message) { * Creates a default options object for connecting with a Hue Bridge. * * @param api The api that contains the username and host for the bridge. - * @returns {{host: *, username: *}} + * @returns {{host: *, username: *, timeout: *}} * @private */ function _defaultOptions(api) { return { "host" : api.host, - "username": api.username + "username": api.username, + "timeout": api.timeout }; } diff --git a/package.json b/package.json index 9a704ae..58d1a25 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-hue-api", - "version": "0.2.3", + "version": "0.2.4", "author": "Peter Murray ", "contributors": [ { diff --git a/test/bridge-connect.js b/test/bridge-connect.js index c03355e..0cb0295 100644 --- a/test/bridge-connect.js +++ b/test/bridge-connect.js @@ -32,4 +32,19 @@ describe("Hue API", function () { hue.connect().fail(failureCheck).done(); }); }); + + describe("#creation", function() { + + it("should have a default timeout", function(){ + var hue = new api.HueApi(testValues.host, testValues.username); + + expect(hue).to.have.property("timeout", 10000); + }); + + it("should have a non-default timeout if specified", function(){ + var hue = new api.HueApi(testValues.host, testValues.username, 30000); + + expect(hue).to.have.property("timeout", 30000); + }); + }); }); \ No newline at end of file diff --git a/test/support/testValues.js b/test/support/testValues.js index d9789cb..a7f1674 100644 --- a/test/support/testValues.js +++ b/test/support/testValues.js @@ -1,7 +1,7 @@ module.exports = { - host : "192.168.2.158", + host : "192.168.2.245", username : "08a902b95915cdd9b75547cb50892dc4", - lightsCount : 8, + lightsCount : 16, locateTimeout: 7000, maxScheduleNameLength: 32, testLightId: 4