# WebConfig.py from datetime import datetime, timezone from zoneinfo import ZoneInfo def on_tickets_updated(ui, count): """Called after each successful ticket update — notifies the frontend.""" now = datetime.now(ZoneInfo("Europe/Zurich")).strftime("%H:%M:%S") ui.send_message("tickets_updated", {"count": count, "time": now}) def init_index_interface(ui): """ Registers the bridge between index.html and Python logic. """ def handle_status_request(client_id, data): """ Triggered when the frontend sends 'get_api_status'. Checks readiness and sends back 'api_status_data'. """ from Tools import check_api_readiness is_ready = check_api_readiness() print(f"DEBUG: API Readiness requested by {client_id}. Result: {is_ready}") # Send the response back to the UI ui.send_message("api_status_data", {"ready": is_ready}) # Register the listener ui.on_message("get_api_status", handle_status_request) def handle_get_arduino_config(client_id, data): from PersistentData import get_config ui.send_message("arduino_config", { "lcd_backlight": int(get_config("arduino_lcd_backlight") or 1), "temp_brightness": int(get_config("arduino_temp_brightness") or 7), "humi_brightness": int(get_config("arduino_humi_brightness") or 4), "co2_brightness": int(get_config("arduino_co2_brightness") or 2), "ring_brightness": int(get_config("arduino_ring_brightness") or 20), }) def handle_save_arduino_config(client_id, data): from PersistentData import save_config try: for key in ["lcd_backlight", "temp_brightness", "humi_brightness", "co2_brightness", "ring_brightness"]: if data.get(key) is not None: save_config(f"arduino_{key}", data[key]) ui.send_message("arduino_config_saved", {"ok": True}) except Exception as e: print(f"ERROR saving arduino config: {e}") ui.send_message("arduino_config_saved", {"ok": False}) ui.on_message("get_arduino_config", handle_get_arduino_config) ui.on_message("save_arduino_config", handle_save_arduino_config) def handle_cleanup_sensor_history(client_id, data): try: import os, PersistentData as PD from influxdb_client import InfluxDBClient url = os.environ.get("INFLUXDB_URL", "http://dbstorage-influx:8086") token = PD.get_config("influxdbToken") or os.environ.get("INFLUXDB_TOKEN", "") org = os.environ.get("INFLUXDB_ORG", "arduino") bucket = os.environ.get("INFLUXDB_BUCKET","arduinostorage") client = InfluxDBClient(url=url, token=token, org=org) delete_api = client.delete_api() start = "1970-01-01T00:00:00Z" stop = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") delete_api.delete(start, stop, "", bucket=bucket, org=org) client.close() print("INFO: Sensor history cleared.") ui.send_message("cleanup_done", {"ok": True, "label": "Sensor history"}) except Exception as e: print(f"ERROR clearing sensor history: {e}") ui.send_message("cleanup_done", {"ok": False, "label": "Sensor history"}) def handle_cleanup_ticket_history(client_id, data): try: import PersistentData PersistentData.db.execute_sql("DELETE FROM history") print("INFO: Ticket history cleared.") ui.send_message("cleanup_done", {"ok": True, "label": "Tickets history"}) except Exception as e: print(f"ERROR clearing ticket history: {e}") ui.send_message("cleanup_done", {"ok": False, "label": "Tickets history"}) ui.on_message("cleanup_sensor_history", handle_cleanup_sensor_history) ui.on_message("cleanup_ticket_history", handle_cleanup_ticket_history)