|
1 | 1 | import sqlite3 |
2 | 2 | import datetime |
3 | 3 | from config import DB_FILE, TRAPEZOID_SQL |
4 | | -from utils import get_historical_avg_temp, calculate_eur |
| 4 | +from utils import get_historical_weather_data, calculate_eur |
5 | 5 |
|
6 | 6 | def get_db_connection(timeout=None): |
7 | 7 | """Hilfsfunktion für eine saubere DB-Verbindung.""" |
@@ -43,13 +43,33 @@ def init_db(): |
43 | 43 | avg_clouds REAL, |
44 | 44 | max_w REAL, |
45 | 45 | avg_temp REAL, |
| 46 | + daylight_duration REAL, |
| 47 | + sunshine_duration REAL, |
46 | 48 | max_w_panel1 REAL, |
47 | 49 | max_w_panel2 REAL, |
48 | 50 | kwh_panel1 REAL, |
49 | 51 | kwh_panel2 REAL, |
50 | 52 | kwh_dc_total REAL |
51 | 53 | ) |
52 | 54 | ''') |
| 55 | + |
| 56 | + # 2. AUTOMATISCHES UPGRADE für neue Spalten |
| 57 | + c.execute("PRAGMA table_info(daily_stats)") |
| 58 | + existing_columns = [col[1] for col in c.fetchall()] |
| 59 | + |
| 60 | + if "daylight_duration" not in existing_columns: |
| 61 | + print("Migriere Datenbank: Spalte daylight_duration wird hinzugefügt...") |
| 62 | + c.execute("ALTER TABLE daily_stats ADD COLUMN daylight_duration REAL") |
| 63 | + |
| 64 | + if "sunshine_duration" not in existing_columns: |
| 65 | + print("Migriere Datenbank: Spalte sunshine_duration wird hinzugefügt...") |
| 66 | + c.execute("ALTER TABLE daily_stats ADD COLUMN sunshine_duration REAL") |
| 67 | + |
| 68 | + # 3. Restliche Spalten (max_w_panel1, etc.) sicherstellen |
| 69 | + # Falls du die auch noch nicht hast, kannst du das Muster einfach fortsetzen: |
| 70 | + for col in ["max_w_panel1", "max_w_panel2", "kwh_panel1", "kwh_panel2", "kwh_dc_total"]: |
| 71 | + if col not in existing_columns: |
| 72 | + c.execute(f"ALTER TABLE daily_stats ADD COLUMN {col} REAL") |
53 | 73 |
|
54 | 74 | # 3. Globale Gesamt-Statistiken |
55 | 75 | c.execute(''' |
@@ -134,7 +154,10 @@ def finalize_day(day): |
134 | 154 | wh_p1 = float(row[5]) if row[5] is not None else 0.0 |
135 | 155 | wh_p2 = float(row[6]) if row[6] is not None else 0.0 |
136 | 156 | wh_dc = float(row[7]) if row[7] is not None else 0.0 |
137 | | - avg_temp = get_historical_avg_temp(day) |
| 157 | + weather = get_historical_weather_data(day) |
| 158 | + avg_temp = weather["temp"] |
| 159 | + daylight_s = weather["daylight_duration"] |
| 160 | + sunshine_s = weather["sunshine_duration"] |
138 | 161 |
|
139 | 162 | kwh = total_wh / 1000.0 |
140 | 163 | kwh_p1 = wh_p1 / 1000.0 |
@@ -168,14 +191,16 @@ def get_price_for_date(date_str): |
168 | 191 |
|
169 | 192 | c.execute(""" |
170 | 193 | INSERT OR REPLACE INTO daily_stats |
171 | | - (day, kwh, eur, avg_clouds, avg_temp, max_w, max_w_panel1, max_w_panel2, kwh_panel1, kwh_panel2, kwh_dc_total) |
172 | | - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
| 194 | + (day, kwh, eur, avg_clouds, avg_temp, daylight_duration, sunshine_duration, max_w, max_w_panel1, max_w_panel2, kwh_panel1, kwh_panel2, kwh_dc_total) |
| 195 | + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
173 | 196 | """, ( |
174 | 197 | day, |
175 | 198 | kwh_db, |
176 | 199 | eur_db, |
177 | 200 | round(avg_clouds, 2), |
178 | 201 | round(avg_temp, 2), |
| 202 | + round(daylight_s, 1), |
| 203 | + round(sunshine_s, 1), # In Sekunden |
179 | 204 | round(max_w, 1), |
180 | 205 | round(max_w_p1, 1), |
181 | 206 | round(max_w_p2, 1), |
|
0 commit comments