spool.py 5.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from datetime import datetime
  2. from sqlalchemy import Boolean, DateTime, Float, Integer, String, func
  3. from sqlalchemy.orm import Mapped, mapped_column, relationship
  4. from backend.app.core.database import Base
  5. class Spool(Base):
  6. """Spool inventory item for tracking filament spools and their properties."""
  7. __tablename__ = "spool"
  8. id: Mapped[int] = mapped_column(primary_key=True)
  9. material: Mapped[str] = mapped_column(String(50)) # PLA, PETG, ABS, etc.
  10. subtype: Mapped[str | None] = mapped_column(String(50)) # Basic, Matte, Silk, etc.
  11. color_name: Mapped[str | None] = mapped_column(String(100)) # "Jade White"
  12. rgba: Mapped[str | None] = mapped_column(String(8)) # RRGGBBAA hex
  13. # Multi-colour gradient stops for filaments with more than one colour
  14. # (e.g. tri-colour, multi-colour). Stored as comma-separated 6- or 8-char
  15. # hex tokens without `#`. Empty/NULL means solid (uses `rgba`). Up to 8
  16. # stops; combination mode is driven by `subtype` (Gradient, Multicolor).
  17. extra_colors: Mapped[str | None] = mapped_column(String(255))
  18. # Visual effect overlay independent of subtype: sparkle, wood, marble,
  19. # glow, matte. Purely a rendering hint — does not affect MQTT/firmware.
  20. effect_type: Mapped[str | None] = mapped_column(String(20))
  21. brand: Mapped[str | None] = mapped_column(String(100)) # "Polymaker"
  22. label_weight: Mapped[int] = mapped_column(Integer, default=1000) # Advertised net weight (g)
  23. core_weight: Mapped[int] = mapped_column(Integer, default=250) # Empty spool weight (g)
  24. core_weight_catalog_id: Mapped[int | None] = mapped_column(
  25. Integer
  26. ) # Reference to spool_catalog entry for core weight
  27. weight_used: Mapped[float] = mapped_column(Float, default=0) # Consumed grams
  28. # Anchor for the resettable "Total Consumed" stat. The displayed counter
  29. # is `weight_used - weight_used_baseline`; the Inventory page's "Reset
  30. # usage to 0" action stamps baseline = weight_used so the counter zeroes
  31. # without disturbing remaining (= label_weight - weight_used). Matches
  32. # Spoolman's split between used_weight and remaining_weight (#1390).
  33. weight_used_baseline: Mapped[float] = mapped_column(Float, default=0)
  34. weight_locked: Mapped[bool] = mapped_column(Boolean, default=False) # Lock weight from AMS auto-sync
  35. last_scale_weight: Mapped[int | None] = mapped_column(Integer) # Last gross weight from scale (g)
  36. last_weighed_at: Mapped[datetime | None] = mapped_column(DateTime) # When last weighed
  37. slicer_filament: Mapped[str | None] = mapped_column(String(50)) # Preset ID (e.g. "GFL99")
  38. slicer_filament_name: Mapped[str | None] = mapped_column(String(100)) # Preset name for slicer
  39. nozzle_temp_min: Mapped[int | None] = mapped_column() # Override min temp
  40. nozzle_temp_max: Mapped[int | None] = mapped_column() # Override max temp
  41. note: Mapped[str | None] = mapped_column(String(500))
  42. added_full: Mapped[bool | None] = mapped_column() # Whether spool was added as full (unused)
  43. # User-defined category (e.g. "Production", "Prototype", "Client A") for
  44. # filtering and per-group low-stock thresholds (#729). Free text — the
  45. # form autocompletes from categories already present on other spools.
  46. category: Mapped[str | None] = mapped_column(String(50))
  47. # Per-spool override of the global inventory low-stock threshold (%).
  48. # NULL falls back to the `low_stock_threshold` setting. Lets users mark
  49. # production spools with a higher threshold (alert earlier) and prototype
  50. # spools with a lower one without changing the global default.
  51. low_stock_threshold_pct: Mapped[int | None] = mapped_column(Integer)
  52. # Cost tracking
  53. cost_per_kg: Mapped[float | None] = mapped_column(Float) # Cost per kilogram
  54. storage_location: Mapped[str | None] = mapped_column(String(255)) # User-editable storage location
  55. last_used: Mapped[datetime | None] = mapped_column(DateTime) # Last time this spool was used in a print
  56. encode_time: Mapped[datetime | None] = mapped_column(DateTime) # When spool was encoded/written to tag
  57. tag_uid: Mapped[str | None] = mapped_column(String(32)) # RFID tag UID (up to 32 hex chars)
  58. tray_uuid: Mapped[str | None] = mapped_column(String(32)) # Bambu Lab spool UUID (32 hex chars)
  59. data_origin: Mapped[str | None] = mapped_column(String(20)) # How data was populated: manual, rfid_auto, nfc_link
  60. tag_type: Mapped[str | None] = mapped_column(String(20)) # Tag vendor: bambulab, generic, etc.
  61. archived_at: Mapped[datetime | None] = mapped_column(DateTime) # NULL = active
  62. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  63. updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
  64. k_profiles: Mapped[list["SpoolKProfile"]] = relationship(back_populates="spool", cascade="all, delete-orphan")
  65. assignments: Mapped[list["SpoolAssignment"]] = relationship(back_populates="spool", cascade="all, delete-orphan")
  66. from backend.app.models.spool_assignment import SpoolAssignment # noqa: E402
  67. from backend.app.models.spool_k_profile import SpoolKProfile # noqa: E402