spool.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. from datetime import datetime
  2. from pydantic import BaseModel, Field, field_validator
  3. # Visual variant applied to a spool's swatch — purely cosmetic, does not
  4. # affect MQTT/firmware. Kept independent of `subtype` so users can override
  5. # the rendering hint without touching Bambu's categorical filament label.
  6. # Mirrors the visual variants the spool form's `KNOWN_VARIANTS` exposes so
  7. # the catalog and spool form share one vocabulary; structural variants like
  8. # gradient/dual-color/tri-color/multicolor combine with `extra_colors` for
  9. # rendering, surface effects (sparkle/wood/marble/glow/matte) layer overlays.
  10. ALLOWED_EFFECT_TYPES = frozenset(
  11. {
  12. # Surface effects
  13. "sparkle",
  14. "wood",
  15. "marble",
  16. "glow",
  17. "matte",
  18. # Sheen / finish variants
  19. "silk",
  20. "galaxy",
  21. "rainbow",
  22. "metal",
  23. "translucent",
  24. # Multi-colour structures (drive gradient rendering when paired with extra_colors)
  25. "gradient",
  26. "dual-color",
  27. "tri-color",
  28. "multicolor",
  29. }
  30. )
  31. # Cap how many gradient stops we accept on input so a paste of arbitrary text
  32. # can't blow up the stored value or downstream rendering.
  33. MAX_EXTRA_COLOR_STOPS = 8
  34. def normalize_extra_colors(value: str | None) -> str | None:
  35. """Parse comma-separated hex tokens into canonical lowercase form.
  36. Accepts 6- or 8-char hex per token, with or without leading `#`. Returns
  37. None for blank input, raises ValueError for malformed tokens or too many
  38. stops. Output is the comma-joined canonical form (no `#`, lowercase).
  39. """
  40. if value is None:
  41. return None
  42. raw = value.strip()
  43. if not raw:
  44. return None
  45. tokens = [tok.strip().lstrip("#").lower() for tok in raw.split(",") if tok.strip()]
  46. if not tokens:
  47. return None
  48. if len(tokens) > MAX_EXTRA_COLOR_STOPS:
  49. raise ValueError(f"extra_colors accepts at most {MAX_EXTRA_COLOR_STOPS} stops")
  50. for tok in tokens:
  51. if len(tok) not in (6, 8):
  52. raise ValueError(f"extra_colors token '{tok}' must be 6 or 8 hex chars")
  53. try:
  54. int(tok, 16)
  55. except ValueError as exc:
  56. raise ValueError(f"extra_colors token '{tok}' is not valid hex") from exc
  57. return ",".join(tokens)
  58. def normalize_effect_type(value: str | None) -> str | None:
  59. if value is None:
  60. return None
  61. trimmed = value.strip().lower()
  62. if not trimmed:
  63. return None
  64. # Tolerate "Dual Color" / "dual_color" / "dual color" → "dual-color" so
  65. # users pasting from spool-subtype labels don't hit a validation wall.
  66. canonical = trimmed.replace("_", "-").replace(" ", "-")
  67. if canonical not in ALLOWED_EFFECT_TYPES:
  68. raise ValueError(f"effect_type must be one of: {sorted(ALLOWED_EFFECT_TYPES)}")
  69. return canonical
  70. class SpoolBase(BaseModel):
  71. material: str = Field(..., min_length=1, max_length=50)
  72. subtype: str | None = None
  73. color_name: str | None = None
  74. rgba: str | None = Field(None, pattern=r"^[0-9A-Fa-f]{8}$")
  75. extra_colors: str | None = None
  76. effect_type: str | None = None
  77. brand: str | None = None
  78. @field_validator("extra_colors")
  79. @classmethod
  80. def _validate_extra_colors(cls, v: str | None) -> str | None:
  81. return normalize_extra_colors(v)
  82. @field_validator("effect_type")
  83. @classmethod
  84. def _validate_effect_type(cls, v: str | None) -> str | None:
  85. return normalize_effect_type(v)
  86. label_weight: int = 1000
  87. core_weight: int = 250
  88. core_weight_catalog_id: int | None = None
  89. weight_used: float = 0
  90. slicer_filament: str | None = None
  91. slicer_filament_name: str | None = None
  92. nozzle_temp_min: int | None = None
  93. nozzle_temp_max: int | None = None
  94. note: str | None = None
  95. tag_uid: str | None = None
  96. tray_uuid: str | None = None
  97. data_origin: str | None = None
  98. tag_type: str | None = None
  99. cost_per_kg: float | None = Field(default=None, ge=0)
  100. weight_locked: bool = False
  101. last_scale_weight: int | None = None
  102. last_weighed_at: datetime | None = None
  103. # User-defined category + per-spool low-stock threshold override (#729).
  104. category: str | None = Field(default=None, max_length=50)
  105. low_stock_threshold_pct: int | None = Field(default=None, ge=1, le=99)
  106. class SpoolCreate(SpoolBase):
  107. pass
  108. class SpoolBulkCreate(BaseModel):
  109. spool: SpoolCreate
  110. quantity: int = Field(default=1, ge=1, le=100)
  111. class SpoolUpdate(BaseModel):
  112. material: str | None = None
  113. subtype: str | None = None
  114. color_name: str | None = None
  115. rgba: str | None = Field(None, pattern=r"^[0-9A-Fa-f]{8}$")
  116. extra_colors: str | None = None
  117. effect_type: str | None = None
  118. brand: str | None = None
  119. @field_validator("extra_colors")
  120. @classmethod
  121. def _validate_extra_colors(cls, v: str | None) -> str | None:
  122. return normalize_extra_colors(v)
  123. @field_validator("effect_type")
  124. @classmethod
  125. def _validate_effect_type(cls, v: str | None) -> str | None:
  126. return normalize_effect_type(v)
  127. label_weight: int | None = None
  128. core_weight: int | None = None
  129. core_weight_catalog_id: int | None = None
  130. weight_used: float | None = None
  131. slicer_filament: str | None = None
  132. slicer_filament_name: str | None = None
  133. nozzle_temp_min: int | None = None
  134. nozzle_temp_max: int | None = None
  135. note: str | None = None
  136. tag_uid: str | None = None
  137. tray_uuid: str | None = None
  138. data_origin: str | None = None
  139. tag_type: str | None = None
  140. cost_per_kg: float | None = Field(default=None, ge=0)
  141. weight_locked: bool | None = None
  142. # User-defined category + per-spool low-stock threshold override (#729).
  143. category: str | None = Field(default=None, max_length=50)
  144. low_stock_threshold_pct: int | None = Field(default=None, ge=1, le=99)
  145. class SpoolKProfileBase(BaseModel):
  146. printer_id: int
  147. extruder: int = 0
  148. nozzle_diameter: str = "0.4"
  149. nozzle_type: str | None = None
  150. k_value: float
  151. name: str | None = None
  152. cali_idx: int | None = None
  153. setting_id: str | None = None
  154. class SpoolKProfileResponse(SpoolKProfileBase):
  155. id: int
  156. spool_id: int
  157. created_at: datetime
  158. class Config:
  159. from_attributes = True
  160. class SpoolResponse(SpoolBase):
  161. id: int
  162. # rgba is intentionally unconstrained on the response side: the write paths
  163. # (SpoolCreate, SpoolUpdate) enforce the 8-char hex pattern, but legacy rows
  164. # or data sourced from AMS firmware / backups may carry malformed values.
  165. # A single bad row must not 500 the entire inventory list endpoint (#1055).
  166. rgba: str | None = None
  167. added_full: bool | None = None
  168. last_used: datetime | None = None
  169. encode_time: datetime | None = None
  170. tag_uid: str | None = None
  171. tray_uuid: str | None = None
  172. data_origin: str | None = None
  173. tag_type: str | None = None
  174. archived_at: datetime | None = None
  175. created_at: datetime
  176. updated_at: datetime
  177. k_profiles: list[SpoolKProfileResponse] = []
  178. class Config:
  179. from_attributes = True
  180. class SpoolAssignmentCreate(BaseModel):
  181. spool_id: int
  182. printer_id: int
  183. ams_id: int
  184. tray_id: int
  185. class SpoolAssignmentResponse(BaseModel):
  186. id: int
  187. spool_id: int
  188. printer_id: int
  189. printer_name: str | None = None
  190. ams_id: int
  191. tray_id: int
  192. fingerprint_color: str | None = None
  193. fingerprint_type: str | None = None
  194. created_at: datetime
  195. spool: SpoolResponse | None = None
  196. configured: bool = False
  197. ams_label: str | None = None # User-defined friendly name for the AMS unit
  198. class Config:
  199. from_attributes = True