state.py 8.5 KB

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