From 7b6b13f9f4c2ccae9a16db64f1f37b21bd4ba519 Mon Sep 17 00:00:00 2001 From: jwansek Date: Thu, 28 Sep 2023 21:36:44 +0000 Subject: Fixed a couple bugs with power meter logging --- database.py | 25 +++++++++++++++---------- devices.py | 2 +- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/database.py b/database.py index c1d67f3..23e06f0 100644 --- a/database.py +++ b/database.py @@ -20,6 +20,16 @@ class PowerDatabase: print(e) if e.args[0] == 1049: self.__connection = self.__build_db() + + with self.__connection.cursor() as cursor: + if "TASMOTA_DEVICES" in os.environ.keys(): + for host, username, password in self.get_tasmota_devices(): + cursor.execute(""" + INSERT INTO tasmota_devices (host, username, password) + VALUES (%s, %s, %s) + ON DUPLICATE KEY UPDATE username = %s, password = %s; + """, (host, username, password, username, password)) + return self def __exit__(self, type, value, traceback): @@ -58,12 +68,6 @@ class PowerDatabase: ); """) - if "TASMOTA_DEVICES" in os.environ.keys(): - devices = os.environ["TASMOTA_DEVICES"].split(",") - for device in devices: - host, username, password = device.split(":") - cursor.execute("INSERT INTO tasmota_devices VALUES (%s, %s, %s);", (host, username, password)) - cursor.execute(""" CREATE TABLE watt_readings ( host VARCHAR(25) NOT NULL, @@ -88,9 +92,10 @@ class PowerDatabase: return self.__connection def get_tasmota_devices(self): - with self.__connection.cursor() as cursor: - cursor.execute("SELECT * FROM tasmota_devices;") - return cursor.fetchall() + o = [] + for d in os.environ["TASMOTA_DEVICES"].split(","): + o.append(d.split(":")) + return o def append_watt_readings(self, host, reading): with self.__connection.cursor() as cursor: @@ -105,7 +110,7 @@ if __name__ == "__main__": if not os.path.exists(".docker"): import dotenv dotenv.load_dotenv(dotenv_path = "db.env") - host = "srv.home" + host = "srv.athome" else: host = "db" diff --git a/devices.py b/devices.py index e3c21ae..27e6545 100644 --- a/devices.py +++ b/devices.py @@ -9,7 +9,7 @@ import os if not os.path.exists(os.path.join("/app", ".docker")): import dotenv dotenv.load_dotenv(dotenv_path = "db.env") - HOST = "srv.home" + HOST = "srv.athome" else: HOST = "db" -- cgit v1.2.3