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