database access for rooms and mqtt client + generator

This commit is contained in:
2025-12-09 09:07:07 +01:00
parent 75f4b7e1eb
commit 0a7b029141
16 changed files with 486 additions and 96 deletions

View File

@@ -4,7 +4,7 @@ import logging
from typing import Generator
import datetime
from templating import SensorInfo
from components import SensorInfo
# main database connector class
# permission validation is not performed on this class
@@ -14,7 +14,7 @@ class DatabaseConnect:
with open("db_creds.csv","r") as f:
credentials = f.read().split(",")
try:
conn = mariadb.connect(
self.conn = mariadb.connect(
user=credentials[0],
password=credentials[1],
host=credentials[2],
@@ -24,23 +24,39 @@ class DatabaseConnect:
except mariadb.Error as e:
logging.fatal(f"Error connecting to database: {e}")
return
self.cursor = conn.cursor()
self.cursor = self.conn.cursor()
self.credentials = f.read().split(",")
super().__init__()
def create_user(self, username: str, pwd: int) -> None:
self.cursor.execute("INSERT INTO Users (username, pwd) VALUE (?,?)",(username, pwd))
logging.info(f"Created user {username}")
def emit_log(self, severity:int, message: str) -> None:
self.cursor.execute("INSERT INTO `log` (`timestamp`, `type` ,`message`) VALUES (?,?,?);",(datetime.datetime.now(), severity, message))
match severity:
case 1:
logging.error(f"Log emitted: {message}")
case 2:
logging.warning(f"Log emitted: {message}")
case 3:
logging.info(f"Log emitted: {message}")
def create_user(self, username: str, salt: bytes, key: bytes) -> None:
self.cursor.execute("INSERT INTO `users` (`username`, `salt`, `key`) VALUES (?,?,?)",(username, salt, key))
self.conn.commit()
self.emit_log(2, f"Created user {username}")
def delete_user(self, id) -> None:
self.cursor.execute("DELETE FROM Users WHERE ID = ?",(id))
logging.info(f"Deleted user {id}")
self.emit_log(2, f"Deleted user ID {id}")
def display_users(self) -> tuple[tuple[int, str, int]]:
self.cursor.execute("SELECT * FROM Users")
return self.cursor.fetchall()
def username_from_id(self, user_id):
self.cursor.execute("SELECT users.`username` FROM users WHERE `ID` = 1;")
self.cursor.execute("SELECT users.`username` FROM users WHERE `ID` = ?;", (user_id,))
return self.cursor.fetchone()
def user_by_username(self, username) -> tuple[int, str, bytes, bytes] | None:
self.cursor.execute("SELECT * FROM users WHERE `username` = ?;", (username,))
return self.cursor.fetchone()
def view_valid_rooms(self, user_id) -> list[tuple[str, str, int]]:
@@ -61,9 +77,9 @@ class DatabaseConnect:
# self.cursor.execute("SELECT rooms.`ID` from rooms WHERE rooms.shortname = ?;",(shortname,))
# return self.cursor.fetchone()
def get_sensors_in_room_shortname(self, shortname) -> list[int]:
self.cursor.execute("SELECT Sensors.`ID` from Sensors LEFT JOIN rooms ON Sensors.`roomID` = rooms.ID WHERE rooms.shortname = ?;",(shortname,))
return [x[0] for x in self.cursor.fetchall()]
def get_sensors_in_room_shortname(self, shortname) -> list[tuple[int, int]]:
self.cursor.execute("SELECT Sensors.`ID`, Sensors.`Type` from Sensors LEFT JOIN rooms ON Sensors.`roomID` = rooms.ID WHERE rooms.shortname = ?;",(shortname,))
return [x for x in self.cursor.fetchall()]
def get_sensors_in_room_id(self, roomid) -> list[tuple[int, int]]:
self.cursor.execute("SELECT Sensors.`ID`, Sensors.`Type` from Sensors LEFT JOIN rooms ON Sensors.`roomID` = rooms.ID WHERE rooms.`ID` = ?;",(roomid,))
@@ -86,6 +102,24 @@ class DatabaseConnect:
def get_sensor_type(self, sensor_ID):
self.cursor.execute("SELECT types.`type_desc` from types LEFT JOIN Sensors ON types.`ID` = Sensors.`type` WHERE Sensors.`ID` = ?;",(sensor_ID,))
return self.cursor.fetchone()[0]
def get_sensor_from_topic(self, topic: str) -> int | None:
self.cursor.execute("SELECT Sensors.`ID` FROM Sensors WHERE Sensors.`mqttTopic` = ?;", (topic,))
return self.cursor.fetchone()[0]
def get_unit_for_type(self, type_id: int) -> str | None:
self.cursor.execute("SELECT `unit` FROM `types` WHERE `ID` = ?;", (type_id,))
return self.cursor.fetchone()[0]
def get_all_topics(self) -> list[str]:
self.cursor.execute("SELECT `mqttTopic` FROM `sensors`;")
return [x[0] for x in self.cursor.fetchall()]
def create_sensor_reading(self, sensor_id: int, timestamp: datetime.datetime, reading: float) -> None:
self.cursor.execute("INSERT INTO `readings` (`sensorID`, `Timestamp`, `reading`) VALUES (?,?,?);",(sensor_id, timestamp, reading))
self.conn.commit()
self.emit_log(3, f"Inserted reading for sensor ID {sensor_id} at {timestamp}: {reading}")
if __name__ == "__main__":
a = DatabaseConnect()