forked from ninjablocks/ninja-zwave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (83 loc) · 2.19 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
var Device = require('./lib/device')
, util = require('util')
, stream = require('stream')
;
// Give our driver a stream interface
util.inherits(zwDriver,stream);
const enabled = false;
if (enabled) {
var SocketInterpreter = require('./lib/socketInterpreter')
}
var zwDriverObject;
var devices = []; // devices that the driver has been notified of
function registerNewDevice(key) {
var device = new Device();
device.G = key;
devices[key] = device;
zwDriverObject.emit('register', device);
return device;
}
function deviceForKey(key) {
var device = undefined;
if (key != undefined) {
device = devices[key];
if (device == undefined) {
device = registerNewDevice(key);
}
}
return device;
}
function dataReceived(key, label, reading) {
var device = deviceForKey(key);
reading = parseFloat(reading);
if (device != undefined) {
device.sendData(reading);
}
}
//setTimeout(
// (function() {
// socket_template_c.write('{jsondata : 2}');
// }
//), 3000);
/**
* 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 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 zwDriver(opts,app) {
var self = this;
zwDriverObject = this;
app.on('client::up',function(){
// The client is now connected to the cloud
// Do stuff with opts, and then commit it to disk
if (!opts.hasMutated) {
opts.hasMutated = true;
}
self.save();
if (enabled) {
process.nextTick(function() {
var si = new SocketInterpreter(dataReceived);
});
}
else {
app.log.info("Z-Wave module is not enabled");
}
});
};
/**
* Called when config data is received from the cloud
* @param {Object} config Configuration data
*/
zwDriver.prototype.config = function(config) {
};
// Export it
module.exports = zwDriver;