state.py 8.1 KB

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