drying_preflight.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """Shared checks run before an AMS drying command is sent.
  2. Both the immediate POST /printers/{id}/drying/start endpoint and the
  3. scheduler's delayed dispatch go through here, so a run that the immediate
  4. path would refuse is never silently published by the scheduled path.
  5. """
  6. from backend.app.services.printer_manager import drying_screen_only, supports_drying
  7. SCREEN_ONLY_DETAIL = "This printer only supports AMS drying from its own screen"
  8. UNSUPPORTED_DETAIL = "Drying not supported for this printer model or firmware version"
  9. # Firmware dry_sf_reason codes, as surfaced by the AMS status payload.
  10. DRY_SF_REASON_MESSAGES = {
  11. 0: "Printer is busy",
  12. 1: "Insufficient power: too many AMS drying or external PSU required",
  13. 2: "AMS is busy",
  14. 3: "Filament is at the AMS outlet, retract it first",
  15. 4: "AMS is already starting a drying cycle",
  16. 5: "Not supported in 2D mode",
  17. 6: "AMS is already drying",
  18. 7: "AMS firmware is upgrading",
  19. 8: "Plug in the external AMS power adapter to start drying",
  20. }
  21. # Codes 1 and 8 mean a power-supply problem the user has to fix; the rest
  22. # clear on their own. The frontend splits blocked states the same way.
  23. POWER_REASON_CODES = frozenset({1, 8})
  24. # Code 3 also needs the user to act, but the fix is retracting filament rather
  25. # than anything to do with power, so it gets its own token instead of the
  26. # generic "cannot dry right now" the transient codes share.
  27. RETRACT_REASON_CODE = 3
  28. WAITING_REASON_POWER = "ams_power_required"
  29. WAITING_REASON_RETRACT = "ams_retract_filament"
  30. WAITING_REASON_BLOCKED = "ams_blocked"
  31. def check_drying_supported(model: str | None, firmware: str | None, *, require_firmware: bool = True) -> str | None:
  32. """Return a message if this printer cannot dry, else None.
  33. Pass require_firmware=False when there is no live status to read a version
  34. from, which leaves only the model check. Dispatch always has a status and
  35. judges both.
  36. """
  37. if drying_screen_only(model):
  38. return SCREEN_ONLY_DETAIL
  39. if require_firmware and not supports_drying(model, firmware):
  40. return UNSUPPORTED_DETAIL
  41. return None
  42. def find_ams_unit(state, ams_id: int) -> dict | None:
  43. """Locate an AMS unit in a live printer status payload."""
  44. for unit in (state.raw_data.get("ams") if state else None) or []:
  45. try:
  46. if int(unit.get("id", -1)) == ams_id:
  47. return unit
  48. except (TypeError, ValueError):
  49. continue
  50. return None
  51. def blocking_reason_codes(unit: dict | None) -> list[int]:
  52. """Known dry_sf_reason codes on this unit, in reported order."""
  53. codes = []
  54. for code in (unit or {}).get("dry_sf_reason") or []:
  55. try:
  56. code_int = int(code)
  57. except (TypeError, ValueError):
  58. continue
  59. if code_int in DRY_SF_REASON_MESSAGES:
  60. codes.append(code_int)
  61. return codes
  62. def primary_reason_code(codes: list[int]) -> int | None:
  63. """The one code to report when the AMS sets several at once.
  64. Power and filament-at-the-outlet need the user to go and do something; the
  65. rest clear on their own. Naming an actionable one is more use than whichever
  66. the firmware happened to list first, and going through here keeps the
  67. immediate endpoint's message and the scheduled row's waiting_reason
  68. describing the same blocked AMS the same way.
  69. """
  70. for code in codes:
  71. if code in POWER_REASON_CODES:
  72. return code
  73. if RETRACT_REASON_CODE in codes:
  74. return RETRACT_REASON_CODE
  75. return codes[0] if codes else None
  76. def waiting_reason_for_codes(codes: list[int]) -> str:
  77. """Map blocking codes onto the token the frontend translates."""
  78. code = primary_reason_code(codes)
  79. if code in POWER_REASON_CODES:
  80. return WAITING_REASON_POWER
  81. if code == RETRACT_REASON_CODE:
  82. return WAITING_REASON_RETRACT
  83. return WAITING_REASON_BLOCKED
  84. def resolve_filament(unit: dict | None, filament: str) -> str:
  85. """Fill an empty filament field from the first loaded tray.
  86. The printer rejects a drying payload with no filament type, so both
  87. callers fall back to the loaded spool and then to PLA.
  88. """
  89. if filament:
  90. return filament
  91. for tray in (unit or {}).get("tray") or []:
  92. tray_type = tray.get("tray_type")
  93. if tray_type:
  94. return str(tray_type)
  95. return "PLA"