printer.py 5.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from datetime import datetime
  2. from sqlalchemy import JSON, Boolean, DateTime, Float, Integer, String, false, func
  3. from sqlalchemy.orm import Mapped, mapped_column, relationship
  4. from backend.app.core.database import Base
  5. class Printer(Base):
  6. __tablename__ = "printers"
  7. # SQLite would give a freed id to the next row; see core/database AUTOINCREMENT_TABLES
  8. __table_args__ = {"sqlite_autoincrement": True}
  9. id: Mapped[int] = mapped_column(primary_key=True)
  10. name: Mapped[str] = mapped_column(String(100))
  11. serial_number: Mapped[str] = mapped_column(String(50), unique=True)
  12. ip_address: Mapped[str] = mapped_column(String(253))
  13. access_code: Mapped[str] = mapped_column(String(20))
  14. model: Mapped[str | None] = mapped_column(String(50))
  15. location: Mapped[str | None] = mapped_column(String(100)) # Group/location name
  16. nozzle_count: Mapped[int] = mapped_column(default=1) # 1 or 2, auto-detected from MQTT
  17. is_active: Mapped[bool] = mapped_column(Boolean, default=True)
  18. auto_archive: Mapped[bool] = mapped_column(Boolean, default=True)
  19. print_hours_offset: Mapped[float] = mapped_column(Float, default=0.0) # Baseline hours to add
  20. # What an hour of printing costs in wear on this machine (#694). Empty or 0
  21. # leaves wear cost off for the printer.
  22. wear_cost_per_hour: Mapped[float | None] = mapped_column(Float, nullable=True)
  23. # Place in the order "Any model" jobs pick printers when Settings > Queue >
  24. # printer order is "priority" (#2915): lowest first, empty last, ties by id.
  25. dispatch_priority: Mapped[int | None] = mapped_column(Integer, nullable=True)
  26. runtime_seconds: Mapped[int] = mapped_column(
  27. default=0
  28. ) # Accumulated active runtime (RUNNING state only — see #1521)
  29. last_runtime_update: Mapped[datetime | None] = mapped_column(
  30. DateTime, nullable=True
  31. ) # Last time runtime was updated
  32. # External camera configuration
  33. external_camera_url: Mapped[str | None] = mapped_column(String(500), nullable=True)
  34. external_camera_type: Mapped[str | None] = mapped_column(String(20), nullable=True) # mjpeg, rtsp, snapshot
  35. external_camera_enabled: Mapped[bool] = mapped_column(Boolean, default=False)
  36. # Optional single-frame snapshot URL — when set, used for snapshot / finish-photo
  37. # / timelapse / plate-detect captures instead of opening the live stream and
  38. # skipping a warm-up frame. Bypasses MJPEG warm-up issues on sources that
  39. # expose a dedicated frame endpoint (e.g. go2rtc's /api/frame.jpeg). #1177.
  40. external_camera_snapshot_url: Mapped[str | None] = mapped_column(String(500), nullable=True)
  41. camera_rotation: Mapped[int] = mapped_column(default=0) # 0, 90, 180, 270 degrees
  42. # Picked for the chamber light while the camera is in use (#1655). Only
  43. # counts when the app setting camera_light_mode is "selected".
  44. camera_light_auto: Mapped[bool] = mapped_column(Boolean, default=False, server_default=false())
  45. # Plate detection - check if build plate is empty before starting print
  46. plate_detection_enabled: Mapped[bool] = mapped_column(Boolean, default=False)
  47. # ROI for plate detection (percentages: 0.0-1.0)
  48. plate_detection_roi_x: Mapped[float | None] = mapped_column(Float, nullable=True) # X start %
  49. plate_detection_roi_y: Mapped[float | None] = mapped_column(Float, nullable=True) # Y start %
  50. plate_detection_roi_w: Mapped[float | None] = mapped_column(Float, nullable=True) # Width %
  51. plate_detection_roi_h: Mapped[float | None] = mapped_column(Float, nullable=True) # Height %
  52. # Queue: True after a print finishes/fails, until user acknowledges the plate is cleared.
  53. # Persisted so the gate survives crashes and power cycles (issue #961).
  54. awaiting_plate_clear: Mapped[bool] = mapped_column(Boolean, default=False)
  55. # Optional per-printer WLED preset mapping. WLED owns all visual effects.
  56. wled_config: Mapped[dict | None] = mapped_column(JSON, nullable=True)
  57. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  58. updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
  59. # Relationships
  60. archives: Mapped[list["PrintArchive"]] = relationship(back_populates="printer", cascade="all, delete-orphan")
  61. # Deleted with the printer, as providers: unassigned plugs are everyone's.
  62. smart_plugs: Mapped[list["SmartPlug"]] = relationship(back_populates="printer", cascade="all, delete-orphan")
  63. # Deleted with the printer: setting their printer_id to NULL (the default)
  64. # would make them providers for all printers.
  65. notification_providers: Mapped[list["NotificationProvider"]] = relationship(
  66. back_populates="printer", cascade="all, delete-orphan"
  67. )
  68. maintenance_items: Mapped[list["PrinterMaintenance"]] = relationship(
  69. back_populates="printer", cascade="all, delete-orphan"
  70. )
  71. kprofile_notes: Mapped[list["KProfileNote"]] = relationship(back_populates="printer", cascade="all, delete-orphan")
  72. ams_history: Mapped[list["AMSSensorHistory"]] = relationship(back_populates="printer", cascade="all, delete-orphan")
  73. sensor_history: Mapped[list["PrinterSensorHistory"]] = relationship(
  74. back_populates="printer", cascade="all, delete-orphan"
  75. )
  76. ha_sensors: Mapped[list["PrinterHASensor"]] = relationship(back_populates="printer", cascade="all, delete-orphan")
  77. from backend.app.models.ams_history import AMSSensorHistory # noqa: E402
  78. from backend.app.models.archive import PrintArchive # noqa: E402
  79. from backend.app.models.kprofile_note import KProfileNote # noqa: E402
  80. from backend.app.models.maintenance import PrinterMaintenance # noqa: E402
  81. from backend.app.models.notification import NotificationProvider # noqa: E402
  82. from backend.app.models.printer_ha_sensor import PrinterHASensor # noqa: E402
  83. from backend.app.models.printer_sensor_history import PrinterSensorHistory # noqa: E402
  84. from backend.app.models.smart_plug import SmartPlug # noqa: E402