spoolbuddy.py 3.1 KB

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