Skip to content

Commit

Permalink
Emit devices from miio.devices() instead of registration
Browse files Browse the repository at this point in the history
  • Loading branch information
aholstenson committed Jan 27, 2018
1 parent 68c452f commit e6fad8b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
30 changes: 8 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,30 +141,16 @@ const devices = miio.devices({
cacheTime: 300 // 5 minutes. Default is 1800 seconds (30 minutes)
});

devices.on('available', reg => {
if(! reg.token) {
console.log(reg.id, 'hides its token');
return;
}

const device = reg.device;
if(! device) {
console.log(reg.id, 'could not be connected to');
return;
devices.on('available', device => {
if(device.matches('placeholder')) {
// This device is either missing a token or could not be connected to
} else {
// Do something useful with device
}

// Do something useful with the device
});

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

// Do whatever you need here
});

devices.on('error', err => {
// err.device points to info about the device
console.log('Something went wrong connecting to device', err);
devices.on('unavailable', device => {
// Device is no longer available and is destroyed
});
```

Expand All @@ -191,7 +177,7 @@ browser.on('available', reg => {
}

// Directly connect to the device anyways - so use miio.devices() if you just do this
miio.device(reg)
reg.connect()
.then(device => {
devices[reg.id] = device;

Expand Down
20 changes: 15 additions & 5 deletions cli/device-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ function asFilter(filter) {

if(typeof filter === 'string') {
return reg => {
return String(reg.id) === filter || reg.address === filter || reg.model === filter;
// Assign management if this filter is filtering a device
const mgmt = reg.management ? reg.management : reg;

// Make sure to remove `miio:` prefix from ids when matching
let id = String(reg.id);
if(id.indexOf('miio:') === 0) {
id = id.substring(5);
}

// Match id, address or model
return id === filter || mgmt.address === filter || mgmt.model === filter;
};
} else if(typeof filter === 'function') {
return filter;
Expand Down Expand Up @@ -45,12 +55,12 @@ module.exports = function(options={}) {
filter: options.filter ? asToplevelFilter(filter) : null,
});

browser.on('available', reg => {
if(filter(reg)) {
result.emit('available', reg.device);
browser.on('available', device => {
if(filter(device)) {
result.emit('available', device);
}
});
browser.on('unavailable', reg => result.emit('unavailable', reg.device));
browser.on('unavailable', device => result.emit('unavailable', device));
}
return result;
};
22 changes: 13 additions & 9 deletions lib/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ const Browser = module.exports.Browser = class Browser extends TimedDiscovery {
address: device.address,
port: device.port,
token: device.token || this._manualToken(device.id),
autoToken: device.autoToken
autoToken: device.autoToken,

connect: function(options={}) {
return connectToDevice(Object.assign({
address: this.address,
port: this.port,
model: this.model
}, options));
}
};

const add = () => this[addService](service);
Expand Down Expand Up @@ -108,18 +116,14 @@ class Devices extends BasicDiscovery {
port: reg.port,
model: reg.model,
withPlaceholder: true
})
.then(device => {
reg.device = device;
return reg;
});
});
});

this._browser.on('available', s => {
this[addService](s);

if(s.device instanceof Children) {
this._bindSubDevices(s.device);
if(s instanceof Children) {
this._bindSubDevices(s);
}
});

Expand Down Expand Up @@ -165,7 +169,7 @@ class Devices extends BasicDiscovery {
}

// Register and emit event
this[addService](reg);
this[addService](sub);
};

device.on('thing:available', handleAvailable);
Expand Down

0 comments on commit e6fad8b

Please sign in to comment.