state.py 7.6 KB

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