state.py 7.7 KB

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