-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
38 lines (33 loc) · 1.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var util = require('util'),
stream = require('stream'),
Device = require('./device'),
config = require('./config');
// Give our driver a stream interface
util.inherits(Driver, stream);
/**
* Called when our client starts up
* @constructor
*
* @param {Object} opts Saved/default driver configuration
* @param {Object} app The app event emitter
* @param {String} app.id The client serial number
*
* @property {Function} save When called will save the contents of `opts`
* @property {Function} config Will be called when config data is received from the Ninja Platform
*
* @fires register - Emit this when you wish to register a device (see Device)
* @fires config - Emit this when you wish to send config data back to the Ninja Platform
*/
function Driver(opts, app) {
this._app = app;
app.on('client::up',function() {
// The client is now connected to the Ninja Platform
config.forEach(this.createDevice.bind(this));
}.bind(this));
};
Driver.prototype.createDevice = function(cfg) {
var device = new Device(this._app, cfg);
this.emit('register', device);
};
// Export it
module.exports = Driver;