cosmo/backend/app/models/schemas/terminal.py

101 lines
3.3 KiB
Python

"""
Pydantic schemas for ESP32 terminal pairing and terminal data delivery
"""
from datetime import datetime
from typing import List, Optional
from pydantic import BaseModel, Field
# --- Pairing (terminal -> backend, no auth) ---
class PairCodeRequest(BaseModel):
device_id: str = Field(
...,
pattern=r"^[A-Za-z0-9:_-]{4,50}$",
description="Stable device id reported by the terminal (e.g. ESP32-S3 MAC)",
)
name: Optional[str] = Field(None, max_length=100, description="Device display name")
class PairCodeResponse(BaseModel):
status: str = Field(..., description="pending = show the code, bound = already paired")
device_id: str
name: Optional[str] = None
code: Optional[str] = Field(None, description="6 digit code to render on the screen")
expires_in: Optional[int] = Field(None, description="Seconds the code stays redeemable")
username: Optional[str] = Field(None, description="Owner, only set when already bound")
class PairStatusResponse(BaseModel):
status: str = Field(
...,
description="pending | bound | expired | invalid_code | unknown | unbound",
)
username: Optional[str] = None
access_token: Optional[str] = Field(
None, description="Long lived terminal token, delivered once after binding"
)
expires_at: Optional[datetime] = None
expires_in: Optional[int] = Field(None, description="Seconds left for the pending code")
poll_interval: int = Field(2, description="Seconds the terminal should wait before polling again")
message: Optional[str] = None
# --- Device management (web -> backend, user auth) ---
class DeviceBindRequest(BaseModel):
code: str = Field(..., pattern=r"^[0-9]{6}$", description="6 digit code shown on the terminal")
class DeviceResponse(BaseModel):
id: str
name: Optional[str] = None
status: str
bound_at: Optional[datetime] = None
last_seen_at: Optional[datetime] = Field(None, description="Last time the device called the API")
online: bool = Field(False, description="Device polled within the last 6 minutes")
# --- Data snapshot (terminal -> backend, terminal token) ---
class TerminalPosition(BaseModel):
time: datetime = Field(..., description="Epoch of the stored position (UTC)")
x: float = Field(..., description="Heliocentric X in AU")
y: float
z: float
vx: Optional[float] = Field(None, description="km/s, NULL when not computed")
vy: Optional[float] = None
vz: Optional[float] = None
distance_au: float = Field(..., description="Distance from the sun in AU")
speed_km_s: Optional[float] = None
source: str
class TerminalEvent(BaseModel):
id: int
title: str
event_type: str
event_time: datetime
description: Optional[str] = None
is_upcoming: bool
class TerminalBody(BaseModel):
id: str
name: str
name_zh: Optional[str] = None
type: str
description: Optional[str] = None
is_active: Optional[bool] = None
icon_ver: Optional[int] = Field(
None,
description="Icon resource maintained in admin; 0/None = no icon, terminal keeps its built-in one",
)
position: Optional[TerminalPosition] = None
events: List[TerminalEvent] = []
class TerminalFollowsResponse(BaseModel):
username: str
device_id: str
server_time: datetime
follow_count: int
bodies: List[TerminalBody]