2026-03-02 23:25:57 +01:00
|
|
|
"""Unraid servers status router — MQTT-only data source."""
|
2026-03-02 01:48:51 +01:00
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
from typing import Any, Dict, List
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter
|
|
|
|
|
|
feat: add Admin Panel with JWT auth, DB settings, and integration management
Complete admin backend with login, where all integrations (weather, news,
Home Assistant, Vikunja, Unraid, MQTT) can be configured via web UI instead
of ENV variables. Two-layer config: ENV seeds DB on first start, then DB
is source of truth. Auto-migration system on startup.
Backend: db.py shared pool, auth.py JWT, settings_service CRUD, seed_service,
admin router (protected), test_connections per integration, config.py rewrite.
Frontend: react-router v6, login page, admin layout with sidebar, 8 settings
pages (General, Weather, News, HA, Vikunja, Unraid, MQTT, ChangePassword),
shared IntegrationForm + TestButton components.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-02 10:37:30 +01:00
|
|
|
from server.config import get_settings
|
2026-03-02 22:41:16 +01:00
|
|
|
from server.services.mqtt_service import mqtt_service
|
2026-03-02 23:25:57 +01:00
|
|
|
from server.services.unraid_service import ServerConfig, fetch_all_servers_mqtt
|
2026-03-02 01:48:51 +01:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/api", tags=["servers"])
|
|
|
|
|
|
2026-03-02 22:41:16 +01:00
|
|
|
|
2026-03-02 01:48:51 +01:00
|
|
|
@router.get("/servers")
|
|
|
|
|
async def get_servers() -> Dict[str, Any]:
|
|
|
|
|
"""Return status information for all configured Unraid servers.
|
|
|
|
|
|
2026-03-02 23:25:57 +01:00
|
|
|
All data comes from the MQTT message store — no HTTP polling,
|
|
|
|
|
no API keys, no cache needed (MQTT data is always fresh).
|
2026-03-02 01:48:51 +01:00
|
|
|
"""
|
|
|
|
|
|
2026-03-02 23:25:57 +01:00
|
|
|
settings = get_settings()
|
2026-03-02 01:48:51 +01:00
|
|
|
|
|
|
|
|
server_configs: List[ServerConfig] = [
|
|
|
|
|
ServerConfig(
|
|
|
|
|
name=srv.name,
|
|
|
|
|
host=srv.host,
|
2026-03-02 23:25:57 +01:00
|
|
|
mqtt_prefix=getattr(srv, "mqtt_prefix", "") or srv.name,
|
2026-03-02 01:48:51 +01:00
|
|
|
)
|
2026-03-02 23:25:57 +01:00
|
|
|
for srv in settings.unraid_servers
|
2026-03-02 01:48:51 +01:00
|
|
|
]
|
|
|
|
|
|
2026-03-02 23:25:57 +01:00
|
|
|
if not server_configs:
|
|
|
|
|
return {"servers": []}
|
|
|
|
|
|
2026-03-02 01:48:51 +01:00
|
|
|
try:
|
2026-03-02 23:25:57 +01:00
|
|
|
servers_data = fetch_all_servers_mqtt(server_configs, mqtt_service.store)
|
2026-03-02 01:48:51 +01:00
|
|
|
except Exception as exc:
|
2026-03-02 23:25:57 +01:00
|
|
|
logger.exception("Failed to read Unraid server data from MQTT")
|
2026-03-02 01:48:51 +01:00
|
|
|
return {
|
|
|
|
|
"servers": [],
|
|
|
|
|
"error": True,
|
|
|
|
|
"message": str(exc),
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-02 23:25:57 +01:00
|
|
|
return {"servers": servers_data}
|