pattern_manager.py 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990
  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. # Cache coordinates in state for frontend preview (avoids re-parsing large files)
  1008. state._current_coordinates = coordinates
  1009. if total_coordinates < 2:
  1010. logger.warning("Not enough coordinates for interpolation")
  1011. return False
  1012. # Pre-calculate rho-based weights for more accurate time estimation
  1013. # Moves near center (low rho) are slower than perimeter moves due to
  1014. # polar geometry - less linear distance per theta change at low rho
  1015. def calc_move_weight(rho):
  1016. # Weight inversely proportional to rho, with floor to avoid extreme values
  1017. # At rho=0: weight≈6.7, at rho=0.5: weight≈1.5, at rho=1.0: weight≈0.87
  1018. return 1.0 / (rho + 0.15)
  1019. coord_weights = [calc_move_weight(rho) for _, rho in coordinates]
  1020. total_weight = sum(coord_weights)
  1021. # Determine if this is a clearing pattern
  1022. is_clear_file = is_clear_pattern(file_path)
  1023. if is_clear_file:
  1024. initial_speed = state.clear_pattern_speed if state.clear_pattern_speed is not None else state.speed
  1025. logger.info(f"Running clearing pattern at initial speed {initial_speed}")
  1026. else:
  1027. logger.info(f"Running normal pattern at initial speed {state.speed}")
  1028. state.execution_progress = (0, total_coordinates, None, 0)
  1029. # stop actions without resetting the playlist, and don't wait for lock (we already have it)
  1030. # Preserve is_clearing flag since stop_actions resets it
  1031. was_clearing = state.is_clearing
  1032. await stop_actions(clear_playlist=False, wait_for_lock=False)
  1033. state.is_clearing = was_clearing
  1034. state.current_playing_file = file_path
  1035. state.stop_requested = False
  1036. # Reset LED idle timeout activity time when pattern starts
  1037. import time as time_module
  1038. state.dw_led_last_activity_time = time_module.time()
  1039. logger.info(f"Starting pattern execution: {file_path}")
  1040. logger.info(f"t: {state.current_theta}, r: {state.current_rho}")
  1041. await reset_theta()
  1042. start_time = time.time()
  1043. total_pause_time = 0 # Track total time spent paused (manual + scheduled)
  1044. completed_weight = 0.0 # Track rho-weighted progress
  1045. smoothed_rate = None # For exponential smoothing of time-per-unit-weight rate
  1046. # For WLED: always trigger (uses hardcoded preset 2)
  1047. # For DW_LED: only trigger if effect is configured
  1048. if state.led_controller and (state.led_provider == "wled" or state.dw_led_playing_effect):
  1049. logger.info(f"Setting LED to playing effect: {state.dw_led_playing_effect}")
  1050. await state.led_controller.effect_playing_async(state.dw_led_playing_effect)
  1051. # Cancel idle timeout when playing starts
  1052. idle_timeout_manager.cancel_timeout()
  1053. with tqdm(
  1054. total=total_coordinates,
  1055. unit="coords",
  1056. desc=f"Executing Pattern {file_path}",
  1057. dynamic_ncols=True,
  1058. disable=False,
  1059. mininterval=1.0
  1060. ) as pbar:
  1061. for i, coordinate in enumerate(coordinates):
  1062. theta, rho = coordinate
  1063. if state.stop_requested:
  1064. logger.info("Execution stopped by user")
  1065. await start_idle_led_timeout()
  1066. break
  1067. if state.skip_requested:
  1068. logger.info("Skipping pattern...")
  1069. await connection_manager.check_idle_async()
  1070. await start_idle_led_timeout()
  1071. break
  1072. # Wait for resume if paused (manual or scheduled)
  1073. manual_pause = state.pause_requested
  1074. # Only check scheduled pause during pattern if "finish pattern first" is NOT enabled
  1075. scheduled_pause = is_in_scheduled_pause_period() if not state.scheduled_pause_finish_pattern else False
  1076. if manual_pause or scheduled_pause:
  1077. pause_start = time.time() # Track when pause started
  1078. if manual_pause and scheduled_pause:
  1079. logger.info("Execution paused (manual + scheduled pause active)...")
  1080. elif manual_pause:
  1081. logger.info("Execution paused (manual)...")
  1082. else:
  1083. logger.info("Execution paused (scheduled pause period)...")
  1084. # Turn off LED controller if scheduled pause and control_wled is enabled
  1085. if state.scheduled_pause_control_wled and state.led_controller:
  1086. logger.info("Turning off LED lights during Still Sands period")
  1087. await state.led_controller.set_power_async(0)
  1088. # Show idle effect for manual pause or scheduled pause without LED control
  1089. # (skip Still Sands check since we handle it above with local scheduled_pause variable)
  1090. if not (scheduled_pause and state.scheduled_pause_control_wled):
  1091. await start_idle_led_timeout(check_still_sands=False)
  1092. # Remember if we turned off LED controller for scheduled pause
  1093. wled_was_off_for_scheduled = scheduled_pause and state.scheduled_pause_control_wled and not manual_pause
  1094. # Wait until both manual pause is released AND we're outside scheduled pause period
  1095. # Also check for stop/skip requests to allow immediate interruption
  1096. interrupted = False
  1097. while state.pause_requested or is_in_scheduled_pause_period():
  1098. # Check for stop/skip first
  1099. if state.stop_requested:
  1100. logger.info("Stop requested during pause, exiting")
  1101. interrupted = True
  1102. break
  1103. if state.skip_requested:
  1104. logger.info("Skip requested during pause, skipping pattern")
  1105. interrupted = True
  1106. break
  1107. if state.pause_requested:
  1108. # For manual pause, wait on multiple events for immediate response
  1109. # Wake on: resume, stop, skip, or timeout (for flag polling fallback)
  1110. pause_event = get_pause_event()
  1111. stop_event = state.get_stop_event()
  1112. skip_event = state.get_skip_event()
  1113. wait_tasks = [asyncio.create_task(pause_event.wait(), name='pause')]
  1114. if stop_event:
  1115. wait_tasks.append(asyncio.create_task(stop_event.wait(), name='stop'))
  1116. if skip_event:
  1117. wait_tasks.append(asyncio.create_task(skip_event.wait(), name='skip'))
  1118. # Add timeout to ensure we periodically check flags even if events aren't set
  1119. # This handles the case where stop is called from sync context (no event loop)
  1120. timeout_task = asyncio.create_task(asyncio.sleep(1.0), name='timeout')
  1121. wait_tasks.append(timeout_task)
  1122. try:
  1123. done, pending = await asyncio.wait(
  1124. wait_tasks, return_when=asyncio.FIRST_COMPLETED
  1125. )
  1126. finally:
  1127. for task in pending:
  1128. task.cancel()
  1129. for task in pending:
  1130. try:
  1131. await task
  1132. except asyncio.CancelledError:
  1133. pass
  1134. else:
  1135. # For scheduled pause, use wait_for_interrupt for instant response
  1136. result = await state.wait_for_interrupt(timeout=1.0)
  1137. if result in ('stopped', 'skipped'):
  1138. interrupted = True
  1139. break
  1140. total_pause_time += time.time() - pause_start # Add pause duration
  1141. if interrupted:
  1142. # Exit the coordinate loop if we were interrupted
  1143. break
  1144. logger.info("Execution resumed...")
  1145. if state.led_controller:
  1146. # Turn LED controller back on if it was turned off for scheduled pause
  1147. # For WLED: always trigger (uses hardcoded preset 2)
  1148. # For DW_LED: only trigger if effect is configured
  1149. should_trigger_led = state.led_provider == "wled" or state.dw_led_playing_effect
  1150. if wled_was_off_for_scheduled:
  1151. if should_trigger_led:
  1152. logger.info("Turning LED lights back on as Still Sands period ended")
  1153. await state.led_controller.set_power_async(1)
  1154. # CRITICAL: Give LED controller time to fully power on before sending more commands
  1155. # Without this delay, rapid-fire requests can crash controllers on resource-constrained Pis
  1156. await asyncio.sleep(0.5)
  1157. else:
  1158. logger.info("No playing effect configured, keeping LEDs off after Still Sands")
  1159. if should_trigger_led:
  1160. await state.led_controller.effect_playing_async(state.dw_led_playing_effect)
  1161. # Cancel idle timeout when resuming from pause
  1162. idle_timeout_manager.cancel_timeout()
  1163. # Dynamically determine the speed for each movement
  1164. # Use clear_pattern_speed if it's set and this is a clear file, otherwise use state.speed
  1165. if is_clear_file and state.clear_pattern_speed is not None:
  1166. current_speed = state.clear_pattern_speed
  1167. else:
  1168. current_speed = state.speed
  1169. await move_polar(theta, rho, current_speed)
  1170. # Update progress for all coordinates including the first one
  1171. pbar.update(1)
  1172. elapsed_time = time.time() - start_time
  1173. coords_done = i + 1
  1174. # Track rho-weighted progress for accurate time estimation
  1175. completed_weight += coord_weights[i]
  1176. remaining_weight = total_weight - completed_weight
  1177. # Calculate actual execution time (excluding pauses)
  1178. active_time = elapsed_time - total_pause_time
  1179. # Need minimum samples for stable estimate (at least 100 coords and 10 seconds)
  1180. if coords_done >= 100 and active_time > 10:
  1181. # Rate is time per unit weight (accounts for slower moves near center)
  1182. current_rate = active_time / completed_weight
  1183. # Smooth the RATE for stability
  1184. if smoothed_rate is not None:
  1185. alpha = 0.02 # Very smooth - 2% new, 98% old
  1186. smoothed_rate = alpha * current_rate + (1 - alpha) * smoothed_rate
  1187. else:
  1188. smoothed_rate = current_rate
  1189. # Remaining time based on weighted remaining work
  1190. estimated_remaining_time = smoothed_rate * remaining_weight
  1191. else:
  1192. estimated_remaining_time = None
  1193. state.execution_progress = (coords_done, total_coordinates, estimated_remaining_time, elapsed_time)
  1194. # Add a small delay to allow other async operations
  1195. await asyncio.sleep(0.001)
  1196. # Update progress one last time to show 100%
  1197. elapsed_time = time.time() - start_time
  1198. actual_execution_time = elapsed_time - total_pause_time
  1199. state.execution_progress = (total_coordinates, total_coordinates, 0, elapsed_time)
  1200. # Give WebSocket a chance to send the final update
  1201. await asyncio.sleep(0.1)
  1202. # Log execution time (only for completed patterns, not stopped/skipped)
  1203. was_completed = not state.stop_requested and not state.skip_requested
  1204. pattern_name = os.path.basename(file_path)
  1205. effective_speed = state.clear_pattern_speed if (is_clear_file and state.clear_pattern_speed is not None) else state.speed
  1206. log_execution_time(
  1207. pattern_name=pattern_name,
  1208. table_type=state.table_type,
  1209. speed=effective_speed,
  1210. actual_time=actual_execution_time,
  1211. total_coordinates=total_coordinates,
  1212. was_completed=was_completed
  1213. )
  1214. if not state.conn:
  1215. logger.error("Device is not connected. Stopping pattern execution.")
  1216. return False
  1217. await connection_manager.check_idle_async()
  1218. # Set LED back to idle when pattern completes normally (not stopped early)
  1219. # This also handles Still Sands: turns off LEDs if in scheduled pause period with LED control
  1220. if not state.stop_requested:
  1221. await start_idle_led_timeout()
  1222. return was_completed
  1223. async def run_theta_rho_file(file_path, is_playlist=False, clear_pattern=None, cache_data=None):
  1224. """Run a theta-rho file with optional pre-execution clear pattern.
  1225. Args:
  1226. file_path: Path to the main .thr file to execute
  1227. is_playlist: True if running as part of a playlist
  1228. clear_pattern: Clear pattern mode ('adaptive', 'clear_from_in', 'clear_from_out', 'none', or None)
  1229. cache_data: Pre-loaded metadata cache for adaptive clear pattern selection
  1230. """
  1231. lock = get_pattern_lock()
  1232. if lock.locked():
  1233. logger.warning("Another pattern is already running. Cannot start a new one.")
  1234. return
  1235. async with lock: # This ensures only one pattern can run at a time
  1236. # Clear any stale pause state from previous playlist
  1237. state.pause_time_remaining = 0
  1238. state.original_pause_time = None
  1239. # Start progress update task only if not part of a playlist
  1240. global progress_update_task
  1241. if not is_playlist and not progress_update_task:
  1242. progress_update_task = asyncio.create_task(broadcast_progress())
  1243. # Run clear pattern first if specified
  1244. if clear_pattern and clear_pattern != 'none':
  1245. clear_file_path = get_clear_pattern_file(clear_pattern, file_path, cache_data)
  1246. if clear_file_path:
  1247. logger.info(f"Running pre-execution clear pattern: {clear_file_path}")
  1248. state.is_clearing = True
  1249. await _execute_pattern_internal(clear_file_path)
  1250. state.is_clearing = False
  1251. # Reset skip flag after clear pattern (if user skipped clear, continue to main)
  1252. state.skip_requested = False
  1253. # Check if stopped during clear pattern
  1254. if state.stop_requested:
  1255. logger.info("Execution stopped during clear pattern")
  1256. if not is_playlist:
  1257. state.current_playing_file = None
  1258. state.execution_progress = None
  1259. return
  1260. # Run the main pattern
  1261. await _execute_pattern_internal(file_path)
  1262. # Only clear state if not part of a playlist
  1263. if not is_playlist:
  1264. state.current_playing_file = None
  1265. state.execution_progress = None
  1266. logger.info("Pattern execution completed and state cleared")
  1267. # Only cancel progress update task if not part of a playlist
  1268. if progress_update_task:
  1269. progress_update_task.cancel()
  1270. try:
  1271. await progress_update_task
  1272. except asyncio.CancelledError:
  1273. pass
  1274. progress_update_task = None
  1275. else:
  1276. logger.info("Pattern execution completed, maintaining state for playlist")
  1277. async def run_theta_rho_files(file_paths, pause_time=0, clear_pattern=None, run_mode="single", shuffle=False):
  1278. """Run multiple .thr files in sequence with options.
  1279. The playlist now stores only main patterns. Clear patterns are executed dynamically
  1280. before each main pattern based on the clear_pattern option.
  1281. """
  1282. state.stop_requested = False
  1283. # Reset LED idle timeout activity time when playlist starts
  1284. import time as time_module
  1285. state.dw_led_last_activity_time = time_module.time()
  1286. # Set initial playlist state only if not already set by caller (playlist_manager).
  1287. # This ensures backward compatibility when this function is called directly.
  1288. if state.playlist_mode is None:
  1289. state.playlist_mode = run_mode
  1290. if state.current_playlist_index is None:
  1291. state.current_playlist_index = 0
  1292. # Start progress update task for the playlist
  1293. global progress_update_task
  1294. if not progress_update_task:
  1295. progress_update_task = asyncio.create_task(broadcast_progress())
  1296. # Shuffle main patterns if requested (before starting)
  1297. if shuffle:
  1298. random.shuffle(file_paths)
  1299. logger.info("Playlist shuffled")
  1300. # Store patterns in state only if not already set by caller.
  1301. # The caller (playlist_manager.run_playlist) sets this before creating the task.
  1302. if state.current_playlist is None:
  1303. state.current_playlist = file_paths
  1304. try:
  1305. while True:
  1306. # Load metadata cache once per playlist iteration (for adaptive clear patterns)
  1307. cache_data = None
  1308. if clear_pattern and clear_pattern in ['adaptive', 'clear_from_in', 'clear_from_out']:
  1309. from modules.core import cache_manager
  1310. cache_data = await asyncio.to_thread(cache_manager.load_metadata_cache)
  1311. logger.info(f"Loaded metadata cache for {len(cache_data.get('data', {}))} patterns")
  1312. # Reset pattern counter at the start of the playlist
  1313. state.patterns_since_last_home = 0
  1314. # Execute main patterns using index-based access
  1315. # This allows the playlist to be reordered during execution
  1316. idx = 0
  1317. while state.current_playlist and idx < len(state.current_playlist):
  1318. state.current_playlist_index = idx
  1319. if state.stop_requested or not state.current_playlist:
  1320. logger.info("Execution stopped")
  1321. return
  1322. # Get the pattern at the current index (may have changed due to reordering)
  1323. file_path = state.current_playlist[idx]
  1324. logger.info(f"Running pattern {idx + 1}/{len(state.current_playlist)}: {file_path}")
  1325. # Clear pause state when starting a new pattern (prevents stale "waiting" UI)
  1326. state.pause_time_remaining = 0
  1327. state.original_pause_time = None
  1328. # Execute the pattern with optional clear pattern
  1329. await run_theta_rho_file(
  1330. file_path,
  1331. is_playlist=True,
  1332. clear_pattern=clear_pattern,
  1333. cache_data=cache_data
  1334. )
  1335. # Increment pattern counter (auto-home check happens after pause time)
  1336. state.patterns_since_last_home += 1
  1337. logger.debug(f"Patterns since last home: {state.patterns_since_last_home}")
  1338. # Check for scheduled pause after pattern completes (when "finish pattern first" is enabled)
  1339. if state.scheduled_pause_finish_pattern and is_in_scheduled_pause_period() and not state.stop_requested and not state.skip_requested:
  1340. logger.info("Pattern completed. Entering Still Sands period (finish pattern first mode)...")
  1341. wled_was_off_for_scheduled = False
  1342. if state.scheduled_pause_control_wled and state.led_controller:
  1343. logger.info("Turning off LED lights during Still Sands period")
  1344. await state.led_controller.set_power_async(0)
  1345. wled_was_off_for_scheduled = True
  1346. else:
  1347. # Show idle effect (WLED control not enabled)
  1348. await start_idle_led_timeout(check_still_sands=False)
  1349. # Wait for scheduled pause to end, but allow stop/skip to interrupt
  1350. result = await wait_with_interrupt(
  1351. is_in_scheduled_pause_period,
  1352. check_stop=True,
  1353. check_skip=True,
  1354. )
  1355. if result == 'completed':
  1356. logger.info("Still Sands period ended. Resuming playlist...")
  1357. if state.led_controller:
  1358. # For WLED: always trigger (uses hardcoded preset 2)
  1359. # For DW_LED: only trigger if effect is configured
  1360. should_trigger_led = state.led_provider == "wled" or state.dw_led_playing_effect
  1361. if wled_was_off_for_scheduled:
  1362. if should_trigger_led:
  1363. logger.info("Turning LED lights back on as Still Sands period ended")
  1364. await state.led_controller.set_power_async(1)
  1365. await asyncio.sleep(0.5)
  1366. else:
  1367. logger.info("No playing effect configured, keeping LEDs off after Still Sands")
  1368. if should_trigger_led:
  1369. await state.led_controller.effect_playing_async(state.dw_led_playing_effect)
  1370. idle_timeout_manager.cancel_timeout()
  1371. # Handle pause between patterns
  1372. 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:
  1373. logger.info(f"Pausing for {pause_time} seconds")
  1374. # Clear current_playing_file to report "idle" state to MQTT/HA during pause
  1375. # This will be set again when the next pattern starts
  1376. state.current_playing_file = None
  1377. # Trigger idle LED state during pause between patterns
  1378. await start_idle_led_timeout(check_still_sands=False)
  1379. state.original_pause_time = pause_time
  1380. pause_start = time.time()
  1381. while time.time() - pause_start < pause_time:
  1382. state.pause_time_remaining = pause_start + pause_time - time.time()
  1383. if state.skip_requested:
  1384. logger.info("Pause interrupted by skip request")
  1385. break
  1386. await asyncio.sleep(1)
  1387. # Clear both pause state vars immediately (so UI updates right away)
  1388. state.pause_time_remaining = 0
  1389. state.original_pause_time = None
  1390. # Auto-home after pause time, before next clear pattern starts
  1391. # Only home if there's a next pattern and we haven't been stopped
  1392. if (state.auto_home_enabled and
  1393. state.patterns_since_last_home >= state.auto_home_after_patterns and
  1394. state.current_playlist and idx < len(state.current_playlist) - 1 and
  1395. not state.stop_requested):
  1396. logger.info(f"Auto-homing triggered after {state.patterns_since_last_home} patterns (before next clear pattern)")
  1397. try:
  1398. success = await asyncio.to_thread(connection_manager.home)
  1399. if success:
  1400. logger.info("Auto-homing completed successfully")
  1401. state.patterns_since_last_home = 0
  1402. else:
  1403. logger.warning("Auto-homing failed, continuing with playlist")
  1404. except Exception as e:
  1405. logger.error(f"Error during auto-homing: {e}")
  1406. state.skip_requested = False
  1407. idx += 1
  1408. if run_mode == "indefinite":
  1409. logger.info("Playlist completed. Restarting as per 'indefinite' run mode")
  1410. if pause_time > 0:
  1411. # Clear current_playing_file to report "idle" state to MQTT/HA during pause
  1412. state.current_playing_file = None
  1413. # Trigger idle LED state during pause between playlist cycles
  1414. await start_idle_led_timeout(check_still_sands=False)
  1415. state.original_pause_time = pause_time
  1416. pause_start = time.time()
  1417. while time.time() - pause_start < pause_time:
  1418. state.pause_time_remaining = pause_start + pause_time - time.time()
  1419. if state.skip_requested:
  1420. logger.info("Pause interrupted by skip request")
  1421. break
  1422. await asyncio.sleep(1)
  1423. # Clear both pause state vars immediately (so UI updates right away)
  1424. state.pause_time_remaining = 0
  1425. state.original_pause_time = None
  1426. continue
  1427. else:
  1428. logger.info("Playlist completed")
  1429. break
  1430. except asyncio.CancelledError:
  1431. # Task was cancelled externally (e.g., by TestClient cleanup, or explicit cancellation).
  1432. # Do NOT clear playlist state - preserve what the caller set.
  1433. logger.info("Playlist task was cancelled externally, preserving state")
  1434. if progress_update_task:
  1435. progress_update_task.cancel()
  1436. try:
  1437. await progress_update_task
  1438. except asyncio.CancelledError:
  1439. pass
  1440. progress_update_task = None
  1441. raise # Re-raise to signal cancellation
  1442. finally:
  1443. if progress_update_task:
  1444. progress_update_task.cancel()
  1445. try:
  1446. await progress_update_task
  1447. except asyncio.CancelledError:
  1448. pass
  1449. progress_update_task = None
  1450. # Check if we're exiting due to CancelledError - if so, don't clear state.
  1451. # State should only be cleared when:
  1452. # 1. Task completed normally (all patterns executed)
  1453. # 2. Task was stopped by user request (stop_requested)
  1454. # NOT when task was cancelled externally (CancelledError)
  1455. import sys
  1456. exc_type = sys.exc_info()[0]
  1457. if exc_type is asyncio.CancelledError:
  1458. logger.info("Task exiting due to cancellation, state preserved for caller")
  1459. else:
  1460. # Normal completion or user-requested stop - clear state
  1461. state.current_playing_file = None
  1462. state.execution_progress = None
  1463. state.current_playlist = None
  1464. state.current_playlist_index = None
  1465. state.playlist_mode = None
  1466. state.pause_time_remaining = 0
  1467. await start_idle_led_timeout()
  1468. logger.info("All requested patterns completed (or stopped) and state cleared")
  1469. async def stop_actions(clear_playlist = True, wait_for_lock = True):
  1470. """Stop all current actions and wait for pattern to fully release.
  1471. Args:
  1472. clear_playlist: Whether to clear playlist state
  1473. wait_for_lock: Whether to wait for pattern_lock to be released. Set to False when
  1474. called from within pattern execution to avoid deadlock.
  1475. Returns:
  1476. True if stopped cleanly, False if timed out waiting for pattern lock
  1477. """
  1478. timed_out = False
  1479. try:
  1480. with state.pause_condition:
  1481. state.pause_requested = False
  1482. state.stop_requested = True
  1483. state.is_clearing = False
  1484. # Always clear pause time between patterns on stop
  1485. state.pause_time_remaining = 0
  1486. state.original_pause_time = None
  1487. if clear_playlist:
  1488. # Clear playlist state
  1489. state.current_playlist = None
  1490. state.current_playlist_index = None
  1491. state.playlist_mode = None
  1492. # Cancel progress update task if we're clearing the playlist
  1493. global progress_update_task
  1494. if progress_update_task and not progress_update_task.done():
  1495. progress_update_task.cancel()
  1496. # Cancel the playlist task itself (late import to avoid circular dependency)
  1497. from modules.core import playlist_manager
  1498. await playlist_manager.cancel_current_playlist()
  1499. state.pause_condition.notify_all()
  1500. # Also set the pause event to wake up any paused patterns
  1501. get_pause_event().set()
  1502. # Send stop command to motion thread to clear its queue
  1503. if motion_controller.running:
  1504. motion_controller.command_queue.put(MotionCommand('stop'))
  1505. # Wait for the pattern lock to be released before continuing
  1506. # This ensures that when stop_actions completes, the pattern has fully stopped
  1507. # Skip this if called from within pattern execution to avoid deadlock
  1508. lock = get_pattern_lock()
  1509. if wait_for_lock and lock.locked():
  1510. logger.info("Waiting for pattern to fully stop...")
  1511. # Use a timeout to prevent hanging forever
  1512. # Use wait_for for Python 3.9 compatibility (asyncio.timeout is 3.11+)
  1513. try:
  1514. async def acquire_stop_lock():
  1515. async with lock:
  1516. logger.info("Pattern lock acquired - pattern has fully stopped")
  1517. await asyncio.wait_for(acquire_stop_lock(), timeout=10.0)
  1518. except asyncio.TimeoutError:
  1519. logger.warning("Timeout waiting for pattern to stop - forcing cleanup")
  1520. timed_out = True
  1521. # Force cleanup of state even if pattern didn't release lock gracefully
  1522. state.current_playing_file = None
  1523. state.execution_progress = None
  1524. state.is_running = False
  1525. # Clear current playing file only when clearing the entire playlist.
  1526. # When clear_playlist=False (called from within pattern execution), the caller
  1527. # will set current_playing_file to the new pattern immediately after.
  1528. if clear_playlist:
  1529. state.current_playing_file = None
  1530. state.execution_progress = None
  1531. # Clear stop_requested now that the pattern has stopped - this allows
  1532. # check_idle_async to work (it exits early if stop_requested is True)
  1533. state.stop_requested = False
  1534. # Wait for hardware to reach idle state before returning
  1535. # This ensures the machine has physically stopped moving
  1536. if not timed_out:
  1537. idle = await connection_manager.check_idle_async(timeout=30.0)
  1538. if not idle:
  1539. logger.warning("Machine did not reach idle after stop")
  1540. # Call async function directly since we're in async context
  1541. await connection_manager.update_machine_position()
  1542. return not timed_out
  1543. except Exception as e:
  1544. logger.error(f"Error during stop_actions: {e}")
  1545. # Force cleanup state on error
  1546. state.current_playing_file = None
  1547. state.execution_progress = None
  1548. state.is_running = False
  1549. # Ensure we still update machine position even if there's an error
  1550. try:
  1551. await connection_manager.update_machine_position()
  1552. except Exception as update_err:
  1553. logger.error(f"Error updating machine position on error: {update_err}")
  1554. return False
  1555. async def move_polar(theta, rho, speed=None):
  1556. """
  1557. Queue a motion command to be executed in the dedicated motion control thread.
  1558. This makes motion control non-blocking for API endpoints.
  1559. Args:
  1560. theta (float): Target theta coordinate
  1561. rho (float): Target rho coordinate
  1562. speed (int, optional): Speed override. If None, uses state.speed
  1563. """
  1564. # Note: stop_requested is cleared once at pattern start (execute_theta_rho_file line 890)
  1565. # Don't clear it here on every coordinate - causes performance issues with event system
  1566. # Ensure motion control thread is running
  1567. if not motion_controller.running:
  1568. motion_controller.start()
  1569. # Create future for async/await pattern
  1570. loop = asyncio.get_event_loop()
  1571. future = loop.create_future()
  1572. # Create and queue motion command
  1573. command = MotionCommand(
  1574. command_type='move',
  1575. theta=theta,
  1576. rho=rho,
  1577. speed=speed,
  1578. future=future
  1579. )
  1580. motion_controller.command_queue.put(command)
  1581. logger.debug(f"Queued motion command: theta={theta}, rho={rho}, speed={speed}")
  1582. # Wait for command completion
  1583. await future
  1584. def pause_execution():
  1585. """Pause pattern execution using asyncio Event."""
  1586. logger.info("Pausing pattern execution")
  1587. state.pause_requested = True
  1588. get_pause_event().clear() # Clear the event to pause execution
  1589. return True
  1590. def resume_execution():
  1591. """Resume pattern execution using asyncio Event."""
  1592. logger.info("Resuming pattern execution")
  1593. state.pause_requested = False
  1594. get_pause_event().set() # Set the event to resume execution
  1595. return True
  1596. async def reset_theta():
  1597. """
  1598. Reset theta to [0, 2π) range and optionally hard reset machine position using $Bye.
  1599. When state.hard_reset_theta is True:
  1600. - $Bye sends a soft reset to FluidNC which resets the controller and clears
  1601. all position counters to 0. This is more reliable than G92 which only sets
  1602. a work coordinate offset without changing the actual machine position (MPos).
  1603. - We wait for machine to be idle before sending $Bye to avoid error:25
  1604. When state.hard_reset_theta is False (default):
  1605. - Only normalizes theta to [0, 2π) range without affecting machine position
  1606. - Faster and doesn't interrupt machine state
  1607. """
  1608. logger.info('Resetting Theta')
  1609. # Always normalize theta to [0, 2π) range
  1610. state.current_theta = state.current_theta % (2 * pi)
  1611. logger.info(f'Theta normalized to {state.current_theta:.4f} radians')
  1612. # Only perform hard reset if enabled
  1613. if state.hard_reset_theta:
  1614. logger.info('Hard reset enabled - performing machine soft reset')
  1615. # Wait for machine to be idle before reset to prevent error:25
  1616. if state.conn and state.conn.is_connected():
  1617. logger.info("Waiting for machine to be idle before reset...")
  1618. idle = await connection_manager.check_idle_async(timeout=30)
  1619. if not idle:
  1620. logger.warning("Machine not idle after 30s, proceeding with reset anyway")
  1621. # Hard reset machine position using $Bye via connection_manager
  1622. success = await connection_manager.perform_soft_reset()
  1623. if not success:
  1624. logger.error("Soft reset failed - theta reset may be unreliable")
  1625. else:
  1626. logger.info('Hard reset disabled - skipping machine soft reset')
  1627. def set_speed(new_speed):
  1628. state.speed = new_speed
  1629. logger.info(f'Set new state.speed {new_speed}')
  1630. def get_status():
  1631. """Get the current status of pattern execution."""
  1632. status = {
  1633. "current_file": state.current_playing_file,
  1634. "is_paused": state.pause_requested or is_in_scheduled_pause_period(),
  1635. "manual_pause": state.pause_requested,
  1636. "scheduled_pause": is_in_scheduled_pause_period(),
  1637. "is_running": bool(state.current_playing_file and not state.stop_requested),
  1638. "is_homing": state.is_homing,
  1639. "sensor_homing_failed": state.sensor_homing_failed,
  1640. "is_clearing": state.is_clearing,
  1641. "progress": None,
  1642. "playlist": None,
  1643. "speed": state.speed,
  1644. "pause_time_remaining": state.pause_time_remaining,
  1645. "original_pause_time": getattr(state, 'original_pause_time', None),
  1646. "connection_status": state.conn.is_connected() if state.conn else False,
  1647. "current_theta": state.current_theta,
  1648. "current_rho": state.current_rho
  1649. }
  1650. # Add playlist information if available
  1651. if state.current_playlist and state.current_playlist_index is not None:
  1652. # When a clear pattern is running, the "next" pattern is the current main pattern
  1653. # (since the clear pattern runs before the main pattern at current_playlist_index)
  1654. if state.is_clearing:
  1655. next_file = state.current_playlist[state.current_playlist_index]
  1656. else:
  1657. next_index = state.current_playlist_index + 1
  1658. next_file = state.current_playlist[next_index] if next_index < len(state.current_playlist) else None
  1659. status["playlist"] = {
  1660. "current_index": state.current_playlist_index,
  1661. "total_files": len(state.current_playlist),
  1662. "mode": state.playlist_mode,
  1663. "next_file": next_file,
  1664. "files": state.current_playlist,
  1665. "name": state.current_playlist_name
  1666. }
  1667. if state.execution_progress:
  1668. current, total, remaining_time, elapsed_time = state.execution_progress
  1669. status["progress"] = {
  1670. "current": current,
  1671. "total": total,
  1672. "remaining_time": remaining_time,
  1673. "elapsed_time": elapsed_time,
  1674. "percentage": (current / total * 100) if total > 0 else 0
  1675. }
  1676. # Add historical execution time if available for this pattern at current speed
  1677. if state.current_playing_file:
  1678. pattern_name = os.path.basename(state.current_playing_file)
  1679. historical_time = get_last_completed_execution_time(pattern_name, state.speed)
  1680. if historical_time:
  1681. status["progress"]["last_completed_time"] = historical_time
  1682. return status
  1683. async def broadcast_progress():
  1684. """Background task to broadcast progress updates."""
  1685. from main import broadcast_status_update
  1686. while True:
  1687. # Send status updates regardless of pattern_lock state
  1688. status = get_status()
  1689. # Use the existing broadcast function from main.py
  1690. await broadcast_status_update(status)
  1691. # Check if we should stop broadcasting
  1692. if not state.current_playlist:
  1693. # If no playlist, only stop if no pattern is being executed
  1694. if not get_pattern_lock().locked():
  1695. logger.info("No playlist or pattern running, stopping broadcast")
  1696. break
  1697. # Wait before next update
  1698. await asyncio.sleep(1)