spoolbuddy.py 5.1 KB

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