forked from OpenZWave/node-openzwave-shared
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
125 lines (111 loc) · 3.56 KB
/
test.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* OpenZWave test program.
*/
var OpenZWave = require('./lib/openzwave-shared.js');
var os = require('os');
var zwave = new OpenZWave({
ConsoleOutput: false,
Logging: false,
SaveConfiguration: false,
DriverMaxAttempts: 3,
PollInterval: 500,
SuppressValueRefresh: true,
});
var nodes = [];
zwave.on('connected', function(homeid) {
console.log('=================== CONNECTED! ====================');
});
zwave.on('driver ready', function(homeid) {
console.log('=================== DRIVER READY! ====================');
console.log('scanning homeid=0x%s...', homeid.toString(16));
});
zwave.on('driver failed', function() {
console.log('failed to start driver');
zwave.disconnect();
process.exit();
});
zwave.on('node added', function(nodeid) {
console.log('=================== NODE ADDED! ====================');
nodes[nodeid] = {
manufacturer: '',
manufacturerid: '',
product: '',
producttype: '',
productid: '',
type: '',
name: '',
loc: '',
classes: {},
ready: false,
};
});
zwave.on('value added', function(nodeid, comclass, value) {
if (!nodes[nodeid]['classes'][comclass])
nodes[nodeid]['classes'][comclass] = {};
nodes[nodeid]['classes'][comclass][value.index] = value;
});
zwave.on('value changed', function(nodeid, comclass, value) {
if (nodes[nodeid]['ready']) {
console.log('node%d: changed: %d:%s:%s->%s', nodeid, comclass,
value['label'],
nodes[nodeid]['classes'][comclass][value.index]['value'],
value['value']);
}
nodes[nodeid]['classes'][comclass][value.index] = value;
});
zwave.on('value removed', function(nodeid, comclass, index) {
if (nodes[nodeid]['classes'][comclass] &&
nodes[nodeid]['classes'][comclass][index])
delete nodes[nodeid]['classes'][comclass][index];
});
zwave.on('node ready', function(nodeid, nodeinfo) {
nodes[nodeid]['manufacturer'] = nodeinfo.manufacturer;
nodes[nodeid]['manufacturerid'] = nodeinfo.manufacturerid;
nodes[nodeid]['product'] = nodeinfo.product;
nodes[nodeid]['producttype'] = nodeinfo.producttype;
nodes[nodeid]['productid'] = nodeinfo.productid;
nodes[nodeid]['type'] = nodeinfo.type;
nodes[nodeid]['name'] = nodeinfo.name;
nodes[nodeid]['loc'] = nodeinfo.loc;
nodes[nodeid]['ready'] = true;
console.log('node%d: %s, %s', nodeid,
nodeinfo.manufacturer ? nodeinfo.manufacturer
: 'id=' + nodeinfo.manufacturerid,
nodeinfo.product ? nodeinfo.product
: 'product=' + nodeinfo.productid +
', type=' + nodeinfo.producttype);
console.log('node%d: name="%s", type="%s", location="%s"', nodeid,
nodeinfo.name,
nodeinfo.type,
nodeinfo.loc);
for (var comclass in nodes[nodeid]['classes']) {
switch (comclass) {
case 0x25: // COMMAND_CLASS_SWITCH_BINARY
case 0x26: // COMMAND_CLASS_SWITCH_MULTILEVEL
zwave.enablePoll(nodeid, comclass);
break;
}
var values = nodes[nodeid]['classes'][comclass];
console.log('node%d: class %d', nodeid, comclass);
for (var idx in values)
console.log('node%d: %s=%s', nodeid, values[idx]['label'], values[idx]['value']);
}
});
zwave.on('notification', function(nodeid, notif, help) {
console.log('node%d: notification(%d): %s', nodeid, notif, help);
});
zwave.on('scan complete', function() {
console.log('scan complete, hit ^C to finish.');
});
var zwavedriverpaths = {
"darwin" : '/dev/cu.usbmodem1411',
"linux" : '/dev/ttyUSB0',
"windows": '\\\\.\\COM3'
}
console.log("connecting to " + zwavedriverpaths[os.platform()]);
zwave.connect( zwavedriverpaths[os.platform()] );
process.on('SIGINT', function() {
console.log('disconnecting...');
zwave.disconnect();
process.exit();
});