forked from stockmopar/ninja-connectedbytcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (92 loc) · 3.18 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
var Socket = require('./lib/socket')
, util = require('util')
, stream = require('stream')
, ConnectedByTCP = require('connectedbytcp')
, configHandlers = require('./lib/config');
// Give our module a stream interface
util.inherits(cbtcp,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 cbtcp(opts,app) {
var self = this;
this._app = app;
this._opts = opts;
this._opts.sockets = opts.sockets || [];
app.once('client::up', this.init.bind(this));
};
/**
* Discover and load TCP Bases
*/
cbtcp.prototype.init = function(){
this._app.log.info("(TCP Lights) init()");
this._opts.sockets.forEach(this.load.bind(this));
};
// Export it
module.exports = cbtcp;
cbtcp.prototype.load = function(host) {
this._app.log.info("(TCP Lights) Device at " + host + " is now being registered");
var self = this;
var app = this._app;
client = new ConnectedByTCP(host);
client.Init(function(error){
app.log.info("(TCP Lights) Init Called");
if(!error){
client.GetState(function(error2,system){
app.log.info("(TCP Lights) Get State CAlled");
system.forEach(function(room) {
self.emit('register',new Socket(app,client,room));
}.bind(this));
}.bind(this));
}
var fetchState = function() {
app.log.info("(TCP Lights) Fetch State Called");
app.fetchlock = 1;
client.GetState(function(error,system){
app.log.info("(TCP Lights) Received Updated State");
setTimeout(fetchState,60000);
});
};
setTimeout(fetchState,1000);
});
};
/**
* Add a particular TCP to this configuration
* @param {[String} host Host of the TCP to remember
*/
cbtcp.prototype.remember = function(host) {
this._app.log.info("(TCP Lights) remember()");
this._opts.sockets.push(host);
this.save();
};
/**
* Called when a user prompts a configuration
* @param {Object} rpc RPC Object
* @param {Function} cb Callback with return data
*/
cbtcp.prototype.config = function(rpc,cb) {
this._app.log.info("(TCP Lights) config()");
var self = this;
if (!rpc) {
return configHandlers.probe.call(this,cb);
}
switch (rpc.method) {
case 'configScan': return configHandlers.configScan.call(this,rpc.params,cb); break;
case 'manual_set_tcp': return configHandlers.manual_set_tcp.call(this,rpc.params,cb); break;
case 'manual_get_tcp': return configHandlers.manual_get_tcp.call(this,rpc.params,cb); break;
case 'manual_show_remove': return configHandlers.manual_show_remove.call(this,rpc.params,cb); break;
case 'manual_remove_tcp': return configHandlers.manual_remove_tcp.call(this,rpc.params,cb); break;
default: return cb(true); break;
}
};