state.py 5.7 KB

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