Add web UI reboot/shutdown controls and system action API
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user