spoolbuddy.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. update_status: str | None = None
  38. update_message: str | None = None
  39. online: bool = False
  40. created_at: datetime
  41. updated_at: datetime
  42. class Config:
  43. from_attributes = True
  44. class HeartbeatRequest(BaseModel):
  45. nfc_ok: bool = False
  46. scale_ok: bool = False
  47. uptime_s: int = 0
  48. firmware_version: str | None = None
  49. ip_address: str | None = None
  50. nfc_reader_type: str | None = None
  51. nfc_connection: str | None = None
  52. class HeartbeatResponse(BaseModel):
  53. pending_command: str | None = None
  54. pending_write_payload: dict | None = None
  55. tare_offset: int
  56. calibration_factor: float
  57. display_brightness: int = 100
  58. display_blank_timeout: int = 0
  59. # --- NFC schemas ---
  60. class TagScannedRequest(BaseModel):
  61. device_id: str
  62. tag_uid: str
  63. tray_uuid: str | None = None
  64. sak: int | None = None
  65. tag_type: str | None = None
  66. raw_blocks: dict | None = None
  67. class TagRemovedRequest(BaseModel):
  68. device_id: str
  69. tag_uid: str
  70. # --- Scale schemas ---
  71. class ScaleReadingRequest(BaseModel):
  72. device_id: str
  73. weight_grams: float
  74. stable: bool = False
  75. raw_adc: int | None = None
  76. class UpdateSpoolWeightRequest(BaseModel):
  77. spool_id: int
  78. weight_grams: float
  79. # --- Calibration schemas ---
  80. class TareRequest(BaseModel):
  81. pass
  82. class SetTareRequest(BaseModel):
  83. tare_offset: int
  84. class SetCalibrationFactorRequest(BaseModel):
  85. known_weight_grams: float = Field(..., gt=0)
  86. raw_adc: int
  87. tare_raw_adc: int | None = None
  88. class CalibrationResponse(BaseModel):
  89. tare_offset: int
  90. calibration_factor: float
  91. # --- Display schemas ---
  92. class WriteTagRequest(BaseModel):
  93. device_id: str
  94. spool_id: int
  95. class WriteTagResultRequest(BaseModel):
  96. device_id: str
  97. spool_id: int
  98. tag_uid: str
  99. success: bool
  100. message: str | None = None
  101. class DisplaySettingsRequest(BaseModel):
  102. brightness: int = Field(ge=0, le=100)
  103. blank_timeout: int = Field(ge=0)