aboutsummaryrefslogtreecommitdiffstats
path: root/mqtt2queue.py
diff options
context:
space:
mode:
authorDennis Eriksen <d@ennis.no>2023-02-01 12:24:22 +0100
committerDennis Eriksen <d@ennis.no>2023-02-01 12:24:22 +0100
commitd86413ad81135454df7ef1c7d7faeb52eb51efa8 (patch)
treefaf9e7cc5bc728065f4d87f86db7c8b190ba42ed /mqtt2queue.py
parentuse new db thingy (diff)
downloadenergyscripts-d86413ad81135454df7ef1c7d7faeb52eb51efa8.tar.gz
adding some common stuff, less code replica
Diffstat (limited to 'mqtt2queue.py')
-rw-r--r--mqtt2queue.py13
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)