index.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Sand Table Controller</title>
  7. <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
  8. <link rel="stylesheet" href="../static/style.css">
  9. </head>
  10. <body>
  11. <h1>Sand Table Controller</h1>
  12. <div class="container">
  13. <!-- Left Column -->
  14. <div class="left-column">
  15. <div class="section">
  16. <h2>Serial Connection</h2>
  17. <label for="serial_ports">Available Ports:</label>
  18. <select id="serial_ports"></select>
  19. <div class="button-group">
  20. <button onclick="connectSerial()">Connect</button>
  21. <button onclick="disconnectSerial()">Disconnect</button>
  22. <button onclick="restartSerial()">Restart</button>
  23. </div>
  24. <p id="serial_status" class="status">Status: Not connected</p>
  25. <input type="number" id="speed_input" placeholder="Set speed" min="1" step="1">
  26. <button onclick="changeSpeed()">Set Speed</button>
  27. </div>
  28. <div class="section">
  29. <h2>Quick Actions</h2>
  30. <div class="button-group">
  31. <button onclick="sendHomeCommand()">Home Device</button>
  32. <button onclick="moveToCenter()">Move to Center</button>
  33. <button onclick="moveToPerimeter()">Move to Perimeter</button>
  34. </br>
  35. <button onclick="runClearIn()">Clear from In</button>
  36. <button onclick="runClearOut()">Clear from Out</button>
  37. <div class="coordinate-input">
  38. <label for="theta_input">θ:</label>
  39. <input type="number" id="theta_input" placeholder="Theta">
  40. <label for="rho_input">ρ:</label>
  41. <input type="number" id="rho_input" placeholder="Rho">
  42. <button onclick="sendCoordinate()">Send</button>
  43. </div>
  44. </div>
  45. </div>
  46. <div class="section">
  47. <h2>Preview</h2>
  48. <canvas id="patternPreviewCanvas" style="width: 100%; height: 100%;"></canvas>
  49. <p id="first_coordinate">First Coordinate: Not available</p>
  50. <p id="last_coordinate">Last Coordinate: Not available</p>
  51. </div>
  52. </div>
  53. <!-- Right Column -->
  54. <div class="right-column">
  55. <div class="section">
  56. <h2>Pattern Files</h2>
  57. <input type="text" id="search_pattern" placeholder="Search files..." oninput="searchPatternFiles()">
  58. <ul id="theta_rho_files"></ul>
  59. <div class="pre-execution-toggles">
  60. <h3>Pre-Execution Action</h3>
  61. <label>
  62. <input type="radio" name="pre_execution" value="clear_in" id="clear_in"> Clear from In
  63. </label>
  64. <label>
  65. <input type="radio" name="pre_execution" value="clear_out" id="clear_out"> Clear from Out
  66. </label>
  67. <label>
  68. <input type="radio" name="pre_execution" value="none" id="no_action" checked> None
  69. </label>
  70. </div>
  71. <div class="button-group">
  72. <button id="run_button" disabled>Run Selected File</button>
  73. <button onclick="stopExecution()" class="delete-button">Stop</button>
  74. </div>
  75. <div class="section">
  76. <h2>Playlist Management</h2>
  77. <div>
  78. <label for="playlist_name">Playlist Name:</label>
  79. <input type="text" id="playlist_name" placeholder="Enter playlist name">
  80. </div>
  81. <div>
  82. <label for="selected_playlist">Select Existing Playlist:</label>
  83. <select id="selected_playlist" onchange="loadPlaylistFiles()">
  84. <option value="" disabled selected>Select a playlist</option>
  85. </select>
  86. </div>
  87. <ul id="selected_files_for_playlist">
  88. <!-- List of selected files for the playlist -->
  89. </ul>
  90. <div class="button-group">
  91. <button onclick="addSelectedFileToPlaylist()">Add Selected File</button>
  92. <button onclick="savePlaylist()">Save Playlist</button>
  93. <button onclick="deletePlaylist()">Delete Playlist</button>
  94. </div>
  95. </div>
  96. </div>
  97. <div class="section">
  98. <h2>Upload new files</h2>
  99. <div class="button-group">
  100. <input type="file" id="upload_file">
  101. <div class="button-group">
  102. <button onclick="uploadThetaRho()">Upload</button>
  103. <button id="delete_selected_button" class="delete-button" onclick="deleteSelectedFile()" disabled>Delete Selected File</button>
  104. </div>
  105. </div>
  106. </div>
  107. </div>
  108. </div>
  109. </div>
  110. <div id="status_log">
  111. <h2>Status Log</h2>
  112. <!-- Messages will be appended here -->
  113. </div>
  114. <script>
  115. let selectedFile = null;
  116. function logMessage(message) {
  117. const log = document.getElementById('status_log');
  118. const entry = document.createElement('p');
  119. entry.textContent = message;
  120. log.appendChild(entry);
  121. log.scrollTop = log.scrollHeight; // Keep log scrolled to the bottom
  122. }
  123. async function loadThetaRhoFiles() {
  124. logMessage('Loading Theta-Rho files...');
  125. const response = await fetch('/list_theta_rho_files');
  126. const files = await response.json();
  127. const ul = document.getElementById('theta_rho_files');
  128. ul.innerHTML = ''; // Clear current list
  129. files.forEach(file => {
  130. const li = document.createElement('li');
  131. li.textContent = file;
  132. // Highlight the selected file when clicked
  133. li.onclick = () => selectFile(file, li);
  134. ul.appendChild(li);
  135. });
  136. logMessage('Theta-Rho files loaded successfully.');
  137. }
  138. async function selectFile(file, listItem) {
  139. selectedFile = file;
  140. // Highlight the selected file
  141. document.querySelectorAll('#theta_rho_files li').forEach(li => li.classList.remove('selected'));
  142. listItem.classList.add('selected');
  143. // Enable buttons
  144. document.getElementById('run_button').disabled = false;
  145. document.getElementById('delete_selected_button').disabled = false;
  146. logMessage(`Selected file: ${file}`);
  147. // Fetch and preview the selected file
  148. await previewPattern(file);
  149. }
  150. async function uploadThetaRho() {
  151. const fileInput = document.getElementById('upload_file');
  152. const file = fileInput.files[0];
  153. if (!file) {
  154. logMessage('No file selected for upload.');
  155. return;
  156. }
  157. logMessage(`Uploading file: ${file.name}...`);
  158. const formData = new FormData();
  159. formData.append('file', file);
  160. const response = await fetch('/upload_theta_rho', {
  161. method: 'POST',
  162. body: formData
  163. });
  164. const result = await response.json();
  165. if (result.success) {
  166. logMessage(`File uploaded successfully: ${file.name}`);
  167. loadThetaRhoFiles();
  168. } else {
  169. logMessage(`Failed to upload file: ${file.name}`);
  170. }
  171. }
  172. async function deleteSelectedFile() {
  173. if (!selectedFile) {
  174. logMessage("No file selected for deletion.");
  175. return;
  176. }
  177. const userConfirmed = confirm(`Are you sure you want to delete the selected file "${selectedFile}"?`);
  178. if (!userConfirmed) return;
  179. logMessage(`Deleting file: ${selectedFile}...`);
  180. const response = await fetch('/delete_theta_rho_file', {
  181. method: 'POST',
  182. headers: { 'Content-Type': 'application/json' },
  183. body: JSON.stringify({ file_name: selectedFile }),
  184. });
  185. const result = await response.json();
  186. if (result.success) {
  187. const ul = document.getElementById('theta_rho_files');
  188. const selectedItem = Array.from(ul.children).find(li => li.classList.contains('selected'));
  189. if (selectedItem) selectedItem.remove();
  190. selectedFile = null;
  191. document.getElementById('run_button').disabled = true;
  192. document.getElementById('delete_selected_button').disabled = true;
  193. logMessage(`File deleted successfully: ${result.file_name}`);
  194. } else {
  195. logMessage(`Failed to delete file: ${selectedFile}`);
  196. }
  197. }
  198. async function runThetaRho() {
  199. if (!selectedFile) {
  200. logMessage("No file selected to run.");
  201. return;
  202. }
  203. // Get the selected pre-execution action
  204. const preExecutionAction = document.querySelector('input[name="pre_execution"]:checked').value;
  205. logMessage(`Running file: ${selectedFile} with pre-execution action: ${preExecutionAction}...`);
  206. const response = await fetch('/run_theta_rho', {
  207. method: 'POST',
  208. headers: { 'Content-Type': 'application/json' },
  209. body: JSON.stringify({ file_name: selectedFile, pre_execution: preExecutionAction })
  210. });
  211. const result = await response.json();
  212. if (result.success) {
  213. logMessage(`File running: ${selectedFile}`);
  214. } else {
  215. logMessage(`Failed to run file: ${selectedFile}`);
  216. }
  217. }
  218. async function stopExecution() {
  219. logMessage('Stopping execution...');
  220. const response = await fetch('/stop_execution', { method: 'POST' });
  221. const result = await response.json();
  222. if (result.success) {
  223. logMessage('Execution stopped.');
  224. } else {
  225. logMessage('Failed to stop execution.');
  226. }
  227. }
  228. async function loadSerialPorts() {
  229. const response = await fetch('/list_serial_ports');
  230. const ports = await response.json();
  231. const select = document.getElementById('serial_ports');
  232. select.innerHTML = '';
  233. ports.forEach(port => {
  234. const option = document.createElement('option');
  235. option.value = port;
  236. option.textContent = port;
  237. select.appendChild(option);
  238. });
  239. logMessage('Serial ports loaded.');
  240. }
  241. async function connectSerial() {
  242. const port = document.getElementById('serial_ports').value;
  243. const response = await fetch('/connect_serial', {
  244. method: 'POST',
  245. headers: { 'Content-Type': 'application/json' },
  246. body: JSON.stringify({ port })
  247. });
  248. const result = await response.json();
  249. if (result.success) {
  250. document.getElementById('serial_status').textContent = `Status: Connected to ${port}`;
  251. logMessage(`Connected to serial port: ${port}`);
  252. } else {
  253. logMessage(`Error connecting to serial port: ${result.error}`);
  254. }
  255. }
  256. async function disconnectSerial() {
  257. const response = await fetch('/disconnect_serial', { method: 'POST' });
  258. const result = await response.json();
  259. if (result.success) {
  260. document.getElementById('serial_status').textContent = 'Status: Disconnected';
  261. logMessage('Serial port disconnected.');
  262. } else {
  263. logMessage(`Error disconnecting: ${result.error}`);
  264. }
  265. }
  266. async function restartSerial() {
  267. const port = document.getElementById('serial_ports').value;
  268. const response = await fetch('/restart_serial', {
  269. method: 'POST',
  270. headers: { 'Content-Type': 'application/json' },
  271. body: JSON.stringify({ port })
  272. });
  273. const result = await response.json();
  274. if (result.success) {
  275. document.getElementById('serial_status').textContent = `Status: Restarted connection to ${port}`;
  276. logMessage('Serial connection restarted.');
  277. } else {
  278. logMessage(`Error restarting serial connection: ${result.error}`);
  279. }
  280. }
  281. async function sendHomeCommand() {
  282. const response = await fetch('/send_home', { method: 'POST' });
  283. const result = await response.json();
  284. if (result.success) {
  285. logMessage('HOME command sent successfully.');
  286. } else {
  287. logMessage('Failed to send HOME command.');
  288. }
  289. }
  290. async function runClearIn() {
  291. await runFile('clear_from_in.thr');
  292. }
  293. async function runClearOut() {
  294. await runFile('clear_from_out.thr');
  295. }
  296. async function runFile(fileName) {
  297. const response = await fetch(`/run_theta_rho_file/${fileName}`, { method: 'POST' });
  298. const result = await response.json();
  299. if (result.success) {
  300. logMessage(`Running file: ${fileName}`);
  301. } else {
  302. logMessage(`Failed to run file: ${fileName}`);
  303. }
  304. }
  305. let allFiles = []; // Store all files for filtering
  306. async function loadThetaRhoFiles() {
  307. logMessage('Loading Theta-Rho files...');
  308. const response = await fetch('/list_theta_rho_files');
  309. const files = await response.json();
  310. allFiles = files; // Store the full list of files
  311. displayFiles(allFiles); // Initially display all files
  312. logMessage('Theta-Rho files loaded successfully.');
  313. }
  314. function displayFiles(files) {
  315. const ul = document.getElementById('theta_rho_files');
  316. ul.innerHTML = ''; // Clear current list
  317. files.forEach(file => {
  318. const li = document.createElement('li');
  319. li.textContent = file;
  320. // Highlight the selected file when clicked
  321. li.onclick = () => selectFile(file, li);
  322. ul.appendChild(li);
  323. });
  324. }
  325. function searchPatternFiles() {
  326. const searchInput = document.getElementById('search_pattern').value.toLowerCase();
  327. const filteredFiles = allFiles.filter(file => file.toLowerCase().includes(searchInput));
  328. displayFiles(filteredFiles); // Display only matching files
  329. }
  330. async function moveToCenter() {
  331. logMessage('Moving to center...');
  332. const response = await fetch('/move_to_center', { method: 'POST' });
  333. const result = await response.json();
  334. if (result.success) {
  335. logMessage('Moved to center successfully.');
  336. } else {
  337. logMessage(`Failed to move to center: ${result.error}`);
  338. }
  339. }
  340. async function moveToPerimeter() {
  341. logMessage('Moving to perimeter...');
  342. const response = await fetch('/move_to_perimeter', { method: 'POST' });
  343. const result = await response.json();
  344. if (result.success) {
  345. logMessage('Moved to perimeter successfully.');
  346. } else {
  347. logMessage(`Failed to move to perimeter: ${result.error}`);
  348. }
  349. }
  350. async function sendCoordinate() {
  351. const theta = parseFloat(document.getElementById('theta_input').value);
  352. const rho = parseFloat(document.getElementById('rho_input').value);
  353. if (isNaN(theta) || isNaN(rho)) {
  354. logMessage('Invalid input: θ and ρ must be numbers.');
  355. return;
  356. }
  357. logMessage(`Sending coordinate: θ=${theta}, ρ=${rho}...`);
  358. const response = await fetch('/send_coordinate', {
  359. method: 'POST',
  360. headers: { 'Content-Type': 'application/json' },
  361. body: JSON.stringify({ theta, rho })
  362. });
  363. const result = await response.json();
  364. if (result.success) {
  365. logMessage(`Coordinate executed successfully: θ=${theta}, ρ=${rho}`);
  366. } else {
  367. logMessage(`Failed to execute coordinate: ${result.error}`);
  368. }
  369. }
  370. async function previewPattern(fileName) {
  371. logMessage(`Fetching data to preview file: ${fileName}...`);
  372. const response = await fetch('/preview_thr', {
  373. method: 'POST',
  374. headers: { 'Content-Type': 'application/json' },
  375. body: JSON.stringify({ file_name: fileName })
  376. });
  377. const result = await response.json();
  378. if (result.success) {
  379. const coordinates = result.coordinates;
  380. // Update coordinates display
  381. if (coordinates.length > 0) {
  382. const firstCoord = coordinates[0];
  383. const lastCoord = coordinates[coordinates.length - 1];
  384. document.getElementById('first_coordinate').textContent = `First Coordinate: θ=${firstCoord[0]}, ρ=${firstCoord[1]}`;
  385. document.getElementById('last_coordinate').textContent = `Last Coordinate: θ=${lastCoord[0]}, ρ=${lastCoord[1]}`;
  386. } else {
  387. document.getElementById('first_coordinate').textContent = 'First Coordinate: Not available';
  388. document.getElementById('last_coordinate').textContent = 'Last Coordinate: Not available';
  389. }
  390. renderPattern(coordinates);
  391. } else {
  392. logMessage(`Failed to fetch preview for file: ${result.error}`);
  393. // Clear the coordinate display on error
  394. document.getElementById('first_coordinate').textContent = 'First Coordinate: Not available';
  395. document.getElementById('last_coordinate').textContent = 'Last Coordinate: Not available';
  396. }
  397. }
  398. function renderPattern(coordinates) {
  399. const canvas = document.getElementById('patternPreviewCanvas');
  400. const ctx = canvas.getContext('2d');
  401. // Make canvas full screen
  402. canvas.width = window.innerWidth;
  403. canvas.height = window.innerHeight;
  404. // Clear the canvas
  405. ctx.clearRect(0, 0, canvas.width, canvas.height);
  406. // Convert polar to Cartesian and draw the pattern
  407. const centerX = canvas.width / 2;
  408. const centerY = canvas.height / 2;
  409. const maxRho = Math.max(...coordinates.map(coord => coord[1]));
  410. const scale = Math.min(canvas.width, canvas.height) / (2 * maxRho); // Scale to fit within the screen
  411. ctx.beginPath();
  412. coordinates.forEach(([theta, rho], index) => {
  413. const x = centerX + rho * Math.cos(theta) * scale;
  414. const y = centerY - rho * Math.sin(theta) * scale; // Invert y-axis for canvas
  415. if (index === 0) {
  416. ctx.moveTo(x, y);
  417. } else {
  418. ctx.lineTo(x, y);
  419. }
  420. });
  421. ctx.closePath();
  422. ctx.stroke();
  423. logMessage('Pattern preview rendered at full screen.');
  424. }
  425. async function checkSerialStatus() {
  426. const response = await fetch('/serial_status');
  427. const status = await response.json();
  428. const statusElement = document.getElementById('serial_status');
  429. if (status.connected) {
  430. const port = status.port || 'Unknown'; // Fallback if port is undefined
  431. statusElement.textContent = `Status: Connected to ${port}`;
  432. logMessage(`Reconnected to serial port: ${port}`);
  433. } else {
  434. statusElement.textContent = 'Status: Not connected';
  435. logMessage('No active serial connection.');
  436. }
  437. }
  438. async function changeSpeed() {
  439. const speedInput = document.getElementById('speed_input');
  440. const speed = parseFloat(speedInput.value);
  441. if (isNaN(speed) || speed <= 0) {
  442. logMessage('Invalid speed. Please enter a positive number.');
  443. return;
  444. }
  445. logMessage(`Setting speed to: ${speed}...`);
  446. const response = await fetch('/set_speed', {
  447. method: 'POST',
  448. headers: { 'Content-Type': 'application/json' },
  449. body: JSON.stringify({ speed })
  450. });
  451. const result = await response.json();
  452. if (result.success) {
  453. document.getElementById('speed_status').textContent = `Current Speed: ${speed}`;
  454. logMessage(`Speed set to: ${speed}`);
  455. } else {
  456. logMessage(`Failed to set speed: ${result.error}`);
  457. }
  458. }
  459. // Call this function on page load
  460. checkSerialStatus();
  461. // Initial load of serial ports and Theta-Rho files
  462. loadSerialPorts();
  463. loadThetaRhoFiles();
  464. document.getElementById('run_button').onclick = runThetaRho;
  465. </script>
  466. </body>
  467. </html>