191 lines
6.7 KiB
Python
191 lines
6.7 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")
|
||
board: Optional[str] = Field(None, description="Panel build the device reports, e.g. atk-dnesp32s3")
|
||
firmware_version: Optional[str] = Field(None, description="Firmware version currently running")
|
||
|
||
|
||
# --- OTA ---
|
||
class FirmwareCheckResponse(BaseModel):
|
||
has_update: bool = Field(..., description="True when a newer enabled package exists for this board")
|
||
version: Optional[str] = Field(None, description="Version to install")
|
||
path: Optional[str] = Field(
|
||
None,
|
||
description="Download path under /upload/; the terminal prepends scheme://host of its API base",
|
||
)
|
||
size: Optional[int] = Field(None, description="Image size in bytes, must match before writing")
|
||
sha256: Optional[str] = Field(None, description="Lowercase hex digest the terminal verifies")
|
||
notes: Optional[str] = Field(None, description="Release notes to show on the screen")
|
||
|
||
|
||
class FirmwareEnabledUpdate(BaseModel):
|
||
enabled: bool = Field(..., description="Disabled packages are never offered to terminals")
|
||
|
||
|
||
class FirmwareAdminItem(BaseModel):
|
||
id: int
|
||
version: str
|
||
board: str
|
||
path: str
|
||
size: int
|
||
sha256: str
|
||
notes: Optional[str] = None
|
||
enabled: bool
|
||
created_at: Optional[datetime] = None
|
||
|
||
|
||
# --- 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]
|
||
|
||
|
||
# --- Rockets (terminal -> backend, terminal token) ---
|
||
class TerminalRocket(BaseModel):
|
||
"""One launch vehicle as the terminal list shows it.
|
||
|
||
字段是"扁平"的:消费方是 320x240 的 MCU 固件,它按定长结构体解析,
|
||
与其在这里镜像数据库的 JSONB 嵌套,不如直接把要显示的量摊平。
|
||
"""
|
||
code: str
|
||
name: str
|
||
name_zh: Optional[str] = None
|
||
manufacturer: Optional[str] = None
|
||
country: Optional[str] = None
|
||
height_m: float
|
||
diameter_m: float
|
||
payload_mass_kg: Optional[float] = None
|
||
target_orbit_km: Optional[float] = None
|
||
thrust_kn: Optional[float] = Field(None, description="起飞推力(芯级+助推器)")
|
||
engines: Optional[int] = Field(None, description="芯级发动机数")
|
||
boosters: Optional[int] = Field(None, description="捆绑助推器数量")
|
||
upcoming: int = Field(0, description="待发射任务数")
|
||
art_ver: int = Field(..., description="程序化立绘图标的参数版本,终端按它缓存点阵")
|
||
|
||
|
||
class TerminalRocketsResponse(BaseModel):
|
||
username: str
|
||
device_id: str
|
||
server_time: datetime
|
||
rocket_count: int
|
||
rockets: List[TerminalRocket]
|
||
|
||
|
||
class TerminalRocketEvent(BaseModel):
|
||
name: str
|
||
net: datetime = Field(..., description="发射时间 T-0(UTC)")
|
||
orbit: Optional[str] = None
|
||
status: Optional[str] = None
|
||
launch_site_name: Optional[str] = None
|
||
|
||
|
||
class TerminalRocketDetail(TerminalRocket):
|
||
description: Optional[str] = None
|
||
target_velocity_mps: Optional[float] = None
|
||
s1_name: Optional[str] = None
|
||
s1_thrust_kn: Optional[float] = None
|
||
s1_isp_s: Optional[float] = None
|
||
s1_engines: Optional[int] = None
|
||
s1_recoverable: Optional[bool] = None
|
||
s2_name: Optional[str] = None
|
||
s2_thrust_kn: Optional[float] = None
|
||
s2_isp_s: Optional[float] = None
|
||
s2_engines: Optional[int] = None
|
||
fairing: bool = Field(True, description="False = 无整流罩(星舰/电子号这类带暴露头锥)")
|
||
booster_thrust_kn: Optional[float] = None
|
||
booster_recoverable: Optional[bool] = None
|
||
events: List[TerminalRocketEvent] = []
|
||
event_total: int = 0
|
||
next_net: Optional[datetime] = None
|