#!/usr/bin/env python3 """ LIFE Multi-Source Pipeline Hämtar data kontinuerligt från flera källor """ import sqlite3 import json import random from datetime import datetime, timedelta import time DB_PATH = "/home/bernt/.openclaw/workspace/rivp-pilot-1/rivp.db" def get_db(): conn = sqlite3.connect(DB_PATH) conn.row_factory = sqlite3.Row return conn def fetch_trafikverket_data(): """Simulerar hämtning från Trafikverket""" return [ {"road": "E4", "type": "ice", "severity": "high", "lat": 59.85, "lon": 17.65}, {"road": "E4", "type": "roadwork", "severity": "medium", "lat": 59.88, "lon": 17.72}, {"road": "272", "type": "flooding", "severity": "low", "lat": 59.92, "lon": 17.55}, ] def fetch_smhi_data(): """Simulerar hämtning från SMHI""" return [ {"location": "Uppsala", "weather": "snow", "temperature": -5, "impact": "high"}, {"location": "Stockholm", "weather": "rain", "temperature": 8, "impact": "medium"}, ] def fetch_quixzoom_data(): """Simulerar hämtning från quiXzoom contributors""" return [ {"road_id": 1, "type": "pothole", "confidence": 0.92, "lat": 59.85, "lon": 17.65}, {"road_id": 2, "type": "crack", "confidence": 0.78, "lat": 59.88, "lon": 17.72}, ] def process_and_save(source_name, data): """Bearbeta och spara data från varje källa""" conn = get_db() c = conn.cursor() count = 0 for item in data: if source_name == "trafikverket": c.execute("SELECT id FROM roads WHERE road_number = ?", (item["road"],)) result = c.fetchone() if result: road_id = result[0] c.execute(''' INSERT INTO observations (road_id, observation_type, latitude, longitude, confidence, severity, detected_date, source) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ''', (road_id, item["type"], item["lat"], item["lon"], 0.9, item["severity"], datetime.now().strftime('%Y-%m-%d'), source_name)) count += 1 elif source_name == "smhi": # SMHI-data påverkar alla vägar i området c.execute("SELECT id FROM roads WHERE county = ?", (item["location"],)) roads = c.fetchall() for road in roads: c.execute(''' INSERT INTO observations (road_id, observation_type, latitude, longitude, confidence, severity, detected_date, source) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ''', (road[0], item["weather"], 0, 0, 0.85, item["impact"], datetime.now().strftime('%Y-%m-%d'), source_name)) count += 1 elif source_name == "quixzoom": c.execute(''' INSERT INTO observations (road_id, observation_type, latitude, longitude, confidence, severity, detected_date, source) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ''', (item["road_id"], item["type"], item["lat"], item["lon"], item["confidence"], "medium", datetime.now().strftime('%Y-%m-%d'), source_name)) count += 1 conn.commit() conn.close() return count def run_pipeline(): """Kör komplett pipeline från alla källor""" print(f"[{datetime.now().isoformat()}] Running multi-source pipeline...") sources = { "trafikverket": fetch_trafikverket_data, "smhi": fetch_smhi_data, "quixzoom": fetch_quixzoom_data } total = 0 for source_name, fetch_func in sources.items(): try: data = fetch_func() saved = process_and_save(source_name, data) total += saved print(f" {source_name}: {saved} observations") except Exception as e: print(f" {source_name}: ERROR - {e}") print(f"[{datetime.now().isoformat()}] Pipeline complete: {total} total observations") return total if __name__ == "__main__": print("="*60) print("LIFE MULTI-SOURCE PIPELINE") print("="*60) run_pipeline() print("="*60)