You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kmftools/energie/db.py

48 lines
1.5 KiB

import mysql.connector
class Database:
def __init__(self, config, logger):
self.config = config
self.logger = logger
def fetch_current_price(self):
"""Haalt de huidige elektriciteitsprijs op uit de database."""
try:
conn = mysql.connector.connect(**self.config)
cursor = conn.cursor()
query = """
SELECT price_fr
FROM daillyprices
WHERE DATE(timestamp) = CURRENT_DATE
AND HOUR(timestamp) = HOUR(NOW());
"""
cursor.execute(query)
result = cursor.fetchone()
cursor.close()
conn.close()
return result[0] if result else None
except Exception as e:
self.logger.error(f"Database fout: {e}")
return None
def fetch_daily_costs(self):
try:
conn = mysql.connector.connect(**self.config)
cursor = conn.cursor()
query = """
SELECT SUM(el_costs) AS total_costs_today
FROM energy_costs
WHERE DATE(starttime) = CURDATE();
"""
cursor.execute(query)
result = cursor.fetchone()
if result:
return result[0] # De eerste kolom is de som van de kosten
else:
return None
except Exception as e:
self.logger.error(
f"Fout bij het ophalen van de dagelijkse kosten: {e}")
return None