aee0f09db8
- Datafabrik: Dockerfile fix, agentorkestrering fungerar - Vision: Identify-modell, FAISS, OCR alla testade - API: Alla 7 integrationstester passerade - Upplösare: Entitetsupplösning verifierad
83 lines
2.5 KiB
Python
83 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Hämta väderdata från SMHI
|
|
"""
|
|
import requests
|
|
import sqlite3
|
|
from datetime import datetime
|
|
|
|
DB_PATH = "/home/bernt/.openclaw/workspace/rivp-pilot-1/rivp.db"
|
|
|
|
def fetch_temperature(station_id="97530"):
|
|
"""Hämta temperatur från SMHI"""
|
|
url = f"https://opendata-download-metobs.smhi.se/api/version/latest/parameter/1/station/{station_id}/period/latest-hour/data.json"
|
|
|
|
try:
|
|
response = requests.get(url, timeout=30)
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
values = data.get("value", [])
|
|
if values:
|
|
latest = values[-1]
|
|
return {
|
|
"station": data["station"]["name"],
|
|
"temperature": float(latest["value"]),
|
|
"timestamp": latest["date"]
|
|
}
|
|
except Exception as e:
|
|
print(f"Fel: {e}")
|
|
|
|
return None
|
|
|
|
def fetch_precipitation(station_id="97530"):
|
|
"""Hämta nederbörd från SMHI"""
|
|
url = f"https://opendata-download-metobs.smhi.se/api/version/latest/parameter/7/station/{station_id}/period/latest-day/data.json"
|
|
|
|
try:
|
|
response = requests.get(url, timeout=30)
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
values = data.get("value", [])
|
|
if values:
|
|
total = sum(float(v["value"]) for v in values if float(v["value"]) >= 0)
|
|
return {
|
|
"station": data["station"]["name"],
|
|
"precipitation_mm": total
|
|
}
|
|
except Exception as e:
|
|
print(f"Fel: {e}")
|
|
|
|
return None
|
|
|
|
def save_weather_observation(road_id, weather_type, value, severity, source="smhi_real"):
|
|
"""Spara väderobservation"""
|
|
conn = sqlite3.connect(DB_PATH)
|
|
c = conn.cursor()
|
|
|
|
c.execute('''
|
|
INSERT INTO observations
|
|
(road_id, observation_type, latitude, longitude, confidence, severity, detected_date, source)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
''', (
|
|
road_id,
|
|
weather_type,
|
|
59.85, # Uppsala lat
|
|
17.65, # Uppsala lon
|
|
0.95,
|
|
severity,
|
|
datetime.now().strftime('%Y-%m-%d'),
|
|
source
|
|
))
|
|
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
if __name__ == "__main__":
|
|
temp = fetch_temperature()
|
|
precip = fetch_precipitation()
|
|
|
|
if temp:
|
|
print(f"Temperatur: {temp['temperature']}°C")
|
|
if precip:
|
|
print(f"Nederbörd: {precip['precipitation_mm']}mm")
|