| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- """
- Device configuration endpoint.
- """
- from typing import Annotated
- from fastapi import APIRouter, Depends, Header, HTTPException, Query, status
- from sqlalchemy import select, update
- from sqlalchemy.ext.asyncio import AsyncSession
- from app.core.database import get_db
- from app.models.device import Device
- router = APIRouter()
- async def _auth_device_token(
- authorization: str | None, db: AsyncSession
- ) -> Device:
- """Authenticate device by token from Authorization header."""
- if not authorization or not authorization.lower().startswith("bearer "):
- raise HTTPException(
- status_code=status.HTTP_401_UNAUTHORIZED,
- detail="Missing token",
- )
- token = authorization.split(None, 1)[1]
- result = await db.execute(select(Device).where(Device.device_token == token))
- device = result.scalar_one_or_none()
- if not device:
- raise HTTPException(
- status_code=status.HTTP_401_UNAUTHORIZED,
- detail="Invalid token",
- )
- return device
- @router.get("/config")
- async def get_device_config(
- db: Annotated[AsyncSession, Depends(get_db)],
- device_id: str = Query(..., description="Device MAC address"),
- eth_ip: str | None = Query(None),
- wlan_ip: str | None = Query(None),
- modem_ip: str | None = Query(None),
- authorization: Annotated[str | None, Header()] = None,
- ):
- """
- Get device configuration.
- Returns config with BLE/WiFi settings, tunnel config, etc.
- """
- device = await _auth_device_token(authorization, db)
- # Default config (like stub)
- default_config = {
- "force_cloud": False,
- "ble": {
- "enabled": True,
- "batch_interval_ms": 2500,
- "uuid_filter_hex": "",
- },
- "wifi": {
- "client_enabled": False,
- "ssid": "PT",
- "psk": "suhariki",
- "monitor_enabled": True,
- "batch_interval_ms": 10000,
- },
- "ssh_tunnel": {
- "enabled": False,
- "server": "192.168.5.4",
- "port": 22,
- "user": "tunnel",
- "remote_port": 0,
- "keepalive_interval": 30,
- },
- "dashboard_tunnel": {
- "enabled": False,
- "server": "192.168.5.4",
- "port": 22,
- "user": "tunnel",
- "remote_port": 0,
- "keepalive_interval": 30,
- },
- "dashboard": {
- "enabled": True,
- },
- "net": {
- "ntp": {
- "servers": ["pool.ntp.org", "time.google.com"],
- },
- },
- "debug": False,
- }
- # Merge with device-specific config overrides
- config = {**default_config, **(device.config or {})}
- return config
|