spoolbuddy.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. class DeviceResponse(BaseModel):
  14. id: int
  15. device_id: str
  16. hostname: str
  17. ip_address: str
  18. firmware_version: str | None = None
  19. has_nfc: bool
  20. has_scale: bool
  21. tare_offset: int
  22. calibration_factor: float
  23. last_seen: datetime | None = None
  24. pending_command: str | None = None
  25. nfc_ok: bool
  26. scale_ok: bool
  27. uptime_s: int
  28. online: bool = False
  29. created_at: datetime
  30. updated_at: datetime
  31. class Config:
  32. from_attributes = True
  33. class HeartbeatRequest(BaseModel):
  34. nfc_ok: bool = False
  35. scale_ok: bool = False
  36. uptime_s: int = 0
  37. firmware_version: str | None = None
  38. ip_address: str | None = None
  39. class HeartbeatResponse(BaseModel):
  40. pending_command: str | None = None
  41. tare_offset: int
  42. calibration_factor: float
  43. # --- NFC schemas ---
  44. class TagScannedRequest(BaseModel):
  45. device_id: str
  46. tag_uid: str
  47. tray_uuid: str | None = None
  48. sak: int | None = None
  49. tag_type: str | None = None
  50. raw_blocks: dict | None = None
  51. class TagRemovedRequest(BaseModel):
  52. device_id: str
  53. tag_uid: str
  54. # --- Scale schemas ---
  55. class ScaleReadingRequest(BaseModel):
  56. device_id: str
  57. weight_grams: float
  58. stable: bool = False
  59. raw_adc: int | None = None
  60. class UpdateSpoolWeightRequest(BaseModel):
  61. spool_id: int
  62. weight_grams: float
  63. # --- Calibration schemas ---
  64. class TareRequest(BaseModel):
  65. pass
  66. class SetTareRequest(BaseModel):
  67. tare_offset: int
  68. class SetCalibrationFactorRequest(BaseModel):
  69. known_weight_grams: float = Field(..., gt=0)
  70. raw_adc: int
  71. tare_raw_adc: int | None = None
  72. class CalibrationResponse(BaseModel):
  73. tare_offset: int
  74. calibration_factor: float