state.py 6.5 KB

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