Skip to content

Commit

Permalink
Refactoring network code
Browse files Browse the repository at this point in the history
The network code for devices is now handled by the file `network.js`
that manages a single socket and information about all devices. This
change implements a cache containing information about device models and
tokens making connections faster and simpler.

Direct device creation has been removed and the only supported way to
get a device is now to use `miio.device(opts)` or `miio.devices()`.
  • Loading branch information
aholstenson committed Jan 18, 2018
1 parent 4d06200 commit c39cbf0
Show file tree
Hide file tree
Showing 12 changed files with 660 additions and 433 deletions.
22 changes: 0 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,28 +227,6 @@ added along side the older one.
[Reporting issues](docs/reporting-issues.md) contains information that is
useful for making any issue you want to report easier to fix.

## Advanced: Skip model and token checks

The `miio.device` function will return a promise that checks that we can
communicate with the device and what model it is. If you wish to skip this
step and just create a reference to a device use `miio.createDevice`:

```javascript
const device = miio.createDevice({
address: '192.168.100.8',
token: 'token-as-hex',
model: 'zhimi.airpurifier.m1'
});
```

You will need to call `device.init()` manually to initialize the device:

```javascript
device.init()
.then(() => /* device is ready for commands */)
.catch(console.error);
```

## Advanced: Raw API-usage and calling the Xiaomi miIO-method directly

It's possible to call any method directly on a device without using the
Expand Down
5 changes: 1 addition & 4 deletions cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ const chalk = require('chalk');
const Packet = require('../lib/packet');
const Device = require('../lib/device');
const { Browser, Devices } = require('../lib/discovery');
const Tokens = require('../lib/tokens');
const tokens = require('../lib/tokens');
const models = require('../lib/models');
const createDevice = require('../lib/createDevice');

const deviceFinder = require('./device-finder');

const tokens = new Tokens();

function info() {
console.log(chalk.bgWhite.black(' INFO '), Array.prototype.join.call(arguments, ' '));
}
Expand Down
20 changes: 3 additions & 17 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,8 @@ const miio = require('./lib');

// Create a new device over the given address
miio.device({
address: '192.168.100.8'
address: 'ipHere',
}).then(device => {
console.log('Connected to device');
console.log(device);
console.log(device.metadata);

console.log(device.pm2_5);

/*
console.log('connected', device.modes());
return device.mode('silent');
if(device.metadata.hasCapability('power')) {
console.log('power is currently', device.power());
return device.togglePower();
}*/

})
.then(p => console.log('got', p))
.catch(console.error);
}).catch(err => console.log('Error occurred:', err));
49 changes: 29 additions & 20 deletions lib/connectToDevice.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
'use strict';

const createDevice = require('./createDevice');
const network = require('./network');

const Device = require('./device');
const models = require('./models');

module.exports = function(options) {
let device = createDevice(options);
return device.call('miIO.info')
.then(data => {
if(options.model) {
// If the model was specified we reuse the device instance
} else {
// If the model was automatically discovered recreate the device
device._fastDestroy();
let handle = network.ref();

// Connecting to a device via IP, ask the network if it knows about it
return network.findDeviceViaAddress(options)
.then(device => {
const deviceHandle = {
ref: network.ref(),
api: device
};

device = createDevice(Object.assign({}, options, {
model: data.model,
token: data.token
}));
// Try to resolve the correct model, otherwise use the generic device
const d = models[device.model];
if(! d) {
return new Device(deviceHandle);
} else {
return new d(deviceHandle);
}
})
.then(() => device.init())
.catch(err => {
// In case initialization was skipped
device._fastDestroy();
.catch(e => {
// Error handling - make sure to always release the handle
handle.release();

throw e;
})
.then(device => {
// Make sure to release the handle
handle.release();

// Perform full destroy
device.destroy();
throw err;
return device.init();
});
};
18 changes: 0 additions & 18 deletions lib/createDevice.js

This file was deleted.

Loading

0 comments on commit c39cbf0

Please sign in to comment.