state.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 = 100
  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. self.pause_time_remaining = 0
  25. # Machine position variables
  26. self.machine_x = 0.0
  27. self.machine_y = 0.0
  28. self.x_steps_per_mm = 0.0
  29. self.y_steps_per_mm = 0.0
  30. self.gear_ratio = 10
  31. # 0 for crash homing, 1 for auto homing
  32. self.homing = 0
  33. self.STATE_FILE = "state.json"
  34. self.mqtt_handler = None # Will be set by the MQTT handler
  35. self.conn = None
  36. self.port = None
  37. self.wled_ip = None
  38. self.led_controller = None
  39. self.skip_requested = False
  40. self.table_type = None
  41. self._playlist_mode = "loop"
  42. self._pause_time = 0
  43. self._clear_pattern = "none"
  44. self._clear_pattern_speed = None # None means use state.speed as default
  45. self.custom_clear_from_in = None # Custom clear from center pattern
  46. self.custom_clear_from_out = None # Custom clear from perimeter pattern
  47. # Application name setting
  48. self.app_name = "Dune Weaver" # Default app name
  49. # auto_play mode settings
  50. self.auto_play_enabled = False
  51. self.auto_play_playlist = None # Playlist to auto-play in auto_play mode
  52. self.auto_play_run_mode = "loop" # "single" or "loop"
  53. self.auto_play_pause_time = 5.0 # Pause between patterns in seconds
  54. self.auto_play_clear_pattern = "adaptive" # Clear pattern option
  55. self.auto_play_shuffle = False # Shuffle playlist
  56. # Still Sands settings
  57. self.scheduled_pause_enabled = False
  58. self.scheduled_pause_time_slots = [] # List of time slot dictionaries
  59. self.scheduled_pause_control_wled = False # Turn off WLED during pause periods
  60. self.load()
  61. @property
  62. def current_playing_file(self):
  63. return self._current_playing_file
  64. @current_playing_file.setter
  65. def current_playing_file(self, value):
  66. self._current_playing_file = value
  67. # force an empty string (and not None) if we need to unset
  68. if value == None:
  69. value = ""
  70. if self.mqtt_handler:
  71. is_running = bool(value and not self._pause_requested)
  72. self.mqtt_handler.update_state(current_file=value, is_running=is_running)
  73. @property
  74. def pause_requested(self):
  75. return self._pause_requested
  76. @pause_requested.setter
  77. def pause_requested(self, value):
  78. self._pause_requested = value
  79. if self.mqtt_handler:
  80. is_running = bool(self._current_playing_file and not value)
  81. self.mqtt_handler.update_state(is_running=is_running)
  82. @property
  83. def speed(self):
  84. return self._speed
  85. @speed.setter
  86. def speed(self, value):
  87. self._speed = value
  88. if self.mqtt_handler and self.mqtt_handler.is_enabled:
  89. self.mqtt_handler.client.publish(f"{self.mqtt_handler.speed_topic}/state", value, retain=True)
  90. @property
  91. def current_playlist(self):
  92. return self._current_playlist
  93. @current_playlist.setter
  94. def current_playlist(self, value):
  95. self._current_playlist = value
  96. # force an empty string (and not None) if we need to unset
  97. if value == None:
  98. value = ""
  99. # Also clear the playlist name when playlist is cleared
  100. self._current_playlist_name = None
  101. if self.mqtt_handler:
  102. self.mqtt_handler.update_state(playlist=value, playlist_name=None)
  103. @property
  104. def current_playlist_name(self):
  105. return self._current_playlist_name
  106. @current_playlist_name.setter
  107. def current_playlist_name(self, value):
  108. self._current_playlist_name = value
  109. if self.mqtt_handler:
  110. self.mqtt_handler.update_state(playlist_name=value)
  111. @property
  112. def playlist_mode(self):
  113. return self._playlist_mode
  114. @playlist_mode.setter
  115. def playlist_mode(self, value):
  116. self._playlist_mode = value
  117. @property
  118. def pause_time(self):
  119. return self._pause_time
  120. @pause_time.setter
  121. def pause_time(self, value):
  122. self._pause_time = value
  123. @property
  124. def clear_pattern(self):
  125. return self._clear_pattern
  126. @clear_pattern.setter
  127. def clear_pattern(self, value):
  128. self._clear_pattern = value
  129. @property
  130. def clear_pattern_speed(self):
  131. return self._clear_pattern_speed
  132. @clear_pattern_speed.setter
  133. def clear_pattern_speed(self, value):
  134. self._clear_pattern_speed = value
  135. def to_dict(self):
  136. """Return a dictionary representation of the state."""
  137. return {
  138. "stop_requested": self.stop_requested,
  139. "pause_requested": self._pause_requested,
  140. "current_playing_file": self._current_playing_file,
  141. "execution_progress": self.execution_progress,
  142. "is_clearing": self.is_clearing,
  143. "current_theta": self.current_theta,
  144. "current_rho": self.current_rho,
  145. "speed": self._speed,
  146. "machine_x": self.machine_x,
  147. "machine_y": self.machine_y,
  148. "x_steps_per_mm": self.x_steps_per_mm,
  149. "y_steps_per_mm": self.y_steps_per_mm,
  150. "gear_ratio": self.gear_ratio,
  151. "homing": self.homing,
  152. "current_playlist": self._current_playlist,
  153. "current_playlist_name": self._current_playlist_name,
  154. "current_playlist_index": self.current_playlist_index,
  155. "playlist_mode": self._playlist_mode,
  156. "pause_time": self._pause_time,
  157. "clear_pattern": self._clear_pattern,
  158. "clear_pattern_speed": self._clear_pattern_speed,
  159. "custom_clear_from_in": self.custom_clear_from_in,
  160. "custom_clear_from_out": self.custom_clear_from_out,
  161. "port": self.port,
  162. "wled_ip": self.wled_ip,
  163. "app_name": self.app_name,
  164. "auto_play_enabled": self.auto_play_enabled,
  165. "auto_play_playlist": self.auto_play_playlist,
  166. "auto_play_run_mode": self.auto_play_run_mode,
  167. "auto_play_pause_time": self.auto_play_pause_time,
  168. "auto_play_clear_pattern": self.auto_play_clear_pattern,
  169. "auto_play_shuffle": self.auto_play_shuffle,
  170. "scheduled_pause_enabled": self.scheduled_pause_enabled,
  171. "scheduled_pause_time_slots": self.scheduled_pause_time_slots,
  172. "scheduled_pause_control_wled": self.scheduled_pause_control_wled,
  173. }
  174. def from_dict(self, data):
  175. """Update state from a dictionary."""
  176. self.stop_requested = data.get("stop_requested", False)
  177. self._pause_requested = data.get("pause_requested", False)
  178. self._current_playing_file = data.get("current_playing_file", None)
  179. self.execution_progress = data.get("execution_progress")
  180. self.is_clearing = data.get("is_clearing", False)
  181. self.current_theta = data.get("current_theta", 0)
  182. self.current_rho = data.get("current_rho", 0)
  183. self._speed = data.get("speed", 150)
  184. self.machine_x = data.get("machine_x", 0.0)
  185. self.machine_y = data.get("machine_y", 0.0)
  186. self.x_steps_per_mm = data.get("x_steps_per_mm", 0.0)
  187. self.y_steps_per_mm = data.get("y_steps_per_mm", 0.0)
  188. self.gear_ratio = data.get('gear_ratio', 10)
  189. self.homing = data.get('homing', 0)
  190. self._current_playlist = data.get("current_playlist", None)
  191. self._current_playlist_name = data.get("current_playlist_name", None)
  192. self.current_playlist_index = data.get("current_playlist_index", None)
  193. self._playlist_mode = data.get("playlist_mode", "loop")
  194. self._pause_time = data.get("pause_time", 0)
  195. self._clear_pattern = data.get("clear_pattern", "none")
  196. self._clear_pattern_speed = data.get("clear_pattern_speed", None)
  197. self.custom_clear_from_in = data.get("custom_clear_from_in", None)
  198. self.custom_clear_from_out = data.get("custom_clear_from_out", None)
  199. self.port = data.get("port", None)
  200. self.wled_ip = data.get('wled_ip', None)
  201. self.app_name = data.get("app_name", "Dune Weaver")
  202. self.auto_play_enabled = data.get("auto_play_enabled", False)
  203. self.auto_play_playlist = data.get("auto_play_playlist", None)
  204. self.auto_play_run_mode = data.get("auto_play_run_mode", "loop")
  205. self.auto_play_pause_time = data.get("auto_play_pause_time", 5.0)
  206. self.auto_play_clear_pattern = data.get("auto_play_clear_pattern", "adaptive")
  207. self.auto_play_shuffle = data.get("auto_play_shuffle", False)
  208. self.scheduled_pause_enabled = data.get("scheduled_pause_enabled", False)
  209. self.scheduled_pause_time_slots = data.get("scheduled_pause_time_slots", [])
  210. self.scheduled_pause_control_wled = data.get("scheduled_pause_control_wled", False)
  211. def save(self):
  212. """Save the current state to a JSON file."""
  213. try:
  214. with open(self.STATE_FILE, "w") as f:
  215. json.dump(self.to_dict(), f)
  216. except Exception as e:
  217. print(f"Error saving state to {self.STATE_FILE}: {e}")
  218. def load(self):
  219. """Load state from a JSON file. If the file doesn't exist, create it with default values."""
  220. if not os.path.exists(self.STATE_FILE):
  221. # File doesn't exist: create one with the current (default) state.
  222. self.save()
  223. return
  224. try:
  225. with open(self.STATE_FILE, "r") as f:
  226. data = json.load(f)
  227. self.from_dict(data)
  228. except Exception as e:
  229. print(f"Error loading state from {self.STATE_FILE}: {e}")
  230. def update_steps_per_mm(self, x_steps, y_steps):
  231. """Update and save steps per mm values."""
  232. self.x_steps_per_mm = x_steps
  233. self.y_steps_per_mm = y_steps
  234. self.save()
  235. def reset_state(self):
  236. """Reset all state variables to their default values."""
  237. self.__init__() # Reinitialize the state
  238. self.save()
  239. # Create a singleton instance that you can import elsewhere:
  240. state = AppState()