state.py 7.5 KB

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