#!/usr/bin/env python3 """Simple web server: one button to turn off all kids' devices.""" import json from http.server import HTTPServer, BaseHTTPRequestHandler from devices import shutdown_all, status_all, curfew_status, _set_budget, _get_budget, BUDGET_SECONDS, TICK_INTERVAL class Handler(BaseHTTPRequestHandler): def do_GET(self): if self.path == "/" or self.path == "/index.html": self.send_response(200) self.send_header("Content-Type", "text/html; charset=utf-8") self.end_headers() self.wfile.write(HTML.encode()) elif self.path == "/status": devices_status, curfew = status_all() response = {"devices": devices_status, "curfew": curfew} self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(json.dumps(response).encode()) else: self.send_error(404) def do_POST(self): content_length = int(self.headers.get("Content-Length", 0)) body = self.rfile.read(content_length).decode() if content_length > 0 else "" if self.path == "/run": try: actions = shutdown_all() status = "ok" except Exception as e: actions = [{"icon": "❌", "title": "Error", "detail": str(e)}] status = "error" self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(json.dumps({"status": status, "actions": actions}).encode()) elif self.path == "/budget": try: data = json.loads(body) if body else {} dev_id = data.get("device", "") delta = int(data.get("delta", 0)) current = _get_budget(dev_id) new_budget = max(0, min(BUDGET_SECONDS, current + delta)) _set_budget(dev_id, new_budget) self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(json.dumps({"status": "ok"}).encode()) except Exception as e: self.send_response(400) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(json.dumps({"status": "error", "detail": str(e)}).encode()) else: self.send_error(404) def log_message(self, format, *args): pass HTML = """\ Kids Devices — OFF

📺 Kids Devices

TV + Gabi's computer + Gaja's computer

""" if __name__ == "__main__": from devices import start_timer port = int(__import__("os").environ.get("PORT", "10000")) start_timer() server = HTTPServer(("0.0.0.0", port), Handler) print(f"🚀 Open http://localhost:{port}") print(f"⏱️ Budget timer running (checks every {TICK_INTERVAL}s, reset at 7:00 AM)") try: server.serve_forever() except KeyboardInterrupt: from devices import stop_timer stop_timer()