1
0

utils.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """MQTT utilities and callback management."""
  2. import os
  3. from typing import Dict, Callable
  4. from modules.core.pattern_manager import (
  5. run_theta_rho_file, stop_actions, pause_execution,
  6. resume_execution, THETA_RHO_DIR,
  7. run_theta_rho_files, list_theta_rho_files
  8. )
  9. from modules.core.playlist_manager import get_playlist, run_playlist
  10. from modules.connection.connection_manager import home
  11. from modules.core.state import state
  12. def create_mqtt_callbacks() -> Dict[str, Callable]:
  13. """Create and return the MQTT callback registry.
  14. Note: run_theta_rho_file and run_playlist are async functions,
  15. while pause_execution, resume_execution, and stop_actions are sync functions.
  16. The MQTT handler will check and handle both async and sync appropriately.
  17. """
  18. def set_speed(speed):
  19. state.speed = speed
  20. def skip_pattern():
  21. state.skip_requested = True
  22. return {
  23. 'run_pattern': run_theta_rho_file, # async function
  24. 'run_playlist': run_playlist, # async function
  25. 'stop': stop_actions, # sync function
  26. 'pause': pause_execution, # sync function
  27. 'resume': resume_execution, # sync function
  28. 'skip': skip_pattern, # sync function
  29. 'home': home,
  30. 'set_speed': set_speed
  31. }
  32. def get_mqtt_state():
  33. """Get the current state for MQTT updates."""
  34. # Get list of pattern files
  35. patterns = list_theta_rho_files()
  36. # Get current execution status
  37. is_running = bool(state.current_playing_file) and not state.stop_requested
  38. # Get serial status
  39. serial_connected = (state.conn.is_connected() if state.conn else False)
  40. serial_port = state.port if serial_connected else None
  41. serial_status = f"connected to {serial_port}" if serial_connected else "disconnected"
  42. return {
  43. 'is_running': is_running,
  44. 'current_file': state.current_playing_file or '',
  45. 'patterns': sorted(patterns),
  46. 'serial': serial_status,
  47. 'current_playlist': state.current_playlist,
  48. 'current_playlist_index': state.current_playlist_index,
  49. 'playlist_mode': state.playlist_mode
  50. }