state.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # state.py
  2. import threading
  3. import json
  4. import os
  5. import logging
  6. logger = logging.getLogger(__name__)
  7. class AppState:
  8. def __init__(self):
  9. # Private variables for properties
  10. self._current_playing_file = None
  11. self._pause_requested = False
  12. self._speed = 150
  13. self._current_playlist = None
  14. self._current_playlist_name = None # New variable for playlist name
  15. # Regular state variables
  16. self.stop_requested = False
  17. self.pause_condition = threading.Condition()
  18. self.execution_progress = None
  19. self.is_clearing = False
  20. self.current_theta = 0
  21. self.current_rho = 0
  22. self.current_playlist_index = 0
  23. self.playlist_mode = "loop"
  24. # Machine position variables
  25. self.machine_x = 0.0
  26. self.machine_y = 0.0
  27. self.x_steps_per_mm = 0.0
  28. self.y_steps_per_mm = 0.0
  29. self.gear_ratio = 10
  30. # 0 for crash homing, 1 for auto homing
  31. self.homing = 0
  32. self.STATE_FILE = "state.json"
  33. self.mqtt_handler = None # Will be set by the MQTT handler
  34. self.conn = None
  35. self.port = None
  36. self.wled_ip = None
  37. self.led_controller = None
  38. self.skip_requested = False
  39. self._playlist_mode = "loop"
  40. self._pause_time = 0
  41. self._clear_pattern = "none"
  42. self.load()
  43. @property
  44. def current_playing_file(self):
  45. return self._current_playing_file
  46. @current_playing_file.setter
  47. def current_playing_file(self, value):
  48. self._current_playing_file = value
  49. # force an empty string (and not None) if we need to unset
  50. if value == None:
  51. value = ""
  52. if self.mqtt_handler:
  53. is_running = bool(value and not self._pause_requested)
  54. self.mqtt_handler.update_state(current_file=value, is_running=is_running)
  55. @property
  56. def pause_requested(self):
  57. return self._pause_requested
  58. @pause_requested.setter
  59. def pause_requested(self, value):
  60. self._pause_requested = value
  61. if self.mqtt_handler:
  62. is_running = bool(self._current_playing_file and not value)
  63. self.mqtt_handler.update_state(is_running=is_running)
  64. @property
  65. def speed(self):
  66. return self._speed
  67. @speed.setter
  68. def speed(self, value):
  69. self._speed = value
  70. if self.mqtt_handler and self.mqtt_handler.is_enabled:
  71. self.mqtt_handler.client.publish(f"{self.mqtt_handler.speed_topic}/state", value, retain=True)
  72. @property
  73. def current_playlist(self):
  74. return self._current_playlist
  75. @current_playlist.setter
  76. def current_playlist(self, value):
  77. self._current_playlist = value
  78. # force an empty string (and not None) if we need to unset
  79. if value == None:
  80. value = ""
  81. # Also clear the playlist name when playlist is cleared
  82. self._current_playlist_name = None
  83. if self.mqtt_handler:
  84. self.mqtt_handler.update_state(playlist=value, playlist_name=None)
  85. @property
  86. def current_playlist_name(self):
  87. return self._current_playlist_name
  88. @current_playlist_name.setter
  89. def current_playlist_name(self, value):
  90. self._current_playlist_name = value
  91. if self.mqtt_handler:
  92. self.mqtt_handler.update_state(playlist_name=value)
  93. @property
  94. def playlist_mode(self):
  95. return self._playlist_mode
  96. @playlist_mode.setter
  97. def playlist_mode(self, value):
  98. self._playlist_mode = value
  99. @property
  100. def pause_time(self):
  101. return self._pause_time
  102. @pause_time.setter
  103. def pause_time(self, value):
  104. self._pause_time = value
  105. @property
  106. def clear_pattern(self):
  107. return self._clear_pattern
  108. @clear_pattern.setter
  109. def clear_pattern(self, value):
  110. self._clear_pattern = value
  111. def to_dict(self):
  112. """Return a dictionary representation of the state."""
  113. return {
  114. "stop_requested": self.stop_requested,
  115. "pause_requested": self._pause_requested,
  116. "current_playing_file": self._current_playing_file,
  117. "execution_progress": self.execution_progress,
  118. "is_clearing": self.is_clearing,
  119. "current_theta": self.current_theta,
  120. "current_rho": self.current_rho,
  121. "speed": self._speed,
  122. "machine_x": self.machine_x,
  123. "machine_y": self.machine_y,
  124. "x_steps_per_mm": self.x_steps_per_mm,
  125. "y_steps_per_mm": self.y_steps_per_mm,
  126. "gear_ratio": self.gear_ratio,
  127. "homing": self.homing,
  128. "current_playlist": self._current_playlist,
  129. "current_playlist_name": self._current_playlist_name,
  130. "current_playlist_index": self.current_playlist_index,
  131. "playlist_mode": self._playlist_mode,
  132. "pause_time": self._pause_time,
  133. "clear_pattern": self._clear_pattern,
  134. "port": self.port,
  135. "wled_ip": self.wled_ip
  136. }
  137. def from_dict(self, data):
  138. """Update state from a dictionary."""
  139. self.stop_requested = data.get("stop_requested", False)
  140. self._pause_requested = data.get("pause_requested", False)
  141. self._current_playing_file = data.get("current_playing_file")
  142. self.execution_progress = data.get("execution_progress")
  143. self.is_clearing = data.get("is_clearing", False)
  144. self.current_theta = data.get("current_theta", 0)
  145. self.current_rho = data.get("current_rho", 0)
  146. self._speed = data.get("speed", 150)
  147. self.machine_x = data.get("machine_x", 0.0)
  148. self.machine_y = data.get("machine_y", 0.0)
  149. self.x_steps_per_mm = data.get("x_steps_per_mm", 0.0)
  150. self.y_steps_per_mm = data.get("y_steps_per_mm", 0.0)
  151. self.gear_ratio = data.get('gear_ratio', 10)
  152. self.homing = data.get('homing', 0)
  153. self._current_playlist = data.get("current_playlist")
  154. self._current_playlist_name = data.get("current_playlist_name")
  155. self.current_playlist_index = data.get("current_playlist_index")
  156. self._playlist_mode = data.get("playlist_mode", "loop")
  157. self._pause_time = data.get("pause_time", 0)
  158. self._clear_pattern = data.get("clear_pattern", "none")
  159. self.port = data.get("port", None)
  160. self.wled_ip = data.get('wled_ip', None)
  161. def save(self):
  162. """Save the current state to a JSON file."""
  163. try:
  164. with open(self.STATE_FILE, "w") as f:
  165. json.dump(self.to_dict(), f)
  166. except Exception as e:
  167. print(f"Error saving state to {self.STATE_FILE}: {e}")
  168. def load(self):
  169. """Load state from a JSON file. If the file doesn't exist, create it with default values."""
  170. if not os.path.exists(self.STATE_FILE):
  171. # File doesn't exist: create one with the current (default) state.
  172. self.save()
  173. return
  174. try:
  175. with open(self.STATE_FILE, "r") as f:
  176. data = json.load(f)
  177. self.from_dict(data)
  178. except Exception as e:
  179. print(f"Error loading state from {self.STATE_FILE}: {e}")
  180. def update_steps_per_mm(self, x_steps, y_steps):
  181. """Update and save steps per mm values."""
  182. self.x_steps_per_mm = x_steps
  183. self.y_steps_per_mm = y_steps
  184. self.save()
  185. def reset_state(self):
  186. """Reset all state variables to their default values."""
  187. self.__init__() # Reinitialize the state
  188. self.save()
  189. # Create a singleton instance that you can import elsewhere:
  190. state = AppState()