-
Notifications
You must be signed in to change notification settings - Fork 3
/
bridge-influx.py
executable file
·40 lines (32 loc) · 1.15 KB
/
bridge-influx.py
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
#!/usr/bin/env python3
import datetime
import fileinput
import influxdb
import json
def make_measurement(timestamp, name, fields):
d = {
'measurement': 'power',
'tags': { 'entity_id': name, 'domain': 'sensor', 'sensor': 'ina3221' },
'time': str(timestamp),
'fields': fields
}
return d
def send_measurement(client, name, raw_data, timestamp, verbose = True):
data = []
for ch, fields in raw_data.items():
if ch.startswith('@'): continue
data += [make_measurement(timestamp, f"{name}.{ch}", fields)]
if verbose:
print(json.dumps(data))
return client.write_points(data)
if __name__ == '__main__':
# Load configuration
with open('config.json', 'r') as fp:
config = json.load(fp)
name = config['name']
client = influxdb.InfluxDBClient(**config['influx'])
for line in fileinput.input():
raw_data = json.loads(line)
sample_time = datetime.datetime.fromisoformat(raw_data['@time'])
names = tuple(filter(lambda x: not x.startswith('@'), raw_data.keys()))
result = send_measurement(client, name, raw_data, sample_time, verbose=True)