Skip to content

Commit

Permalink
Added discovery functionality.
Browse files Browse the repository at this point in the history
Fixes Mik13#5
  • Loading branch information
Mik13 committed Jan 11, 2019
1 parent 2e0b0a5 commit 3a530d0
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 4 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ An API for Nuki Bridge
## How it works

### Get Bridge Connection
``` js
```js
var NukiBridgeApi = require('nuki-bridge-api');

var ip = '127.0.0.1';
Expand All @@ -14,8 +14,20 @@ var token = 'token';
var bridge = new NukiBridgeApi.Bridge(ip, port, token);
```

### Bridge Discovery
```js
var NukiBridgeApi = require('nuki-bridge-api');

var bridge = NukiBridgeApi.DiscoveredBridge.discover().then(function connectNow (bridges) {
// Connect to a bridge
return bridges[0].connect();
}).then(function gotRealBridge (bridge) {
// Do something with the bridge
});
```

### Get Nukis
``` js
```js
var bridge = new NukiBridgeApi.Bridge(ip, port, token);

bridge.list().then(function gotNukis (nukis) {
Expand Down Expand Up @@ -81,7 +93,7 @@ nuki.addCallback('localhost', 12321, true).then(function gotCallbackRegistered (
});
```

###Callbacks
### Callbacks
The callbacks which you get with `getCallbacks` or `addCallback` have the functions
`remove` which removes the callback from the bridge (and closes the webserver which
gets created when you call `addCallback` with `listen=true`) and `startListen` which
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
var Bridge = require('./lib/bridge');
var DiscoveredBridge = require('./lib/bridge-discovery');
var lockAction = require('./lib/lock-action');
var lockState = require('./lib/lock-state');

module.exports = {
Bridge: Bridge,
DiscoveredBridge: DiscoveredBridge,
lockAction: lockAction,
lockState: lockState
};
};
97 changes: 97 additions & 0 deletions lib/bridge-discovery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
var request = require('request-promise');
var Bridge = require('./bridge');
var Promise = require('bluebird');

/**
* The constructor for a discovered bridge. Authentication is not done yet ({@see DiscoveredBridge.connect}).
*
* @class DiscoveredBridge
* @param {String} ip the ip of the bridge
* @param {String} port the port of the bridge
* @returns {DiscoveredBridge}
* @constructor
*/
var DiscoveredBridge = function DiscoveredBridge (ip, port) {
if (!(this instanceof DiscoveredBridge)) {
return new DiscoveredBridge(ip, port);
}

this.ip = ip;
this.port = port;
};

/**
* Connects to a discovered bridge.
* If you call without token, you will have to push the button on the bridge to gain access.
*
* @param {String} [token] if given, will use this one instead of requesting auth
* @returns {Promise<Bridge>}
*/
DiscoveredBridge.prototype.connect = function connect (token) {
const self = this;

if (token) {
return Promise.resolve(new Bridge(this.ip, this.port, token));
}

return this
._request('auth')
.then(function getToken (response) {
if (response.success) {
return new Bridge(self.ip, self.port, response.token);
}

throw new Error('connect to discovered bridge not successful: ' + JSON.stringify(response));
});
};

/**
* This function requests an action.
*
* @param {String} action the name of the action
* @param {String} [additionalParameter] additional get parameters
* @returns {Promise<Object>}
* @private
*/
DiscoveredBridge.prototype._request = function _request (action, additionalParameter) {
var url = 'http://' + this.ip + ':' + this.port + '/' + action;

if (additionalParameter) {
url += additionalParameter;
}

return request({
uri: url,
json: true
});
};

/**
* Discovers the bridges.
*
* @returns {Promise<DiscoveredBridge[]>}
*/
DiscoveredBridge.discover = function discover () {
if (DiscoveredBridge.testMode) {
return Promise.resolve([new DiscoveredBridge('127.0.0.1', 8881)]);
}

return request({
uri: 'https://api.nuki.io/discover/bridges',
json: true
}).then(function processBridges (response) {
return response.bridges.map(function processBridge (bridge) {
return new DiscoveredBridge(bridge.ip, bridge.port);
});
});
};

/**
* Enables the test mode, which means, discovery only finds one bridge (can be used with nukiio-dummy-bridge)
* and connect does not call the auth endpoint but uses a hardcoded token.
*/
DiscoveredBridge.enableTestMode = function enableTestMode () {
DiscoveredBridge.testMode = true;
};

module.exports = DiscoveredBridge;
12 changes: 12 additions & 0 deletions lib/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ Bridge.prototype.reboot = function reboot () {
._request('reboot');
};

/**
* Enables or disabled the auth-endpoint and publication to the discovery url.
*
* @memberof Bridge#
* @param {Boolean} enable
* @returns {Promise}
*/
Bridge.prototype.configAuth = function configAuth (enable) {
return this
._request('configAuth', '&enable=' + (enable ? 1 : 0));
};

module.exports = Bridge;

/**
Expand Down
19 changes: 19 additions & 0 deletions test/nuki-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,23 @@ describe('Nuki Bridge API', function () {
});
});
});

describe('Bridge Discovery', function () {
before(function () {
API.DiscoveredBridge.enableTestMode();
});

it('should be able to get all local bridges', function () {
return API.DiscoveredBridge.discover().then(function (bridges) {
assert.ok(bridges.length);

return bridges[0].connect('token').then(function (bridge) {
assert.ok(bridge);
assert.ok(bridge.token);

bridgeInstance = bridge;
});
});
});
});
});

0 comments on commit 3a530d0

Please sign in to comment.