webui: use partial status polling
This commit is contained in:
@@ -240,6 +240,22 @@ input{width:100%;border:1px solid var(--line);border-radius:12px;padding:12px;ba
|
|||||||
<script>
|
<script>
|
||||||
const API_BASE="/api/v1";
|
const API_BASE="/api/v1";
|
||||||
function api(path){return API_BASE+path;}
|
function api(path){return API_BASE+path;}
|
||||||
|
|
||||||
|
const STATUS_FIELDS_OVERVIEW="battery,temps,relays,vehicle,network,alarms,system";
|
||||||
|
|
||||||
|
function isConfigTabActive(){
|
||||||
|
return $("configPage")?.classList.contains("active") || false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function statusUrl(){
|
||||||
|
const needsFullStatus=isConfigTabActive() || !window.lastStatus?.config;
|
||||||
|
return needsFullStatus ? api("/status") : api("/status?fields="+STATUS_FIELDS_OVERVIEW);
|
||||||
|
}
|
||||||
|
|
||||||
|
function mergeStatus(previous,next){
|
||||||
|
if(!previous) return next;
|
||||||
|
return Object.assign({},previous,next);
|
||||||
|
}
|
||||||
const $=id=>document.getElementById(id);
|
const $=id=>document.getElementById(id);
|
||||||
function fmt(n,d=1){return typeof n==="number"?n.toFixed(d):"--"}
|
function fmt(n,d=1){return typeof n==="number"?n.toFixed(d):"--"}
|
||||||
function temp(v){return typeof v==="number"?fmt(v,1)+"°F":"Offline"}
|
function temp(v){return typeof v==="number"?fmt(v,1)+"°F":"Offline"}
|
||||||
@@ -332,6 +348,7 @@ async function relay(id,on){
|
|||||||
await load();
|
await load();
|
||||||
}
|
}
|
||||||
function render(data){
|
function render(data){
|
||||||
|
data=mergeStatus(window.lastStatus,data);
|
||||||
window.lastStatus=data;
|
window.lastStatus=data;
|
||||||
$("conn").textContent="Live";
|
$("conn").textContent="Live";
|
||||||
$("conn").className="pill good";
|
$("conn").className="pill good";
|
||||||
@@ -562,7 +579,7 @@ function renderConfigControls(data){
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function saveRelayConfig(){
|
async function saveRelayConfig(){
|
||||||
const r=await fetch(api("/status"),{cache:"no-store"});
|
const r=await fetch(api("/status?fields=config"),{cache:"no-store"});
|
||||||
const data=await r.json();
|
const data=await r.json();
|
||||||
const relays=data.config?.relays||[];
|
const relays=data.config?.relays||[];
|
||||||
|
|
||||||
@@ -587,7 +604,7 @@ async function saveRelayConfig(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function saveTempConfig(){
|
async function saveTempConfig(){
|
||||||
const r=await fetch(api("/status"),{cache:"no-store"});
|
const r=await fetch(api("/status?fields=config"),{cache:"no-store"});
|
||||||
const data=await r.json();
|
const data=await r.json();
|
||||||
const temps=data.config?.temperature_sensors||[];
|
const temps=data.config?.temperature_sensors||[];
|
||||||
|
|
||||||
@@ -649,7 +666,7 @@ async function connectWifi(){
|
|||||||
}
|
}
|
||||||
async function load(){
|
async function load(){
|
||||||
try{
|
try{
|
||||||
const r=await fetch(api("/status"),{cache:"no-store"});
|
const r=await fetch(statusUrl(),{cache:"no-store"});
|
||||||
render(await r.json());
|
render(await r.json());
|
||||||
}catch(e){
|
}catch(e){
|
||||||
$("conn").textContent="Offline";
|
$("conn").textContent="Offline";
|
||||||
|
|||||||
@@ -300,3 +300,14 @@ def test_status_endpoint_supports_field_selection_and_cache_headers():
|
|||||||
assert 'server.header("If-Modified-Since")' in source
|
assert 'server.header("If-Modified-Since")' in source
|
||||||
assert 'server.send(304)' in source
|
assert 'server.send(304)' in source
|
||||||
assert 'server.collectHeaders(statusCacheHeaders, 2)' in source
|
assert 'server.collectHeaders(statusCacheHeaders, 2)' in source
|
||||||
|
|
||||||
|
|
||||||
|
def test_embedded_webui_uses_partial_status_for_overview_polling():
|
||||||
|
source = firmware_source()
|
||||||
|
|
||||||
|
assert 'const STATUS_FIELDS_OVERVIEW="battery,temps,relays,vehicle,network,alarms,system";' in source
|
||||||
|
assert 'function statusUrl()' in source
|
||||||
|
assert 'api("/status?fields="+STATUS_FIELDS_OVERVIEW)' in source
|
||||||
|
assert 'fetch(statusUrl(),{cache:"no-store"})' in source
|
||||||
|
assert 'function mergeStatus(previous,next)' in source
|
||||||
|
assert 'api("/status?fields=config")' in source
|
||||||
|
|||||||
Reference in New Issue
Block a user