mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-29 11:26:25 -05:00
controller: de-duplicate the hardware toggles
The Hotspot, Bluetooth, Desktop, VNC and Throttle toggles had each been written out longhand at four layers. Collapse the shared parts: - useSubsystemToggle() replaces five near-identical hooks. This also fixes a latent bug: only useDesktop/useVnc restored state when a request failed, so a failed Bluetooth/Throttle/Hotspot toggle left at null and — since the status poll sees no change — the toggle stayed disabled and read as unsupported until it remounted. - SubsystemToggle renders all five, with the Desktop's stop confirmation and VNC's desktop gating as declared options rather than bespoke copies. - The fallback UI's four update*/toggle* pairs become one config-driven implementation (-100 lines). - _raise_for_action() centralises the 13 endpoint failure checks, including VNC's 409-vs-500 split. Deliberately not shared: the six get_*_status_dict() builders (payloads genuinely differ) and the per-subsystem mechanisms (nmcli/rfkill/sysfs/systemctl). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
5313ab57ef
commit
07af0c9f28
5 changed files with 410 additions and 515 deletions
|
|
@ -69,6 +69,20 @@ from controller.lib.samba import (
|
|||
router = APIRouter(tags=["admin"])
|
||||
|
||||
|
||||
def _raise_for_action(result: dict):
|
||||
"""
|
||||
Turn a failed subsystem action into an HTTPException.
|
||||
|
||||
Actions all report `{"success": bool, "error": str}`. A precondition failure
|
||||
(VNC needs a running desktop) is a 409 so callers can tell it from a command
|
||||
that was attempted and failed.
|
||||
"""
|
||||
if result.get("success"):
|
||||
return
|
||||
status_code = 409 if result.get("precondition_failed") else 500
|
||||
raise HTTPException(status_code=status_code, detail=result.get("error", "Failed"))
|
||||
|
||||
|
||||
# --- Hotspot ---
|
||||
|
||||
@router.get("/api/hotspot/status", response_model=HotspotStatusResponse)
|
||||
|
|
@ -119,8 +133,7 @@ async def hotspot_settings_update(request: HotspotSettingsRequest) -> HotspotSet
|
|||
async def hotspot_start() -> HotspotActionResponse:
|
||||
"""Start the WiFi hotspot."""
|
||||
result = start_hotspot()
|
||||
if not result.get("success"):
|
||||
raise HTTPException(status_code=500, detail=result.get("error", "Failed"))
|
||||
_raise_for_action(result)
|
||||
return HotspotActionResponse(**result)
|
||||
|
||||
|
||||
|
|
@ -128,8 +141,7 @@ async def hotspot_start() -> HotspotActionResponse:
|
|||
async def hotspot_stop() -> HotspotActionResponse:
|
||||
"""Stop the WiFi hotspot by turning the WiFi radio off."""
|
||||
result = stop_hotspot()
|
||||
if not result.get("success"):
|
||||
raise HTTPException(status_code=500, detail=result.get("error", "Failed"))
|
||||
_raise_for_action(result)
|
||||
return HotspotActionResponse(**result)
|
||||
|
||||
|
||||
|
|
@ -145,8 +157,7 @@ async def bluetooth_status() -> BluetoothStatusResponse:
|
|||
async def bluetooth_unblock() -> BluetoothActionResponse:
|
||||
"""Unblock the Bluetooth radio (turn it on)."""
|
||||
result = await unblock_bluetooth()
|
||||
if not result.get("success"):
|
||||
raise HTTPException(status_code=500, detail=result.get("error", "Failed"))
|
||||
_raise_for_action(result)
|
||||
return BluetoothActionResponse(**result)
|
||||
|
||||
|
||||
|
|
@ -154,8 +165,7 @@ async def bluetooth_unblock() -> BluetoothActionResponse:
|
|||
async def bluetooth_block() -> BluetoothActionResponse:
|
||||
"""Block the Bluetooth radio (turn it off)."""
|
||||
result = await block_bluetooth()
|
||||
if not result.get("success"):
|
||||
raise HTTPException(status_code=500, detail=result.get("error", "Failed"))
|
||||
_raise_for_action(result)
|
||||
return BluetoothActionResponse(**result)
|
||||
|
||||
|
||||
|
|
@ -171,8 +181,7 @@ async def desktop_status() -> DesktopStatusResponse:
|
|||
async def desktop_start() -> DesktopActionResponse:
|
||||
"""Start the graphical desktop until stopped or reboot."""
|
||||
result = await start_desktop()
|
||||
if not result.get("success"):
|
||||
raise HTTPException(status_code=500, detail=result.get("error", "Failed"))
|
||||
_raise_for_action(result)
|
||||
return DesktopActionResponse(**result)
|
||||
|
||||
|
||||
|
|
@ -180,8 +189,7 @@ async def desktop_start() -> DesktopActionResponse:
|
|||
async def desktop_stop() -> DesktopActionResponse:
|
||||
"""Stop the graphical desktop; it returns on the next boot (fail open)."""
|
||||
result = await stop_desktop()
|
||||
if not result.get("success"):
|
||||
raise HTTPException(status_code=500, detail=result.get("error", "Failed"))
|
||||
_raise_for_action(result)
|
||||
return DesktopActionResponse(**result)
|
||||
|
||||
|
||||
|
|
@ -197,10 +205,7 @@ async def vnc_status() -> VncStatusResponse:
|
|||
async def vnc_start() -> VncActionResponse:
|
||||
"""Start the VNC server; requires a running desktop."""
|
||||
result = await start_vnc()
|
||||
if not result.get("success"):
|
||||
# 409 distinguishes "the desktop is not running" from a systemctl failure.
|
||||
status_code = 409 if result.get("precondition_failed") else 500
|
||||
raise HTTPException(status_code=status_code, detail=result.get("error", "Failed"))
|
||||
_raise_for_action(result)
|
||||
return VncActionResponse(success=True)
|
||||
|
||||
|
||||
|
|
@ -208,8 +213,7 @@ async def vnc_start() -> VncActionResponse:
|
|||
async def vnc_stop() -> VncActionResponse:
|
||||
"""Stop the VNC server; allowed even when the desktop is stopped."""
|
||||
result = await stop_vnc()
|
||||
if not result.get("success"):
|
||||
raise HTTPException(status_code=500, detail=result.get("error", "Failed"))
|
||||
_raise_for_action(result)
|
||||
return VncActionResponse(success=True)
|
||||
|
||||
|
||||
|
|
@ -251,8 +255,7 @@ async def throttle_status() -> ThrottleStatusResponse:
|
|||
async def throttle_enable() -> ThrottleActionResponse:
|
||||
"""Enable CPU throttle (powersave mode)."""
|
||||
result = enable_throttle()
|
||||
if not result.get("success"):
|
||||
raise HTTPException(status_code=500, detail=result.get("error", "Failed"))
|
||||
_raise_for_action(result)
|
||||
return ThrottleActionResponse(**result)
|
||||
|
||||
|
||||
|
|
@ -260,8 +263,7 @@ async def throttle_enable() -> ThrottleActionResponse:
|
|||
async def throttle_disable() -> ThrottleActionResponse:
|
||||
"""Disable CPU throttle (normal performance)."""
|
||||
result = disable_throttle()
|
||||
if not result.get("success"):
|
||||
raise HTTPException(status_code=500, detail=result.get("error", "Failed"))
|
||||
_raise_for_action(result)
|
||||
return ThrottleActionResponse(**result)
|
||||
|
||||
|
||||
|
|
@ -277,8 +279,7 @@ async def timezone_status() -> TimezoneStatusResponse:
|
|||
async def timezone_set(request: TimezoneSetRequest) -> TimezoneSetResponse:
|
||||
"""Set the system timezone."""
|
||||
result = set_timezone(request.timezone)
|
||||
if not result.get("success"):
|
||||
raise HTTPException(status_code=500, detail=result.get("error", "Failed"))
|
||||
_raise_for_action(result)
|
||||
return TimezoneSetResponse(**result)
|
||||
|
||||
|
||||
|
|
@ -294,8 +295,7 @@ async def system_shutdown() -> SystemActionResponse:
|
|||
)
|
||||
|
||||
result = shutdown_system()
|
||||
if not result.get("success"):
|
||||
raise HTTPException(status_code=500, detail=result.get("error", "Failed"))
|
||||
_raise_for_action(result)
|
||||
return SystemActionResponse(**result)
|
||||
|
||||
|
||||
|
|
@ -309,8 +309,7 @@ async def system_reboot() -> SystemActionResponse:
|
|||
)
|
||||
|
||||
result = reboot_system()
|
||||
if not result.get("success"):
|
||||
raise HTTPException(status_code=500, detail=result.get("error", "Failed"))
|
||||
_raise_for_action(result)
|
||||
return SystemActionResponse(**result)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -972,10 +972,10 @@
|
|||
<h2>System Actions</h2>
|
||||
{% if not docker_mode %}
|
||||
<div class="action-buttons" style="margin-bottom: 1rem;">
|
||||
<button id="hotspot-btn" class="btn" onclick="toggleHotspot()" disabled>Loading...</button>
|
||||
<button id="bluetooth-btn" class="btn" onclick="toggleBluetooth()" disabled>Loading...</button>
|
||||
<button id="desktop-btn" class="btn" onclick="toggleDesktop()" disabled>Loading...</button>
|
||||
<button id="vnc-btn" class="btn" onclick="toggleVnc()" disabled>Loading...</button>
|
||||
<button id="hotspot-btn" class="btn" onclick="toggleSubsystem('hotspot')" disabled>Loading...</button>
|
||||
<button id="bluetooth-btn" class="btn" onclick="toggleSubsystem('bluetooth')" disabled>Loading...</button>
|
||||
<button id="desktop-btn" class="btn" onclick="toggleSubsystem('desktop')" disabled>Loading...</button>
|
||||
<button id="vnc-btn" class="btn" onclick="toggleSubsystem('vnc')" disabled>Loading...</button>
|
||||
</div>
|
||||
<details style="margin-bottom: 1rem;">
|
||||
<summary style="cursor: pointer;">Hotspot Settings</summary>
|
||||
|
|
@ -1384,48 +1384,101 @@
|
|||
}
|
||||
}
|
||||
|
||||
// --- Hotspot ---
|
||||
let hotspotEnabled = null;
|
||||
// --- Hardware toggle buttons ---
|
||||
// The Hotspot, Bluetooth, Desktop and VNC buttons all behave the same way: read a
|
||||
// status endpoint, label the button from it, and POST start/stop when clicked.
|
||||
const TOGGLE_BUTTONS = {
|
||||
hotspot: {
|
||||
id: 'hotspot-btn', label: 'Hotspot',
|
||||
statusUrl: 'api/hotspot/status', startUrl: 'api/hotspot/start', stopUrl: 'api/hotspot/stop',
|
||||
isOn: data => data.enabled,
|
||||
},
|
||||
bluetooth: {
|
||||
id: 'bluetooth-btn', label: 'Bluetooth',
|
||||
statusUrl: 'api/bluetooth/status', startUrl: 'api/bluetooth/unblock', stopUrl: 'api/bluetooth/block',
|
||||
isOn: data => data.enabled,
|
||||
startText: 'Enable Bluetooth', stopText: 'Disable Bluetooth',
|
||||
},
|
||||
desktop: {
|
||||
id: 'desktop-btn', label: 'Desktop',
|
||||
statusUrl: 'api/desktop/status', startUrl: 'api/desktop/start', stopUrl: 'api/desktop/stop',
|
||||
isOn: data => data.running,
|
||||
confirmStop: 'Stop the desktop? Anyone using this WROLPi\'s own screen will lose their session'
|
||||
+ ' and the display will drop to a terminal. The desktop will return on the next reboot.',
|
||||
// Starting or stopping the desktop changes whether VNC can run.
|
||||
alsoRefresh: ['vnc'],
|
||||
},
|
||||
vnc: {
|
||||
id: 'vnc-btn', label: 'VNC',
|
||||
statusUrl: 'api/vnc/status', startUrl: 'api/vnc/start', stopUrl: 'api/vnc/stop',
|
||||
isOn: data => data.running,
|
||||
// VNC serves the desktop session, so it cannot be started without one.
|
||||
canStart: data => data.can_start,
|
||||
onTitle: data => data.desktop_running ? '' : 'The Desktop is stopped, so VNC clients see nothing',
|
||||
},
|
||||
};
|
||||
|
||||
async function updateHotspotButton() {
|
||||
const btn = document.getElementById('hotspot-btn');
|
||||
const toggleStates = {};
|
||||
|
||||
async function updateToggleButton(name) {
|
||||
const config = TOGGLE_BUTTONS[name];
|
||||
const btn = document.getElementById(config.id);
|
||||
if (!btn) return;
|
||||
const startText = config.startText || `Start ${config.label}`;
|
||||
const stopText = config.stopText || `Stop ${config.label}`;
|
||||
try {
|
||||
const data = await apiCall('api/hotspot/status');
|
||||
hotspotEnabled = data.enabled;
|
||||
const data = await apiCall(config.statusUrl);
|
||||
toggleStates[name] = config.isOn(data);
|
||||
btn.title = '';
|
||||
if (!data.available) {
|
||||
btn.textContent = 'Hotspot Unavailable';
|
||||
btn.textContent = `${config.label} Unavailable`;
|
||||
btn.disabled = true;
|
||||
btn.className = 'btn';
|
||||
} else if (data.enabled) {
|
||||
btn.textContent = 'Stop Hotspot';
|
||||
btn.title = data.reason || '';
|
||||
} else if (toggleStates[name]) {
|
||||
btn.textContent = stopText;
|
||||
btn.disabled = false;
|
||||
btn.className = 'btn btn-warning';
|
||||
if (config.onTitle) btn.title = config.onTitle(data);
|
||||
} else if (config.canStart && !config.canStart(data)) {
|
||||
// Installed and stopped, but something else must happen first.
|
||||
btn.textContent = startText;
|
||||
btn.disabled = true;
|
||||
btn.className = 'btn';
|
||||
btn.title = data.reason || '';
|
||||
} else {
|
||||
btn.textContent = 'Start Hotspot';
|
||||
btn.textContent = startText;
|
||||
btn.disabled = false;
|
||||
btn.className = 'btn btn-primary';
|
||||
}
|
||||
} catch (e) {
|
||||
btn.textContent = 'Hotspot Unavailable';
|
||||
btn.textContent = `${config.label} Unavailable`;
|
||||
btn.disabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
async function toggleHotspot() {
|
||||
const btn = document.getElementById('hotspot-btn');
|
||||
async function refreshToggleButtons() {
|
||||
await Promise.all(Object.keys(TOGGLE_BUTTONS).map(name => updateToggleButton(name)));
|
||||
}
|
||||
|
||||
async function toggleSubsystem(name) {
|
||||
const config = TOGGLE_BUTTONS[name];
|
||||
const btn = document.getElementById(config.id);
|
||||
const on = toggleStates[name];
|
||||
if (on && config.confirmStop && !confirm(config.confirmStop)) {
|
||||
return;
|
||||
}
|
||||
btn.disabled = true;
|
||||
btn.innerHTML = '<span class="loading"></span>';
|
||||
try {
|
||||
if (hotspotEnabled) {
|
||||
await apiCall('api/hotspot/stop', 'POST');
|
||||
} else {
|
||||
await apiCall('api/hotspot/start', 'POST');
|
||||
}
|
||||
await apiCall(on ? config.stopUrl : config.startUrl, 'POST');
|
||||
} catch (e) {
|
||||
alert(`Hotspot error: ${e.message}`);
|
||||
alert(`${config.label} error: ${e.message}`);
|
||||
}
|
||||
await updateToggleButton(name);
|
||||
for (const other of config.alsoRefresh || []) {
|
||||
await updateToggleButton(other);
|
||||
}
|
||||
await updateHotspotButton();
|
||||
}
|
||||
|
||||
async function loadHotspotSettings() {
|
||||
|
|
@ -1496,160 +1549,13 @@
|
|||
device: document.getElementById('hotspot-device').value,
|
||||
protocol: document.getElementById('hotspot-protocol').value || null,
|
||||
});
|
||||
await updateHotspotButton();
|
||||
await updateToggleButton('hotspot');
|
||||
} catch (e) {
|
||||
alert(`Failed to save hotspot settings: ${e.message}`);
|
||||
}
|
||||
btn.disabled = false;
|
||||
}
|
||||
|
||||
// --- Bluetooth ---
|
||||
let bluetoothEnabled = null;
|
||||
|
||||
async function updateBluetoothButton() {
|
||||
const btn = document.getElementById('bluetooth-btn');
|
||||
if (!btn) return;
|
||||
try {
|
||||
const data = await apiCall('api/bluetooth/status');
|
||||
bluetoothEnabled = data.enabled;
|
||||
if (!data.available) {
|
||||
btn.textContent = 'Bluetooth Unavailable';
|
||||
btn.disabled = true;
|
||||
btn.className = 'btn';
|
||||
} else if (data.enabled) {
|
||||
btn.textContent = 'Disable Bluetooth';
|
||||
btn.disabled = false;
|
||||
btn.className = 'btn btn-warning';
|
||||
} else {
|
||||
btn.textContent = 'Enable Bluetooth';
|
||||
btn.disabled = false;
|
||||
btn.className = 'btn btn-primary';
|
||||
}
|
||||
} catch (e) {
|
||||
btn.textContent = 'Bluetooth Unavailable';
|
||||
btn.disabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
async function toggleBluetooth() {
|
||||
const btn = document.getElementById('bluetooth-btn');
|
||||
btn.disabled = true;
|
||||
btn.innerHTML = '<span class="loading"></span>';
|
||||
try {
|
||||
if (bluetoothEnabled) {
|
||||
await apiCall('api/bluetooth/block', 'POST');
|
||||
} else {
|
||||
await apiCall('api/bluetooth/unblock', 'POST');
|
||||
}
|
||||
} catch (e) {
|
||||
alert(`Bluetooth error: ${e.message}`);
|
||||
}
|
||||
await updateBluetoothButton();
|
||||
}
|
||||
|
||||
// --- Desktop ---
|
||||
let desktopRunning = null;
|
||||
|
||||
async function updateDesktopButton() {
|
||||
const btn = document.getElementById('desktop-btn');
|
||||
if (!btn) return;
|
||||
try {
|
||||
const data = await apiCall('api/desktop/status');
|
||||
desktopRunning = data.running;
|
||||
if (!data.available) {
|
||||
btn.textContent = 'Desktop Unavailable';
|
||||
btn.disabled = true;
|
||||
btn.className = 'btn';
|
||||
} else if (data.running) {
|
||||
btn.textContent = 'Stop Desktop';
|
||||
btn.disabled = false;
|
||||
btn.className = 'btn btn-warning';
|
||||
} else {
|
||||
btn.textContent = 'Start Desktop';
|
||||
btn.disabled = false;
|
||||
btn.className = 'btn btn-primary';
|
||||
}
|
||||
} catch (e) {
|
||||
btn.textContent = 'Desktop Unavailable';
|
||||
btn.disabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
async function toggleDesktop() {
|
||||
const btn = document.getElementById('desktop-btn');
|
||||
if (desktopRunning && !confirm(
|
||||
'Stop the desktop? Anyone using this WROLPi\'s own screen will lose their session'
|
||||
+ ' and the display will drop to a terminal. The desktop will return on the next reboot.')) {
|
||||
return;
|
||||
}
|
||||
btn.disabled = true;
|
||||
btn.innerHTML = '<span class="loading"></span>';
|
||||
try {
|
||||
if (desktopRunning) {
|
||||
await apiCall('api/desktop/stop', 'POST');
|
||||
} else {
|
||||
await apiCall('api/desktop/start', 'POST');
|
||||
}
|
||||
} catch (e) {
|
||||
alert(`Desktop error: ${e.message}`);
|
||||
}
|
||||
await updateDesktopButton();
|
||||
await updateVncButton();
|
||||
}
|
||||
|
||||
// --- VNC ---
|
||||
let vncRunning = null;
|
||||
|
||||
async function updateVncButton() {
|
||||
const btn = document.getElementById('vnc-btn');
|
||||
if (!btn) return;
|
||||
try {
|
||||
const data = await apiCall('api/vnc/status');
|
||||
vncRunning = data.running;
|
||||
if (!data.available) {
|
||||
btn.textContent = 'VNC Unavailable';
|
||||
btn.disabled = true;
|
||||
btn.className = 'btn';
|
||||
btn.title = data.reason || '';
|
||||
} else if (data.running) {
|
||||
btn.textContent = 'Stop VNC';
|
||||
btn.disabled = false;
|
||||
btn.className = 'btn btn-warning';
|
||||
// VNC serves the desktop session, so say when there is nothing to serve.
|
||||
btn.title = data.desktop_running ? '' : 'The Desktop is stopped, so VNC clients see nothing';
|
||||
} else if (!data.can_start) {
|
||||
btn.textContent = 'Start VNC';
|
||||
btn.disabled = true;
|
||||
btn.className = 'btn';
|
||||
btn.title = data.reason || '';
|
||||
} else {
|
||||
btn.textContent = 'Start VNC';
|
||||
btn.disabled = false;
|
||||
btn.className = 'btn btn-primary';
|
||||
btn.title = '';
|
||||
}
|
||||
} catch (e) {
|
||||
btn.textContent = 'VNC Unavailable';
|
||||
btn.disabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
async function toggleVnc() {
|
||||
const btn = document.getElementById('vnc-btn');
|
||||
btn.disabled = true;
|
||||
btn.innerHTML = '<span class="loading"></span>';
|
||||
try {
|
||||
if (vncRunning) {
|
||||
await apiCall('api/vnc/stop', 'POST');
|
||||
} else {
|
||||
await apiCall('api/vnc/start', 'POST');
|
||||
}
|
||||
} catch (e) {
|
||||
alert(`VNC error: ${e.message}`);
|
||||
}
|
||||
await updateVncButton();
|
||||
}
|
||||
|
||||
async function waitForController(maxWaitMs = 60000) {
|
||||
const start = Date.now();
|
||||
while (Date.now() - start < maxWaitMs) {
|
||||
|
|
@ -3030,10 +2936,7 @@
|
|||
const updateInterval = 10000;
|
||||
|
||||
// Fetch hardware button status immediately
|
||||
updateHotspotButton();
|
||||
updateBluetoothButton();
|
||||
updateDesktopButton();
|
||||
updateVncButton();
|
||||
refreshToggleButtons();
|
||||
loadHotspotSettings();
|
||||
|
||||
// Initial update after a short delay (to not overlap with initial load)
|
||||
|
|
@ -3050,10 +2953,7 @@
|
|||
updateServicesTable();
|
||||
loadDisks();
|
||||
loadSmart();
|
||||
updateHotspotButton();
|
||||
updateBluetoothButton();
|
||||
updateDesktopButton();
|
||||
updateVncButton();
|
||||
refreshToggleButtons();
|
||||
}, updateInterval);
|
||||
}
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue