Skip to content

Commit

Permalink
Adding a configurable timeout for requests to the bridge, fixess #16
Browse files Browse the repository at this point in the history
  • Loading branch information
osirisoft-pmurray committed Dec 18, 2014
1 parent c2055f9 commit 04bb156
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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 <http://www.apache.org/licenses/LICENSE-2.0>.

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")

6 changes: 2 additions & 4 deletions hue-api/httpPromise.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function _buildOptions(command, parameters) {
body,
urlObj = {
protocol: parameters.ssl ? "https" : "http",
hostname: parameters.host,
hostname: parameters.host
};

// if (parameters.host) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 5 additions & 3 deletions hue-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
};
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-hue-api",
"version": "0.2.3",
"version": "0.2.4",
"author": "Peter Murray <[email protected]>",
"contributors": [
{
Expand Down
15 changes: 15 additions & 0 deletions test/bridge-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
4 changes: 2 additions & 2 deletions test/support/testValues.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 04bb156

Please sign in to comment.