plate_clear.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. """Which printers the plate-clear gate holds (#2860).
  2. "Require plate-clear confirmation" is an app setting, and a printer may
  3. override it: on, off, or no override (the column is null) to follow the
  4. setting. A mixed fleet needs this, for example a printer with an automatic
  5. plate changer next to one that is cleared by hand.
  6. The overrides are cached here, by printer id, so the scheduler, WLED and the
  7. notification can ask without a query per printer. They are loaded at startup
  8. and kept current by the printer routes. This module imports nothing from the
  9. app, so every service can use it.
  10. """
  11. _overrides: dict[int, bool] = {}
  12. def set_override(printer_id: int, value: bool | None) -> None:
  13. """Record a printer's override; None follows the app setting."""
  14. if value is None:
  15. _overrides.pop(printer_id, None)
  16. else:
  17. _overrides[printer_id] = bool(value)
  18. def load_overrides(rows: dict[int, bool | None]) -> None:
  19. """Replace every cached override with the rows read from the database."""
  20. _overrides.clear()
  21. for printer_id, value in rows.items():
  22. set_override(printer_id, value)
  23. def applies(printer_id: int, setting: bool) -> bool:
  24. """Whether the gate applies to this printer, given the app setting."""
  25. return _overrides.get(printer_id, setting)