1
0

utils.py 2.0 KB

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