spoolbuddy.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import json
  2. from datetime import datetime
  3. from typing import Literal
  4. from pydantic import BaseModel, Field, field_validator
  5. # --- Device schemas ---
  6. class DeviceRegisterRequest(BaseModel):
  7. device_id: str = Field(..., min_length=1, max_length=50)
  8. hostname: str = Field(..., min_length=1, max_length=100)
  9. ip_address: str = Field(..., min_length=1, max_length=45)
  10. firmware_version: str | None = Field(None, max_length=20)
  11. has_nfc: bool = True
  12. has_scale: bool = True
  13. tare_offset: int = 0
  14. calibration_factor: float = 1.0
  15. nfc_reader_type: str | None = Field(None, max_length=20)
  16. nfc_connection: str | None = Field(None, max_length=20)
  17. backend_url: str | None = Field(None, max_length=255)
  18. has_backlight: bool = False
  19. class DeviceResponse(BaseModel):
  20. id: int
  21. device_id: str
  22. hostname: str
  23. ip_address: str
  24. firmware_version: str | None = None
  25. has_nfc: bool
  26. has_scale: bool
  27. tare_offset: int
  28. calibration_factor: float
  29. nfc_reader_type: str | None = None
  30. nfc_connection: str | None = None
  31. backend_url: str | None = None
  32. display_brightness: int = 100
  33. display_blank_timeout: int = 0
  34. has_backlight: bool = False
  35. last_calibrated_at: datetime | None = None
  36. last_seen: datetime | None = None
  37. pending_command: str | None = None
  38. nfc_ok: bool
  39. scale_ok: bool
  40. uptime_s: int
  41. update_status: str | None = None
  42. update_message: str | None = None
  43. system_stats: dict | None = None
  44. online: bool = False
  45. ssh_public_key: str | None = None
  46. created_at: datetime
  47. updated_at: datetime
  48. class Config:
  49. from_attributes = True
  50. class HeartbeatRequest(BaseModel):
  51. nfc_ok: bool = False
  52. scale_ok: bool = False
  53. uptime_s: int = 0
  54. firmware_version: str | None = Field(None, max_length=20)
  55. ip_address: str | None = Field(None, max_length=45)
  56. nfc_reader_type: str | None = Field(None, max_length=20)
  57. nfc_connection: str | None = Field(None, max_length=20)
  58. backend_url: str | None = Field(None, max_length=255)
  59. system_stats: dict | None = None
  60. @field_validator("system_stats")
  61. @classmethod
  62. def _limit_system_stats_size(cls, v: dict | None) -> dict | None:
  63. if v is not None and len(json.dumps(v)) > 4096:
  64. raise ValueError("system_stats must not exceed 4096 bytes when JSON-encoded")
  65. return v
  66. class HeartbeatResponse(BaseModel):
  67. pending_command: str | None = None
  68. pending_write_payload: dict | None = None
  69. pending_system_payload: dict | None = None
  70. tare_offset: int
  71. calibration_factor: float
  72. display_brightness: int = 100
  73. display_blank_timeout: int = 0
  74. # --- NFC schemas ---
  75. class TagScannedRequest(BaseModel):
  76. device_id: str = Field(..., max_length=50)
  77. tag_uid: str = Field(..., max_length=32)
  78. tray_uuid: str | None = Field(None, max_length=32, pattern=r"^[0-9A-Fa-f]*$")
  79. sak: int | None = None
  80. tag_type: str | None = Field(None, max_length=50)
  81. raw_blocks: dict | None = None
  82. class TagRemovedRequest(BaseModel):
  83. device_id: str = Field(..., max_length=50)
  84. tag_uid: str = Field(..., max_length=32)
  85. # --- Scale schemas ---
  86. class ScaleReadingRequest(BaseModel):
  87. device_id: str = Field(..., max_length=50)
  88. weight_grams: float = Field(..., ge=0.0, le=100_000.0)
  89. stable: bool = False
  90. raw_adc: int | None = None
  91. class UpdateSpoolWeightRequest(BaseModel):
  92. spool_id: int = Field(..., gt=0)
  93. weight_grams: float
  94. # --- Calibration schemas ---
  95. class SetTareRequest(BaseModel):
  96. tare_offset: int
  97. class SetCalibrationFactorRequest(BaseModel):
  98. known_weight_grams: float = Field(..., gt=0)
  99. raw_adc: int
  100. tare_raw_adc: int | None = None
  101. class CalibrationResponse(BaseModel):
  102. tare_offset: int
  103. calibration_factor: float
  104. # --- Display schemas ---
  105. class WriteTagRequest(BaseModel):
  106. device_id: str = Field(..., max_length=50)
  107. spool_id: int = Field(..., gt=0)
  108. class WriteTagResultRequest(BaseModel):
  109. device_id: str = Field(..., max_length=50)
  110. spool_id: int = Field(..., gt=0)
  111. tag_uid: str = Field(..., min_length=8, max_length=30, pattern=r"^[0-9A-Fa-f]+$")
  112. success: bool
  113. message: str | None = Field(None, max_length=500)
  114. class DisplaySettingsRequest(BaseModel):
  115. brightness: int = Field(ge=0, le=100)
  116. blank_timeout: int = Field(ge=0)
  117. class SystemConfigRequest(BaseModel):
  118. backend_url: str = Field(..., min_length=1, max_length=255)
  119. api_key: str | None = Field(default=None, max_length=255)
  120. class SystemCommandRequest(BaseModel):
  121. command: str = Field(
  122. ..., max_length=50, description="System command: reboot, shutdown, restart_daemon, restart_browser"
  123. )
  124. class SystemCommandResultRequest(BaseModel):
  125. command: str = Field(..., max_length=50)
  126. success: bool
  127. message: str | None = Field(None, max_length=500)
  128. class UpdateStatusRequest(BaseModel):
  129. status: Literal["updating", "complete", "error"]
  130. message: str | None = Field(None, max_length=255)
  131. # --- Diagnostics schemas ---
  132. class DiagnosticResultRequest(BaseModel):
  133. diagnostic: str = Field(..., max_length=50, description="Diagnostic type: 'nfc', 'scale', or 'read_tag'")
  134. success: bool
  135. output: str = Field(..., max_length=10_000)
  136. exit_code: int = Field(..., ge=-255, le=255)