cosmo/backend/app/models/db/firmware.py

43 lines
1.9 KiB
Python

"""
FirmwareVersion ORM model - OTA packages served to ESP32 terminals
A package is only ever handed to a terminal whose reported board matches `board`
exactly: flashing the wrong panel build leaves the device with a black screen and
only a serial reflash can recover it, so this column is a hard gate, not a label.
"""
from sqlalchemy import Column, Integer, String, Boolean, TIMESTAMP, UniqueConstraint
from sqlalchemy.sql import func
from app.database import Base
# Whitelist of board ids. Keep in sync with BOARD_NAME in
# terminal/firmware/cosmo_terminal/src/board_config.h
FIRMWARE_BOARDS = ("atk-dnesp32s3", "xingzhi-cube-1.54tft", "waveshare-3p49")
class FirmwareVersion(Base):
"""One uploaded firmware image, immutable once created."""
__tablename__ = "firmware_versions"
id = Column(Integer, primary_key=True, autoincrement=True)
version = Column(String(32), nullable=False, comment="Semver reported by the terminal, e.g. 1.2.0")
board = Column(String(40), nullable=False, comment="Must equal the firmware BOARD_NAME")
file_path = Column(
String(500),
nullable=False,
comment="Relative path from the upload directory, served under /upload/",
)
file_size = Column(Integer, nullable=False, comment="Bytes, checked by the terminal before writing")
sha256 = Column(String(64), nullable=False, comment="Lowercase hex digest of the image")
notes = Column(String(500), nullable=True, comment="What changed, shown on the terminal")
enabled = Column(Boolean, nullable=False, default=True, comment="Disabled rows are never offered")
created_at = Column(TIMESTAMP, server_default=func.now())
__table_args__ = (
UniqueConstraint("board", "version", name="uq_firmware_board_version"),
)
def __repr__(self):
return f"<FirmwareVersion(board='{self.board}', version='{self.version}', enabled={self.enabled})>"