Files
led-controller/web/static/settings_editor.js
jimmyandClaude Haiku 5.5 50ca46fee6 feat: sequences v2, driver frame sync and submodule updates
- Sequences v2, bridges and sim-controller changes from the working tree.
- Records the led-driver, led-tool and led-simulator commits above.
- Includes dmx-driver/ and graphify-out/.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-11-11 14:10:56 +13:00

93 lines
3.0 KiB
JavaScript

(function () {
const messageEl = document.getElementById('flash');
function flash(text, isError) {
if (!messageEl) return;
messageEl.textContent = text;
messageEl.className = isError ? 'flash error show' : 'flash show';
setTimeout(() => messageEl.classList.remove('show'), 5000);
}
function errorMessage(e) {
if (!e) return 'Unknown error';
if (typeof e === 'string') return e;
return e.message || String(e);
}
// ESP-NOW: PUT /devices/<id> stores the fields and sends them to the driver in one message
const espnowDevice = document.getElementById('espnow-device');
const espnowSend = document.getElementById('espnow-send');
const espnowFields = document.querySelectorAll('[data-espnow]');
let espnowDevices = {};
function showEspnowDevice() {
const dev = espnowDevices[espnowDevice.value] || {};
espnowFields.forEach((el) => {
const v = dev[el.getAttribute('data-espnow')];
el.value = v === undefined || v === null ? '' : String(v);
});
}
async function loadEspnowDevices() {
const res = await fetch('/devices');
if (!res.ok) throw new Error(`Could not list drivers (${res.status})`);
const all = await res.json();
espnowDevices = {};
espnowDevice.innerHTML = '';
for (const [id, dev] of Object.entries(all)) {
if (dev.transport !== 'espnow') continue;
espnowDevices[id] = dev;
const opt = document.createElement('option');
opt.value = id;
opt.textContent = `${dev.name || id} (${id})`;
espnowDevice.appendChild(opt);
}
espnowSend.disabled = !espnowDevice.options.length;
showEspnowDevice();
}
function espnowChanges() {
const dev = espnowDevices[espnowDevice.value] || {};
const out = {};
espnowFields.forEach((el) => {
const key = el.getAttribute('data-espnow');
const raw = el.value.trim();
if (raw === '' || raw === String(dev[key] ?? '')) return;
if (el.type === 'number') out[key] = parseInt(raw, 10);
else if (key === 'reversed') out[key] = raw === 'true';
else out[key] = raw;
});
return out;
}
espnowDevice.addEventListener('change', showEspnowDevice);
espnowSend.addEventListener('click', async () => {
const id = espnowDevice.value;
const body = espnowChanges();
if (!Object.keys(body).length) {
flash('Nothing has changed.', true);
return;
}
espnowSend.disabled = true;
try {
const res = await fetch(`/devices/${encodeURIComponent(id)}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error || `Send failed (${res.status})`);
espnowDevices[id] = data;
espnowDevice.selectedOptions[0].textContent = `${data.name || id} (${id})`;
flash(`Sent to ${data.name || id}.`, false);
} catch (e) {
flash(errorMessage(e), true);
} finally {
espnowSend.disabled = false;
}
});
loadEspnowDevices().catch((e) => flash(e.message, true));
})();