|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Entity Reader/Writer</title> |
| 6 | + <style> |
| 7 | + body { font-family: sans-serif; padding: 20px; } |
| 8 | + .controls { display: flex; align-items: center; gap: 20px; margin: 20px; } |
| 9 | + </style> |
| 10 | +</head> |
| 11 | +<body> |
| 12 | + <div class="controls"> |
| 13 | + <select id="select_read"> |
| 14 | + <option value="" disabled selected>Loading…</option> |
| 15 | + </select> |
| 16 | + <input id="input_read" type="text" disabled/> |
| 17 | + <button id="button_read">Read</button> |
| 18 | + </div> |
| 19 | + <div class="controls"> |
| 20 | + <select id="select_write"> |
| 21 | + <option value="" disabled selected>Loading…</option> |
| 22 | + </select> |
| 23 | + <input id="input_write" type="text"/> |
| 24 | + <button id="button_write">Write</button> |
| 25 | + </div> |
| 26 | + |
| 27 | + <script> |
| 28 | + // Populate dropdown on page load |
| 29 | + const baseURL = "https://brickserver.ucsd.edu/brickapi/v1/apps/api"; |
| 30 | + const params = new URL(document.location).searchParams; |
| 31 | + const token = params.get("token"); |
| 32 | + const headers = { |
| 33 | + "Authorization": `Bearer ${token}` |
| 34 | + }; |
| 35 | + |
| 36 | + function initSelect(id, entities) { |
| 37 | + const sel = document.getElementById(id); |
| 38 | + sel.innerHTML = ''; // clear "Loading…" placeholder |
| 39 | + entities.forEach(e => { |
| 40 | + const opt = document.createElement('option'); |
| 41 | + opt.value = e; |
| 42 | + opt.textContent = e; |
| 43 | + sel.appendChild(opt); |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + async function loadEntities() { |
| 48 | + try { |
| 49 | + const resp = await fetch(`${baseURL}/list`, {headers: headers}); |
| 50 | + if (!resp.ok) throw new Error('Network response was not ok'); |
| 51 | + const result = await resp.json(); // expect ["ent1", "ent2", ...] |
| 52 | + initSelect("select_read", result.data.read[""].concat(result.data.write[""])); |
| 53 | + initSelect("select_write", result.data.write[""]); |
| 54 | + |
| 55 | + } catch (err) { |
| 56 | + console.error(err); |
| 57 | + alert('Failed to load entities.'); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Call read or write API with the selected entity |
| 62 | + async function callApi(action) { |
| 63 | + const entity = document.getElementById(`select_${action}`).value; |
| 64 | + if (!entity) { |
| 65 | + alert('Select an entity first.'); |
| 66 | + return; |
| 67 | + } |
| 68 | + try { |
| 69 | + let url = `${baseURL}/${action}?entity_id=${entity}`; |
| 70 | + if (action === "write") { |
| 71 | + const value = document.getElementById("input_write").value; |
| 72 | + url += `&value=${value}`; |
| 73 | + } |
| 74 | + const resp = await fetch(url, {headers: headers}); |
| 75 | + if (!resp.ok) throw new Error(`${action} failed`); |
| 76 | + const result = await resp.json(); |
| 77 | + console.log(result); |
| 78 | + if (action === "read") { |
| 79 | + document.getElementById("input_read").value = result.data.results[0].detail; |
| 80 | + } |
| 81 | + alert(`${action} succeeded: ${JSON.stringify(result)}`); |
| 82 | + } catch (err) { |
| 83 | + console.error(err); |
| 84 | + alert(`${action} error: ${err.message}`); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + document.getElementById('button_read').addEventListener('click', () => callApi('read')); |
| 89 | + document.getElementById('button_write').addEventListener('click', () => callApi('write')); |
| 90 | + window.addEventListener('DOMContentLoaded', loadEntities); |
| 91 | + </script> |
| 92 | +</body> |
| 93 | +</html> |
0 commit comments