Skip to content

Commit

Permalink
Adding support for manual tokens in miio.discover()
Browse files Browse the repository at this point in the history
  • Loading branch information
aholstenson committed May 16, 2017
1 parent 16e4933 commit d5608bd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const devices = miio.devices({
cacheTime: 300 // 5 minutes. Default is 1800 seconds (30 minutes)
});

browser.on('available', reg => {
devices.on('available', reg => {
if(! reg.token) {
console.log(reg.id, 'hides its token');
return;
Expand All @@ -133,13 +133,13 @@ browser.on('available', reg => {
// Do something useful with the device
});

browser.on('unavailable', reg => {
devices.on('unavailable', reg => {
if(! reg.device) return;

// Do whatever you need here
});

browser.on('error', err => {
devices.on('error', err => {
// err.device points to info about the device
console.log('Something went wrong connecting to device', err);
});
Expand All @@ -148,9 +148,10 @@ browser.on('error', err => {
`miio.devices()` supports these options:

* `cacheTime`, the maximum amount of seconds a device can be unreachable before it becomes unavailable. Default: `1800`
* `useTokenStorage`, if tokens should be fetched from storage (see device management). Default: `true`
* `filter`, function used to filter what devices are connected to. Default: `reg => true`
* `skipSubDevices`, if sub devices on Aqara gateways should be skipped. Default: `false`
* `useTokenStorage`, if tokens should be fetched from storage (see device management). Default: `true`
* `tokens`, object with manual mapping between ids and tokens (advanced, use [Device management](docs/management.md) if possible)

Example using `miio.browse()`:

Expand Down
10 changes: 8 additions & 2 deletions lib/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Browser {
this.tokens = new Tokens();
}

this.manualTokens = options.tokens || {};

this._events = new EventEmitter();
this._services = {};

Expand All @@ -36,6 +38,10 @@ class Browser {
this._events.removeListener(event, cb);
}

_manualToken(id) {
return this.manualTokens[id] || null;
}

start() {
this._socket = dgram.createSocket('udp4');
this._socket.bind();
Expand All @@ -56,7 +62,7 @@ class Browser {
id: id,
address: rinfo.address,
port: rinfo.port,
token: storedToken,
token: storedToken || this._manualToken(id),
autoToken: false
});
})
Expand All @@ -66,7 +72,7 @@ class Browser {
id: id,
address: rinfo.address,
port: rinfo.port,
token: token,
token: token || this._manualToken(id),
autoToken: true
});
}
Expand Down

0 comments on commit d5608bd

Please sign in to comment.