virtual_printer.py 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from datetime import datetime
  2. from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, String, func
  3. from sqlalchemy.orm import Mapped, mapped_column
  4. from backend.app.core.database import Base
  5. # Canonical VP mode values. The legacy values `immediate` (→ archive) and
  6. # `print_queue` (→ queue) shipped before the UI labels were aligned with the
  7. # wire format. `normalize_vp_mode()` translates input from either form and
  8. # the DB migration in `core/database.py` rewrites existing rows once at boot.
  9. VP_MODE_ARCHIVE = "archive"
  10. VP_MODE_REVIEW = "review"
  11. VP_MODE_QUEUE = "queue"
  12. VP_MODE_PROXY = "proxy"
  13. VP_MODE_VALUES = (VP_MODE_ARCHIVE, VP_MODE_REVIEW, VP_MODE_QUEUE, VP_MODE_PROXY)
  14. # Legacy → canonical map. Kept narrow on purpose so unrelated typos surface
  15. # instead of getting silently re-pointed at a default.
  16. _VP_MODE_ALIASES = {
  17. "immediate": VP_MODE_ARCHIVE,
  18. "print_queue": VP_MODE_QUEUE,
  19. }
  20. def normalize_vp_mode(value: str | None) -> str | None:
  21. """Map legacy wire values (`immediate`, `print_queue`) to canonical names.
  22. Returns `None` unchanged so callers can decide whether to apply a default.
  23. Returns unknown values unchanged so validators still see them and reject.
  24. """
  25. if value is None:
  26. return None
  27. return _VP_MODE_ALIASES.get(value, value)
  28. class VirtualPrinter(Base):
  29. """Virtual printer configuration for multi-instance support."""
  30. __tablename__ = "virtual_printers"
  31. id: Mapped[int] = mapped_column(primary_key=True)
  32. name: Mapped[str] = mapped_column(String(100), default="Bambuddy")
  33. enabled: Mapped[bool] = mapped_column(Boolean, default=False)
  34. mode: Mapped[str] = mapped_column(String(20), default=VP_MODE_ARCHIVE) # archive|review|queue|proxy
  35. auto_dispatch: Mapped[bool] = mapped_column(Boolean, server_default="true") # queue mode: auto-start or manual
  36. queue_force_color_match: Mapped[bool] = mapped_column(
  37. Boolean, server_default="false"
  38. ) # queue mode: pin per-slot type+color from the 3MF onto the queue
  39. # item so the scheduler refuses to dispatch onto a printer with the wrong
  40. # filament loaded (#1188).
  41. save_ams_mapping: Mapped[bool] = mapped_column(
  42. Boolean, server_default="false"
  43. ) # queue mode: keep the slicer's own live-resolved AMS-slot pick (the
  44. # `ams_mapping` field on the MQTT `project_file` command) instead of
  45. # re-deriving one from the file's static type/color. Stamps it on the queue
  46. # item so THIS print dispatches to those trays, and onto the archive's
  47. # `extra_data.slicer_ams_mapping` so a later reprint can reuse the same
  48. # physical spools. Off by default: taking the slicer's pick makes the
  49. # scheduler skip `_compute_ams_mapping_for_printer`, and with it
  50. # `prefer_lowest_filament`, its AMS-backup gate (#1766) and the
  51. # inventory-remain overrides — so it stays opt-in per virtual printer
  52. # rather than changing behaviour for upgraders (#2700).
  53. gcode_injection: Mapped[bool] = mapped_column(
  54. Boolean, server_default="false"
  55. ) # queue mode: opt this VP's Send/Print jobs into per-model G-code snippet
  56. # injection (#1516). Default off so existing gcode_snippets users don't
  57. # silently start injecting; no-op when no snippets exist for the model.
  58. model: Mapped[str | None] = mapped_column(String(50), nullable=True) # SSDP model code (server mode)
  59. access_code: Mapped[str | None] = mapped_column(String(8), nullable=True) # 8 chars (server mode)
  60. target_printer_id: Mapped[int | None] = mapped_column(
  61. Integer, ForeignKey("printers.id", ondelete="SET NULL"), nullable=True
  62. ) # proxy mode
  63. bind_ip: Mapped[str | None] = mapped_column(String(45), nullable=True) # dedicated IP (proxy mode)
  64. remote_interface_ip: Mapped[str | None] = mapped_column(String(45), nullable=True) # SSDP advertise IP
  65. tailscale_disabled: Mapped[bool] = mapped_column(
  66. Boolean, server_default="true"
  67. ) # opt-in: user must explicitly enable; auto-detect only runs then
  68. serial_suffix: Mapped[str] = mapped_column(String(9), default="391800001") # unique per printer
  69. position: Mapped[int] = mapped_column(Integer, default=0)
  70. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  71. updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())