state.py 7.5 KB

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