state.py 5.8 KB

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