diff --git a/docker-compose.yml b/docker-compose.yml index 3ed3f84..04ecae7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,3 +8,6 @@ services: clustering: build: . command: python3 cluster.py + influx: + build: . + command: python3 mqtt2influx.py diff --git a/mqtt2influx.py b/mqtt2influx.py new file mode 100644 index 0000000..c09ae2a --- /dev/null +++ b/mqtt2influx.py @@ -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]) \ No newline at end of file