forked from jperkin/node-openzwave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
129 lines (116 loc) · 3.34 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
126
127
128
129
/*
* OpenZWave test program.
*/
var OpenZWave = require('./lib/openzwave.js');
var zwave = new OpenZWave('/dev/ttyUSB0', {
saveconfig: true,
});
var nodes = [];
zwave.on('driver ready', function(homeid) {
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) {
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 (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 (idx in values)
console.log('node%d: %s=%s', nodeid, values[idx]['label'], values[idx]['value']);
}
});
zwave.on('notification', function(nodeid, notif) {
switch (notif) {
case 0:
console.log('node%d: message complete', nodeid);
break;
case 1:
console.log('node%d: timeout', nodeid);
break;
case 2:
console.log('node%d: nop', nodeid);
break;
case 3:
console.log('node%d: node awake', nodeid);
break;
case 4:
console.log('node%d: node sleep', nodeid);
break;
case 5:
console.log('node%d: node dead', nodeid);
break;
case 6:
console.log('node%d: node alive', nodeid);
break;
}
});
zwave.on('scan complete', function() {
console.log('scan complete, hit ^C to finish.');
});
zwave.connect();
process.on('SIGINT', function() {
console.log('disconnecting...');
zwave.disconnect();
process.exit();
});