Add web UI reboot/shutdown controls and system action API

This commit is contained in:
2026-02-13 02:35:50 +01:00
parent 0d4e769292
commit 761fc49461
3 changed files with 71 additions and 1 deletions

View File

@@ -1,5 +1,7 @@
import json
import queue
import subprocess
import threading
from typing import Any, Dict
from flask import Flask, Response, jsonify, render_template, request, stream_with_context
@@ -70,6 +72,27 @@ class WebPortal:
self.network_manager.refresh_state()
return jsonify({"ok": True, "message": message})
@self.app.route("/api/system/<action>", methods=["POST"])
def system_action(action: str) -> Response:
commands = {
"reboot": ["systemctl", "reboot"],
"shutdown": ["systemctl", "poweroff"],
}
cmd = commands.get(action)
if cmd is None:
return jsonify({"ok": False, "message": "Unknown action"}), 400
self.state.update_status(f"System action requested: {action}", "")
def _exec() -> None:
try:
subprocess.run(cmd, capture_output=True, text=True, timeout=20, check=False)
except Exception:
pass
threading.Thread(target=_exec, daemon=True, name=f"system-{action}").start()
return jsonify({"ok": True, "message": f"{action} triggered"})
@self.app.route("/events/serial")
def serial_events() -> Response:
@stream_with_context