update: Major Overhault to all subsystems
This commit is contained in:
@@ -1,15 +1,24 @@
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional, List
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class UpdateType(str, Enum):
|
||||
optional = "optional" # user-initiated only
|
||||
mandatory = "mandatory" # auto-installs on next reboot
|
||||
emergency = "emergency" # auto-installs on reboot + daily check + MQTT push
|
||||
|
||||
|
||||
class FirmwareVersion(BaseModel):
|
||||
id: str
|
||||
hw_type: str # "vs", "vp", "vx"
|
||||
channel: str # "stable", "beta", "alpha", "testing"
|
||||
version: str # semver e.g. "1.4.2"
|
||||
hw_type: str # e.g. "vesper", "vesper_plus", "vesper_pro"
|
||||
channel: str # "stable", "beta", "alpha", "testing"
|
||||
version: str # semver e.g. "1.5"
|
||||
filename: str
|
||||
size_bytes: int
|
||||
sha256: str
|
||||
update_type: UpdateType = UpdateType.mandatory
|
||||
min_fw_version: Optional[str] = None # minimum fw version required to install this
|
||||
uploaded_at: str
|
||||
notes: Optional[str] = None
|
||||
is_latest: bool = False
|
||||
@@ -20,12 +29,19 @@ class FirmwareListResponse(BaseModel):
|
||||
total: int
|
||||
|
||||
|
||||
class FirmwareLatestResponse(BaseModel):
|
||||
class FirmwareMetadataResponse(BaseModel):
|
||||
"""Returned by both /latest and /{version}/info endpoints."""
|
||||
hw_type: str
|
||||
channel: str
|
||||
version: str
|
||||
size_bytes: int
|
||||
sha256: str
|
||||
update_type: UpdateType
|
||||
min_fw_version: Optional[str] = None
|
||||
download_url: str
|
||||
uploaded_at: str
|
||||
notes: Optional[str] = None
|
||||
|
||||
|
||||
# Keep backwards-compatible alias
|
||||
FirmwareLatestResponse = FirmwareMetadataResponse
|
||||
|
||||
@@ -4,7 +4,7 @@ from typing import Optional
|
||||
|
||||
from auth.models import TokenPayload
|
||||
from auth.dependencies import require_permission
|
||||
from firmware.models import FirmwareVersion, FirmwareListResponse, FirmwareLatestResponse
|
||||
from firmware.models import FirmwareVersion, FirmwareListResponse, FirmwareMetadataResponse, UpdateType
|
||||
from firmware import service
|
||||
|
||||
router = APIRouter(prefix="/api/firmware", tags=["firmware"])
|
||||
@@ -15,6 +15,8 @@ async def upload_firmware(
|
||||
hw_type: str = Form(...),
|
||||
channel: str = Form(...),
|
||||
version: str = Form(...),
|
||||
update_type: UpdateType = Form(UpdateType.mandatory),
|
||||
min_fw_version: Optional[str] = Form(None),
|
||||
notes: Optional[str] = Form(None),
|
||||
file: UploadFile = File(...),
|
||||
_user: TokenPayload = Depends(require_permission("manufacturing", "add")),
|
||||
@@ -25,6 +27,8 @@ async def upload_firmware(
|
||||
channel=channel,
|
||||
version=version,
|
||||
file_bytes=file_bytes,
|
||||
update_type=update_type,
|
||||
min_fw_version=min_fw_version,
|
||||
notes=notes,
|
||||
)
|
||||
|
||||
@@ -39,7 +43,7 @@ def list_firmware(
|
||||
return FirmwareListResponse(firmware=items, total=len(items))
|
||||
|
||||
|
||||
@router.get("/{hw_type}/{channel}/latest", response_model=FirmwareLatestResponse)
|
||||
@router.get("/{hw_type}/{channel}/latest", response_model=FirmwareMetadataResponse)
|
||||
def get_latest_firmware(hw_type: str, channel: str):
|
||||
"""Returns metadata for the latest firmware for a given hw_type + channel.
|
||||
No auth required — devices call this endpoint to check for updates.
|
||||
@@ -47,6 +51,14 @@ def get_latest_firmware(hw_type: str, channel: str):
|
||||
return service.get_latest(hw_type, channel)
|
||||
|
||||
|
||||
@router.get("/{hw_type}/{channel}/{version}/info", response_model=FirmwareMetadataResponse)
|
||||
def get_firmware_info(hw_type: str, channel: str, version: str):
|
||||
"""Returns metadata for a specific firmware version.
|
||||
No auth required — devices call this to resolve upgrade chains.
|
||||
"""
|
||||
return service.get_version_info(hw_type, channel, version)
|
||||
|
||||
|
||||
@router.get("/{hw_type}/{channel}/{version}/firmware.bin")
|
||||
def download_firmware(hw_type: str, channel: str, version: str):
|
||||
"""Download the firmware binary. No auth required — devices call this directly."""
|
||||
|
||||
@@ -8,11 +8,11 @@ from fastapi import HTTPException
|
||||
from config import settings
|
||||
from shared.firebase import get_db
|
||||
from shared.exceptions import NotFoundError
|
||||
from firmware.models import FirmwareVersion, FirmwareLatestResponse
|
||||
from firmware.models import FirmwareVersion, FirmwareMetadataResponse, UpdateType
|
||||
|
||||
COLLECTION = "firmware_versions"
|
||||
|
||||
VALID_HW_TYPES = {"vs", "vp", "vx"}
|
||||
VALID_HW_TYPES = {"vesper", "vesper_plus", "vesper_pro", "chronos", "chronos_pro", "agnus", "agnus_mini"}
|
||||
VALID_CHANNELS = {"stable", "beta", "alpha", "testing"}
|
||||
|
||||
|
||||
@@ -36,23 +36,43 @@ def _doc_to_firmware_version(doc) -> FirmwareVersion:
|
||||
filename=data.get("filename", "firmware.bin"),
|
||||
size_bytes=data.get("size_bytes", 0),
|
||||
sha256=data.get("sha256", ""),
|
||||
update_type=data.get("update_type", UpdateType.mandatory),
|
||||
min_fw_version=data.get("min_fw_version"),
|
||||
uploaded_at=uploaded_str,
|
||||
notes=data.get("notes"),
|
||||
is_latest=data.get("is_latest", False),
|
||||
)
|
||||
|
||||
|
||||
def _fw_to_metadata_response(fw: FirmwareVersion) -> FirmwareMetadataResponse:
|
||||
download_url = f"/api/firmware/{fw.hw_type}/{fw.channel}/{fw.version}/firmware.bin"
|
||||
return FirmwareMetadataResponse(
|
||||
hw_type=fw.hw_type,
|
||||
channel=fw.channel,
|
||||
version=fw.version,
|
||||
size_bytes=fw.size_bytes,
|
||||
sha256=fw.sha256,
|
||||
update_type=fw.update_type,
|
||||
min_fw_version=fw.min_fw_version,
|
||||
download_url=download_url,
|
||||
uploaded_at=fw.uploaded_at,
|
||||
notes=fw.notes,
|
||||
)
|
||||
|
||||
|
||||
def upload_firmware(
|
||||
hw_type: str,
|
||||
channel: str,
|
||||
version: str,
|
||||
file_bytes: bytes,
|
||||
update_type: UpdateType = UpdateType.mandatory,
|
||||
min_fw_version: str | None = None,
|
||||
notes: str | None = None,
|
||||
) -> FirmwareVersion:
|
||||
if hw_type not in VALID_HW_TYPES:
|
||||
raise HTTPException(status_code=400, detail=f"Invalid hw_type. Must be one of: {', '.join(VALID_HW_TYPES)}")
|
||||
raise HTTPException(status_code=400, detail=f"Invalid hw_type. Must be one of: {', '.join(sorted(VALID_HW_TYPES))}")
|
||||
if channel not in VALID_CHANNELS:
|
||||
raise HTTPException(status_code=400, detail=f"Invalid channel. Must be one of: {', '.join(VALID_CHANNELS)}")
|
||||
raise HTTPException(status_code=400, detail=f"Invalid channel. Must be one of: {', '.join(sorted(VALID_CHANNELS))}")
|
||||
|
||||
dest = _storage_path(hw_type, channel, version)
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
@@ -83,6 +103,8 @@ def upload_firmware(
|
||||
"filename": "firmware.bin",
|
||||
"size_bytes": len(file_bytes),
|
||||
"sha256": sha256,
|
||||
"update_type": update_type.value,
|
||||
"min_fw_version": min_fw_version,
|
||||
"uploaded_at": now,
|
||||
"notes": notes,
|
||||
"is_latest": True,
|
||||
@@ -108,7 +130,7 @@ def list_firmware(
|
||||
return items
|
||||
|
||||
|
||||
def get_latest(hw_type: str, channel: str) -> FirmwareLatestResponse:
|
||||
def get_latest(hw_type: str, channel: str) -> FirmwareMetadataResponse:
|
||||
if hw_type not in VALID_HW_TYPES:
|
||||
raise HTTPException(status_code=400, detail=f"Invalid hw_type '{hw_type}'")
|
||||
if channel not in VALID_CHANNELS:
|
||||
@@ -126,18 +148,29 @@ def get_latest(hw_type: str, channel: str) -> FirmwareLatestResponse:
|
||||
if not docs:
|
||||
raise NotFoundError("Firmware")
|
||||
|
||||
fw = _doc_to_firmware_version(docs[0])
|
||||
download_url = f"/api/firmware/{hw_type}/{channel}/{fw.version}/firmware.bin"
|
||||
return FirmwareLatestResponse(
|
||||
hw_type=fw.hw_type,
|
||||
channel=fw.channel,
|
||||
version=fw.version,
|
||||
size_bytes=fw.size_bytes,
|
||||
sha256=fw.sha256,
|
||||
download_url=download_url,
|
||||
uploaded_at=fw.uploaded_at,
|
||||
notes=fw.notes,
|
||||
return _fw_to_metadata_response(_doc_to_firmware_version(docs[0]))
|
||||
|
||||
|
||||
def get_version_info(hw_type: str, channel: str, version: str) -> FirmwareMetadataResponse:
|
||||
"""Fetch metadata for a specific version. Used by devices resolving upgrade chains."""
|
||||
if hw_type not in VALID_HW_TYPES:
|
||||
raise HTTPException(status_code=400, detail=f"Invalid hw_type '{hw_type}'")
|
||||
if channel not in VALID_CHANNELS:
|
||||
raise HTTPException(status_code=400, detail=f"Invalid channel '{channel}'")
|
||||
|
||||
db = get_db()
|
||||
docs = list(
|
||||
db.collection(COLLECTION)
|
||||
.where("hw_type", "==", hw_type)
|
||||
.where("channel", "==", channel)
|
||||
.where("version", "==", version)
|
||||
.limit(1)
|
||||
.stream()
|
||||
)
|
||||
if not docs:
|
||||
raise NotFoundError("Firmware version")
|
||||
|
||||
return _fw_to_metadata_response(_doc_to_firmware_version(docs[0]))
|
||||
|
||||
|
||||
def get_firmware_path(hw_type: str, channel: str, version: str) -> Path:
|
||||
|
||||
Reference in New Issue
Block a user