Skip to content

Commit 511a855

Browse files
author
jacquesbach
committed
The sunshine_duration and daylight_duration in seconds are now stored in the daily_stats table.
1 parent 29fb7b8 commit 511a855

3 files changed

Lines changed: 46 additions & 21 deletions

File tree

database.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sqlite3
22
import datetime
33
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
55

66
def get_db_connection(timeout=None):
77
"""Hilfsfunktion für eine saubere DB-Verbindung."""
@@ -43,13 +43,33 @@ def init_db():
4343
avg_clouds REAL,
4444
max_w REAL,
4545
avg_temp REAL,
46+
daylight_duration REAL,
47+
sunshine_duration REAL,
4648
max_w_panel1 REAL,
4749
max_w_panel2 REAL,
4850
kwh_panel1 REAL,
4951
kwh_panel2 REAL,
5052
kwh_dc_total REAL
5153
)
5254
''')
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")
5373

5474
# 3. Globale Gesamt-Statistiken
5575
c.execute('''
@@ -134,7 +154,10 @@ def finalize_day(day):
134154
wh_p1 = float(row[5]) if row[5] is not None else 0.0
135155
wh_p2 = float(row[6]) if row[6] is not None else 0.0
136156
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"]
138161

139162
kwh = total_wh / 1000.0
140163
kwh_p1 = wh_p1 / 1000.0
@@ -168,14 +191,16 @@ def get_price_for_date(date_str):
168191

169192
c.execute("""
170193
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
173196
""", (
174197
day,
175198
kwh_db,
176199
eur_db,
177200
round(avg_clouds, 2),
178201
round(avg_temp, 2),
202+
round(daylight_s, 1),
203+
round(sunshine_s, 1), # In Sekunden
179204
round(max_w, 1),
180205
round(max_w_p1, 1),
181206
round(max_w_p2, 1),

routes.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,12 +1043,3 @@ def shap_summary():
10431043
]
10441044

10451045
return jsonify(sorted(result, key=lambda x: x["mean_abs_shap"], reverse=True))
1046-
1047-
1048-
1049-
1050-
1051-
1052-
1053-
# ... (Kopiere hier die restlichen Routen wie /api/data, /api/roi, /api/heatmap etc. hinein)
1054-
# Ersetze dabei immer @app.route durch @api_bp.route

utils.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ def calculate_sun_elevation(date):
2828
elevation = 90 - abs(LATITUDE - decl)
2929
return max(elevation, 0)
3030

31-
def get_historical_avg_temp(day):
31+
def get_historical_weather_data(day):
3232
"""
33-
Holt Tagesmitteltemperatur von OpenMeteo Historical API.
33+
Holt Wetterdaten (Temp, Daylight, Sunshine) von OpenMeteo Historical API.
34+
Einheiten: Temp (°C), Daylight/Sunshine (Sekunden).
3435
"""
3536
try:
3637
url = (
@@ -39,17 +40,25 @@ def get_historical_avg_temp(day):
3940
f"&longitude={LONGITUDE}"
4041
f"&start_date={day}"
4142
f"&end_date={day}"
42-
f"&daily=temperature_2m_mean"
43+
f"&daily=temperature_2m_mean,daylight_duration,sunshine_duration"
4344
f"&timezone=Europe/Berlin"
4445
)
4546
r = requests.get(url, timeout=5)
4647
data = r.json().get("daily", {})
47-
temps = data.get("temperature_2m_mean", [])
48-
if temps:
49-
return float(temps[0])
48+
49+
# Extraktion mit Fallback auf 0.0
50+
temp = data.get("temperature_2m_mean", [0.0])[0]
51+
daylight = data.get("daylight_duration", [0.0])[0]
52+
sunshine = data.get("sunshine_duration", [0.0])[0]
53+
54+
return {
55+
"temp": float(temp) if temp is not None else 0.0,
56+
"daylight_duration": float(daylight) if daylight is not None else 0.0,
57+
"sunshine_duration": float(sunshine) if sunshine is not None else 0.0
58+
}
5059
except Exception as e:
51-
print("Historical Temp Error:", e)
52-
return 0.0
60+
print(f"Historical Weather Error ({day}):", e)
61+
return {"temp": 0.0, "daylight_duration": 0.0, "sunshine_duration": 0.0}
5362

5463
def get_weather_forecast(days=7):
5564
"""Holt die Wettervorhersage für die nächsten Tage."""

0 commit comments

Comments
 (0)