[LOG+DevicePage_JS] refining the device page JS

...while I am at it :)
This commit is contained in:
chromoxdor
2026-02-04 12:08:41 +01:00
parent 730c966c91
commit 7f14ce8d58
+54 -78
View File
@@ -1,88 +1,64 @@
loopDeLoop(1000, 0);
loopDeLoop(1000);
function elId(e) {
return document.getElementById(e);
return document.getElementById(e);
}
function loopDeLoop(timeForNext, activeRequests) {
var maximumRequests = 1;
var c;
var k;
var err = '';
var url = '/json?view=sensorupdate';
var check = 0;
if (isNaN(activeRequests)) {
activeRequests = maximumRequests;
}
if (timeForNext == null) {
timeForNext = 1000;
}
i = setInterval(function() {
if (check > 0) {
clearInterval(i);
return;
}
++activeRequests;
if (activeRequests > maximumRequests) {
check = 1;
} else {
fetch(url).then(function(response) {
var valueEntry;
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' + response.status);
return;
const SENSOR_URL = '/json?view=sensorupdate';
function loopDeLoop(timeForNext = 1000) {
setTimeout(function () {
fetch(SENSOR_URL)
.then(response => {
if (!response.ok) {
throw new Error(response.status);
}
response.json().then(function(data) {
timeForNext = data.TTL;
var showUoM = data.ShowUoM === undefined || (data.ShowUoM && data.ShowUoM !== 'false');
for (c = 0; c < data.Sensors.length; c++) {
if (data.Sensors[c].hasOwnProperty('TaskValues')) {
for (k = 0; k < data.Sensors[c].TaskValues.length; k++) {
try {
valueEntry = data.Sensors[c].TaskValues[k].Value;
} catch (err) {
valueEntry = err.name;
} finally {
if (valueEntry !== 'TypeError') {
var tempValue = data.Sensors[c].TaskValues[k].Value;
var decimalsValue = data.Sensors[c].TaskValues[k].NrDecimals;
if (decimalsValue < 255) {
tempValue = parseFloat(tempValue).toFixed(decimalsValue);
}
var tempUoM = data.Sensors[c].TaskValues[k].UoM;
var tempPres = data.Sensors[c].TaskValues[k].Presentation;
if (tempPres) {
tempValue = tempPres;
} else
if (tempUoM && showUoM) {
tempValue += ' ' + tempUoM;
}
var valueID = 'value_' + (data.Sensors[c].TaskNumber - 1) + '_' + (data.Sensors[c].TaskValues[k].ValueNumber - 1);
var valueNameID = 'valuename_' + (data.Sensors[c].TaskNumber - 1) + '_' + (data.Sensors[c].TaskValues[k].ValueNumber - 1);
var valueElement = elId(valueID);
var valueNameElement = elId(valueNameID);
if (valueElement) {
valueElement.innerHTML = tempValue;
}
if (valueNameElement) {
valueNameElement.innerHTML = data.Sensors[c].TaskValues[k].Name + ':';
}
}
}
}
return response.json();
})
.then(data => {
timeForNext = data.TTL || timeForNext;
const showUoM = data.ShowUoM === undefined || (data.ShowUoM && data.ShowUoM !== 'false');
for (let c = 0; c < data.Sensors.length; c++) {
const sensor = data.Sensors[c];
if (!sensor.TaskValues) continue;
for (let k = 0; k < sensor.TaskValues.length; k++) {
const taskValue = sensor.TaskValues[k];
if (!taskValue || taskValue.Value == null) continue;
let tempValue = taskValue.Value;
const decimals = taskValue.NrDecimals;
if (decimals < 255) {
tempValue = parseFloat(tempValue).toFixed(decimals);
}
if (taskValue.Presentation) {
tempValue = taskValue.Presentation;
} else if (taskValue.UoM && showUoM) {
tempValue += ' ' + taskValue.UoM;
}
const baseId = (sensor.TaskNumber - 1) + '_' + (taskValue.ValueNumber - 1);
const valueEl = elId('value_' + baseId);
if (valueEl) {
valueEl.innerHTML = tempValue;
}
const nameEl = elId('valuename_' + baseId);
if (nameEl) {
nameEl.innerHTML = taskValue.Name + ':';
}
}
clearInterval(i);
loopDeLoop(timeForNext, 0);
return;
});
}).catch(function(err) {
}
loopDeLoop(timeForNext);
})
.catch(err => {
console.log(err.message);
timeForNext = 5000;
clearInterval(i);
loopDeLoop(timeForNext, 0);
return;
loopDeLoop(5000);
});
check = 1;
}
}, timeForNext);
}