state.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # state.py
  2. import threading
  3. import json
  4. import os
  5. class AppState:
  6. def __init__(self):
  7. # Private variables for properties
  8. self._current_playing_file = None
  9. self._pause_requested = False
  10. self._speed = 250
  11. self._current_playlist = None
  12. # Regular state variables
  13. self.stop_requested = False
  14. self.pause_condition = threading.Condition()
  15. self.execution_progress = None
  16. self.is_clearing = False
  17. self.current_theta = 0
  18. self.current_rho = 0
  19. self.current_playlist_index = 0
  20. self.playlist_mode = None
  21. # Machine position variables
  22. self.machine_x = 0.0
  23. self.machine_y = 0.0
  24. self.STATE_FILE = "state.json"
  25. self.mqtt_handler = None # Will be set by the MQTT handler
  26. self.load()
  27. @property
  28. def current_playing_file(self):
  29. return self._current_playing_file
  30. @current_playing_file.setter
  31. def current_playing_file(self, value):
  32. self._current_playing_file = value
  33. # force an empty string (and not None) if we need to unset
  34. if value == None:
  35. value = ""
  36. if self.mqtt_handler:
  37. is_running = bool(value and not self._pause_requested)
  38. self.mqtt_handler.update_state(current_file=value, is_running=is_running)
  39. @property
  40. def pause_requested(self):
  41. return self._pause_requested
  42. @pause_requested.setter
  43. def pause_requested(self, value):
  44. self._pause_requested = value
  45. if self.mqtt_handler:
  46. is_running = bool(self._current_playing_file and not value)
  47. self.mqtt_handler.update_state(is_running=is_running)
  48. @property
  49. def speed(self):
  50. return self._speed
  51. @speed.setter
  52. def speed(self, value):
  53. self._speed = value
  54. if self.mqtt_handler and self.mqtt_handler.is_enabled:
  55. self.mqtt_handler.client.publish(f"{self.mqtt_handler.speed_topic}/state", value, retain=True)
  56. @property
  57. def current_playlist(self):
  58. return self._current_playlist
  59. @current_playlist.setter
  60. def current_playlist(self, value):
  61. self._current_playlist = value
  62. # force an empty string (and not None) if we need to unset
  63. if value == None:
  64. value = ""
  65. if self.mqtt_handler:
  66. self.mqtt_handler.update_state(playlist=value)
  67. def to_dict(self):
  68. """Return a dictionary representation of the state."""
  69. return {
  70. "stop_requested": self.stop_requested,
  71. "pause_requested": self._pause_requested,
  72. "current_playing_file": self._current_playing_file,
  73. "execution_progress": self.execution_progress,
  74. "is_clearing": self.is_clearing,
  75. "current_theta": self.current_theta,
  76. "current_rho": self.current_rho,
  77. "speed": self._speed,
  78. "machine_x": self.machine_x,
  79. "machine_y": self.machine_y,
  80. "current_playlist": self._current_playlist,
  81. "current_playlist_index": self.current_playlist_index,
  82. "playlist_mode": self.playlist_mode
  83. }
  84. def from_dict(self, data):
  85. """Update state from a dictionary."""
  86. self.stop_requested = data.get("stop_requested", False)
  87. self._pause_requested = data.get("pause_requested", False)
  88. self._current_playing_file = data.get("current_playing_file")
  89. self.execution_progress = data.get("execution_progress")
  90. self.is_clearing = data.get("is_clearing", False)
  91. self.current_theta = data.get("current_theta", 0)
  92. self.current_rho = data.get("current_rho", 0)
  93. self._speed = data.get("speed", 250)
  94. self.machine_x = data.get("machine_x", 0.0)
  95. self.machine_y = data.get("machine_y", 0.0)
  96. self._current_playlist = data.get("current_playlist")
  97. self.current_playlist_index = data.get("current_playlist_index")
  98. self.playlist_mode = data.get("playlist_mode")
  99. def save(self):
  100. """Save the current state to a JSON file."""
  101. with open(self.STATE_FILE, "w") as f:
  102. json.dump(self.to_dict(), f)
  103. def load(self):
  104. """Load state from a JSON file. If the file doesn't exist, create it with default values."""
  105. if not os.path.exists(self.STATE_FILE):
  106. # File doesn't exist: create one with the current (default) state.
  107. self.save()
  108. return
  109. try:
  110. with open(self.STATE_FILE, "r") as f:
  111. data = json.load(f)
  112. self.from_dict(data)
  113. except Exception as e:
  114. print(f"Error loading state from {self.STATE_FILE}: {e}")
  115. # Create a singleton instance that you can import elsewhere:
  116. state = AppState()