forked from aholstenson/miio
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
4d06200
commit c39cbf0
Showing
12 changed files
with
660 additions
and
433 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.