1
0

state.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # state.py
  2. import threading
  3. import json
  4. import os
  5. class AppState:
  6. def __init__(self):
  7. # Execution state variables
  8. self.stop_requested = False
  9. self.pause_requested = False
  10. self.pause_condition = threading.Condition()
  11. self.current_playing_file = None
  12. self.execution_progress = None
  13. self.is_clearing = False
  14. self.current_theta = 0
  15. self.current_rho = 0
  16. self.speed = 250
  17. # Machine position variables
  18. self.machine_x = 0.0
  19. self.machine_y = 0.0
  20. self.x_steps_per_mm = 0.0
  21. self.y_steps_per_mm = 0.0
  22. self.gear_ratio = 10
  23. self.STATE_FILE = "state.json"
  24. self.load()
  25. def to_dict(self):
  26. """Return a dictionary representation of the state."""
  27. return {
  28. "stop_requested": self.stop_requested,
  29. "pause_requested": self.pause_requested,
  30. "current_playing_file": self.current_playing_file,
  31. "execution_progress": self.execution_progress,
  32. "is_clearing": self.is_clearing,
  33. "current_theta": self.current_theta,
  34. "current_rho": self.current_rho,
  35. "speed": self.speed,
  36. "machine_x": self.machine_x,
  37. "machine_y": self.machine_y,
  38. "x_steps_per_mm": self.x_steps_per_mm,
  39. "y_steps_per_mm": self.y_steps_per_mm,
  40. "gear_ratio": self.gear_ratio
  41. }
  42. def from_dict(self, data):
  43. """Update state from a dictionary."""
  44. self.stop_requested = data.get("stop_requested", False)
  45. self.pause_requested = data.get("pause_requested", False)
  46. self.current_playing_file = data.get("current_playing_file")
  47. self.execution_progress = data.get("execution_progress")
  48. self.is_clearing = data.get("is_clearing", False)
  49. self.current_theta = data.get("current_theta", 0)
  50. self.current_rho = data.get("current_rho", 0)
  51. self.speed = data.get("speed", 250)
  52. self.machine_x = data.get("machine_x", 0.0)
  53. self.machine_y = data.get("machine_y", 0.0)
  54. self.x_steps_per_mm = data.get("x_steps_per_mm", 0.0)
  55. self.y_steps_per_mm = data.get("y_steps_per_mm", 0.0)
  56. self.gear_ratio = data.get('gear_ratio', 10)
  57. def save(self):
  58. """Save the current state to a JSON file."""
  59. try:
  60. with open(self.STATE_FILE, "w") as f:
  61. json.dump(self.to_dict(), f)
  62. except Exception as e:
  63. print(f"Error saving state to {self.STATE_FILE}: {e}")
  64. def load(self):
  65. """Load state from a JSON file. If the file doesn't exist, create it with default values."""
  66. if not os.path.exists(self.STATE_FILE):
  67. # File doesn't exist: create one with the current (default) state.
  68. self.save()
  69. return
  70. try:
  71. with open(self.STATE_FILE, "r") as f:
  72. data = json.load(f)
  73. self.from_dict(data)
  74. except Exception as e:
  75. print(f"Error loading state from {self.STATE_FILE}: {e}")
  76. def update_steps_per_mm(self, x_steps, y_steps):
  77. """Update and save steps per mm values."""
  78. self.x_steps_per_mm = x_steps
  79. self.y_steps_per_mm = y_steps
  80. self.save()
  81. def reset_state(self):
  82. """Reset all state variables to their default values."""
  83. self.__init__() # Reinitialize the state
  84. self.save()
  85. # Create a singleton instance that you can import elsewhere:
  86. state = AppState()