| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- """
- 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.
- Updates device last_seen_at timestamp.
- """
- device = await _auth_device_token(authorization, db)
- # Update last_seen_at timestamp
- from datetime import datetime, timezone
- device.last_seen_at = datetime.now(timezone.utc)
- await db.commit()
- # Return device config from database
- # Config was copied from default_config.json during registration
- return device.config or {}
|