1
0

pattern_manager.py 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042
  1. import os
  2. from zoneinfo import ZoneInfo
  3. import threading
  4. import time
  5. import random
  6. import logging
  7. from datetime import datetime, time as datetime_time
  8. from tqdm import tqdm
  9. from modules.connection import connection_manager
  10. from modules.core.state import state
  11. from math import pi, isnan, isinf
  12. import asyncio
  13. import json
  14. from modules.led.idle_timeout_manager import idle_timeout_manager
  15. import queue
  16. from dataclasses import dataclass
  17. from typing import Optional, Callable, Literal
  18. # Configure logging
  19. logger = logging.getLogger(__name__)
  20. # Global state
  21. THETA_RHO_DIR = './patterns'
  22. os.makedirs(THETA_RHO_DIR, exist_ok=True)
  23. # Execution time log file (JSON Lines format - one JSON object per line)
  24. EXECUTION_LOG_FILE = './execution_times.jsonl'
  25. async def wait_with_interrupt(
  26. condition_fn: Callable[[], bool],
  27. check_stop: bool = True,
  28. check_skip: bool = True,
  29. interval: float = 1.0,
  30. ) -> Literal['completed', 'stopped', 'skipped']:
  31. """
  32. Wait while condition_fn() returns True, with instant interrupt support.
  33. Uses asyncio.Event for instant response to stop/skip requests rather than
  34. polling at fixed intervals. This ensures users get immediate feedback when
  35. pressing stop or skip buttons.
  36. Args:
  37. condition_fn: Function that returns True while waiting should continue
  38. check_stop: Whether to respond to stop requests (default True)
  39. check_skip: Whether to respond to skip requests (default True)
  40. interval: How often to re-check condition_fn in seconds (default 1.0)
  41. Returns:
  42. 'completed' - condition_fn() returned False (normal completion)
  43. 'stopped' - stop was requested
  44. 'skipped' - skip was requested
  45. Example:
  46. result = await wait_with_interrupt(
  47. lambda: state.pause_requested or is_in_scheduled_pause_period()
  48. )
  49. if result == 'stopped':
  50. return # Exit pattern execution
  51. if result == 'skipped':
  52. break # Skip to next pattern
  53. """
  54. while condition_fn():
  55. result = await state.wait_for_interrupt(
  56. timeout=interval,
  57. check_stop=check_stop,
  58. check_skip=check_skip,
  59. )
  60. if result == 'stopped':
  61. return 'stopped'
  62. if result == 'skipped':
  63. return 'skipped'
  64. # 'timeout' means we should re-check condition_fn
  65. return 'completed'
  66. def log_execution_time(pattern_name: str, table_type: str, speed: int, actual_time: float,
  67. total_coordinates: int, was_completed: bool):
  68. """Log pattern execution time to JSON Lines file for analysis.
  69. Args:
  70. pattern_name: Name of the pattern file
  71. table_type: Type of table (e.g., 'dune_weaver', 'dune_weaver_mini')
  72. speed: Speed setting used (0-255)
  73. actual_time: Actual execution time in seconds (excluding pauses)
  74. total_coordinates: Total number of coordinates in the pattern
  75. was_completed: Whether the pattern completed normally (not stopped/skipped)
  76. """
  77. # Format time as HH:MM:SS
  78. hours, remainder = divmod(int(actual_time), 3600)
  79. minutes, seconds = divmod(remainder, 60)
  80. time_formatted = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
  81. log_entry = {
  82. "timestamp": datetime.now().isoformat(),
  83. "pattern_name": pattern_name,
  84. "table_type": table_type or "unknown",
  85. "speed": speed,
  86. "actual_time_seconds": round(actual_time, 2),
  87. "actual_time_formatted": time_formatted,
  88. "total_coordinates": total_coordinates,
  89. "completed": was_completed
  90. }
  91. try:
  92. with open(EXECUTION_LOG_FILE, 'a') as f:
  93. f.write(json.dumps(log_entry) + '\n')
  94. logger.info(f"Execution time logged: {pattern_name} - {time_formatted} (speed: {speed}, table: {table_type})")
  95. except Exception as e:
  96. logger.error(f"Failed to log execution time: {e}")
  97. def get_last_completed_execution_time(pattern_name: str, speed: float) -> Optional[dict]:
  98. """Get the last completed execution time for a pattern at a specific speed.
  99. Args:
  100. pattern_name: Name of the pattern file (e.g., 'circle.thr')
  101. speed: Speed setting to match
  102. Returns:
  103. Dict with execution time info if found, None otherwise.
  104. Format: {"actual_time_seconds": float, "actual_time_formatted": str, "timestamp": str}
  105. """
  106. if not os.path.exists(EXECUTION_LOG_FILE):
  107. return None
  108. try:
  109. matching_entry = None
  110. with open(EXECUTION_LOG_FILE, 'r') as f:
  111. for line in f:
  112. line = line.strip()
  113. if not line:
  114. continue
  115. try:
  116. entry = json.loads(line)
  117. # Only consider fully completed patterns (100% finished)
  118. if (entry.get('completed', False) and
  119. entry.get('pattern_name') == pattern_name and
  120. entry.get('speed') == speed):
  121. # Keep the most recent match (last one in file)
  122. matching_entry = entry
  123. except json.JSONDecodeError:
  124. continue
  125. if matching_entry:
  126. return {
  127. "actual_time_seconds": matching_entry.get('actual_time_seconds'),
  128. "actual_time_formatted": matching_entry.get('actual_time_formatted'),
  129. "timestamp": matching_entry.get('timestamp')
  130. }
  131. return None
  132. except Exception as e:
  133. logger.error(f"Failed to read execution time log: {e}")
  134. return None
  135. def get_pattern_execution_history(pattern_name: str) -> Optional[dict]:
  136. """Get the most recent completed execution for a pattern (any speed).
  137. Args:
  138. pattern_name: Name of the pattern file (e.g., 'circle.thr')
  139. Returns:
  140. Dict with execution time info if found, None otherwise.
  141. Format: {"actual_time_seconds": float, "actual_time_formatted": str,
  142. "speed": int, "timestamp": str}
  143. """
  144. if not os.path.exists(EXECUTION_LOG_FILE):
  145. return None
  146. try:
  147. matching_entry = None
  148. with open(EXECUTION_LOG_FILE, 'r') as f:
  149. for line in f:
  150. line = line.strip()
  151. if not line:
  152. continue
  153. try:
  154. entry = json.loads(line)
  155. # Only consider fully completed patterns
  156. if (entry.get('completed', False) and
  157. entry.get('pattern_name') == pattern_name):
  158. # Keep the most recent match (last one in file)
  159. matching_entry = entry
  160. except json.JSONDecodeError:
  161. continue
  162. if matching_entry:
  163. return {
  164. "actual_time_seconds": matching_entry.get('actual_time_seconds'),
  165. "actual_time_formatted": matching_entry.get('actual_time_formatted'),
  166. "speed": matching_entry.get('speed'),
  167. "timestamp": matching_entry.get('timestamp')
  168. }
  169. return None
  170. except Exception as e:
  171. logger.error(f"Failed to read execution time log: {e}")
  172. return None
  173. # Asyncio primitives - initialized lazily to avoid event loop issues
  174. # These must be created in the context of the running event loop
  175. pause_event: Optional[asyncio.Event] = None
  176. pattern_lock: Optional[asyncio.Lock] = None
  177. progress_update_task = None
  178. def get_pause_event() -> asyncio.Event:
  179. """Get or create the pause event in the current event loop."""
  180. global pause_event
  181. if pause_event is None:
  182. pause_event = asyncio.Event()
  183. pause_event.set() # Initially not paused
  184. return pause_event
  185. def get_pattern_lock() -> asyncio.Lock:
  186. """Get or create the pattern lock in the current event loop."""
  187. global pattern_lock
  188. if pattern_lock is None:
  189. pattern_lock = asyncio.Lock()
  190. return pattern_lock
  191. # Cache timezone at module level - read once per session (cleared when user changes timezone)
  192. _cached_timezone = None
  193. _cached_zoneinfo = None
  194. def _get_timezone():
  195. """Get and cache the timezone for Still Sands. Uses user-selected timezone if set, otherwise system timezone."""
  196. global _cached_timezone, _cached_zoneinfo
  197. if _cached_timezone is not None:
  198. return _cached_zoneinfo
  199. user_tz = 'UTC' # Default fallback
  200. # First, check if user has selected a specific timezone in settings
  201. if state.scheduled_pause_timezone:
  202. user_tz = state.scheduled_pause_timezone
  203. logger.info(f"Still Sands using timezone: {user_tz} (user-selected)")
  204. else:
  205. # Fall back to system timezone detection
  206. try:
  207. if os.path.exists('/etc/host-timezone'):
  208. with open('/etc/host-timezone', 'r') as f:
  209. user_tz = f.read().strip()
  210. logger.info(f"Still Sands using timezone: {user_tz} (from host system)")
  211. # Fallback to /etc/timezone if host-timezone doesn't exist
  212. elif os.path.exists('/etc/timezone'):
  213. with open('/etc/timezone', 'r') as f:
  214. user_tz = f.read().strip()
  215. logger.info(f"Still Sands using timezone: {user_tz} (from container)")
  216. # Fallback to TZ environment variable
  217. elif os.environ.get('TZ'):
  218. user_tz = os.environ.get('TZ')
  219. logger.info(f"Still Sands using timezone: {user_tz} (from environment)")
  220. else:
  221. logger.info("Still Sands using timezone: UTC (system default)")
  222. except Exception as e:
  223. logger.debug(f"Could not read timezone: {e}")
  224. # Cache the timezone
  225. _cached_timezone = user_tz
  226. try:
  227. _cached_zoneinfo = ZoneInfo(user_tz)
  228. except Exception as e:
  229. logger.warning(f"Invalid timezone '{user_tz}', falling back to system time: {e}")
  230. _cached_zoneinfo = None
  231. return _cached_zoneinfo
  232. def is_in_scheduled_pause_period():
  233. """Check if current time falls within any scheduled pause period."""
  234. if not state.scheduled_pause_enabled or not state.scheduled_pause_time_slots:
  235. return False
  236. # Get cached timezone (user-selected or system default)
  237. tz_info = _get_timezone()
  238. try:
  239. # Get current time in user's timezone
  240. if tz_info:
  241. now = datetime.now(tz_info)
  242. else:
  243. now = datetime.now()
  244. except Exception as e:
  245. logger.warning(f"Error getting current time: {e}")
  246. now = datetime.now()
  247. current_time = now.time()
  248. current_weekday = now.strftime("%A").lower() # monday, tuesday, etc.
  249. for slot in state.scheduled_pause_time_slots:
  250. # Parse start and end times
  251. try:
  252. start_time = datetime_time.fromisoformat(slot['start_time'])
  253. end_time = datetime_time.fromisoformat(slot['end_time'])
  254. except (ValueError, KeyError):
  255. logger.warning(f"Invalid time format in scheduled pause slot: {slot}")
  256. continue
  257. # Check if this slot applies to today
  258. slot_applies_today = False
  259. days_setting = slot.get('days', 'daily')
  260. if days_setting == 'daily':
  261. slot_applies_today = True
  262. elif days_setting == 'weekdays':
  263. slot_applies_today = current_weekday in ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']
  264. elif days_setting == 'weekends':
  265. slot_applies_today = current_weekday in ['saturday', 'sunday']
  266. elif days_setting == 'custom':
  267. custom_days = slot.get('custom_days', [])
  268. slot_applies_today = current_weekday in custom_days
  269. if not slot_applies_today:
  270. continue
  271. # Check if current time is within the pause period
  272. if start_time <= end_time:
  273. # Normal case: start and end are on the same day
  274. if start_time <= current_time <= end_time:
  275. return True
  276. else:
  277. # Time spans midnight: start is before midnight, end is after midnight
  278. if current_time >= start_time or current_time <= end_time:
  279. return True
  280. return False
  281. async def check_table_is_idle() -> bool:
  282. """
  283. Check if the table is currently idle by querying actual machine status.
  284. Returns True if idle, False if playing/moving.
  285. This checks the real machine state rather than relying on state variables,
  286. making it more reliable for detecting when table is truly idle.
  287. """
  288. # Use the connection_manager's is_machine_idle() function
  289. # Run it in a thread since it's a synchronous function
  290. return await asyncio.to_thread(connection_manager.is_machine_idle)
  291. async def start_idle_led_timeout(check_still_sands: bool = True):
  292. """
  293. Set LED to idle state and start timeout if enabled.
  294. Handles Still Sands: if in scheduled pause period with LED control enabled,
  295. turns off LEDs instead of showing idle effect.
  296. Should be called whenever the table goes idle.
  297. Args:
  298. check_still_sands: If True, checks Still Sands period and turns off LEDs if applicable.
  299. Set to False when caller already handles Still Sands logic
  300. (e.g., during pause with "finish pattern first" mode).
  301. """
  302. if not state.led_controller:
  303. return
  304. # Still Sands with LED control: turn off instead of idle effect
  305. if check_still_sands and is_in_scheduled_pause_period() and state.scheduled_pause_control_wled:
  306. logger.info("Turning off LED lights during Still Sands period")
  307. await state.led_controller.set_power_async(0)
  308. return
  309. # Normal flow: show idle effect
  310. # For WLED: always trigger (uses hardcoded preset 1)
  311. # For DW_LED: only trigger if effect is configured
  312. if state.led_provider != "wled" and not state.dw_led_idle_effect:
  313. logger.debug("No idle effect configured, leaving LEDs unchanged")
  314. return
  315. await state.led_controller.effect_idle_async(state.dw_led_idle_effect)
  316. # Start timeout if enabled
  317. if not state.dw_led_idle_timeout_enabled:
  318. logger.debug("Idle LED timeout not enabled")
  319. return
  320. timeout_minutes = state.dw_led_idle_timeout_minutes
  321. if timeout_minutes <= 0:
  322. logger.debug("Idle LED timeout not configured (timeout <= 0)")
  323. return
  324. logger.debug(f"Starting idle LED timeout: {timeout_minutes} minutes")
  325. idle_timeout_manager.start_idle_timeout(
  326. timeout_minutes=timeout_minutes,
  327. state=state,
  328. check_idle_callback=check_table_is_idle
  329. )
  330. # Motion Control Thread Infrastructure
  331. @dataclass
  332. class MotionCommand:
  333. """Represents a motion command for the motion control thread."""
  334. command_type: str # 'move', 'stop', 'pause', 'resume', 'shutdown'
  335. theta: Optional[float] = None
  336. rho: Optional[float] = None
  337. speed: Optional[float] = None
  338. callback: Optional[Callable] = None
  339. future: Optional[asyncio.Future] = None
  340. class MotionControlThread:
  341. """Dedicated thread for hardware motion control operations."""
  342. def __init__(self):
  343. self.command_queue = queue.Queue()
  344. self.thread = None
  345. self.running = False
  346. self.paused = False
  347. def start(self):
  348. """Start the motion control thread with elevated priority."""
  349. if self.thread and self.thread.is_alive():
  350. return
  351. self.running = True
  352. self.thread = threading.Thread(target=self._motion_loop, daemon=True)
  353. self.thread.start()
  354. logger.info("Motion control thread started")
  355. def stop(self):
  356. """Stop the motion control thread."""
  357. if not self.running:
  358. return
  359. self.running = False
  360. # Send shutdown command
  361. self.command_queue.put(MotionCommand('shutdown'))
  362. if self.thread and self.thread.is_alive():
  363. self.thread.join(timeout=5.0)
  364. logger.info("Motion control thread stopped")
  365. def _motion_loop(self):
  366. """Main loop for the motion control thread."""
  367. logger.info("Motion control thread loop started")
  368. while self.running:
  369. try:
  370. # Get command with timeout to allow periodic checks
  371. command = self.command_queue.get(timeout=1.0)
  372. if command.command_type == 'shutdown':
  373. break
  374. elif command.command_type == 'move':
  375. self._execute_move(command)
  376. elif command.command_type == 'pause':
  377. self.paused = True
  378. elif command.command_type == 'resume':
  379. self.paused = False
  380. elif command.command_type == 'stop':
  381. # Clear any pending commands
  382. while not self.command_queue.empty():
  383. try:
  384. self.command_queue.get_nowait()
  385. except queue.Empty:
  386. break
  387. self.command_queue.task_done()
  388. except queue.Empty:
  389. # Timeout - continue loop for shutdown check
  390. continue
  391. except Exception as e:
  392. logger.error(f"Error in motion control thread: {e}")
  393. logger.info("Motion control thread loop ended")
  394. def _execute_move(self, command: MotionCommand):
  395. """Execute a move command in the motion thread."""
  396. try:
  397. # Wait if paused
  398. while self.paused and self.running:
  399. time.sleep(0.1)
  400. if not self.running:
  401. return
  402. # Execute the actual motion using sync version
  403. self._move_polar_sync(command.theta, command.rho, command.speed)
  404. # Signal completion if future provided
  405. if command.future and not command.future.done():
  406. command.future.get_loop().call_soon_threadsafe(
  407. command.future.set_result, None
  408. )
  409. except Exception as e:
  410. logger.error(f"Error executing move command: {e}")
  411. if command.future and not command.future.done():
  412. command.future.get_loop().call_soon_threadsafe(
  413. command.future.set_exception, e
  414. )
  415. def _move_polar_sync(self, theta: float, rho: float, speed: Optional[float] = None):
  416. """Synchronous version of move_polar for use in motion thread."""
  417. # Check for valid machine position (can be None if homing failed)
  418. if state.machine_x is None or state.machine_y is None:
  419. logger.error("Cannot execute move: machine position unknown (homing may have failed)")
  420. logger.error("Please home the machine before running patterns")
  421. state.stop_requested = True
  422. return
  423. # This is the original sync logic but running in dedicated thread
  424. if state.table_type == 'dune_weaver_mini':
  425. x_scaling_factor = 2
  426. y_scaling_factor = 3.7
  427. else:
  428. x_scaling_factor = 2
  429. y_scaling_factor = 5
  430. delta_theta = theta - state.current_theta
  431. delta_rho = rho - state.current_rho
  432. x_increment = delta_theta * 100 / (2 * pi * x_scaling_factor)
  433. y_increment = delta_rho * 100 / y_scaling_factor
  434. x_total_steps = state.x_steps_per_mm * (100/x_scaling_factor)
  435. y_total_steps = state.y_steps_per_mm * (100/y_scaling_factor)
  436. offset = x_increment * (x_total_steps * x_scaling_factor / (state.gear_ratio * y_total_steps * y_scaling_factor))
  437. if state.table_type == 'dune_weaver_mini' or state.y_steps_per_mm == 546:
  438. y_increment -= offset
  439. else:
  440. y_increment += offset
  441. new_x_abs = state.machine_x + x_increment
  442. new_y_abs = state.machine_y + y_increment
  443. # Use provided speed or fall back to state.speed
  444. actual_speed = speed if speed is not None else state.speed
  445. # Validate coordinates before sending to prevent GRBL error:2
  446. if isnan(new_x_abs) or isnan(new_y_abs) or isinf(new_x_abs) or isinf(new_y_abs):
  447. logger.error(f"Motion thread: Invalid coordinates detected - X:{new_x_abs}, Y:{new_y_abs}")
  448. logger.error(f" theta:{theta}, rho:{rho}, current_theta:{state.current_theta}, current_rho:{state.current_rho}")
  449. logger.error(f" x_steps_per_mm:{state.x_steps_per_mm}, y_steps_per_mm:{state.y_steps_per_mm}, gear_ratio:{state.gear_ratio}")
  450. state.stop_requested = True
  451. return
  452. # Call sync version of send_grbl_coordinates in this thread
  453. # Use 2 decimal precision to reduce GRBL parsing overhead
  454. self._send_grbl_coordinates_sync(round(new_x_abs, 2), round(new_y_abs, 2), actual_speed)
  455. # Update state
  456. state.current_theta = theta
  457. state.current_rho = rho
  458. state.machine_x = new_x_abs
  459. state.machine_y = new_y_abs
  460. def _send_grbl_coordinates_sync(self, x: float, y: float, speed: int = 600, timeout: int = 2, home: bool = False):
  461. """Synchronous version of send_grbl_coordinates for motion thread.
  462. Waits for 'ok' with a timeout. GRBL sends 'ok' after the move completes,
  463. which can take many seconds at slow speeds. We use a generous timeout
  464. (120 seconds) to handle slow movements, but prevent indefinite hangs.
  465. Includes retry logic for serial corruption errors (common on Pi 3B+).
  466. """
  467. gcode = f"$J=G91 G21 Y{y:.2f} F{speed}" if home else f"G1 X{x:.2f} Y{y:.2f} F{speed}"
  468. max_wait_time = 120 # Maximum seconds to wait for 'ok' response
  469. max_corruption_retries = 10 # Max retries for corruption-type errors
  470. max_timeout_retries = 10 # Max retries for timeout (lost 'ok' response)
  471. corruption_retry_count = 0
  472. timeout_retry_count = 0
  473. # GRBL error codes that indicate likely serial corruption (syntax errors)
  474. # These are recoverable by resending the command
  475. corruption_error_codes = {
  476. 'error:1', # Expected command letter
  477. 'error:2', # Bad number format
  478. 'error:20', # Invalid gcode ID (e.g., G5s instead of G53)
  479. 'error:21', # Invalid gcode command value
  480. 'error:22', # Invalid gcode command value in negative
  481. 'error:23', # Invalid gcode command value in decimal
  482. }
  483. while True:
  484. # Check stop_requested at the start of each iteration
  485. if state.stop_requested:
  486. logger.debug("Motion thread: Stop requested, aborting command")
  487. return False
  488. try:
  489. # Clear any stale input data before sending to prevent interleaving
  490. # This helps with timing issues on slower UARTs like Pi 3B+
  491. if hasattr(state.conn, 'reset_input_buffer'):
  492. state.conn.reset_input_buffer()
  493. logger.debug(f"Motion thread sending G-code: {gcode}")
  494. state.conn.send(gcode + "\n")
  495. # Small delay for serial buffer to stabilize on slower UARTs
  496. # Prevents timing-related corruption on Pi 3B+
  497. time.sleep(0.005)
  498. # Wait for 'ok' with timeout
  499. wait_start = time.time()
  500. while True:
  501. # Check stop_requested while waiting
  502. if state.stop_requested:
  503. logger.debug("Motion thread: Stop requested while waiting for response")
  504. return False
  505. # Check for timeout
  506. elapsed = time.time() - wait_start
  507. if elapsed > max_wait_time:
  508. logger.warning(f"Motion thread: Timeout ({max_wait_time}s) waiting for 'ok' response")
  509. logger.warning(f"Motion thread: Failed command was: {gcode}")
  510. # Attempt to recover by checking machine status
  511. # The 'ok' might have been lost but command may have executed
  512. logger.info("Motion thread: Attempting timeout recovery - checking machine status")
  513. logger.info(f"Motion thread: Current retry counts - timeout: {timeout_retry_count}/{max_timeout_retries}, corruption: {corruption_retry_count}/{max_corruption_retries}")
  514. try:
  515. # Check connection state first
  516. conn_type = type(state.conn).__name__ if state.conn else "None"
  517. logger.info(f"Motion thread: Connection type: {conn_type}")
  518. if not state.conn:
  519. logger.error("Motion thread: Connection object is None!")
  520. raise Exception("Connection is None")
  521. # Clear buffer first
  522. if hasattr(state.conn, 'reset_input_buffer'):
  523. state.conn.reset_input_buffer()
  524. logger.info("Motion thread: Input buffer cleared")
  525. else:
  526. logger.warning("Motion thread: Connection has no reset_input_buffer method")
  527. # Check if there's data waiting before we send
  528. if hasattr(state.conn, 'in_waiting'):
  529. waiting = state.conn.in_waiting()
  530. logger.info(f"Motion thread: Bytes waiting in buffer after clear: {waiting}")
  531. # Send status query
  532. logger.info("Motion thread: Sending status query '?'...")
  533. state.conn.send("?\n")
  534. time.sleep(0.2)
  535. logger.info("Motion thread: Status query sent, reading responses...")
  536. # Try to read status response
  537. status_response = None
  538. responses_received = []
  539. for i in range(10):
  540. resp = state.conn.readline()
  541. if resp:
  542. responses_received.append(resp)
  543. logger.info(f"Motion thread: Recovery response [{i+1}/10]: '{resp}'")
  544. if '<' in resp or 'Idle' in resp or 'Run' in resp or 'Hold' in resp or 'Alarm' in resp:
  545. status_response = resp
  546. logger.info(f"Motion thread: Found valid status response: '{resp}'")
  547. break
  548. # Also check for 'ok' that might have been delayed
  549. if resp.lower() == 'ok':
  550. logger.info("Motion thread: Received delayed 'ok' during recovery - SUCCESS")
  551. return True
  552. else:
  553. logger.debug(f"Motion thread: Recovery read [{i+1}/10]: no data (timeout)")
  554. time.sleep(0.05)
  555. # Log summary of what we received
  556. if responses_received:
  557. logger.info(f"Motion thread: Total responses received during recovery: {len(responses_received)}")
  558. logger.info(f"Motion thread: All responses: {responses_received}")
  559. else:
  560. logger.warning("Motion thread: No responses received during recovery - connection may be dead")
  561. if status_response:
  562. if 'Idle' in status_response:
  563. # Machine is idle - command likely completed, 'ok' was lost
  564. logger.info("Motion thread: Machine is Idle - assuming command completed (ok was lost) - SUCCESS")
  565. return True
  566. elif 'Run' in status_response:
  567. # Machine still running - extend timeout
  568. logger.info("Motion thread: Machine still running, extending wait time")
  569. wait_start = time.time() # Reset timeout
  570. continue
  571. elif 'Hold' in status_response:
  572. # Machine is in Hold state - attempt to resume
  573. logger.warning(f"Motion thread: Machine in Hold state: '{status_response}'")
  574. logger.info("Motion thread: Sending cycle start command '~' to resume from Hold...")
  575. # Send cycle start command to resume
  576. state.conn.send("~\n")
  577. time.sleep(0.3) # Give time for resume to process
  578. # Re-check status after resume attempt
  579. state.conn.send("?\n")
  580. time.sleep(0.2)
  581. # Read new status
  582. resume_response = None
  583. for _ in range(5):
  584. resp = state.conn.readline()
  585. if resp:
  586. logger.info(f"Motion thread: Post-resume response: '{resp}'")
  587. if '<' in resp:
  588. resume_response = resp
  589. break
  590. time.sleep(0.05)
  591. if resume_response:
  592. if 'Idle' in resume_response:
  593. logger.info("Motion thread: Machine resumed and is now Idle - SUCCESS")
  594. return True
  595. elif 'Run' in resume_response:
  596. logger.info("Motion thread: Machine resumed and running, extending wait time")
  597. wait_start = time.time()
  598. continue
  599. elif 'Hold' in resume_response:
  600. # Still in Hold - may need user intervention
  601. logger.warning(f"Motion thread: Still in Hold after resume: '{resume_response}'")
  602. else:
  603. logger.warning("Motion thread: No response after resume attempt")
  604. elif 'Alarm' in status_response:
  605. # Machine is in Alarm state - attempt to unlock
  606. logger.warning(f"Motion thread: Machine in ALARM state: '{status_response}'")
  607. logger.info("Motion thread: Sending $X to unlock from Alarm...")
  608. # Send unlock command
  609. state.conn.send("$X\n")
  610. time.sleep(0.5) # Give time for unlock to process
  611. # Re-check status after unlock attempt
  612. state.conn.send("?\n")
  613. time.sleep(0.2)
  614. # Read new status
  615. unlock_response = None
  616. for _ in range(5):
  617. resp = state.conn.readline()
  618. if resp:
  619. logger.info(f"Motion thread: Post-unlock response: '{resp}'")
  620. if '<' in resp:
  621. unlock_response = resp
  622. break
  623. time.sleep(0.05)
  624. if unlock_response:
  625. if 'Idle' in unlock_response:
  626. logger.info("Motion thread: Machine unlocked and is now Idle - retrying command")
  627. # Don't return True - we need to resend the failed command
  628. break # Break inner loop to retry the command
  629. elif 'Alarm' in unlock_response:
  630. # Still in Alarm - underlying issue persists (e.g., sensor triggered)
  631. logger.error(f"Motion thread: Still in ALARM after unlock: '{unlock_response}'")
  632. logger.error("Motion thread: Machine may need physical attention")
  633. state.stop_requested = True
  634. return False
  635. else:
  636. logger.warning("Motion thread: No response after unlock attempt")
  637. else:
  638. logger.warning(f"Motion thread: Unrecognized status response: '{status_response}'")
  639. else:
  640. logger.warning("Motion thread: No valid status response found in any received data")
  641. # No valid status response - connection may be dead
  642. timeout_retry_count += 1
  643. if timeout_retry_count <= max_timeout_retries:
  644. logger.warning(f"Motion thread: Recovery failed, will retry command ({timeout_retry_count}/{max_timeout_retries})")
  645. time.sleep(0.1)
  646. break # Break inner loop to resend command
  647. else:
  648. logger.error(f"Motion thread: Max timeout retries ({max_timeout_retries}) exceeded")
  649. except Exception as e:
  650. logger.error(f"Motion thread: Error during timeout recovery: {e}")
  651. import traceback
  652. logger.error(f"Motion thread: Traceback: {traceback.format_exc()}")
  653. # Max retries exceeded or recovery failed
  654. logger.error("=" * 60)
  655. logger.error("Motion thread: TIMEOUT RECOVERY FAILED - STOPPING PATTERN")
  656. logger.error(f" Failed command: {gcode}")
  657. logger.error(f" Timeout retries used: {timeout_retry_count}/{max_timeout_retries}")
  658. logger.error(f" Corruption retries used: {corruption_retry_count}/{max_corruption_retries}")
  659. logger.error(" Possible causes:")
  660. logger.error(" - Serial connection lost or unstable")
  661. logger.error(" - Hardware controller unresponsive")
  662. logger.error(" - USB power issue (try powered hub)")
  663. logger.error("=" * 60)
  664. state.stop_requested = True
  665. return False
  666. response = state.conn.readline()
  667. if response:
  668. logger.debug(f"Motion thread response: {response}")
  669. if response.lower() == "ok":
  670. logger.debug("Motion thread: Command execution confirmed.")
  671. # Reset corruption retry count on success
  672. if corruption_retry_count > 0:
  673. logger.info(f"Motion thread: Command succeeded after {corruption_retry_count} corruption retry(ies)")
  674. return True
  675. # Handle GRBL errors
  676. if response.lower().startswith("error"):
  677. error_code = response.lower().split()[0] if response else ""
  678. # Check if this is a corruption-type error (recoverable)
  679. if error_code in corruption_error_codes:
  680. corruption_retry_count += 1
  681. if corruption_retry_count <= max_corruption_retries:
  682. logger.warning(f"Motion thread: Likely serial corruption detected ({response})")
  683. logger.warning(f"Motion thread: Retrying command ({corruption_retry_count}/{max_corruption_retries}): {gcode}")
  684. # Clear buffer and wait longer before retry
  685. if hasattr(state.conn, 'reset_input_buffer'):
  686. state.conn.reset_input_buffer()
  687. time.sleep(0.02) # 20ms delay before retry
  688. break # Break inner loop to retry send
  689. else:
  690. logger.error(f"Motion thread: Max corruption retries ({max_corruption_retries}) exceeded")
  691. logger.error(f"Motion thread: GRBL error received: {response}")
  692. logger.error(f"Failed command: {gcode}")
  693. logger.error("Stopping pattern due to persistent serial corruption")
  694. state.stop_requested = True
  695. return False
  696. else:
  697. # Non-corruption error - stop immediately
  698. logger.error(f"Motion thread: GRBL error received: {response}")
  699. logger.error(f"Failed command: {gcode}")
  700. logger.error("Stopping pattern due to GRBL error")
  701. state.stop_requested = True
  702. return False
  703. # Handle GRBL alarms - machine needs attention
  704. if "alarm" in response.lower():
  705. logger.error(f"Motion thread: GRBL ALARM: {response}")
  706. logger.error("Machine alarm triggered - stopping pattern")
  707. state.stop_requested = True
  708. return False
  709. # FluidNC may echo commands back before sending 'ok'
  710. # Silently ignore echoed G-code commands (G0, G1, $J, etc.)
  711. if response.startswith(('G0', 'G1', 'G2', 'G3', '$J', 'M')):
  712. logger.debug(f"Motion thread: Ignoring echoed command: {response}")
  713. continue # Read next line to get 'ok'
  714. # Check for corruption indicator in MSG:ERR responses
  715. if 'MSG:ERR' in response and 'Bad GCode' in response:
  716. corruption_retry_count += 1
  717. if corruption_retry_count <= max_corruption_retries:
  718. logger.warning(f"Motion thread: Corrupted command detected: {response}")
  719. logger.warning(f"Motion thread: Retrying command ({corruption_retry_count}/{max_corruption_retries}): {gcode}")
  720. # Don't break yet - wait for the error:XX that follows
  721. continue
  722. # If we've exceeded retries, the error:XX handler above will catch it
  723. # Log truly unexpected responses
  724. logger.warning(f"Motion thread: Unexpected response: '{response}'")
  725. else:
  726. # Log periodically when waiting for response (every 30s)
  727. if int(elapsed) > 0 and int(elapsed) % 30 == 0 and elapsed - int(elapsed) < 0.1:
  728. logger.warning(f"Motion thread: Still waiting for 'ok' after {int(elapsed)}s for command: {gcode}")
  729. else:
  730. # Inner while loop completed without break - shouldn't happen normally
  731. # This means we hit timeout, which is handled above
  732. continue
  733. except Exception as e:
  734. error_str = str(e)
  735. logger.warning(f"Motion thread error sending command: {error_str}")
  736. # Immediately return for device not configured errors
  737. if "Device not configured" in error_str or "Errno 6" in error_str:
  738. logger.error(f"Motion thread: Device configuration error detected: {error_str}")
  739. state.stop_requested = True
  740. state.conn = None
  741. state.is_connected = False
  742. logger.info("Connection marked as disconnected due to device error")
  743. return False
  744. # Retry on exception or corruption error
  745. logger.warning(f"Motion thread: Retrying {gcode}...")
  746. time.sleep(0.1)
  747. # Global motion control thread instance
  748. motion_controller = MotionControlThread()
  749. async def cleanup_pattern_manager():
  750. """Clean up pattern manager resources"""
  751. global progress_update_task, pattern_lock, pause_event
  752. try:
  753. # Signal stop to allow any running pattern to exit gracefully
  754. state.stop_requested = True
  755. # Stop motion control thread
  756. motion_controller.stop()
  757. # Cancel progress update task if running
  758. if progress_update_task and not progress_update_task.done():
  759. try:
  760. progress_update_task.cancel()
  761. # Wait for task to actually cancel
  762. try:
  763. await progress_update_task
  764. except asyncio.CancelledError:
  765. pass
  766. except Exception as e:
  767. logger.error(f"Error cancelling progress update task: {e}")
  768. # Clean up pattern lock - wait for it to be released naturally, don't force release
  769. # Force releasing an asyncio.Lock can corrupt internal state if held by another coroutine
  770. current_lock = pattern_lock
  771. if current_lock and current_lock.locked():
  772. logger.info("Pattern lock is held, waiting for release (max 5s)...")
  773. try:
  774. # Wait with timeout for the lock to become available
  775. # Use wait_for for Python 3.9 compatibility (asyncio.timeout is 3.11+)
  776. async def acquire_lock():
  777. async with current_lock:
  778. pass # Lock acquired means previous holder released it
  779. await asyncio.wait_for(acquire_lock(), timeout=5.0)
  780. logger.info("Pattern lock released normally")
  781. except asyncio.TimeoutError:
  782. logger.warning("Timed out waiting for pattern lock - creating fresh lock")
  783. except Exception as e:
  784. logger.error(f"Error waiting for pattern lock: {e}")
  785. # Clean up pause event - wake up any waiting tasks, then create fresh event
  786. current_event = pause_event
  787. if current_event:
  788. try:
  789. current_event.set() # Wake up any waiting tasks
  790. except Exception as e:
  791. logger.error(f"Error setting pause event: {e}")
  792. # Clean up pause condition from state
  793. if state.pause_condition:
  794. try:
  795. with state.pause_condition:
  796. state.pause_condition.notify_all()
  797. state.pause_condition = threading.Condition()
  798. except Exception as e:
  799. logger.error(f"Error cleaning up pause condition: {e}")
  800. # Clear all state variables
  801. state.current_playing_file = None
  802. state.execution_progress = 0
  803. state.is_running = False
  804. state.pause_requested = False
  805. state.stop_requested = True
  806. state.is_clearing = False
  807. # Reset machine position
  808. await connection_manager.update_machine_position()
  809. logger.info("Pattern manager resources cleaned up")
  810. except Exception as e:
  811. logger.error(f"Error during pattern manager cleanup: {e}")
  812. finally:
  813. # Reset to fresh instances instead of None to allow continued operation
  814. progress_update_task = None
  815. pattern_lock = asyncio.Lock() # Fresh lock instead of None
  816. pause_event = asyncio.Event() # Fresh event instead of None
  817. pause_event.set() # Initially not paused
  818. def list_theta_rho_files():
  819. files = []
  820. for root, dirs, filenames in os.walk(THETA_RHO_DIR):
  821. # Skip cached_images directories to avoid scanning thousands of WebP files
  822. if 'cached_images' in dirs:
  823. dirs.remove('cached_images')
  824. # Filter .thr files during traversal for better performance
  825. thr_files = [f for f in filenames if f.endswith('.thr')]
  826. for file in thr_files:
  827. relative_path = os.path.relpath(os.path.join(root, file), THETA_RHO_DIR)
  828. # Normalize path separators to always use forward slashes for consistency across platforms
  829. relative_path = relative_path.replace(os.sep, '/')
  830. files.append(relative_path)
  831. logger.debug(f"Found {len(files)} theta-rho files")
  832. return files
  833. def parse_theta_rho_file(file_path):
  834. """Parse a theta-rho file and return a list of (theta, rho) pairs."""
  835. coordinates = []
  836. try:
  837. logger.debug(f"Parsing theta-rho file: {file_path}")
  838. with open(file_path, 'r', encoding='utf-8') as file:
  839. for line in file:
  840. line = line.strip()
  841. if not line or line.startswith("#"):
  842. continue
  843. try:
  844. theta, rho = map(float, line.split())
  845. coordinates.append((theta, rho))
  846. except ValueError:
  847. logger.warning(f"Skipping invalid line: {line}")
  848. continue
  849. except Exception as e:
  850. logger.error(f"Error reading file: {e}")
  851. return coordinates
  852. logger.debug(f"Parsed {len(coordinates)} coordinates from {file_path}")
  853. return coordinates
  854. def get_first_rho_from_cache(file_path, cache_data=None):
  855. """Get the first rho value from cached metadata, falling back to file parsing if needed.
  856. Args:
  857. file_path: Path to the pattern file
  858. cache_data: Optional pre-loaded cache data dict to avoid repeated disk I/O
  859. """
  860. try:
  861. # Import cache_manager locally to avoid circular import
  862. from modules.core import cache_manager
  863. # Try to get from metadata cache first
  864. # Use relative path from THETA_RHO_DIR to match cache keys (which include subdirectories)
  865. file_name = os.path.relpath(file_path, THETA_RHO_DIR)
  866. # Use provided cache_data if available, otherwise load from disk
  867. if cache_data is not None:
  868. # Extract metadata directly from provided cache
  869. data_section = cache_data.get('data', {})
  870. if file_name in data_section:
  871. cached_entry = data_section[file_name]
  872. metadata = cached_entry.get('metadata')
  873. # When cache_data is provided, trust it without checking mtime
  874. # This significantly speeds up bulk operations (playlists with 1000+ patterns)
  875. # by avoiding 1000+ os.path.getmtime() calls on slow storage (e.g., Pi SD cards)
  876. if metadata and 'first_coordinate' in metadata:
  877. return metadata['first_coordinate']['y']
  878. else:
  879. # Fall back to loading cache from disk (original behavior)
  880. metadata = cache_manager.get_pattern_metadata(file_name)
  881. if metadata and 'first_coordinate' in metadata:
  882. # In the cache, 'x' is theta and 'y' is rho
  883. return metadata['first_coordinate']['y']
  884. # Fallback to parsing the file if not in cache
  885. logger.debug(f"Metadata not cached for {file_name}, parsing file")
  886. coordinates = parse_theta_rho_file(file_path)
  887. if coordinates:
  888. return coordinates[0][1] # Return rho value
  889. return None
  890. except Exception as e:
  891. logger.warning(f"Error getting first rho from cache for {file_path}: {str(e)}")
  892. return None
  893. def get_clear_pattern_file(clear_pattern_mode, path=None, cache_data=None):
  894. """Return a .thr file path based on pattern_name and table type.
  895. Args:
  896. clear_pattern_mode: The clear pattern mode to use
  897. path: Optional path to the pattern file for adaptive mode
  898. cache_data: Optional pre-loaded cache data dict to avoid repeated disk I/O
  899. """
  900. if not clear_pattern_mode or clear_pattern_mode == 'none':
  901. return
  902. # Define patterns for each table type
  903. clear_patterns = {
  904. 'dune_weaver': {
  905. 'clear_from_out': './patterns/clear_from_out.thr',
  906. 'clear_from_in': './patterns/clear_from_in.thr',
  907. 'clear_sideway': './patterns/clear_sideway.thr'
  908. },
  909. 'dune_weaver_mini': {
  910. 'clear_from_out': './patterns/clear_from_out_mini.thr',
  911. 'clear_from_in': './patterns/clear_from_in_mini.thr',
  912. 'clear_sideway': './patterns/clear_sideway_mini.thr'
  913. },
  914. 'dune_weaver_mini_pro': {
  915. 'clear_from_out': './patterns/clear_from_out_mini.thr',
  916. 'clear_from_in': './patterns/clear_from_in_mini.thr',
  917. 'clear_sideway': './patterns/clear_sideway_mini.thr'
  918. },
  919. 'dune_weaver_pro': {
  920. 'clear_from_out': './patterns/clear_from_out_pro.thr',
  921. 'clear_from_out_Ultra': './patterns/clear_from_out_Ultra.thr',
  922. 'clear_from_in': './patterns/clear_from_in_pro.thr',
  923. 'clear_from_in_Ultra': './patterns/clear_from_in_Ultra.thr',
  924. 'clear_sideway': './patterns/clear_sideway_pro.thr'
  925. }
  926. }
  927. # Get patterns for current table type, fallback to standard patterns if type not found
  928. table_patterns = clear_patterns.get(state.table_type, clear_patterns['dune_weaver'])
  929. # Check for custom patterns first
  930. if state.custom_clear_from_out and clear_pattern_mode in ['clear_from_out', 'adaptive']:
  931. if clear_pattern_mode == 'adaptive':
  932. # For adaptive mode, use cached metadata to check first rho
  933. if path:
  934. first_rho = get_first_rho_from_cache(path, cache_data)
  935. if first_rho is not None and first_rho < 0.5:
  936. # Use custom clear_from_out if set
  937. custom_path = os.path.join('./patterns', state.custom_clear_from_out)
  938. if os.path.exists(custom_path):
  939. logger.debug(f"Using custom clear_from_out: {custom_path}")
  940. return custom_path
  941. elif clear_pattern_mode == 'clear_from_out':
  942. custom_path = os.path.join('./patterns', state.custom_clear_from_out)
  943. if os.path.exists(custom_path):
  944. logger.debug(f"Using custom clear_from_out: {custom_path}")
  945. return custom_path
  946. if state.custom_clear_from_in and clear_pattern_mode in ['clear_from_in', 'adaptive']:
  947. if clear_pattern_mode == 'adaptive':
  948. # For adaptive mode, use cached metadata to check first rho
  949. if path:
  950. first_rho = get_first_rho_from_cache(path, cache_data)
  951. if first_rho is not None and first_rho >= 0.5:
  952. # Use custom clear_from_in if set
  953. custom_path = os.path.join('./patterns', state.custom_clear_from_in)
  954. if os.path.exists(custom_path):
  955. logger.debug(f"Using custom clear_from_in: {custom_path}")
  956. return custom_path
  957. elif clear_pattern_mode == 'clear_from_in':
  958. custom_path = os.path.join('./patterns', state.custom_clear_from_in)
  959. if os.path.exists(custom_path):
  960. logger.debug(f"Using custom clear_from_in: {custom_path}")
  961. return custom_path
  962. logger.debug(f"Clear pattern mode: {clear_pattern_mode} for table type: {state.table_type}")
  963. if clear_pattern_mode == "random":
  964. return random.choice(list(table_patterns.values()))
  965. if clear_pattern_mode == 'adaptive':
  966. if not path:
  967. logger.warning("No path provided for adaptive clear pattern")
  968. return random.choice(list(table_patterns.values()))
  969. # Use cached metadata to get first rho value
  970. first_rho = get_first_rho_from_cache(path, cache_data)
  971. if first_rho is None:
  972. logger.warning("Could not determine first rho value for adaptive clear pattern")
  973. return random.choice(list(table_patterns.values()))
  974. if first_rho < 0.5:
  975. return table_patterns['clear_from_out']
  976. else:
  977. return table_patterns['clear_from_in']
  978. else:
  979. if clear_pattern_mode not in table_patterns:
  980. return False
  981. return table_patterns[clear_pattern_mode]
  982. def is_clear_pattern(file_path):
  983. """Check if a file path is a clear pattern file."""
  984. # Get all possible clear pattern files for all table types
  985. clear_patterns = []
  986. for table_type in ['dune_weaver', 'dune_weaver_mini', 'dune_weaver_pro']:
  987. clear_patterns.extend([
  988. f'./patterns/clear_from_out{("_" + table_type.split("_")[-1]) if table_type != "dune_weaver" else ""}.thr',
  989. f'./patterns/clear_from_in{("_" + table_type.split("_")[-1]) if table_type != "dune_weaver" else ""}.thr',
  990. f'./patterns/clear_sideway{("_" + table_type.split("_")[-1]) if table_type != "dune_weaver" else ""}.thr'
  991. ])
  992. # Normalize paths for comparison
  993. normalized_path = os.path.normpath(file_path)
  994. normalized_clear_patterns = [os.path.normpath(p) for p in clear_patterns]
  995. # Check if the file path matches any clear pattern path
  996. return normalized_path in normalized_clear_patterns
  997. async def _execute_pattern_internal(file_path):
  998. """Internal function to execute a pattern file. Must be called with lock already held.
  999. Args:
  1000. file_path: Path to the .thr file to execute
  1001. Returns:
  1002. True if pattern completed successfully, False if stopped/skipped
  1003. """
  1004. # Run file parsing in thread to avoid blocking the event loop
  1005. coordinates = await asyncio.to_thread(parse_theta_rho_file, file_path)
  1006. total_coordinates = len(coordinates)
  1007. if total_coordinates < 2:
  1008. logger.warning("Not enough coordinates for interpolation")
  1009. return False
  1010. # Normalize theta values to avoid unnecessary revolutions at pattern start.
  1011. # Many community patterns have theta starting at high values (e.g., 498 rad ≈ 79 revolutions).
  1012. # Some patterns also start with two "0 0" origin points before jumping to a large theta.
  1013. # Detect this preamble and use the third coordinate as the reference instead.
  1014. ref_theta = coordinates[0][0]
  1015. if (len(coordinates) >= 3
  1016. and abs(coordinates[0][0]) < 1e-9 and abs(coordinates[0][1]) < 1e-9
  1017. and abs(coordinates[1][0]) < 1e-9 and abs(coordinates[1][1]) < 1e-9):
  1018. ref_theta = coordinates[2][0]
  1019. theta_offset = ref_theta - (ref_theta % (2 * pi))
  1020. if abs(theta_offset) > 1e-9:
  1021. coordinates = [(theta - theta_offset, rho) for theta, rho in coordinates]
  1022. logger.info(f"Normalized pattern theta by {theta_offset:.2f} rad ({theta_offset / (2 * pi):.1f} revolutions)")
  1023. # Cache coordinates in state for frontend preview (avoids re-parsing large files)
  1024. state._current_coordinates = coordinates
  1025. # Pre-calculate rho-based weights for more accurate time estimation
  1026. # Moves near center (low rho) are slower than perimeter moves due to
  1027. # polar geometry - less linear distance per theta change at low rho
  1028. def calc_move_weight(rho):
  1029. # Weight inversely proportional to rho, with floor to avoid extreme values
  1030. # At rho=0: weight≈6.7, at rho=0.5: weight≈1.5, at rho=1.0: weight≈0.87
  1031. return 1.0 / (rho + 0.15)
  1032. coord_weights = [calc_move_weight(rho) for _, rho in coordinates]
  1033. total_weight = sum(coord_weights)
  1034. # Determine if this is a clearing pattern
  1035. is_clear_file = is_clear_pattern(file_path)
  1036. if is_clear_file:
  1037. initial_speed = state.clear_pattern_speed if state.clear_pattern_speed is not None else state.speed
  1038. logger.info(f"Running clearing pattern at initial speed {initial_speed}")
  1039. else:
  1040. logger.info(f"Running normal pattern at initial speed {state.speed}")
  1041. state.execution_progress = (0, total_coordinates, None, 0)
  1042. # stop actions without resetting the playlist, and don't wait for lock (we already have it)
  1043. # Preserve is_clearing flag since stop_actions resets it
  1044. was_clearing = state.is_clearing
  1045. await stop_actions(clear_playlist=False, wait_for_lock=False)
  1046. state.is_clearing = was_clearing
  1047. state.current_playing_file = file_path
  1048. state.stop_requested = False
  1049. # Reset LED idle timeout activity time when pattern starts
  1050. import time as time_module
  1051. state.dw_led_last_activity_time = time_module.time()
  1052. logger.info(f"Starting pattern execution: {file_path}")
  1053. logger.info(f"t: {state.current_theta}, r: {state.current_rho}")
  1054. await reset_theta()
  1055. start_time = time.time()
  1056. total_pause_time = 0 # Track total time spent paused (manual + scheduled)
  1057. completed_weight = 0.0 # Track rho-weighted progress
  1058. smoothed_rate = None # For exponential smoothing of time-per-unit-weight rate
  1059. # For WLED: always trigger (uses hardcoded preset 2)
  1060. # For DW_LED: only trigger if effect is configured
  1061. if state.led_controller and (state.led_provider == "wled" or state.dw_led_playing_effect):
  1062. logger.info(f"Setting LED to playing effect: {state.dw_led_playing_effect}")
  1063. await state.led_controller.effect_playing_async(state.dw_led_playing_effect)
  1064. # Cancel idle timeout when playing starts
  1065. idle_timeout_manager.cancel_timeout()
  1066. with tqdm(
  1067. total=total_coordinates,
  1068. unit="coords",
  1069. desc=f"Executing Pattern {file_path}",
  1070. dynamic_ncols=True,
  1071. disable=False,
  1072. mininterval=1.0
  1073. ) as pbar:
  1074. for i, coordinate in enumerate(coordinates):
  1075. theta, rho = coordinate
  1076. if state.stop_requested:
  1077. logger.info("Execution stopped by user")
  1078. await start_idle_led_timeout()
  1079. break
  1080. if state.skip_requested:
  1081. logger.info("Skipping pattern...")
  1082. await connection_manager.check_idle_async()
  1083. await start_idle_led_timeout()
  1084. break
  1085. # Wait for resume if paused (manual or scheduled)
  1086. manual_pause = state.pause_requested
  1087. # Only check scheduled pause during pattern if "finish pattern first" is NOT enabled
  1088. scheduled_pause = is_in_scheduled_pause_period() if not state.scheduled_pause_finish_pattern else False
  1089. if manual_pause or scheduled_pause:
  1090. pause_start = time.time() # Track when pause started
  1091. if manual_pause and scheduled_pause:
  1092. logger.info("Execution paused (manual + scheduled pause active)...")
  1093. elif manual_pause:
  1094. logger.info("Execution paused (manual)...")
  1095. else:
  1096. logger.info("Execution paused (scheduled pause period)...")
  1097. # Turn off LED controller if scheduled pause and control_wled is enabled
  1098. if state.scheduled_pause_control_wled and state.led_controller:
  1099. logger.info("Turning off LED lights during Still Sands period")
  1100. await state.led_controller.set_power_async(0)
  1101. # Show idle effect for manual pause or scheduled pause without LED control
  1102. # (skip Still Sands check since we handle it above with local scheduled_pause variable)
  1103. if not (scheduled_pause and state.scheduled_pause_control_wled):
  1104. await start_idle_led_timeout(check_still_sands=False)
  1105. # Remember if we turned off LED controller for scheduled pause
  1106. wled_was_off_for_scheduled = scheduled_pause and state.scheduled_pause_control_wled and not manual_pause
  1107. # Wait until both manual pause is released AND we're outside scheduled pause period
  1108. # Also check for stop/skip requests to allow immediate interruption
  1109. interrupted = False
  1110. while state.pause_requested or is_in_scheduled_pause_period():
  1111. # Check for stop/skip first
  1112. if state.stop_requested:
  1113. logger.info("Stop requested during pause, exiting")
  1114. interrupted = True
  1115. break
  1116. if state.skip_requested:
  1117. logger.info("Skip requested during pause, skipping pattern")
  1118. interrupted = True
  1119. break
  1120. if state.pause_requested:
  1121. # For manual pause, wait on multiple events for immediate response
  1122. # Wake on: resume, stop, skip, or timeout (for flag polling fallback)
  1123. pause_event = get_pause_event()
  1124. stop_event = state.get_stop_event()
  1125. skip_event = state.get_skip_event()
  1126. wait_tasks = [asyncio.create_task(pause_event.wait(), name='pause')]
  1127. if stop_event:
  1128. wait_tasks.append(asyncio.create_task(stop_event.wait(), name='stop'))
  1129. if skip_event:
  1130. wait_tasks.append(asyncio.create_task(skip_event.wait(), name='skip'))
  1131. # Add timeout to ensure we periodically check flags even if events aren't set
  1132. # This handles the case where stop is called from sync context (no event loop)
  1133. timeout_task = asyncio.create_task(asyncio.sleep(1.0), name='timeout')
  1134. wait_tasks.append(timeout_task)
  1135. try:
  1136. done, pending = await asyncio.wait(
  1137. wait_tasks, return_when=asyncio.FIRST_COMPLETED
  1138. )
  1139. finally:
  1140. for task in pending:
  1141. task.cancel()
  1142. for task in pending:
  1143. try:
  1144. await task
  1145. except asyncio.CancelledError:
  1146. pass
  1147. else:
  1148. # For scheduled pause, use wait_for_interrupt for instant response
  1149. result = await state.wait_for_interrupt(timeout=1.0)
  1150. if result in ('stopped', 'skipped'):
  1151. interrupted = True
  1152. break
  1153. total_pause_time += time.time() - pause_start # Add pause duration
  1154. if interrupted:
  1155. # Exit the coordinate loop if we were interrupted
  1156. break
  1157. logger.info("Execution resumed...")
  1158. if state.led_controller:
  1159. # Always power LEDs back on if they were turned off for scheduled pause,
  1160. # regardless of whether a playing effect is configured
  1161. if wled_was_off_for_scheduled:
  1162. logger.info("Turning LED lights back on as Still Sands period ended")
  1163. await state.led_controller.set_power_async(1)
  1164. # CRITICAL: Give LED controller time to fully power on before sending more commands
  1165. # Without this delay, rapid-fire requests can crash controllers on resource-constrained Pis
  1166. await asyncio.sleep(0.5)
  1167. # Apply playing effect if configured
  1168. # For WLED: always trigger (uses hardcoded preset 2)
  1169. # For DW_LED: only trigger if effect is configured
  1170. should_trigger_led = state.led_provider == "wled" or state.dw_led_playing_effect
  1171. if should_trigger_led:
  1172. await state.led_controller.effect_playing_async(state.dw_led_playing_effect)
  1173. # Cancel idle timeout when resuming from pause
  1174. idle_timeout_manager.cancel_timeout()
  1175. # Dynamically determine the speed for each movement
  1176. # Use clear_pattern_speed if it's set and this is a clear file, otherwise use state.speed
  1177. if is_clear_file and state.clear_pattern_speed is not None:
  1178. current_speed = state.clear_pattern_speed
  1179. else:
  1180. current_speed = state.speed
  1181. await move_polar(theta, rho, current_speed)
  1182. # Update progress for all coordinates including the first one
  1183. pbar.update(1)
  1184. elapsed_time = time.time() - start_time
  1185. coords_done = i + 1
  1186. # Track rho-weighted progress for accurate time estimation
  1187. completed_weight += coord_weights[i]
  1188. remaining_weight = total_weight - completed_weight
  1189. # Calculate actual execution time (excluding pauses)
  1190. active_time = elapsed_time - total_pause_time
  1191. # Need minimum samples for stable estimate (at least 100 coords and 10 seconds)
  1192. if coords_done >= 100 and active_time > 10:
  1193. # Rate is time per unit weight (accounts for slower moves near center)
  1194. current_rate = active_time / completed_weight
  1195. # Smooth the RATE for stability
  1196. if smoothed_rate is not None:
  1197. alpha = 0.02 # Very smooth - 2% new, 98% old
  1198. smoothed_rate = alpha * current_rate + (1 - alpha) * smoothed_rate
  1199. else:
  1200. smoothed_rate = current_rate
  1201. # Remaining time based on weighted remaining work
  1202. estimated_remaining_time = smoothed_rate * remaining_weight
  1203. else:
  1204. estimated_remaining_time = None
  1205. state.execution_progress = (coords_done, total_coordinates, estimated_remaining_time, elapsed_time)
  1206. # Add a small delay to allow other async operations
  1207. await asyncio.sleep(0.001)
  1208. # Update progress one last time to show 100%
  1209. elapsed_time = time.time() - start_time
  1210. actual_execution_time = elapsed_time - total_pause_time
  1211. state.execution_progress = (total_coordinates, total_coordinates, 0, elapsed_time)
  1212. # Give WebSocket a chance to send the final update
  1213. await asyncio.sleep(0.1)
  1214. # Log execution time (only for completed patterns, not stopped/skipped)
  1215. was_completed = not state.stop_requested and not state.skip_requested
  1216. pattern_name = os.path.basename(file_path)
  1217. effective_speed = state.clear_pattern_speed if (is_clear_file and state.clear_pattern_speed is not None) else state.speed
  1218. log_execution_time(
  1219. pattern_name=pattern_name,
  1220. table_type=state.table_type,
  1221. speed=effective_speed,
  1222. actual_time=actual_execution_time,
  1223. total_coordinates=total_coordinates,
  1224. was_completed=was_completed
  1225. )
  1226. if not state.conn:
  1227. logger.error("Device is not connected. Stopping pattern execution.")
  1228. return False
  1229. await connection_manager.check_idle_async()
  1230. # Set LED back to idle when pattern completes normally (not stopped early)
  1231. # This also handles Still Sands: turns off LEDs if in scheduled pause period with LED control
  1232. # Skip during clear pattern - the main pattern starts immediately after, so triggering
  1233. # idle LED here would cause a brief flicker (idle effect → playing effect in ~0.3s)
  1234. if not state.stop_requested and not state.is_clearing:
  1235. await start_idle_led_timeout()
  1236. return was_completed
  1237. async def run_theta_rho_file(file_path, is_playlist=False, clear_pattern=None, cache_data=None):
  1238. """Run a theta-rho file with optional pre-execution clear pattern.
  1239. Args:
  1240. file_path: Path to the main .thr file to execute
  1241. is_playlist: True if running as part of a playlist
  1242. clear_pattern: Clear pattern mode ('adaptive', 'clear_from_in', 'clear_from_out', 'none', or None)
  1243. cache_data: Pre-loaded metadata cache for adaptive clear pattern selection
  1244. """
  1245. lock = get_pattern_lock()
  1246. if lock.locked():
  1247. logger.warning("Another pattern is already running. Cannot start a new one.")
  1248. return
  1249. async with lock: # This ensures only one pattern can run at a time
  1250. # Clear any stale pause state from previous playlist
  1251. state.pause_time_remaining = 0
  1252. state.original_pause_time = None
  1253. # Start progress update task only if not part of a playlist
  1254. global progress_update_task
  1255. if not is_playlist and not progress_update_task:
  1256. progress_update_task = asyncio.create_task(broadcast_progress())
  1257. # Run clear pattern first if specified
  1258. if clear_pattern and clear_pattern != 'none':
  1259. clear_file_path = get_clear_pattern_file(clear_pattern, file_path, cache_data)
  1260. if clear_file_path:
  1261. logger.info(f"Running pre-execution clear pattern: {clear_file_path}")
  1262. state.is_clearing = True
  1263. await _execute_pattern_internal(clear_file_path)
  1264. state.is_clearing = False
  1265. # Reset skip flag after clear pattern (if user skipped clear, continue to main)
  1266. state.skip_requested = False
  1267. # Check if stopped during clear pattern
  1268. if state.stop_requested:
  1269. logger.info("Execution stopped during clear pattern")
  1270. if not is_playlist:
  1271. state.current_playing_file = None
  1272. state.execution_progress = None
  1273. return
  1274. # Run the main pattern
  1275. await _execute_pattern_internal(file_path)
  1276. # Only clear state if not part of a playlist
  1277. if not is_playlist:
  1278. state.current_playing_file = None
  1279. state.execution_progress = None
  1280. logger.info("Pattern execution completed and state cleared")
  1281. # Only cancel progress update task if not part of a playlist
  1282. if progress_update_task:
  1283. progress_update_task.cancel()
  1284. try:
  1285. await progress_update_task
  1286. except asyncio.CancelledError:
  1287. pass
  1288. progress_update_task = None
  1289. else:
  1290. logger.info("Pattern execution completed, maintaining state for playlist")
  1291. async def run_theta_rho_files(file_paths, pause_time=0, clear_pattern=None, run_mode="single", shuffle=False):
  1292. """Run multiple .thr files in sequence with options.
  1293. The playlist now stores only main patterns. Clear patterns are executed dynamically
  1294. before each main pattern based on the clear_pattern option.
  1295. """
  1296. state.stop_requested = False
  1297. # Reset LED idle timeout activity time when playlist starts
  1298. import time as time_module
  1299. state.dw_led_last_activity_time = time_module.time()
  1300. # Set initial playlist state only if not already set by caller (playlist_manager).
  1301. # This ensures backward compatibility when this function is called directly.
  1302. if state.playlist_mode is None:
  1303. state.playlist_mode = run_mode
  1304. if state.current_playlist_index is None:
  1305. state.current_playlist_index = 0
  1306. # Start progress update task for the playlist
  1307. global progress_update_task
  1308. if not progress_update_task:
  1309. progress_update_task = asyncio.create_task(broadcast_progress())
  1310. # Shuffle main patterns if requested (before starting)
  1311. if shuffle:
  1312. random.shuffle(file_paths)
  1313. logger.info("Playlist shuffled")
  1314. # Store patterns in state only if not already set by caller.
  1315. # The caller (playlist_manager.run_playlist) sets this before creating the task.
  1316. if state.current_playlist is None:
  1317. state.current_playlist = file_paths
  1318. try:
  1319. while True:
  1320. # Load metadata cache once per playlist iteration (for adaptive clear patterns)
  1321. cache_data = None
  1322. if clear_pattern and clear_pattern in ['adaptive', 'clear_from_in', 'clear_from_out']:
  1323. from modules.core import cache_manager
  1324. cache_data = await asyncio.to_thread(cache_manager.load_metadata_cache)
  1325. logger.info(f"Loaded metadata cache for {len(cache_data.get('data', {}))} patterns")
  1326. # Reset pattern counter at the start of the playlist
  1327. state.patterns_since_last_home = 0
  1328. # Execute main patterns using index-based access
  1329. # This allows the playlist to be reordered during execution
  1330. idx = 0
  1331. while state.current_playlist and idx < len(state.current_playlist):
  1332. state.current_playlist_index = idx
  1333. if state.stop_requested or not state.current_playlist:
  1334. logger.info("Execution stopped")
  1335. return
  1336. # Get the pattern at the current index (may have changed due to reordering)
  1337. file_path = state.current_playlist[idx]
  1338. logger.info(f"Running pattern {idx + 1}/{len(state.current_playlist)}: {file_path}")
  1339. # Clear pause state when starting a new pattern (prevents stale "waiting" UI)
  1340. state.pause_time_remaining = 0
  1341. state.original_pause_time = None
  1342. # Execute the pattern with optional clear pattern
  1343. await run_theta_rho_file(
  1344. file_path,
  1345. is_playlist=True,
  1346. clear_pattern=clear_pattern,
  1347. cache_data=cache_data
  1348. )
  1349. # Increment pattern counter (auto-home check happens after pause time)
  1350. state.patterns_since_last_home += 1
  1351. logger.debug(f"Patterns since last home: {state.patterns_since_last_home}")
  1352. # Check for scheduled pause after pattern completes (when "finish pattern first" is enabled)
  1353. if state.scheduled_pause_finish_pattern and is_in_scheduled_pause_period() and not state.stop_requested and not state.skip_requested:
  1354. logger.info("Pattern completed. Entering Still Sands period (finish pattern first mode)...")
  1355. wled_was_off_for_scheduled = False
  1356. if state.scheduled_pause_control_wled and state.led_controller:
  1357. logger.info("Turning off LED lights during Still Sands period")
  1358. await state.led_controller.set_power_async(0)
  1359. wled_was_off_for_scheduled = True
  1360. else:
  1361. # Show idle effect (WLED control not enabled)
  1362. await start_idle_led_timeout(check_still_sands=False)
  1363. # Wait for scheduled pause to end, but allow stop/skip to interrupt
  1364. result = await wait_with_interrupt(
  1365. is_in_scheduled_pause_period,
  1366. check_stop=True,
  1367. check_skip=True,
  1368. )
  1369. if result == 'completed':
  1370. logger.info("Still Sands period ended. Resuming playlist...")
  1371. if state.led_controller:
  1372. # Always power LEDs back on if they were turned off for scheduled pause,
  1373. # regardless of whether a playing effect is configured
  1374. if wled_was_off_for_scheduled:
  1375. logger.info("Turning LED lights back on as Still Sands period ended")
  1376. await state.led_controller.set_power_async(1)
  1377. await asyncio.sleep(0.5)
  1378. # Apply playing effect if configured
  1379. # For WLED: always trigger (uses hardcoded preset 2)
  1380. # For DW_LED: only trigger if effect is configured
  1381. should_trigger_led = state.led_provider == "wled" or state.dw_led_playing_effect
  1382. if should_trigger_led:
  1383. await state.led_controller.effect_playing_async(state.dw_led_playing_effect)
  1384. idle_timeout_manager.cancel_timeout()
  1385. # Handle pause between patterns
  1386. if state.current_playlist and idx < len(state.current_playlist) - 1 and not state.stop_requested and pause_time > 0 and not state.skip_requested:
  1387. logger.info(f"Pausing for {pause_time} seconds")
  1388. # Clear current_playing_file to report "idle" state to MQTT/HA during pause
  1389. # This will be set again when the next pattern starts
  1390. state.current_playing_file = None
  1391. # Trigger idle LED state during pause between patterns
  1392. await start_idle_led_timeout(check_still_sands=True)
  1393. state.original_pause_time = pause_time
  1394. pause_start = time.time()
  1395. # Track Still Sands state for edge detection during long pauses
  1396. was_in_still_sands = is_in_scheduled_pause_period() and state.scheduled_pause_control_wled
  1397. while time.time() - pause_start < pause_time:
  1398. state.pause_time_remaining = pause_start + pause_time - time.time()
  1399. if state.skip_requested or state.stop_requested:
  1400. if state.stop_requested:
  1401. logger.info("Pause interrupted by stop request")
  1402. else:
  1403. logger.info("Pause interrupted by skip request")
  1404. break
  1405. # Monitor Still Sands transitions during pause
  1406. in_still_sands = is_in_scheduled_pause_period() and state.scheduled_pause_control_wled
  1407. if in_still_sands and not was_in_still_sands:
  1408. # Entering Still Sands period — turn off LEDs
  1409. logger.info("Still Sands period started during pause, turning off LEDs")
  1410. if state.led_controller:
  1411. await state.led_controller.set_power_async(0)
  1412. elif not in_still_sands and was_in_still_sands:
  1413. # Leaving Still Sands period — restore idle effect
  1414. logger.info("Still Sands period ended during pause, restoring idle LED effect")
  1415. await start_idle_led_timeout(check_still_sands=False)
  1416. was_in_still_sands = in_still_sands
  1417. await asyncio.sleep(1)
  1418. # Clear both pause state vars immediately (so UI updates right away)
  1419. state.pause_time_remaining = 0
  1420. state.original_pause_time = None
  1421. # Auto-home after pause time, before next clear pattern starts
  1422. # Only home if there's a next pattern and we haven't been stopped
  1423. if (state.auto_home_enabled and
  1424. state.patterns_since_last_home >= state.auto_home_after_patterns and
  1425. state.current_playlist and idx < len(state.current_playlist) - 1 and
  1426. not state.stop_requested):
  1427. logger.info(f"Auto-homing triggered after {state.patterns_since_last_home} patterns (before next clear pattern)")
  1428. try:
  1429. success = await asyncio.to_thread(connection_manager.home)
  1430. if success:
  1431. logger.info("Auto-homing completed successfully")
  1432. state.patterns_since_last_home = 0
  1433. else:
  1434. logger.warning("Auto-homing failed, continuing with playlist")
  1435. except Exception as e:
  1436. logger.error(f"Error during auto-homing: {e}")
  1437. state.skip_requested = False
  1438. idx += 1
  1439. if run_mode == "indefinite":
  1440. logger.info("Playlist completed. Restarting as per 'indefinite' run mode")
  1441. if pause_time > 0:
  1442. # Clear current_playing_file to report "idle" state to MQTT/HA during pause
  1443. state.current_playing_file = None
  1444. # Trigger idle LED state during pause between playlist cycles
  1445. await start_idle_led_timeout(check_still_sands=True)
  1446. state.original_pause_time = pause_time
  1447. pause_start = time.time()
  1448. # Track Still Sands state for edge detection during long pauses
  1449. was_in_still_sands = is_in_scheduled_pause_period() and state.scheduled_pause_control_wled
  1450. while time.time() - pause_start < pause_time:
  1451. state.pause_time_remaining = pause_start + pause_time - time.time()
  1452. if state.skip_requested or state.stop_requested:
  1453. if state.stop_requested:
  1454. logger.info("Pause interrupted by stop request")
  1455. else:
  1456. logger.info("Pause interrupted by skip request")
  1457. break
  1458. # Monitor Still Sands transitions during pause
  1459. in_still_sands = is_in_scheduled_pause_period() and state.scheduled_pause_control_wled
  1460. if in_still_sands and not was_in_still_sands:
  1461. # Entering Still Sands period — turn off LEDs
  1462. logger.info("Still Sands period started during pause, turning off LEDs")
  1463. if state.led_controller:
  1464. await state.led_controller.set_power_async(0)
  1465. elif not in_still_sands and was_in_still_sands:
  1466. # Leaving Still Sands period — restore idle effect
  1467. logger.info("Still Sands period ended during pause, restoring idle LED effect")
  1468. await start_idle_led_timeout(check_still_sands=False)
  1469. was_in_still_sands = in_still_sands
  1470. await asyncio.sleep(1)
  1471. # Clear both pause state vars immediately (so UI updates right away)
  1472. state.pause_time_remaining = 0
  1473. state.original_pause_time = None
  1474. continue
  1475. else:
  1476. logger.info("Playlist completed")
  1477. break
  1478. except asyncio.CancelledError:
  1479. # Task was cancelled externally (e.g., by TestClient cleanup, or explicit cancellation).
  1480. # Do NOT clear playlist state - preserve what the caller set.
  1481. logger.info("Playlist task was cancelled externally, preserving state")
  1482. if progress_update_task:
  1483. progress_update_task.cancel()
  1484. try:
  1485. await progress_update_task
  1486. except asyncio.CancelledError:
  1487. pass
  1488. progress_update_task = None
  1489. raise # Re-raise to signal cancellation
  1490. finally:
  1491. if progress_update_task:
  1492. progress_update_task.cancel()
  1493. try:
  1494. await progress_update_task
  1495. except asyncio.CancelledError:
  1496. pass
  1497. progress_update_task = None
  1498. # Check if we're exiting due to CancelledError - if so, don't clear state.
  1499. # State should only be cleared when:
  1500. # 1. Task completed normally (all patterns executed)
  1501. # 2. Task was stopped by user request (stop_requested)
  1502. # NOT when task was cancelled externally (CancelledError)
  1503. import sys
  1504. exc_type = sys.exc_info()[0]
  1505. if exc_type is asyncio.CancelledError:
  1506. logger.info("Task exiting due to cancellation, state preserved for caller")
  1507. else:
  1508. # Normal completion or user-requested stop - clear state
  1509. state.current_playing_file = None
  1510. state.execution_progress = None
  1511. state.current_playlist = None
  1512. state.current_playlist_index = None
  1513. state.playlist_mode = None
  1514. state.pause_time_remaining = 0
  1515. # Persist cleared state so server restart won't load stale playlist
  1516. state.save()
  1517. await start_idle_led_timeout()
  1518. logger.info("All requested patterns completed (or stopped) and state cleared")
  1519. async def stop_actions(clear_playlist = True, wait_for_lock = True):
  1520. """Stop all current actions and wait for pattern to fully release.
  1521. Args:
  1522. clear_playlist: Whether to clear playlist state
  1523. wait_for_lock: Whether to wait for pattern_lock to be released. Set to False when
  1524. called from within pattern execution to avoid deadlock.
  1525. Returns:
  1526. True if stopped cleanly, False if timed out waiting for pattern lock
  1527. """
  1528. timed_out = False
  1529. try:
  1530. with state.pause_condition:
  1531. state.pause_requested = False
  1532. state.stop_requested = True
  1533. state.is_clearing = False
  1534. # Always clear pause time between patterns on stop
  1535. state.pause_time_remaining = 0
  1536. state.original_pause_time = None
  1537. if clear_playlist:
  1538. # Clear playlist state
  1539. state.current_playlist = None
  1540. state.current_playlist_index = None
  1541. state.playlist_mode = None
  1542. # Cancel progress update task if we're clearing the playlist
  1543. global progress_update_task
  1544. if progress_update_task and not progress_update_task.done():
  1545. progress_update_task.cancel()
  1546. # Cancel the playlist task itself (late import to avoid circular dependency)
  1547. from modules.core import playlist_manager
  1548. await playlist_manager.cancel_current_playlist()
  1549. state.pause_condition.notify_all()
  1550. # Also set the pause event to wake up any paused patterns
  1551. get_pause_event().set()
  1552. # Send stop command to motion thread to clear its queue
  1553. if motion_controller.running:
  1554. motion_controller.command_queue.put(MotionCommand('stop'))
  1555. # Wait for the pattern lock to be released before continuing
  1556. # This ensures that when stop_actions completes, the pattern has fully stopped
  1557. # Skip this if called from within pattern execution to avoid deadlock
  1558. lock = get_pattern_lock()
  1559. if wait_for_lock and lock.locked():
  1560. logger.info("Waiting for pattern to fully stop...")
  1561. # Use a timeout to prevent hanging forever
  1562. # Use wait_for for Python 3.9 compatibility (asyncio.timeout is 3.11+)
  1563. try:
  1564. async def acquire_stop_lock():
  1565. async with lock:
  1566. logger.info("Pattern lock acquired - pattern has fully stopped")
  1567. await asyncio.wait_for(acquire_stop_lock(), timeout=10.0)
  1568. except asyncio.TimeoutError:
  1569. logger.warning("Timeout waiting for pattern to stop - forcing cleanup")
  1570. timed_out = True
  1571. # Force cleanup of state even if pattern didn't release lock gracefully
  1572. state.current_playing_file = None
  1573. state.execution_progress = None
  1574. state.is_running = False
  1575. # Clear current playing file only when clearing the entire playlist.
  1576. # When clear_playlist=False (called from within pattern execution), the caller
  1577. # will set current_playing_file to the new pattern immediately after.
  1578. if clear_playlist:
  1579. state.current_playing_file = None
  1580. state.execution_progress = None
  1581. # Clear stop_requested now that the pattern has stopped - this allows
  1582. # check_idle_async to work (it exits early if stop_requested is True)
  1583. state.stop_requested = False
  1584. # Wait for hardware to reach idle state before returning
  1585. # This ensures the machine has physically stopped moving
  1586. if not timed_out:
  1587. idle = await connection_manager.check_idle_async(timeout=30.0)
  1588. if not idle:
  1589. logger.warning("Machine did not reach idle after stop")
  1590. # Call async function directly since we're in async context
  1591. await connection_manager.update_machine_position()
  1592. return not timed_out
  1593. except Exception as e:
  1594. logger.error(f"Error during stop_actions: {e}")
  1595. # Force cleanup state on error
  1596. state.current_playing_file = None
  1597. state.execution_progress = None
  1598. state.is_running = False
  1599. # Ensure we still update machine position even if there's an error
  1600. try:
  1601. await connection_manager.update_machine_position()
  1602. except Exception as update_err:
  1603. logger.error(f"Error updating machine position on error: {update_err}")
  1604. return False
  1605. async def move_polar(theta, rho, speed=None):
  1606. """
  1607. Queue a motion command to be executed in the dedicated motion control thread.
  1608. This makes motion control non-blocking for API endpoints.
  1609. Args:
  1610. theta (float): Target theta coordinate
  1611. rho (float): Target rho coordinate
  1612. speed (int, optional): Speed override. If None, uses state.speed
  1613. """
  1614. # Note: stop_requested is cleared once at pattern start (execute_theta_rho_file line 890)
  1615. # Don't clear it here on every coordinate - causes performance issues with event system
  1616. # Ensure motion control thread is running
  1617. if not motion_controller.running:
  1618. motion_controller.start()
  1619. # Create future for async/await pattern
  1620. loop = asyncio.get_event_loop()
  1621. future = loop.create_future()
  1622. # Create and queue motion command
  1623. command = MotionCommand(
  1624. command_type='move',
  1625. theta=theta,
  1626. rho=rho,
  1627. speed=speed,
  1628. future=future
  1629. )
  1630. motion_controller.command_queue.put(command)
  1631. logger.debug(f"Queued motion command: theta={theta}, rho={rho}, speed={speed}")
  1632. # Wait for command completion
  1633. await future
  1634. def pause_execution():
  1635. """Pause pattern execution using asyncio Event."""
  1636. logger.info("Pausing pattern execution")
  1637. state.pause_requested = True
  1638. get_pause_event().clear() # Clear the event to pause execution
  1639. return True
  1640. def resume_execution():
  1641. """Resume pattern execution using asyncio Event."""
  1642. logger.info("Resuming pattern execution")
  1643. state.pause_requested = False
  1644. get_pause_event().set() # Set the event to resume execution
  1645. return True
  1646. async def reset_theta():
  1647. """
  1648. Reset theta to [0, 2π) range and optionally hard reset machine position using $Bye.
  1649. When state.hard_reset_theta is True:
  1650. - $Bye sends a soft reset to FluidNC which resets the controller and clears
  1651. all position counters to 0. This is more reliable than G92 which only sets
  1652. a work coordinate offset without changing the actual machine position (MPos).
  1653. - We wait for machine to be idle before sending $Bye to avoid error:25
  1654. When state.hard_reset_theta is False (default):
  1655. - Only normalizes theta to [0, 2π) range without affecting machine position
  1656. - Faster and doesn't interrupt machine state
  1657. """
  1658. logger.info('Resetting Theta')
  1659. # Always normalize theta to [0, 2π) range
  1660. state.current_theta = state.current_theta % (2 * pi)
  1661. logger.info(f'Theta normalized to {state.current_theta:.4f} radians')
  1662. # Only perform hard reset if enabled
  1663. if state.hard_reset_theta:
  1664. logger.info('Hard reset enabled - performing machine soft reset')
  1665. # Wait for machine to be idle before reset to prevent error:25
  1666. if state.conn and state.conn.is_connected():
  1667. logger.info("Waiting for machine to be idle before reset...")
  1668. idle = await connection_manager.check_idle_async(timeout=30)
  1669. if not idle:
  1670. logger.warning("Machine not idle after 30s, proceeding with reset anyway")
  1671. # Hard reset machine position using $Bye via connection_manager
  1672. success = await connection_manager.perform_soft_reset()
  1673. if not success:
  1674. logger.error("Soft reset failed - theta reset may be unreliable")
  1675. else:
  1676. logger.info('Hard reset disabled - skipping machine soft reset')
  1677. def set_speed(new_speed):
  1678. state.speed = new_speed
  1679. logger.info(f'Set new state.speed {new_speed}')
  1680. def get_status():
  1681. """Get the current status of pattern execution."""
  1682. status = {
  1683. "current_file": state.current_playing_file,
  1684. "is_paused": state.pause_requested or is_in_scheduled_pause_period(),
  1685. "manual_pause": state.pause_requested,
  1686. "scheduled_pause": is_in_scheduled_pause_period(),
  1687. "is_running": bool(state.current_playing_file and not state.stop_requested),
  1688. "is_homing": state.is_homing,
  1689. "sensor_homing_failed": state.sensor_homing_failed,
  1690. "is_clearing": state.is_clearing,
  1691. "progress": None,
  1692. "playlist": None,
  1693. "speed": state.speed,
  1694. "pause_time_remaining": state.pause_time_remaining,
  1695. "original_pause_time": getattr(state, 'original_pause_time', None),
  1696. "connection_status": state.conn.is_connected() if state.conn else False,
  1697. "current_theta": state.current_theta,
  1698. "current_rho": state.current_rho
  1699. }
  1700. # Add playlist information if available
  1701. if state.current_playlist and state.current_playlist_index is not None:
  1702. # When a clear pattern is running, the "next" pattern is the current main pattern
  1703. # (since the clear pattern runs before the main pattern at current_playlist_index)
  1704. if state.is_clearing:
  1705. next_file = state.current_playlist[state.current_playlist_index]
  1706. else:
  1707. next_index = state.current_playlist_index + 1
  1708. next_file = state.current_playlist[next_index] if next_index < len(state.current_playlist) else None
  1709. status["playlist"] = {
  1710. "current_index": state.current_playlist_index,
  1711. "total_files": len(state.current_playlist),
  1712. "mode": state.playlist_mode,
  1713. "next_file": next_file,
  1714. "files": state.current_playlist,
  1715. "name": state.current_playlist_name
  1716. }
  1717. if state.execution_progress:
  1718. current, total, remaining_time, elapsed_time = state.execution_progress
  1719. status["progress"] = {
  1720. "current": current,
  1721. "total": total,
  1722. "remaining_time": remaining_time,
  1723. "elapsed_time": elapsed_time,
  1724. "percentage": (current / total * 100) if total > 0 else 0
  1725. }
  1726. # Add historical execution time if available for this pattern at current speed
  1727. if state.current_playing_file:
  1728. pattern_name = os.path.basename(state.current_playing_file)
  1729. historical_time = get_last_completed_execution_time(pattern_name, state.speed)
  1730. if historical_time:
  1731. status["progress"]["last_completed_time"] = historical_time
  1732. return status
  1733. async def broadcast_progress():
  1734. """Background task to broadcast progress updates."""
  1735. from main import broadcast_status_update
  1736. while True:
  1737. # Send status updates regardless of pattern_lock state
  1738. status = get_status()
  1739. # Use the existing broadcast function from main.py
  1740. await broadcast_status_update(status)
  1741. # Check if we should stop broadcasting
  1742. if not state.current_playlist:
  1743. # If no playlist, only stop if no pattern is being executed
  1744. if not get_pattern_lock().locked():
  1745. logger.info("No playlist or pattern running, stopping broadcast")
  1746. break
  1747. # Wait before next update
  1748. await asyncio.sleep(1)