index.html 20 KB

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