Add influxdb connector

This commit is contained in:
Asaril 2020-03-12 19:03:23 +01:00
parent b8e964078a
commit e3e57c3b09
2 changed files with 35 additions and 0 deletions

View file

@ -8,3 +8,6 @@ services:
clustering:
build: .
command: python3 cluster.py
influx:
build: .
command: python3 mqtt2influx.py

32
mqtt2influx.py Normal file
View file

@ -0,0 +1,32 @@
import paho.mqtt.subscribe as sub
import json
from influxdb import InfluxDBClient
import datetime
DB = ('habctrl', 8086)
BROKER = ('homeproxy', 1883)
TOPIC_PREFIX = 'scalefix'
TOPIC_WEIGHTS = TOPIC_PREFIX+'/weights'
def handle_msg(client, userdata, message):
data = json.loads(message.payload.decode('utf-8'))
print(f'got data for {data["user"]}: {data["weight"]}')
client = InfluxDBClient(DB[0], DB[1], 'scalefix', 'weighting', 'scale')
json_body = [
{
"measurement": "weights",
"tags": {
"user": data['user'],
},
"time": datetime.datetime.fromtimestamp(data['timestamp']).isoformat(),
"fields": {
"value": data['weight']
}
}
]
client.write_points(json_body)
client.close()
sub.callback(handle_msg, [TOPIC_WEIGHTS], hostname=BROKER[0], port=BROKER[1])