1
0

app.py 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  1. from flask import Flask, request, jsonify, render_template
  2. import os
  3. import serial
  4. import time
  5. import random
  6. import threading
  7. import serial.tools.list_ports
  8. import math
  9. import json
  10. from datetime import datetime
  11. import subprocess
  12. app = Flask(__name__)
  13. # Configuration
  14. THETA_RHO_DIR = './patterns'
  15. IGNORE_PORTS = ['/dev/cu.debug-console', '/dev/cu.Bluetooth-Incoming-Port']
  16. CLEAR_PATTERNS = {
  17. "clear_from_in": "./patterns/clear_from_in.thr",
  18. "clear_from_out": "./patterns/clear_from_out.thr",
  19. "clear_sideway": "./patterns/clear_sideway.thr"
  20. }
  21. os.makedirs(THETA_RHO_DIR, exist_ok=True)
  22. # Serial connection (First available will be selected by default)
  23. ser = None
  24. ser_port = None # Global variable to store the serial port name
  25. stop_requested = False
  26. pause_requested = False
  27. pause_condition = threading.Condition()
  28. # Global variables to store device information
  29. arduino_table_name = None
  30. arduino_driver_type = 'Unknown'
  31. # Table status
  32. current_playing_file = None
  33. execution_progress = None
  34. firmware_version = 'Unknown'
  35. current_playing_index = None
  36. current_playlist = None
  37. is_clearing = False
  38. serial_lock = threading.Lock()
  39. PLAYLISTS_FILE = os.path.join(os.getcwd(), "playlists.json")
  40. MOTOR_TYPE_MAPPING = {
  41. "TMC2209": "./firmware/arduino_code_TMC2209/arduino_code_TMC2209.ino",
  42. "DRV8825": "./firmware/arduino_code/arduino_code.ino",
  43. "esp32": "./firmware/esp32/esp32.ino"
  44. }
  45. # Ensure the file exists and contains at least an empty JSON object
  46. if not os.path.exists(PLAYLISTS_FILE):
  47. with open(PLAYLISTS_FILE, "w") as f:
  48. json.dump({}, f, indent=2)
  49. def get_ino_firmware_details(ino_file_path):
  50. """
  51. Extract firmware details, including version and motor type, from the given .ino file.
  52. Args:
  53. ino_file_path (str): Path to the .ino file.
  54. Returns:
  55. dict: Dictionary containing firmware details such as version and motor type, or None if not found.
  56. """
  57. try:
  58. if not ino_file_path:
  59. raise ValueError("Invalid path: ino_file_path is None or empty.")
  60. firmware_details = {"version": None, "motorType": None}
  61. with open(ino_file_path, "r") as file:
  62. for line in file:
  63. # Extract firmware version
  64. if "firmwareVersion" in line:
  65. start = line.find('"') + 1
  66. end = line.rfind('"')
  67. if start != -1 and end != -1 and start < end:
  68. firmware_details["version"] = line[start:end]
  69. # Extract motor type
  70. if "motorType" in line:
  71. start = line.find('"') + 1
  72. end = line.rfind('"')
  73. if start != -1 and end != -1 and start < end:
  74. firmware_details["motorType"] = line[start:end]
  75. if not firmware_details["version"]:
  76. print(f"Firmware version not found in file: {ino_file_path}")
  77. if not firmware_details["motorType"]:
  78. print(f"Motor type not found in file: {ino_file_path}")
  79. return firmware_details if any(firmware_details.values()) else None
  80. except FileNotFoundError:
  81. print(f"File not found: {ino_file_path}")
  82. return None
  83. except Exception as e:
  84. print(f"Error reading .ino file: {str(e)}")
  85. return None
  86. def check_git_updates():
  87. try:
  88. # Fetch the latest updates from the remote repository
  89. subprocess.run(["git", "fetch", "--tags", "--force"], check=True)
  90. # Get the latest tag from the remote
  91. latest_remote_tag = subprocess.check_output(
  92. ["git", "describe", "--tags", "--abbrev=0", "origin/main"]
  93. ).strip().decode()
  94. # Get the latest tag from the local branch
  95. latest_local_tag = subprocess.check_output(
  96. ["git", "describe", "--tags", "--abbrev=0"]
  97. ).strip().decode()
  98. # Count how many tags the local branch is behind
  99. tag_behind_count = 0
  100. if latest_local_tag != latest_remote_tag:
  101. tags = subprocess.check_output(
  102. ["git", "tag", "--merged", "origin/main"], text=True
  103. ).splitlines()
  104. found_local = False
  105. for tag in tags:
  106. if tag == latest_local_tag:
  107. found_local = True
  108. elif found_local:
  109. tag_behind_count += 1
  110. if tag == latest_remote_tag:
  111. break
  112. # Check if there are new commits
  113. updates_available = latest_remote_tag != latest_local_tag
  114. return {
  115. "updates_available": updates_available,
  116. "tag_behind_count": tag_behind_count, # Tags behind
  117. "latest_remote_tag": latest_remote_tag,
  118. "latest_local_tag": latest_local_tag,
  119. }
  120. except subprocess.CalledProcessError as e:
  121. print(f"Error checking Git updates: {e}")
  122. return {
  123. "updates_available": False,
  124. "tag_behind_count": 0,
  125. "latest_remote_tag": None,
  126. "latest_local_tag": None,
  127. }
  128. def list_serial_ports():
  129. """Return a list of available serial ports."""
  130. ports = serial.tools.list_ports.comports()
  131. return [port.device for port in ports if port.device not in IGNORE_PORTS]
  132. def connect_to_serial(port=None, baudrate=115200):
  133. """Automatically connect to the first available serial port or a specified port."""
  134. global ser, ser_port, arduino_table_name, arduino_driver_type, firmware_version
  135. try:
  136. if port is None:
  137. ports = list_serial_ports()
  138. if not ports:
  139. print("No serial port connected")
  140. return False
  141. port = ports[0] # Auto-select the first available port
  142. with serial_lock:
  143. if ser and ser.is_open:
  144. ser.close()
  145. ser = serial.Serial(port, baudrate, timeout=2) # Set timeout to avoid infinite waits
  146. ser_port = port # Store the connected port globally
  147. print(f"Connected to serial port: {port}")
  148. time.sleep(2) # Allow time for the connection to establish
  149. # Read initial startup messages from Arduino
  150. arduino_table_name = None
  151. arduino_driver_type = None
  152. while ser.in_waiting > 0:
  153. line = ser.readline().decode().strip()
  154. print(f"Arduino: {line}") # Print the received message
  155. # Store the device details based on the expected messages
  156. if "Table:" in line:
  157. arduino_table_name = line.replace("Table: ", "").strip()
  158. elif "Drivers:" in line:
  159. arduino_driver_type = line.replace("Drivers: ", "").strip()
  160. elif "Version:" in line:
  161. firmware_version = line.replace("Version: ", "").strip()
  162. # Display stored values
  163. print(f"Detected Table: {arduino_table_name or 'Unknown'}")
  164. print(f"Detected Drivers: {arduino_driver_type or 'Unknown'}")
  165. return True # Successfully connected
  166. except serial.SerialException as e:
  167. print(f"Failed to connect to serial port {port}: {e}")
  168. port = None # Reset the port to try the next available one
  169. print("Max retries reached. Could not connect to a serial port.")
  170. return False
  171. def disconnect_serial():
  172. """Disconnect the current serial connection."""
  173. global ser, ser_port
  174. if ser and ser.is_open:
  175. ser.close()
  176. ser = None
  177. ser_port = None # Reset the port name
  178. def restart_serial(port, baudrate=115200):
  179. """Restart the serial connection."""
  180. disconnect_serial()
  181. connect_to_serial(port, baudrate)
  182. def parse_theta_rho_file(file_path):
  183. """
  184. Parse a theta-rho file and return a list of (theta, rho) pairs.
  185. Normalizes the list so the first theta is always 0.
  186. """
  187. coordinates = []
  188. try:
  189. with open(file_path, 'r') as file:
  190. for line in file:
  191. line = line.strip()
  192. # Skip header or comment lines (starting with '#' or empty lines)
  193. if not line or line.startswith("#"):
  194. continue
  195. # Parse lines with theta and rho separated by spaces
  196. try:
  197. theta, rho = map(float, line.split())
  198. coordinates.append((theta, rho))
  199. except ValueError:
  200. print(f"Skipping invalid line: {line}")
  201. continue
  202. except Exception as e:
  203. print(f"Error reading file: {e}")
  204. return coordinates
  205. # ---- Normalization Step ----
  206. if coordinates:
  207. # Take the first coordinate's theta
  208. first_theta = coordinates[0][0]
  209. # Shift all thetas so the first coordinate has theta=0
  210. normalized = []
  211. for (theta, rho) in coordinates:
  212. normalized.append((theta - first_theta, rho))
  213. # Replace original list with normalized data
  214. coordinates = normalized
  215. return coordinates
  216. def send_coordinate_batch(ser, coordinates):
  217. """Send a batch of theta-rho pairs to the Arduino."""
  218. # print("Sending batch:", coordinates)
  219. batch_str = ";".join(f"{theta:.5f},{rho:.5f}" for theta, rho in coordinates) + ";\n"
  220. ser.write(batch_str.encode())
  221. def send_command(command):
  222. """Send a single command to the Arduino."""
  223. ser.write(f"{command}\n".encode())
  224. print(f"Sent: {command}")
  225. # Wait for "R" acknowledgment from Arduino
  226. while True:
  227. with serial_lock:
  228. if ser.in_waiting > 0:
  229. response = ser.readline().decode().strip()
  230. print(f"Arduino response: {response}")
  231. if response == "R":
  232. print("Command execution completed.")
  233. break
  234. def wait_for_start_time(schedule_hours):
  235. """
  236. Keep checking every 30 seconds if the time is within the schedule to resume execution.
  237. """
  238. global pause_requested
  239. start_time, end_time = schedule_hours
  240. while pause_requested:
  241. now = datetime.now().time()
  242. if start_time <= now < end_time:
  243. print("Resuming execution: Within schedule.")
  244. pause_requested = False
  245. with pause_condition:
  246. pause_condition.notify_all()
  247. break # Exit the loop once resumed
  248. else:
  249. time.sleep(30) # Wait for 30 seconds before checking again
  250. # Function to check schedule based on start and end time
  251. def schedule_checker(schedule_hours):
  252. """
  253. Pauses/resumes execution based on a given time range.
  254. Parameters:
  255. - schedule_hours (tuple): (start_time, end_time) as `datetime.time` objects.
  256. """
  257. global pause_requested
  258. if not schedule_hours:
  259. return # No scheduling restriction
  260. start_time, end_time = schedule_hours
  261. now = datetime.now().time() # Get the current time as `datetime.time`
  262. # Check if we are currently within the scheduled time
  263. if start_time <= now < end_time:
  264. if pause_requested:
  265. print("Starting execution: Within schedule.")
  266. pause_requested = False # Resume execution
  267. with pause_condition:
  268. pause_condition.notify_all()
  269. else:
  270. if not pause_requested:
  271. print("Pausing execution: Outside schedule.")
  272. pause_requested = True # Pause execution
  273. # Start a background thread to periodically check for start time
  274. threading.Thread(target=wait_for_start_time, args=(schedule_hours,), daemon=True).start()
  275. def run_theta_rho_file(file_path, schedule_hours=None):
  276. """Run a theta-rho file by sending data in optimized batches."""
  277. global stop_requested, current_playing_file, execution_progress
  278. stop_requested = False
  279. current_playing_file = file_path # Track current playing file
  280. execution_progress = (0, 0) # Reset progress
  281. coordinates = parse_theta_rho_file(file_path)
  282. total_coordinates = len(coordinates)
  283. if total_coordinates < 2:
  284. print("Not enough coordinates for interpolation.")
  285. current_playing_file = None # Clear tracking if failed
  286. execution_progress = None
  287. return
  288. execution_progress = (0, total_coordinates) # Update total coordinates
  289. batch_size = 10 # Smaller batches may smooth movement further
  290. for i in range(0, total_coordinates, batch_size):
  291. if stop_requested:
  292. print("Execution stopped by user after completing the current batch.")
  293. break
  294. with pause_condition:
  295. while pause_requested:
  296. print("Execution paused...")
  297. pause_condition.wait() # This will block execution until notified
  298. batch = coordinates[i:i + batch_size]
  299. if i == 0:
  300. send_coordinate_batch(ser, batch)
  301. execution_progress = (i + batch_size, total_coordinates) # Update progress
  302. continue
  303. while True:
  304. schedule_checker(schedule_hours) # Check if within schedule
  305. with serial_lock:
  306. if ser.in_waiting > 0:
  307. response = ser.readline().decode().strip()
  308. if response == "R":
  309. send_coordinate_batch(ser, batch)
  310. execution_progress = (i + batch_size, total_coordinates) # Update progress
  311. break
  312. else:
  313. print(f"Arduino response: {response}")
  314. reset_theta()
  315. ser.write("FINISHED\n".encode())
  316. # Clear tracking variables when done
  317. current_playing_file = None
  318. execution_progress = None
  319. print("Pattern execution completed.")
  320. def get_clear_pattern_file(pattern_name):
  321. """Return a .thr file path based on pattern_name."""
  322. if pattern_name == "random":
  323. # Randomly pick one of the three known patterns
  324. return random.choice(list(CLEAR_PATTERNS.values()))
  325. # If pattern_name is invalid or absent, default to 'clear_from_in'
  326. return CLEAR_PATTERNS.get(pattern_name, CLEAR_PATTERNS["clear_from_in"])
  327. def run_theta_rho_files(
  328. file_paths,
  329. pause_time=0,
  330. clear_pattern=None,
  331. run_mode="single",
  332. shuffle=False,
  333. schedule_hours=None
  334. ):
  335. """
  336. Runs multiple .thr files in sequence with options for pausing, clearing, shuffling, and looping.
  337. Parameters:
  338. - file_paths (list): List of file paths to run.
  339. - pause_time (float): Seconds to pause between patterns.
  340. - clear_pattern (str): Specific clear pattern to run ("clear_in", "clear_out", "clear_sideway", or "random").
  341. - run_mode (str): "single" for one-time run or "indefinite" for looping.
  342. - shuffle (bool): Whether to shuffle the playlist before running.
  343. """
  344. global stop_requested
  345. global current_playlist
  346. global current_playing_index
  347. stop_requested = False # Reset stop flag at the start
  348. if shuffle:
  349. random.shuffle(file_paths)
  350. print("Playlist shuffled.")
  351. current_playlist = file_paths
  352. while True:
  353. for idx, path in enumerate(file_paths):
  354. current_playing_index = idx
  355. schedule_checker(schedule_hours)
  356. if stop_requested:
  357. print("Execution stopped before starting next pattern.")
  358. return
  359. if clear_pattern:
  360. if stop_requested:
  361. print("Execution stopped before running the next clear pattern.")
  362. return
  363. # Determine the clear pattern to run
  364. clear_file_path = get_clear_pattern_file(clear_pattern)
  365. print(f"Running clear pattern: {clear_file_path}")
  366. run_theta_rho_file(clear_file_path, schedule_hours)
  367. if not stop_requested:
  368. # Run the main pattern
  369. print(f"Running pattern {idx + 1} of {len(file_paths)}: {path}")
  370. run_theta_rho_file(path, schedule_hours)
  371. if idx < len(file_paths) -1:
  372. if stop_requested:
  373. print("Execution stopped before running the next clear pattern.")
  374. return
  375. # Pause after each pattern if requested
  376. if pause_time > 0:
  377. print(f"Pausing for {pause_time} seconds...")
  378. time.sleep(pause_time)
  379. # After completing the playlist
  380. if run_mode == "indefinite":
  381. print("Playlist completed. Restarting as per 'indefinite' run mode.")
  382. if pause_time > 0:
  383. print(f"Pausing for {pause_time} seconds before restarting...")
  384. time.sleep(pause_time)
  385. if shuffle:
  386. random.shuffle(file_paths)
  387. print("Playlist reshuffled for the next loop.")
  388. continue
  389. else:
  390. print("Playlist completed.")
  391. break
  392. # Reset theta after execution or stopping
  393. reset_theta()
  394. ser.write("FINISHED\n".encode())
  395. print("All requested patterns completed (or stopped).")
  396. def reset_theta():
  397. """Reset theta on the Arduino."""
  398. ser.write("RESET_THETA\n".encode())
  399. while True:
  400. with serial_lock:
  401. if ser.in_waiting > 0:
  402. response = ser.readline().decode().strip()
  403. print(f"Arduino response: {response}")
  404. if response == "THETA_RESET":
  405. print("Theta successfully reset.")
  406. break
  407. time.sleep(0.5) # Small delay to avoid busy waiting
  408. # Flask API Endpoints
  409. @app.route('/')
  410. def index():
  411. return render_template('index.html')
  412. @app.route('/list_serial_ports', methods=['GET'])
  413. def list_ports():
  414. return jsonify(list_serial_ports())
  415. @app.route('/connect_serial', methods=['POST'])
  416. def connect_serial():
  417. port = request.json.get('port')
  418. if not port:
  419. return jsonify({'error': 'No port provided'}), 400
  420. try:
  421. connect_to_serial(port)
  422. return jsonify({'success': True})
  423. except Exception as e:
  424. return jsonify({'error': str(e)}), 500
  425. @app.route('/disconnect_serial', methods=['POST'])
  426. def disconnect():
  427. try:
  428. disconnect_serial()
  429. return jsonify({'success': True})
  430. except Exception as e:
  431. return jsonify({'error': str(e)}), 500
  432. @app.route('/restart_serial', methods=['POST'])
  433. def restart():
  434. port = request.json.get('port')
  435. if not port:
  436. return jsonify({'error': 'No port provided'}), 400
  437. try:
  438. restart_serial(port)
  439. return jsonify({'success': True})
  440. except Exception as e:
  441. return jsonify({'error': str(e)}), 500
  442. @app.route('/list_theta_rho_files', methods=['GET'])
  443. def list_theta_rho_files():
  444. files = []
  445. for root, _, filenames in os.walk(THETA_RHO_DIR):
  446. for file in filenames:
  447. # Construct the relative file path
  448. relative_path = os.path.relpath(os.path.join(root, file), THETA_RHO_DIR)
  449. files.append(relative_path)
  450. return jsonify(sorted(files))
  451. @app.route('/upload_theta_rho', methods=['POST'])
  452. def upload_theta_rho():
  453. custom_patterns_dir = os.path.join(THETA_RHO_DIR, 'custom_patterns')
  454. os.makedirs(custom_patterns_dir, exist_ok=True) # Ensure the directory exists
  455. file = request.files['file']
  456. if file:
  457. file.save(os.path.join(custom_patterns_dir, file.filename))
  458. return jsonify({'success': True})
  459. return jsonify({'success': False})
  460. @app.route('/run_theta_rho', methods=['POST'])
  461. def run_theta_rho():
  462. file_name = request.json.get('file_name')
  463. pre_execution = request.json.get('pre_execution') # 'clear_in', 'clear_out', 'clear_sideway', or 'none'
  464. if not file_name:
  465. return jsonify({'error': 'No file name provided'}), 400
  466. file_path = os.path.join(THETA_RHO_DIR, file_name)
  467. if not os.path.exists(file_path):
  468. return jsonify({'error': 'File not found'}), 404
  469. try:
  470. # Build a list of files to run in sequence
  471. files_to_run = []
  472. if pre_execution == 'clear_in':
  473. files_to_run.append('./patterns/clear_from_in.thr')
  474. elif pre_execution == 'clear_out':
  475. files_to_run.append('./patterns/clear_from_out.thr')
  476. elif pre_execution == 'clear_sideway':
  477. files_to_run.append('./patterns/clear_sideway.thr')
  478. elif pre_execution == 'none':
  479. pass # No pre-execution action required
  480. # Finally, add the main file
  481. files_to_run.append(file_path)
  482. # Run them in one shot using run_theta_rho_files (blocking call)
  483. threading.Thread(
  484. target=run_theta_rho_files,
  485. args=(files_to_run,),
  486. kwargs={
  487. 'pause_time': 0,
  488. 'clear_pattern': None
  489. }
  490. ).start()
  491. return jsonify({'success': True})
  492. except Exception as e:
  493. return jsonify({'error': str(e)}), 500
  494. @app.route('/stop_execution', methods=['POST'])
  495. def stop_execution():
  496. global pause_requested
  497. with pause_condition:
  498. pause_requested = False
  499. pause_condition.notify_all()
  500. global stop_requested, current_playing_index, current_playlist, is_clearing, current_playing_file, execution_progress
  501. stop_requested = True
  502. current_playing_index = None
  503. current_playlist = None
  504. is_clearing = False
  505. current_playing_file = None
  506. execution_progress = None
  507. return jsonify({'success': True})
  508. @app.route('/send_home', methods=['POST'])
  509. def send_home():
  510. """Send the HOME command to the Arduino."""
  511. try:
  512. send_command("HOME")
  513. return jsonify({'success': True})
  514. except Exception as e:
  515. return jsonify({'error': str(e)}), 500
  516. @app.route('/run_theta_rho_file/<file_name>', methods=['POST'])
  517. def run_specific_theta_rho_file(file_name):
  518. """Run a specific theta-rho file."""
  519. file_path = os.path.join(THETA_RHO_DIR, file_name)
  520. if not os.path.exists(file_path):
  521. return jsonify({'error': 'File not found'}), 404
  522. threading.Thread(target=run_theta_rho_file, args=(file_path,)).start()
  523. return jsonify({'success': True})
  524. @app.route('/delete_theta_rho_file', methods=['POST'])
  525. def delete_theta_rho_file():
  526. data = request.json
  527. file_name = data.get('file_name')
  528. if not file_name:
  529. return jsonify({"success": False, "error": "No file name provided"}), 400
  530. file_path = os.path.join(THETA_RHO_DIR, file_name)
  531. if not os.path.exists(file_path):
  532. return jsonify({"success": False, "error": "File not found"}), 404
  533. try:
  534. os.remove(file_path)
  535. return jsonify({"success": True})
  536. except Exception as e:
  537. return jsonify({"success": False, "error": str(e)}), 500
  538. @app.route('/move_to_center', methods=['POST'])
  539. def move_to_center():
  540. """Move the sand table to the center position."""
  541. try:
  542. if ser is None or not ser.is_open:
  543. return jsonify({"success": False, "error": "Serial connection not established"}), 400
  544. coordinates = [(0, 0)] # Center position
  545. send_coordinate_batch(ser, coordinates)
  546. return jsonify({"success": True})
  547. except Exception as e:
  548. return jsonify({"success": False, "error": str(e)}), 500
  549. @app.route('/move_to_perimeter', methods=['POST'])
  550. def move_to_perimeter():
  551. """Move the sand table to the perimeter position."""
  552. try:
  553. if ser is None or not ser.is_open:
  554. return jsonify({"success": False, "error": "Serial connection not established"}), 400
  555. MAX_RHO = 1
  556. coordinates = [(0, MAX_RHO)] # Perimeter position
  557. send_coordinate_batch(ser, coordinates)
  558. return jsonify({"success": True})
  559. except Exception as e:
  560. return jsonify({"success": False, "error": str(e)}), 500
  561. @app.route('/preview_thr', methods=['POST'])
  562. def preview_thr():
  563. file_name = request.json.get('file_name')
  564. if not file_name:
  565. return jsonify({'error': 'No file name provided'}), 400
  566. file_path = os.path.join(THETA_RHO_DIR, file_name)
  567. if not os.path.exists(file_path):
  568. return jsonify({'error': 'File not found'}), 404
  569. try:
  570. # Parse the .thr file with transformations
  571. coordinates = parse_theta_rho_file(file_path)
  572. return jsonify({'success': True, 'coordinates': coordinates})
  573. except Exception as e:
  574. return jsonify({'error': str(e)}), 500
  575. @app.route('/send_coordinate', methods=['POST'])
  576. def send_coordinate():
  577. """Send a single (theta, rho) coordinate to the Arduino."""
  578. global ser
  579. if ser is None or not ser.is_open:
  580. return jsonify({"success": False, "error": "Serial connection not established"}), 400
  581. try:
  582. data = request.json
  583. theta = data.get('theta')
  584. rho = data.get('rho')
  585. if theta is None or rho is None:
  586. return jsonify({"success": False, "error": "Theta and Rho are required"}), 400
  587. # Send the coordinate to the Arduino
  588. send_coordinate_batch(ser, [(theta, rho)])
  589. reset_theta()
  590. return jsonify({"success": True})
  591. except Exception as e:
  592. return jsonify({"success": False, "error": str(e)}), 500
  593. # Expose files for download if needed
  594. @app.route('/download/<filename>', methods=['GET'])
  595. def download_file(filename):
  596. """Download a file from the theta-rho directory."""
  597. return send_from_directory(THETA_RHO_DIR, filename)
  598. @app.route('/serial_status', methods=['GET'])
  599. def serial_status():
  600. global ser, ser_port
  601. return jsonify({
  602. 'connected': ser.is_open if ser else False,
  603. 'port': ser_port # Include the port name
  604. })
  605. @app.route('/pause_execution', methods=['POST'])
  606. def pause_execution():
  607. """Pause the current execution."""
  608. global pause_requested
  609. with pause_condition:
  610. pause_requested = True
  611. return jsonify({'success': True, 'message': 'Execution paused'})
  612. @app.route('/status', methods=['GET'])
  613. def get_status():
  614. """Returns the current status of the sand table."""
  615. global is_clearing
  616. if current_playing_file in CLEAR_PATTERNS.values():
  617. is_clearing = True
  618. else:
  619. is_clearing = False
  620. return jsonify({
  621. "ser_port": ser_port,
  622. "stop_requested": stop_requested,
  623. "pause_requested": pause_requested,
  624. "current_playing_file": current_playing_file,
  625. "execution_progress": execution_progress,
  626. "current_playing_index": current_playing_index,
  627. "current_playlist": current_playlist,
  628. "is_clearing": is_clearing
  629. })
  630. @app.route('/resume_execution', methods=['POST'])
  631. def resume_execution():
  632. """Resume execution after pausing."""
  633. global pause_requested
  634. with pause_condition:
  635. pause_requested = False
  636. pause_condition.notify_all() # Unblock the waiting thread
  637. return jsonify({'success': True, 'message': 'Execution resumed'})
  638. def load_playlists():
  639. """
  640. Load the entire playlists dictionary from the JSON file.
  641. Returns something like: {
  642. "My Playlist": ["file1.thr", "file2.thr"],
  643. "Another": ["x.thr"]
  644. }
  645. """
  646. with open(PLAYLISTS_FILE, "r") as f:
  647. return json.load(f)
  648. def save_playlists(playlists_dict):
  649. """
  650. Save the entire playlists dictionary back to the JSON file.
  651. """
  652. with open(PLAYLISTS_FILE, "w") as f:
  653. json.dump(playlists_dict, f, indent=2)
  654. @app.route("/list_all_playlists", methods=["GET"])
  655. def list_all_playlists():
  656. """
  657. Returns a list of all playlist names.
  658. Example return: ["My Playlist", "Another Playlist"]
  659. """
  660. playlists_dict = load_playlists()
  661. playlist_names = list(playlists_dict.keys())
  662. return jsonify(playlist_names)
  663. @app.route("/get_playlist", methods=["GET"])
  664. def get_playlist():
  665. """
  666. GET /get_playlist?name=My%20Playlist
  667. Returns: { "name": "My Playlist", "files": [... ] }
  668. """
  669. playlist_name = request.args.get("name", "")
  670. if not playlist_name:
  671. return jsonify({"error": "Missing playlist 'name' parameter"}), 400
  672. playlists_dict = load_playlists()
  673. if playlist_name not in playlists_dict:
  674. return jsonify({"error": f"Playlist '{playlist_name}' not found"}), 404
  675. files = playlists_dict[playlist_name] # e.g. ["file1.thr", "file2.thr"]
  676. return jsonify({
  677. "name": playlist_name,
  678. "files": files
  679. })
  680. @app.route("/create_playlist", methods=["POST"])
  681. def create_playlist():
  682. """
  683. POST /create_playlist
  684. Body: { "name": "My Playlist", "files": ["file1.thr", "file2.thr"] }
  685. Creates or overwrites a playlist with the given name.
  686. """
  687. data = request.get_json()
  688. if not data or "name" not in data or "files" not in data:
  689. return jsonify({"success": False, "error": "Playlist 'name' and 'files' are required"}), 400
  690. playlist_name = data["name"]
  691. files = data["files"]
  692. # Load all playlists
  693. playlists_dict = load_playlists()
  694. # Overwrite or create new
  695. playlists_dict[playlist_name] = files
  696. # Save changes
  697. save_playlists(playlists_dict)
  698. return jsonify({
  699. "success": True,
  700. "message": f"Playlist '{playlist_name}' created/updated"
  701. })
  702. @app.route("/modify_playlist", methods=["POST"])
  703. def modify_playlist():
  704. """
  705. POST /modify_playlist
  706. Body: { "name": "My Playlist", "files": ["file1.thr", "file2.thr"] }
  707. Updates (or creates) the existing playlist with a new file list.
  708. You can 404 if you only want to allow modifications to existing playlists.
  709. """
  710. data = request.get_json()
  711. if not data or "name" not in data or "files" not in data:
  712. return jsonify({"success": False, "error": "Playlist 'name' and 'files' are required"}), 400
  713. playlist_name = data["name"]
  714. files = data["files"]
  715. # Load all playlists
  716. playlists_dict = load_playlists()
  717. # Optional: If you want to disallow creating a new playlist here:
  718. # if playlist_name not in playlists_dict:
  719. # return jsonify({"success": False, "error": f"Playlist '{playlist_name}' not found"}), 404
  720. # Overwrite or create new
  721. playlists_dict[playlist_name] = files
  722. # Save
  723. save_playlists(playlists_dict)
  724. return jsonify({"success": True, "message": f"Playlist '{playlist_name}' updated"})
  725. @app.route("/delete_playlist", methods=["DELETE"])
  726. def delete_playlist():
  727. """
  728. DELETE /delete_playlist
  729. Body: { "name": "My Playlist" }
  730. Removes the playlist from the single JSON file.
  731. """
  732. data = request.get_json()
  733. if not data or "name" not in data:
  734. return jsonify({"success": False, "error": "Missing 'name' field"}), 400
  735. playlist_name = data["name"]
  736. playlists_dict = load_playlists()
  737. if playlist_name not in playlists_dict:
  738. return jsonify({"success": False, "error": f"Playlist '{playlist_name}' not found"}), 404
  739. # Remove from dict
  740. del playlists_dict[playlist_name]
  741. save_playlists(playlists_dict)
  742. return jsonify({
  743. "success": True,
  744. "message": f"Playlist '{playlist_name}' deleted"
  745. })
  746. @app.route('/add_to_playlist', methods=['POST'])
  747. def add_to_playlist():
  748. data = request.json
  749. playlist_name = data.get('playlist_name')
  750. pattern = data.get('pattern')
  751. # Load existing playlists
  752. with open('playlists.json', 'r') as f:
  753. playlists = json.load(f)
  754. # Add pattern to the selected playlist
  755. if playlist_name in playlists:
  756. playlists[playlist_name].append(pattern)
  757. with open('playlists.json', 'w') as f:
  758. json.dump(playlists, f)
  759. return jsonify(success=True)
  760. else:
  761. return jsonify(success=False, error='Playlist not found'), 404
  762. @app.route("/run_playlist", methods=["POST"])
  763. def run_playlist():
  764. """
  765. POST /run_playlist
  766. Body (JSON):
  767. {
  768. "playlist_name": "My Playlist",
  769. "pause_time": 1.0, # Optional: seconds to pause between patterns
  770. "clear_pattern": "random", # Optional: "clear_in", "clear_out", "clear_sideway", or "random"
  771. "run_mode": "single", # 'single' or 'indefinite'
  772. "shuffle": True # true or false
  773. "start_time": ""
  774. "end_time": ""
  775. }
  776. """
  777. data = request.get_json()
  778. # Validate input
  779. if not data or "playlist_name" not in data:
  780. return jsonify({"success": False, "error": "Missing 'playlist_name' field"}), 400
  781. playlist_name = data["playlist_name"]
  782. pause_time = data.get("pause_time", 0)
  783. clear_pattern = data.get("clear_pattern", None)
  784. run_mode = data.get("run_mode", "single") # Default to 'single' run
  785. shuffle = data.get("shuffle", False) # Default to no shuffle
  786. start_time = data.get("start_time", None)
  787. end_time = data.get("end_time", None)
  788. # Validate pause_time
  789. if not isinstance(pause_time, (int, float)) or pause_time < 0:
  790. return jsonify({"success": False, "error": "'pause_time' must be a non-negative number"}), 400
  791. # Validate clear_pattern
  792. valid_patterns = ["clear_in", "clear_out", "clear_sideway", "random"]
  793. if clear_pattern not in valid_patterns:
  794. clear_pattern = None
  795. # Validate run_mode
  796. if run_mode not in ["single", "indefinite"]:
  797. return jsonify({"success": False, "error": "'run_mode' must be 'single' or 'indefinite'"}), 400
  798. # Validate shuffle
  799. if not isinstance(shuffle, bool):
  800. return jsonify({"success": False, "error": "'shuffle' must be a boolean value"}), 400
  801. schedule_hours = None
  802. if start_time and end_time:
  803. try:
  804. # Convert HH:MM to datetime.time objects
  805. start_time_obj = datetime.strptime(start_time, "%H:%M").time()
  806. end_time_obj = datetime.strptime(end_time, "%H:%M").time()
  807. # Ensure start_time is before end_time
  808. if start_time_obj >= end_time_obj:
  809. return jsonify({"success": False, "error": "'start_time' must be earlier than 'end_time'"}), 400
  810. # Create schedule tuple with full time
  811. schedule_hours = (start_time_obj, end_time_obj)
  812. except ValueError:
  813. return jsonify({"success": False, "error": "Invalid time format. Use HH:MM (e.g., '09:30')"}), 400
  814. # Load playlists
  815. playlists = load_playlists()
  816. if playlist_name not in playlists:
  817. return jsonify({"success": False, "error": f"Playlist '{playlist_name}' not found"}), 404
  818. file_paths = playlists[playlist_name]
  819. file_paths = [os.path.join(THETA_RHO_DIR, file) for file in file_paths]
  820. if not file_paths:
  821. return jsonify({"success": False, "error": f"Playlist '{playlist_name}' is empty"}), 400
  822. # Start the playlist execution in a separate thread
  823. try:
  824. threading.Thread(
  825. target=run_theta_rho_files,
  826. args=(file_paths,),
  827. kwargs={
  828. 'pause_time': pause_time,
  829. 'clear_pattern': clear_pattern,
  830. 'run_mode': run_mode,
  831. 'shuffle': shuffle,
  832. 'schedule_hours': schedule_hours
  833. },
  834. daemon=True # Daemonize thread to exit with the main program
  835. ).start()
  836. return jsonify({"success": True, "message": f"Playlist '{playlist_name}' is now running."})
  837. except Exception as e:
  838. return jsonify({"success": False, "error": str(e)}), 500
  839. @app.route('/set_speed', methods=['POST'])
  840. def set_speed():
  841. """Set the speed for the Arduino."""
  842. global ser
  843. if ser is None or not ser.is_open:
  844. return jsonify({"success": False, "error": "Serial connection not established"}), 400
  845. try:
  846. # Parse the speed value from the request
  847. data = request.json
  848. speed = data.get('speed')
  849. if speed is None:
  850. return jsonify({"success": False, "error": "Speed is required"}), 400
  851. if not isinstance(speed, (int, float)) or speed <= 0:
  852. return jsonify({"success": False, "error": "Invalid speed value"}), 400
  853. # Send the SET_SPEED command to the Arduino
  854. command = f"SET_SPEED {speed}"
  855. send_command(command)
  856. return jsonify({"success": True, "speed": speed})
  857. except Exception as e:
  858. return jsonify({"success": False, "error": str(e)}), 500
  859. @app.route('/get_firmware_info', methods=['GET', 'POST'])
  860. def get_firmware_info():
  861. """
  862. Compare the installed firmware version and motor type with the one in the .ino file.
  863. """
  864. global firmware_version, arduino_driver_type, ser
  865. if ser is None or not ser.is_open:
  866. return jsonify({"success": False, "error": "Arduino not connected or serial port not open"}), 400
  867. try:
  868. if request.method == "GET":
  869. # Attempt to retrieve installed firmware details from the Arduino
  870. ser.reset_input_buffer()
  871. ser.reset_output_buffer()
  872. ser.write(b"GET_VERSION\n")
  873. time.sleep(0.5)
  874. installed_version = firmware_version
  875. installed_type = arduino_driver_type
  876. # If Arduino provides valid details, proceed with comparison
  877. if installed_version != 'Unknown' and installed_type != 'Unknown':
  878. ino_path = MOTOR_TYPE_MAPPING.get(installed_type)
  879. firmware_details = get_ino_firmware_details(ino_path)
  880. if not firmware_details or not firmware_details.get("version") or not firmware_details.get("motorType"):
  881. return jsonify({"success": False, "error": "Failed to retrieve .ino firmware details"}), 500
  882. update_available = (
  883. installed_version != firmware_details["version"] or
  884. installed_type != firmware_details["motorType"]
  885. )
  886. return jsonify({
  887. "success": True,
  888. "installedVersion": installed_version,
  889. "installedType": installed_type,
  890. "inoVersion": firmware_details["version"],
  891. "inoType": firmware_details["motorType"],
  892. "updateAvailable": update_available
  893. })
  894. # If Arduino details are unknown, indicate the need for POST
  895. return jsonify({
  896. "success": True,
  897. "installedVersion": installed_version,
  898. "installedType": installed_type,
  899. "updateAvailable": False
  900. })
  901. elif request.method == "POST":
  902. motor_type = request.json.get("motorType", None)
  903. if not motor_type or motor_type not in MOTOR_TYPE_MAPPING:
  904. return jsonify({
  905. "success": False,
  906. "error": "Invalid or missing motor type"
  907. }), 400
  908. # Fetch firmware details for the given motor type
  909. ino_path = MOTOR_TYPE_MAPPING[motor_type]
  910. firmware_details = get_ino_firmware_details(ino_path)
  911. if not firmware_details:
  912. return jsonify({
  913. "success": False,
  914. "error": "Failed to retrieve .ino firmware details"
  915. }), 500
  916. return jsonify({
  917. "success": True,
  918. "installedVersion": 'Unknown',
  919. "installedType": motor_type,
  920. "inoVersion": firmware_details["version"],
  921. "inoType": firmware_details["motorType"],
  922. "updateAvailable": True
  923. })
  924. except Exception as e:
  925. return jsonify({"success": False, "error": str(e)}), 500
  926. @app.route('/flash_firmware', methods=['POST'])
  927. def flash_firmware():
  928. """
  929. Flash the pre-compiled firmware to the connected device (Arduino or ESP32).
  930. """
  931. global ser_port
  932. # Ensure the device is connected
  933. if ser_port is None or ser is None or not ser.is_open:
  934. return jsonify({"success": False, "error": "No device connected or connection lost"}), 400
  935. try:
  936. data = request.json
  937. motor_type = data.get("motorType", None)
  938. # Validate motor type
  939. if not motor_type or motor_type not in MOTOR_TYPE_MAPPING:
  940. return jsonify({"success": False, "error": "Invalid or missing motor type"}), 400
  941. # Determine the firmware file
  942. ino_file_path = MOTOR_TYPE_MAPPING[motor_type] # Path to .ino file
  943. hex_file_path = f"{ino_file_path}.hex"
  944. bin_file_path = f"{ino_file_path}.bin" # For ESP32 firmware
  945. # Check the device type
  946. if motor_type.lower() == "esp32":
  947. if not os.path.exists(bin_file_path):
  948. return jsonify({"success": False, "error": f"Firmware binary not found: {bin_file_path}"}), 404
  949. # Flash ESP32 firmware
  950. flash_command = [
  951. "esptool.py",
  952. "--chip", "esp32",
  953. "--port", ser_port,
  954. "--baud", "115200",
  955. "write_flash", "-z", "0x1000", bin_file_path
  956. ]
  957. else:
  958. if not os.path.exists(hex_file_path):
  959. return jsonify({"success": False, "error": f"Hex file not found: {hex_file_path}"}), 404
  960. # Flash Arduino firmware
  961. flash_command = [
  962. "avrdude",
  963. "-v",
  964. "-c", "arduino",
  965. "-p", "atmega328p",
  966. "-P", ser_port,
  967. "-b", "115200",
  968. "-D",
  969. "-U", f"flash:w:{hex_file_path}:i"
  970. ]
  971. # Execute the flash command
  972. flash_process = subprocess.run(flash_command, capture_output=True, text=True)
  973. if flash_process.returncode != 0:
  974. return jsonify({
  975. "success": False,
  976. "error": flash_process.stderr
  977. }), 500
  978. return jsonify({"success": True, "message": "Firmware flashed successfully"})
  979. except Exception as e:
  980. return jsonify({"success": False, "error": str(e)}), 500
  981. @app.route('/check_software_update', methods=['GET'])
  982. def check_updates():
  983. update_info = check_git_updates()
  984. return jsonify(update_info)
  985. @app.route('/update_software', methods=['POST'])
  986. def update_software():
  987. error_log = []
  988. def run_command(command, error_message):
  989. try:
  990. subprocess.run(command, check=True)
  991. except subprocess.CalledProcessError as e:
  992. print(f"{error_message}: {e}")
  993. error_log.append(error_message)
  994. # Fetch the latest version tag from remote
  995. try:
  996. subprocess.run(["git", "fetch", "--tags"], check=True)
  997. latest_remote_tag = subprocess.check_output(
  998. ["git", "describe", "--tags", "--abbrev=0", "origin/main"]
  999. ).strip().decode()
  1000. except subprocess.CalledProcessError as e:
  1001. error_log.append(f"Failed to fetch tags or get latest remote tag: {e}")
  1002. return jsonify({
  1003. "success": False,
  1004. "error": "Failed to fetch tags or determine the latest version.",
  1005. "details": error_log
  1006. }), 500
  1007. # Checkout the latest tag
  1008. run_command(["git", "checkout", latest_remote_tag, '--force'], f"Failed to checkout version {latest_remote_tag}")
  1009. # Restart Docker containers
  1010. run_command(["docker", "compose", "up", "-d"], "Failed to restart Docker containers")
  1011. # Check if the update was successful
  1012. update_status = check_git_updates()
  1013. if (
  1014. update_status["updates_available"] is False
  1015. and update_status["latest_local_tag"] == update_status["latest_remote_tag"]
  1016. ):
  1017. # Update was successful
  1018. return jsonify({"success": True})
  1019. else:
  1020. # Update failed; include the errors in the response
  1021. return jsonify({
  1022. "success": False,
  1023. "error": "Update incomplete",
  1024. "details": error_log
  1025. }), 500
  1026. if __name__ == '__main__':
  1027. # Auto-connect to serial
  1028. connect_to_serial()
  1029. app.run(debug=False, host='0.0.0.0', port=8080)