spoolbuddy.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. from datetime import datetime
  2. from pydantic import BaseModel, Field
  3. # --- Device schemas ---
  4. class DeviceRegisterRequest(BaseModel):
  5. device_id: str = Field(..., min_length=1, max_length=50)
  6. hostname: str = Field(..., min_length=1, max_length=100)
  7. ip_address: str = Field(..., min_length=1, max_length=45)
  8. firmware_version: str | None = None
  9. has_nfc: bool = True
  10. has_scale: bool = True
  11. tare_offset: int = 0
  12. calibration_factor: float = 1.0
  13. nfc_reader_type: str | None = None
  14. nfc_connection: str | None = None
  15. has_backlight: bool = False
  16. class DeviceResponse(BaseModel):
  17. id: int
  18. device_id: str
  19. hostname: str
  20. ip_address: str
  21. firmware_version: str | None = None
  22. has_nfc: bool
  23. has_scale: bool
  24. tare_offset: int
  25. calibration_factor: float
  26. nfc_reader_type: str | None = None
  27. nfc_connection: str | None = None
  28. display_brightness: int = 100
  29. display_blank_timeout: int = 0
  30. has_backlight: bool = False
  31. last_calibrated_at: datetime | None = None
  32. last_seen: datetime | None = None
  33. pending_command: str | None = None
  34. nfc_ok: bool
  35. scale_ok: bool
  36. uptime_s: int
  37. online: bool = False
  38. created_at: datetime
  39. updated_at: datetime
  40. class Config:
  41. from_attributes = True
  42. class HeartbeatRequest(BaseModel):
  43. nfc_ok: bool = False
  44. scale_ok: bool = False
  45. uptime_s: int = 0
  46. firmware_version: str | None = None
  47. ip_address: str | None = None
  48. nfc_reader_type: str | None = None
  49. nfc_connection: str | None = None
  50. class HeartbeatResponse(BaseModel):
  51. pending_command: str | None = None
  52. pending_write_payload: dict | None = None
  53. tare_offset: int
  54. calibration_factor: float
  55. display_brightness: int = 100
  56. display_blank_timeout: int = 0
  57. # --- NFC schemas ---
  58. class TagScannedRequest(BaseModel):
  59. device_id: str
  60. tag_uid: str
  61. tray_uuid: str | None = None
  62. sak: int | None = None
  63. tag_type: str | None = None
  64. raw_blocks: dict | None = None
  65. class TagRemovedRequest(BaseModel):
  66. device_id: str
  67. tag_uid: str
  68. # --- Scale schemas ---
  69. class ScaleReadingRequest(BaseModel):
  70. device_id: str
  71. weight_grams: float
  72. stable: bool = False
  73. raw_adc: int | None = None
  74. class UpdateSpoolWeightRequest(BaseModel):
  75. spool_id: int
  76. weight_grams: float
  77. # --- Calibration schemas ---
  78. class TareRequest(BaseModel):
  79. pass
  80. class SetTareRequest(BaseModel):
  81. tare_offset: int
  82. class SetCalibrationFactorRequest(BaseModel):
  83. known_weight_grams: float = Field(..., gt=0)
  84. raw_adc: int
  85. tare_raw_adc: int | None = None
  86. class CalibrationResponse(BaseModel):
  87. tare_offset: int
  88. calibration_factor: float
  89. # --- Display schemas ---
  90. class WriteTagRequest(BaseModel):
  91. device_id: str
  92. spool_id: int
  93. class WriteTagResultRequest(BaseModel):
  94. device_id: str
  95. spool_id: int
  96. tag_uid: str
  97. success: bool
  98. message: str | None = None
  99. class DisplaySettingsRequest(BaseModel):
  100. brightness: int = Field(ge=0, le=100)
  101. blank_timeout: int = Field(ge=0)