fixing ui stuff
This commit is contained in:
@@ -63,7 +63,7 @@ static const char MANAGEMENT_HTML[] PROGMEM = R"BOOMHTML(
|
||||
</section>
|
||||
|
||||
<section class="card full">
|
||||
<div class="row" style="justify-content:space-between"><div><h2>Upload queue</h2><p class="sub">Files stay on the SD card until the server returns a successful response.</p></div><button id="sendAll">Send all</button></div>
|
||||
<div class="row" style="justify-content:space-between"><div><h2>Upload queue</h2><p class="sub">Files stay on the SD card until the server returns a successful response. Discard permanently removes recordings from this device and adjusts today's count.</p></div><div class="row"><button id="sendAll">Send all</button><button class="danger" id="clearQueue">Clear queue</button><button class="secondary" id="resetToday">Reset today count</button></div></div>
|
||||
<div class="queue" id="eventList"><div class="empty">Loading queue…</div></div>
|
||||
<div class="message" id="syncMessage"></div>
|
||||
</section>
|
||||
@@ -85,10 +85,12 @@ const $=id=>document.getElementById(id);let statusBusy=false,queueBusy=false;
|
||||
function fmtBytes(n){if(n<1024)return n+' B';if(n<1048576)return (n/1024).toFixed(1)+' kB';return (n/1048576).toFixed(1)+' MB'}
|
||||
function drawWave(points){const c=$('wave'),g=c.getContext('2d'),w=c.width,h=c.height;g.clearRect(0,0,w,h);g.strokeStyle='#d9e1df';g.lineWidth=1;g.beginPath();g.moveTo(0,h/2+.5);g.lineTo(w,h/2+.5);g.stroke();if(!points||!points.length)return;g.strokeStyle='#143f35';g.lineWidth=2;g.beginPath();points.forEach((p,i)=>{const x=i*(w/(points.length-1));const y=h/2-(p/100)*(h*.43);i?g.lineTo(x,y):g.moveTo(x,y)});g.stroke()}
|
||||
async function api(path,opts={}){const r=await fetch(path,{cache:'no-store',...opts});let data={};try{data=await r.json()}catch(e){}if(!r.ok)throw new Error(data.error||('HTTP '+r.status));return data}
|
||||
async function refreshStatus(){if(statusBusy)return;statusBusy=true;try{const s=await api('/api/status');$('today').textContent=s.today;$('queued').textContent=s.queue;$('mic').textContent=s.capturing?'Saving':'Listening';$('sd').textContent=s.sd?'Ready':'Missing';$('deviceAddress').textContent='IP: '+s.ip;$('ssidLine').textContent='Wi-Fi: '+(s.ssid||'—');$('networkTop').textContent=s.wifi?('Connected · '+s.ip):(s.ap?'Setup AP · '+s.ip:'Offline');$('netDot').className='statusdot '+(s.wifi?'ok':(s.ap?'warn':'bad'));if(document.activeElement!==$('sensitivity')){$('sensitivity').value=s.sensitivity;$('sensValue').textContent=s.sensitivity}$('syncMessage').textContent=s.syncMessage||'';drawWave(s.waveform)}catch(e){$('networkTop').textContent='Interface unavailable';$('netDot').className='statusdot bad'}finally{statusBusy=false}}
|
||||
async function refreshQueue(){if(queueBusy)return;queueBusy=true;try{const items=await api('/api/events'),box=$('eventList');box.textContent='';if(!items.length){const e=document.createElement('div');e.className='empty';e.textContent='Queue is empty. Nice.';box.appendChild(e)}else items.forEach(item=>{const row=document.createElement('div');row.className='event';const info=document.createElement('div');const name=document.createElement('strong');name.textContent=item.name;const size=document.createElement('small');size.textContent=fmtBytes(item.bytes);info.append(name,size);const b=document.createElement('button');b.className='secondary';b.textContent='Send';b.onclick=async()=>{b.disabled=true;try{await api('/api/sync',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},body:new URLSearchParams({file:item.name})});$('syncMessage').textContent='Upload queued: '+item.name}catch(e){$('syncMessage').textContent=e.message}finally{setTimeout(refreshQueue,1200)}};row.append(info,b);box.appendChild(row)})}catch(e){$('eventList').textContent='Could not read queue: '+e.message}finally{queueBusy=false}}
|
||||
async function refreshStatus(){if(statusBusy)return;statusBusy=true;try{const s=await api('/api/status');$('today').textContent=s.today;$('queued').textContent=s.queue;$('clearQueue').disabled=!s.queue;$('mic').textContent=s.capturing?'Saving':'Listening';$('sd').textContent=s.sd?'Ready':'Missing';$('deviceAddress').textContent='IP: '+s.ip;$('ssidLine').textContent='Wi-Fi: '+(s.ssid||'—');$('networkTop').textContent=s.wifi?('Connected · '+s.ip):(s.ap?'Setup AP · '+s.ip:'Offline');$('netDot').className='statusdot '+(s.wifi?'ok':(s.ap?'warn':'bad'));if(document.activeElement!==$('sensitivity')){$('sensitivity').value=s.sensitivity;$('sensValue').textContent=s.sensitivity}$('syncMessage').textContent=s.syncMessage||'';drawWave(s.waveform)}catch(e){$('networkTop').textContent='Interface unavailable';$('netDot').className='statusdot bad'}finally{statusBusy=false}}
|
||||
async function refreshQueue(){if(queueBusy)return;queueBusy=true;try{const items=await api('/api/events'),box=$('eventList');box.textContent='';if(!items.length){const e=document.createElement('div');e.className='empty';e.textContent='Queue is empty. Nice.';box.appendChild(e)}else items.forEach(item=>{const row=document.createElement('div');row.className='event';const info=document.createElement('div');const name=document.createElement('strong');name.textContent=item.name;const size=document.createElement('small');size.textContent=fmtBytes(item.bytes);info.append(name,size);const actions=document.createElement('div');actions.className='row';const b=document.createElement('button');b.className='secondary';b.textContent='Send';b.onclick=async()=>{b.disabled=true;try{await api('/api/sync',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},body:new URLSearchParams({file:item.name})});$('syncMessage').textContent='Upload queued: '+item.name}catch(e){$('syncMessage').textContent=e.message}finally{setTimeout(refreshQueue,1200)}};const discard=document.createElement('button');discard.className='danger';discard.textContent='Discard';discard.setAttribute('aria-label','Discard '+item.name);discard.onclick=async()=>{if(!confirm('Permanently discard '+item.name+' from this device?'))return;discard.disabled=true;try{await api('/api/events/discard',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded','X-BoomCast-Action':'discard'},body:new URLSearchParams({file:item.name})});$('syncMessage').textContent='Discarded '+item.name;await refreshQueue();await refreshStatus()}catch(e){$('syncMessage').textContent=e.message}finally{discard.disabled=false}};actions.append(b,discard);row.append(info,actions);box.appendChild(row)})}catch(e){$('eventList').textContent='Could not read queue: '+e.message}finally{queueBusy=false}}
|
||||
$('sensitivity').addEventListener('input',e=>$('sensValue').textContent=e.target.value);$('sensitivity').addEventListener('change',async e=>{try{const d=await api('/api/sensitivity',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},body:new URLSearchParams({value:e.target.value})});$('sensMessage').textContent='Saved at '+d.value+'%'}catch(err){$('sensMessage').textContent=err.message}});
|
||||
$('sendAll').onclick=async()=>{try{await api('/api/sync',{method:'POST'});$('syncMessage').textContent='Upload of queued events requested.'}catch(e){$('syncMessage').textContent=e.message}};
|
||||
$('clearQueue').onclick=async()=>{const count=Number($('queued').textContent)||0;if(!count)return;if(!confirm('Permanently discard all '+count+' queued recording(s) from this device? They cannot be recovered.'))return;const b=$('clearQueue');b.disabled=true;try{const result=await api('/api/events/discard',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded','X-BoomCast-Action':'discard'},body:new URLSearchParams({all:'1'})});$('syncMessage').textContent='Discarded '+result.removed+' queued recording(s).';await refreshQueue();await refreshStatus()}catch(e){$('syncMessage').textContent=e.message}finally{b.disabled=false}};
|
||||
$('resetToday').onclick=async()=>{if(!confirm('Reset the ePaper today count to zero? Queued recordings will stay on the SD card.'))return;const b=$('resetToday');b.disabled=true;try{await api('/api/today/reset',{method:'POST',headers:{'X-BoomCast-Action':'discard'}});$('syncMessage').textContent='Today count reset.';await refreshStatus()}catch(e){$('syncMessage').textContent=e.message}finally{b.disabled=false}};
|
||||
$('scanWifi').onclick=async()=>{const b=$('scanWifi');b.disabled=true;$('wifiMessage').textContent='Scanning…';try{const nets=await api('/api/wifi/scan'),sel=$('wifiSelect');sel.textContent='';const empty=document.createElement('option');empty.value='';empty.textContent='Choose a network or type it below';sel.appendChild(empty);nets.forEach(n=>{const o=document.createElement('option');o.value=n.ssid;o.textContent=n.ssid+' · '+n.rssi+' dBm'+(n.secure?' · secured':'');sel.appendChild(o)});$('wifiMessage').textContent=nets.length+' network(s) found.'}catch(e){$('wifiMessage').textContent=e.message}finally{b.disabled=false}};
|
||||
$('wifiSelect').onchange=e=>{if(e.target.value)$('ssidInput').value=e.target.value};
|
||||
$('saveWifi').onclick=async()=>{const ssid=$('ssidInput').value.trim(),password=$('passwordInput').value;if(!ssid){$('wifiMessage').textContent='Enter an SSID first.';return}try{await api('/api/wifi',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},body:new URLSearchParams({ssid,password})});$('wifiMessage').textContent='Saved. BoomCast is rebooting…'}catch(e){$('wifiMessage').textContent=e.message}};
|
||||
@@ -118,6 +120,8 @@ void BoomWeb::sendJson(int code, const String& json) {
|
||||
}
|
||||
|
||||
void BoomWeb::begin() {
|
||||
const char* actionHeaders[] = {"X-BoomCast-Action"};
|
||||
server_.collectHeaders(actionHeaders, 1);
|
||||
server_.on("/", HTTP_GET, [this]() { handleRoot(); });
|
||||
server_.on("/api/status", HTTP_GET, [this]() { handleStatus(); });
|
||||
server_.on("/api/events", HTTP_GET, [this]() { handleEvents(); });
|
||||
@@ -126,6 +130,8 @@ void BoomWeb::begin() {
|
||||
server_.on("/api/wifi/forget", HTTP_POST, [this]() { handleWifiForget(); });
|
||||
server_.on("/api/sensitivity", HTTP_POST, [this]() { handleSensitivity(); });
|
||||
server_.on("/api/sync", HTTP_POST, [this]() { handleSync(); });
|
||||
server_.on("/api/events/discard", HTTP_POST, [this]() { handleDiscard(); });
|
||||
server_.on("/api/today/reset", HTTP_POST, [this]() { handleTodayReset(); });
|
||||
server_.on("/api/reboot", HTTP_POST, [this]() { handleReboot(); });
|
||||
server_.onNotFound([this]() {
|
||||
if (!authorise()) return;
|
||||
@@ -232,6 +238,56 @@ void BoomWeb::handleSync() {
|
||||
sendJson(202, "{\"ok\":true,\"queued\":true}");
|
||||
}
|
||||
|
||||
void BoomWeb::handleDiscard() {
|
||||
if (!authorise()) return;
|
||||
// Require a non-simple request header so another site cannot submit a
|
||||
// cross-origin form using cached HTTP Basic credentials.
|
||||
if (server_.header("X-BoomCast-Action") != "discard") {
|
||||
sendJson(403, "{\"error\":\"Discard action header required\"}");
|
||||
return;
|
||||
}
|
||||
if (!boomQueue.ready()) {
|
||||
sendJson(503, "{\"error\":\"SD card is unavailable\"}");
|
||||
return;
|
||||
}
|
||||
if (server_.arg("all") == "1") {
|
||||
int removed = boomQueue.clearAll();
|
||||
if (removed < 0) {
|
||||
sendJson(500, "{\"error\":\"Could not remove every queued recording\"}");
|
||||
return;
|
||||
}
|
||||
boomState.setSyncing(false, "Discarded " + String(removed) + " queued recording(s)");
|
||||
sendJson(200, "{\"ok\":true,\"removed\":" + String(removed) + "}");
|
||||
return;
|
||||
}
|
||||
String file = server_.arg("file");
|
||||
if (!file.length() || file.indexOf('/') >= 0 || file.indexOf('\\') >= 0 || !file.endsWith(".wav")) {
|
||||
sendJson(400, "{\"error\":\"Valid queued filename required\"}");
|
||||
return;
|
||||
}
|
||||
if (!boomQueue.exists(file)) {
|
||||
sendJson(404, "{\"error\":\"Queued recording not found\"}");
|
||||
return;
|
||||
}
|
||||
if (!boomQueue.discard(file)) {
|
||||
sendJson(500, "{\"error\":\"Could not discard recording\"}");
|
||||
return;
|
||||
}
|
||||
boomState.setSyncing(false, "Discarded " + file);
|
||||
sendJson(200, "{\"ok\":true,\"removed\":1}");
|
||||
}
|
||||
|
||||
void BoomWeb::handleTodayReset() {
|
||||
if (!authorise()) return;
|
||||
if (server_.header("X-BoomCast-Action") != "discard") {
|
||||
sendJson(403, "{\"error\":\"Reset action header required\"}");
|
||||
return;
|
||||
}
|
||||
boomState.resetTodayEvents();
|
||||
boomState.setSyncing(false, "Today count reset");
|
||||
sendJson(200, "{\"ok\":true,\"today\":0}");
|
||||
}
|
||||
|
||||
void BoomWeb::handleReboot() {
|
||||
if (!authorise()) return;
|
||||
sendJson(200, "{\"ok\":true,\"rebooting\":true}");
|
||||
|
||||
Reference in New Issue
Block a user