From d86413ad81135454df7ef1c7d7faeb52eb51efa8 Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Wed, 1 Feb 2023 12:24:22 +0100 Subject: adding some common stuff, less code replica --- mqtt2queue.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'mqtt2queue.py') 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) -- cgit v1.2.3