Files
led-controller/web/static/outputs.js
T
jimmyandClaude Opus 5.5 1c222771a5 refactor: move the v2 controller to the repository root
The ESP-IDF project, docs, tools, tests and web UI move from v2/ to the
root, and the Makefile, README, build scripts and test runner drop the
v2/ prefix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 23:44:58 +13:00

220 lines
9.0 KiB
JavaScript

// Pixel outputs (db/output.json): DDP, Art-Net, sACN, USB-DMX fixtures the Pi renders for.
const OUTPUT_PROTOCOL_LABELS = {
ddp: 'DDP',
artnet: 'Art-Net',
sacn: 'sACN',
dmx_serial: 'USB-DMX',
};
async function fetchOutputs() {
const res = await fetch('/outputs', { headers: { Accept: 'application/json' } });
if (!res.ok) throw new Error('Failed to load outputs');
return res.json();
}
function outputSummary(out) {
const proto = OUTPUT_PROTOCOL_LABELS[out.protocol] || out.protocol;
let where = '';
if (out.protocol === 'dmx_serial') {
where = `${out.serial_port || '?'} ch ${out.channel || 1}`;
} else {
where = out.host || (out.protocol === 'sacn' ? 'multicast' : 'broadcast');
if (out.port) where += `:${out.port}`;
if (out.protocol !== 'ddp' && out.universe != null) where += ` u${out.universe}`;
}
return `${proto} · ${where} · ${out.pixel_count} px${out.enabled === false ? ' · disabled' : ''}`;
}
function renderOutputsList(outputs) {
const container = document.getElementById('outputs-list-modal');
if (!container) return;
container.innerHTML = '';
const ids = Object.keys(outputs || {});
if (ids.length === 0) {
const p = document.createElement('p');
p.className = 'muted-text';
p.textContent = 'No outputs yet.';
container.appendChild(p);
return;
}
ids.forEach((id) => {
const out = outputs[id];
const row = document.createElement('div');
row.className = 'profiles-row';
row.style.display = 'flex';
row.style.alignItems = 'center';
row.style.gap = '0.5rem';
row.style.flexWrap = 'wrap';
const label = document.createElement('span');
label.style.flex = '1';
label.innerHTML = '';
const name = document.createElement('strong');
name.textContent = out.name;
const meta = document.createElement('span');
meta.className = 'muted-text';
meta.textContent = ` ${outputSummary(out)}`;
label.appendChild(name);
label.appendChild(meta);
const testBtn = document.createElement('button');
testBtn.type = 'button';
testBtn.className = 'btn btn-secondary btn-small';
testBtn.textContent = 'Test';
testBtn.title = 'Red, green, blue, white, then black';
testBtn.addEventListener('click', async () => {
testBtn.disabled = true;
testBtn.textContent = 'Testing…';
try {
const res = await fetch(`/outputs/${encodeURIComponent(id)}/test`, { method: 'POST' });
const data = await res.json().catch(() => ({}));
if (!res.ok) throw new Error(data.error || res.statusText);
const st = data.status || {};
testBtn.textContent = st.errors ? `Errors: ${st.errors}` : 'Sent';
if (st.last_error) testBtn.title = st.last_error;
} catch (e) {
testBtn.textContent = 'Failed';
testBtn.title = String(e.message || e);
} finally {
testBtn.disabled = false;
setTimeout(() => { testBtn.textContent = 'Test'; }, 2500);
}
});
const editBtn = document.createElement('button');
editBtn.type = 'button';
editBtn.className = 'btn btn-secondary btn-small';
editBtn.textContent = 'Edit';
editBtn.addEventListener('click', () => openOutputEditor(id, out));
const delBtn = document.createElement('button');
delBtn.type = 'button';
delBtn.className = 'btn btn-danger btn-small';
delBtn.textContent = 'Delete';
delBtn.addEventListener('click', async () => {
if (!confirm(`Delete output "${out.name}"?`)) return;
const res = await fetch(`/outputs/${encodeURIComponent(id)}`, { method: 'DELETE' });
if (!res.ok) {
alert('Failed to delete output.');
return;
}
loadOutputsModal();
});
row.append(label, testBtn, editBtn, delBtn);
container.appendChild(row);
});
}
async function loadOutputsModal() {
const container = document.getElementById('outputs-list-modal');
try {
renderOutputsList(await fetchOutputs());
} catch (e) {
if (container) container.textContent = 'Failed to load outputs.';
}
}
function syncOutputProtocolFields() {
const proto = document.getElementById('edit-output-protocol').value;
document.querySelectorAll('#edit-output-form .engine-field').forEach((el) => {
const list = (el.dataset.protocols || '').split(/\s+/);
el.hidden = !list.includes(proto);
});
}
function setOutputField(id, value) {
const el = document.getElementById(id);
if (!el) return;
if (el.type === 'checkbox') el.checked = !!value;
else el.value = value == null ? '' : value;
}
function openOutputEditor(id, out) {
const o = out || {
name: '', protocol: 'ddp', pixel_count: 60, pixel_offset: 0, color_order: 'rgb',
brightness: 255, fps: 40, channel: 1, dmx_mode: 'enttec_pro', enabled: true, reverse: false,
};
document.getElementById('edit-output-title').textContent = id ? 'Edit output' : 'Add output';
setOutputField('edit-output-id', id || '');
setOutputField('edit-output-name', o.name);
setOutputField('edit-output-protocol', o.protocol);
setOutputField('edit-output-host', o.host);
setOutputField('edit-output-port', o.port);
setOutputField('edit-output-universe', o.universe);
setOutputField('edit-output-serial-port', o.serial_port);
setOutputField('edit-output-dmx-mode', o.dmx_mode || 'enttec_pro');
setOutputField('edit-output-channel', o.channel || 1);
setOutputField('edit-output-pixel-count', o.pixel_count);
setOutputField('edit-output-pixel-offset', o.pixel_offset || 0);
setOutputField('edit-output-color-order', o.color_order || 'rgb');
setOutputField('edit-output-brightness', o.brightness == null ? 255 : o.brightness);
setOutputField('edit-output-fps', o.fps || 40);
setOutputField('edit-output-reverse', o.reverse);
setOutputField('edit-output-enabled', o.enabled !== false);
const err = document.getElementById('edit-output-error');
err.hidden = true;
syncOutputProtocolFields();
document.getElementById('edit-output-modal').classList.add('active');
}
function readOutputForm() {
const val = (id) => document.getElementById(id).value.trim();
const num = (id) => (val(id) === '' ? null : Number(val(id)));
return {
name: val('edit-output-name'),
protocol: val('edit-output-protocol'),
host: val('edit-output-host'),
port: num('edit-output-port'),
universe: num('edit-output-universe'),
serial_port: val('edit-output-serial-port'),
dmx_mode: val('edit-output-dmx-mode'),
channel: num('edit-output-channel') || 1,
pixel_count: num('edit-output-pixel-count'),
pixel_offset: num('edit-output-pixel-offset') || 0,
color_order: val('edit-output-color-order'),
brightness: num('edit-output-brightness'),
fps: num('edit-output-fps'),
reverse: document.getElementById('edit-output-reverse').checked,
enabled: document.getElementById('edit-output-enabled').checked,
};
}
document.addEventListener('DOMContentLoaded', () => {
const btn = document.getElementById('outputs-btn');
const modal = document.getElementById('outputs-modal');
const editModal = document.getElementById('edit-output-modal');
const form = document.getElementById('edit-output-form');
if (!btn || !modal || !form) return;
btn.addEventListener('click', () => {
modal.classList.add('active');
loadOutputsModal();
});
document.getElementById('outputs-close-btn').addEventListener('click', () => modal.classList.remove('active'));
document.getElementById('output-add-btn').addEventListener('click', () => openOutputEditor(null, null));
document.getElementById('edit-output-close-btn').addEventListener('click', () => editModal.classList.remove('active'));
document.getElementById('edit-output-protocol').addEventListener('change', syncOutputProtocolFields);
form.addEventListener('submit', async (e) => {
e.preventDefault();
const id = document.getElementById('edit-output-id').value;
const err = document.getElementById('edit-output-error');
err.hidden = true;
const res = await fetch(id ? `/outputs/${encodeURIComponent(id)}` : '/outputs', {
method: id ? 'PUT' : 'POST',
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
body: JSON.stringify(readOutputForm()),
});
const data = await res.json().catch(() => ({}));
if (!res.ok) {
err.textContent = data.error || 'Save failed';
err.hidden = false;
return;
}
editModal.classList.remove('active');
loadOutputsModal();
});
});