1
0

utils.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. return {
  21. 'run_pattern': run_theta_rho_file, # async function
  22. 'run_playlist': run_playlist, # async function
  23. 'stop': stop_actions, # sync function
  24. 'pause': pause_execution, # sync function
  25. 'resume': resume_execution, # sync function
  26. 'home': home,
  27. 'set_speed': set_speed
  28. }
  29. def get_mqtt_state():
  30. """Get the current state for MQTT updates."""
  31. # Get list of pattern files
  32. patterns = list_theta_rho_files()
  33. # Get current execution status
  34. is_running = bool(state.current_playing_file) and not state.stop_requested
  35. # Get serial status
  36. serial_connected = (state.conn.is_connected() if state.conn else False)
  37. serial_port = state.port if serial_connected else None
  38. serial_status = f"connected to {serial_port}" if serial_connected else "disconnected"
  39. return {
  40. 'is_running': is_running,
  41. 'current_file': state.current_playing_file or '',
  42. 'patterns': sorted(patterns),
  43. 'serial': serial_status,
  44. 'current_playlist': state.current_playlist,
  45. 'current_playlist_index': state.current_playlist_index,
  46. 'playlist_mode': state.playlist_mode
  47. }