diff options
author | Dennis Eriksen <d@ennis.no> | 2023-02-01 20:32:11 +0100 |
---|---|---|
committer | Dennis Eriksen <d@ennis.no> | 2023-02-01 20:32:11 +0100 |
commit | 8d186d39483beff64a1c11f80c6ca5e56dd7bbc5 (patch) | |
tree | 2c5a64ace4bd8eabd4d65014c5313bd7edd76191 /mqtt2queue.py | |
parent | run queue in batches (diff) | |
download | energyscripts-8d186d39483beff64a1c11f80c6ca5e56dd7bbc5.tar.gz |
moving and renaming/breaking everything
Diffstat (limited to 'mqtt2queue.py')
-rw-r--r-- | mqtt2queue.py | 54 |
1 files changed, 0 insertions, 54 deletions
diff --git a/mqtt2queue.py b/mqtt2queue.py deleted file mode 100644 index 24ee207..0000000 --- a/mqtt2queue.py +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/env python3 - -import os -import json -from datetime import datetime -import paho.mqtt.client as mqtt - -from common import dbi - -mqtt_server = os.environ['el_mqtt_server'] -mqtt_port = int(os.environ['el_mqtt_port']) -keepalive = int(os.environ['el_mqtt_keepalive']) -mqtt_topic = os.environ['el_mqtt_topic'] - -# The callback for when the client receives a CONNACK response from the server. -def on_connect(client, userdata, flags, rc): - print("Connected with result code "+str(rc)) - - # Subscribing in on_connect() means that if we lose the connection and - # reconnect then subscriptions will be renewed. - client.subscribe(mqtt_topic) - -# The callback for when a PUBLISH message is received from the server. -def on_message(client, userdata, msg): - name = msg.topic.split('/')[1] - data = json.loads(msg.payload) - - if name.startswith('tmp') and 'temperature' in data and 'humidity' in data: - sql = "INSERT INTO mqtt_temps (name, temperature, humidity, battery, linkquality, voltage, time) VALUES(%s,%s,%s,%s,%s,%s,%s)" - values = (name, data['temperature'], data['humidity'], data['battery'], data['linkquality'], data['voltage'], datetime.utcnow()) - - elif name == 'HAN' and 'current' in data: - sql = "INSERT INTO mqtt_han (name, current, power, voltage, linkquality, time) VALUES(%s,%s,%s,%s,%s,%s)" - values = (name, data['current'], data['power'], data['voltage'], data['linkquality'], datetime.utcnow()) - - else: - return - - dbi(sql, values, verbose=True) - - - -# mqtt -client = mqtt.Client() -client.on_connect = on_connect -client.on_message = on_message - -client.connect(mqtt_server, mqtt_port, keepalive) - -# Blocking call that processes network traffic, dispatches callbacks and -# handles reconnecting. -# Other loop*() functions are available that give a threaded interface and a -# manual interface. -client.loop_forever() |