This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.js
executable file
·63 lines (51 loc) · 1.7 KB
/
service.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
#!/usr/bin/env node
require('dotenv').config(); // load config
var OZW = require('openzwave-shared'), // load z-wave
request = require('request'); // load request
var zwave = new OZW({
Logging: false, // disable file logging (OZWLog.txt)
ConsoleOutput: false, // disable console logging
SaveLogLevel: 0,
QueueLogLevel: 0,
DumpTriggerLevel: 0
});
console.log('Loading...');
zwave.on('scan complete', function () {
console.log('Scan complete. Listening for events. Hit ctrl+C to finish.');
});
zwave.on('driver failed', function () {
console.log('driver failed');
console.log('trying to disconnect...');
zwave.disconnect();
});
// labeles to monitor
var labels = ['Energy', 'Power'];
zwave.on('value changed', function (nodeid, comclass, value) {
// we only care about main events on entire controller
if (labels.indexOf(value.label) < 0 && value.instance != 1) {
return false;
}
// send data to home server
request.post({
url: process.env.API_SERVER + '/api/v1/' + value.label.toLowerCase(), form: {
sensor_id: nodeid,
node: nodeid,
instance: value.instance,
value: value.value
}, headers: {
'API-TOKEN': process.env.API_TOKEN
}
}, function (err, httpResponse, body) {
// in most cases we do not care, but we want to see error
if (httpResponse.statusCode != 200) {
// console.log('Error pushing data: ' + httpResponse.statusMessage); // MK Logger
}
});
});
zwave.connect('/dev/ttyACM0');
process.on('SIGINT', function () {
// console.log(nodes);
console.log('disconnecting...');
zwave.disconnect();
process.exit();
});