config.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """
  2. Device configuration endpoint.
  3. """
  4. from typing import Annotated
  5. from fastapi import APIRouter, Depends, Header, HTTPException, Query, status
  6. from sqlalchemy import select, update
  7. from sqlalchemy.ext.asyncio import AsyncSession
  8. from app.core.database import get_db
  9. from app.models.device import Device
  10. router = APIRouter()
  11. async def _auth_device_token(
  12. authorization: str | None, db: AsyncSession
  13. ) -> Device:
  14. """Authenticate device by token from Authorization header."""
  15. if not authorization or not authorization.lower().startswith("bearer "):
  16. raise HTTPException(
  17. status_code=status.HTTP_401_UNAUTHORIZED,
  18. detail="Missing token",
  19. )
  20. token = authorization.split(None, 1)[1]
  21. result = await db.execute(select(Device).where(Device.device_token == token))
  22. device = result.scalar_one_or_none()
  23. if not device:
  24. raise HTTPException(
  25. status_code=status.HTTP_401_UNAUTHORIZED,
  26. detail="Invalid token",
  27. )
  28. return device
  29. @router.get("/config")
  30. async def get_device_config(
  31. db: Annotated[AsyncSession, Depends(get_db)],
  32. device_id: str = Query(..., description="Device MAC address"),
  33. eth_ip: str | None = Query(None),
  34. wlan_ip: str | None = Query(None),
  35. modem_ip: str | None = Query(None),
  36. authorization: Annotated[str | None, Header()] = None,
  37. ):
  38. """
  39. Get device configuration.
  40. Returns config with BLE/WiFi settings, tunnel config, etc.
  41. """
  42. device = await _auth_device_token(authorization, db)
  43. # Default config (like stub)
  44. default_config = {
  45. "force_cloud": False,
  46. "ble": {
  47. "enabled": True,
  48. "batch_interval_ms": 2500,
  49. "uuid_filter_hex": "",
  50. },
  51. "wifi": {
  52. "client_enabled": False,
  53. "ssid": "PT",
  54. "psk": "suhariki",
  55. "monitor_enabled": True,
  56. "batch_interval_ms": 10000,
  57. },
  58. "ssh_tunnel": {
  59. "enabled": False,
  60. "server": "192.168.5.4",
  61. "port": 22,
  62. "user": "tunnel",
  63. "remote_port": 0,
  64. "keepalive_interval": 30,
  65. },
  66. "dashboard_tunnel": {
  67. "enabled": False,
  68. "server": "192.168.5.4",
  69. "port": 22,
  70. "user": "tunnel",
  71. "remote_port": 0,
  72. "keepalive_interval": 30,
  73. },
  74. "dashboard": {
  75. "enabled": True,
  76. },
  77. "net": {
  78. "ntp": {
  79. "servers": ["pool.ntp.org", "time.google.com"],
  80. },
  81. },
  82. "debug": False,
  83. }
  84. # Merge with device-specific config overrides
  85. config = {**default_config, **(device.config or {})}
  86. return config