-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
48 lines (38 loc) · 1.4 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
const yaml = require('js-yaml');
const fs = require('fs');
const config = yaml.safeLoad(fs.readFileSync(__dirname + '/config.yml', 'utf8'));
const Influx = require('influxdb-nodejs');
const InfluxClient = new Influx(config.InfluxDB);
const Log = require('log'),
log = new Log('debug');
const { Scanner } = require('homebridge-mi-hygrothermograph/lib/scanner');
XiScanner = new Scanner(log);
XiScanner.start();
var deviceName = function (id) {
if (typeof config.Devices[id] === 'undefined') return id;
else return config.Devices[id];
}
var writeDataPoint = function (type, value, unit, id) {
log.debug('Writing', type.toLowerCase(), 'to Influx:', value, deviceName(id));
data = tag = {};
data[unit] = value;
tag['device'] = deviceName(id);
if (config.PostUnknown || tag['device'] !== id) {
InfluxClient.write(type)
.tag(tag)
.field(data)
.then(() => log.debug('Write point success'))
.catch(log.error);
} else {
log.debug('Not posting %s information as device is unknown.', id);
}
}
XiScanner.on('temperatureChange', function (value, id) {
writeDataPoint('Temperature', value, 'celcius', id);
});
XiScanner.on('humidityChange', function (value, id) {
writeDataPoint('Humidity', value, 'percent', id);
});
XiScanner.on('batteryChange', function (value, id) {
writeDataPoint('Battery', value, 'percent', id);
});