state.py 6.0 KB

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