virtual_printer.py 5.0 KB

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