-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
executable file
·143 lines (114 loc) · 3.39 KB
/
start.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env node
'use strict';
var tools = require('./tools');
var OZW = require('openzwave-shared');
var mqtt = require('mqtt');
var defaultconfig = {
usbport: '/dev/ttyACM0',
broker: 'mqtt://localhost'
};
var config = tools.readConfiguration('config.json', {default: defaultconfig, alias: {config: 'c'}});
var zwave = new OZW({ConsoleOutput: false});
var client = mqtt.connect(config.broker);
var nodes = [];
function debugMessage (message) {
client.publish('zwave/debug', message);
console.log(message);
}
zwave.on('driver ready', function (homeid) {
debugMessage('Driver ready, scanning homeid: ' + homeid);
});
zwave.on('driver failed', function () {
debugMessage('Driver failed');
zwave.disconnect();
process.exit();
});
zwave.on('scan complete', function () {
debugMessage('Scan complete');
});
zwave.on('node added', function (nodeid) {
debugMessage('Adding node: ' + nodeid + '...\n');
nodes[nodeid] = {
classes: {},
ready: false
};
});
zwave.on('value added', function (nodeid, comclass, value) {
var message;
var time = new Date().toISOString();
value = tools.convertSI(value);
message = {
info: 'value added',
node: value.node_id,
label: value.label,
value: value.value,
timestamp: time
};
client.publish('zwave/value', JSON.stringify(message));
if (!nodes[nodeid]['classes'][comclass]) {
nodes[nodeid]['classes'][comclass] = {};
}
nodes[nodeid]['classes'][comclass][value.index] = value;
});
zwave.on('value changed', function (nodeid, comclass, value) {
var message;
var time = new Date().toISOString();
value = tools.convertSI(value);
message = {
info: 'value changed',
node: value.node_id,
label: value.label,
value: value.value,
timestamp: time
};
client.publish('zwave/value', JSON.stringify(message));
nodes[nodeid]['classes'][comclass][value.index] = value;
});
zwave.on('node ready', function (nodeid, nodeinfo) {
var values;
nodes[nodeid].info = nodeinfo;
nodes[nodeid]['ready'] = true;
console.log('node' + nodeid + ': ' + nodeinfo.manufacturer + ' ' + nodeinfo.product);
for (let comclass in nodes[nodeid]['classes']) {
switch (comclass) {
case 0x25: // COMMAND_CLASS_SWITCH_BINARY
case 0x26: // COMMAND_CLASS_SWITCH_MULTILEVEL
zwave.enablePoll(nodeid, comclass);
break;
}
values = nodes[nodeid]['classes'][comclass];
console.log('node%d: class %d', nodeid, comclass);
for (let idx in values) {
console.log('node' + nodeid + ': ' + values[idx].label + '=' + values[idx].value);
}
}
});
client.on('message', function (topic, message) {
console.log(message.toString());
if (tools.topicMatch(topic, 'server/request/#')) {
handleRequest(topic, message);
}
});
function handleRequest (topic, message) {
console.log('Got request');
console.log(message.toString());
topic = topic.split('/').slice(2).join('/');
client.publish('server/response/' + topic, message + ' - pong');
}
process.on('SIGINT', function () {
console.log('disconnecting...');
zwave.disconnect(config.usbport);
client.end();
process.exit();
});
client.on('connect', function () {
client.subscribe('server/request/#');
// client.publish('presence', 'Hello mqtt');
});
function getNodeNeighbors (nodeid) {
var neighbors;
neighbors = zwave.getNodeNeighbors(nodeid);
return neighbors;
}
console.log('connecting...');
zwave.connect(config.usbport);