diff options
author | Dennis Eriksen <d@ennis.no> | 2023-02-01 12:24:22 +0100 |
---|---|---|
committer | Dennis Eriksen <d@ennis.no> | 2023-02-01 12:24:22 +0100 |
commit | d86413ad81135454df7ef1c7d7faeb52eb51efa8 (patch) | |
tree | faf9e7cc5bc728065f4d87f86db7c8b190ba42ed /mqtt2queue.py | |
parent | use new db thingy (diff) | |
download | energyscripts-d86413ad81135454df7ef1c7d7faeb52eb51efa8.tar.gz |
adding some common stuff, less code replica
Diffstat (limited to 'mqtt2queue.py')
-rw-r--r-- | mqtt2queue.py | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/mqtt2queue.py b/mqtt2queue.py index 102b1eb..24ee207 100644 --- a/mqtt2queue.py +++ b/mqtt2queue.py @@ -2,18 +2,16 @@ import os import json -import pickle from datetime import datetime -from litequeue import SQLQueue 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'] -q = SQLQueue("litequeue.db", maxsize=None) - # 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)) @@ -30,12 +28,15 @@ def on_message(client, userdata, msg): 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()) - q.put(pickle.dumps([sql, values])) 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()) - q.put(pickle.dumps([sql, values])) + + else: + return + + dbi(sql, values, verbose=True) |