1
0

utils.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """MQTT utilities and callback management."""
  2. from typing import Dict, Callable
  3. from modules.core.pattern_manager import list_theta_rho_files
  4. from modules.core import execution
  5. from modules.connection.connection_manager import home
  6. from modules.core.state import state
  7. def create_mqtt_callbacks() -> Dict[str, Callable]:
  8. """Create and return the MQTT callback registry.
  9. All execution actions are firmware-delegated (modules/core/execution).
  10. The MQTT handler checks and handles both async and sync callables.
  11. """
  12. return {
  13. 'run_pattern': execution.run_pattern, # async
  14. 'run_playlist': execution.start_playlist, # async
  15. 'stop': execution.stop, # async
  16. 'pause': execution.pause, # async
  17. 'resume': execution.resume, # async
  18. 'skip': execution.skip, # async
  19. 'home': home,
  20. 'set_speed': execution.set_speed, # async
  21. }
  22. def get_mqtt_state():
  23. """Get the current state for MQTT updates."""
  24. patterns = list_theta_rho_files()
  25. status = execution.get_cached_status()
  26. is_running = bool(status.get("is_running"))
  27. board_connected = (state.conn.is_connected() if state.conn else False)
  28. board_status = f"connected to {state.port}" if board_connected else "disconnected"
  29. return {
  30. 'is_running': is_running,
  31. 'current_file': state.current_playing_file or '',
  32. 'patterns': sorted(patterns),
  33. 'serial': board_status,
  34. 'current_playlist': state.current_playlist,
  35. 'current_playlist_index': (status.get("playlist") or {}).get("current_index"),
  36. 'playlist_mode': state.playlist_mode
  37. }