forked from thatguydan/ninja-limitlessLED
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
70 lines (58 loc) · 1.89 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var LimitlessLEDRGB = require('./lib/LimitlessLEDRGB')
, util = require('util')
, stream = require('stream')
, configHandlers = require('./lib/config')
// Give our module a stream interface
util.inherits(myModule,stream);
/**
* Called when our client starts up
* @constructor
*
* @param {Object} opts Saved/default module 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 cloud
*
* @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 cloud
*/
function myModule(opts,app) {
var self = this;
this._log = app.log;
this._opts = opts;
app.on('client::up',function(){
// Register a device
if (opts.ipAddress) {
this.register();
}
}.bind(this));
};
/**
* Called when config data is received from the cloud
* @param {Object} config Configuration data
*/
myModule.prototype.config = function(rpc, cb) {
var self = this;
if (!rpc) {
return configHandlers.probe.call(this,cb);
}
switch (rpc.method) {
case 'get_ip_port': return configHandlers.get_ip_port.call(this,rpc.params,cb); break;
case 'store_ip_port': return configHandlers.store_ip_port.call(this,rpc.params,cb); break;
default: return cb(true); break;
}
};
myModule.prototype.setIpPort = function(ipAddress, port) {
this._opts.ipAddress = ipAddress;
this._opts.port = port;
this.save();
this.register();
};
myModule.prototype.register = function() {
var llrgb = new LimitlessLEDRGB(this._opts.ipAddress, this._opts.port);
this.emit('register', llrgb);
};
// Export it
module.exports = myModule;