-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (83 loc) · 2.52 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
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
const Influx = require('influx'),
config = require('config'),
P1 = require('./lib/p1')
const influxConfig = config.get('influxdb'),
p1Config = config.get('p1')
var lastGasValue
const influx = new Influx.InfluxDB({
host: influxConfig.host,
database: influxConfig.database,
username: influxConfig.username,
password: influxConfig.password,
schema: [
{
measurement: 'electricity',
tags: [],
fields: {
received: Influx.FieldType.FLOAT,
received_value_low: Influx.FieldType.FLOAT,
received_value_high: Influx.FieldType.FLOAT
}
},
{
measurement: 'gas',
tags: [],
fields: {
received: Influx.FieldType.FLOAT,
received_value: Influx.FieldType.FLOAT,
}
}
]
})
const sp = new P1.Reader({
port: p1Config.port,
portConfig: {
baudRate: p1Config.baudRate
}
})
sp.on('reading', function (packet) {
var points = [];
var gasValue = packet.gas.reading;
if (lastGasValue)
{
var gasUsed = gasValue - lastGasValue
if (gasUsed) {
if (p1Config.debug) {
console.log(`Gas. Value: ${gasValue}, Last value: ${lastGasValue}, Used: ${gasUsed}`)
}
points.push({
measurement: 'gas',
fields: {
received: gasUsed,
received_value: gasValue
},
});
}
}
lastGasValue = gasValue;
var electricityUsed = packet.electricity.received.actual.reading;
if (electricityUsed) {
var electricityReceivedLow = packet.electricity.received.tariff1.reading;
var electricityReceivedHigh = packet.electricity.received.tariff2.reading;
if (p1Config.debug)
{
console.log(`Electricity. Low: ${electricityReceivedLow}, High: ${electricityReceivedHigh}, Used: ${electricityUsed}`)
}
points.push({
measurement: 'electricity',
fields: {
received: electricityUsed,
received_value_low: electricityReceivedLow,
received_value_high: electricityReceivedHigh
},
});
}
if (points.length) {
influx.writePoints(points).catch(err => {
console.error('Error writing to InfluxDB', err)
})
}
})
sp.on('error', function (error) {
console.error('P1Reader error', error)
})