forked from IT-Syndikat/website
refactor js a bit; traffic light now loads instantly on all pages
This commit is contained in:
parent
a4b3a3650b
commit
6495c6fda8
1 changed files with 48 additions and 34 deletions
|
@ -1,45 +1,59 @@
|
|||
const spaceApiUrl = 'https://spaceapi.it-syndikat.org/api/';
|
||||
const spaceApiUrl = "https://spaceapi.it-syndikat.org/api/";
|
||||
let statusValue;
|
||||
|
||||
function setSignalColour(colour) {
|
||||
for (const c of ["red", "yellow", "green"]) {
|
||||
document.getElementById(`signal-${c}`).style.display = (c == colour) ? "block" : "none";
|
||||
}
|
||||
for (const c of ["red", "yellow", "green"]) {
|
||||
document.getElementById(`signal-${c}`).style.display =
|
||||
c == colour ? "block" : "none";
|
||||
}
|
||||
}
|
||||
|
||||
fetch(spaceApiUrl)
|
||||
.then(response => response.json()) // Parse the JSON from the response
|
||||
.then(data => {
|
||||
const coreTemperatureValue = data.sensors.temperature[0].value;
|
||||
const coreHumidityValue = data.sensors.humidity[0].value;
|
||||
statusValue = data.state.open;
|
||||
document.getElementById('temperature-display').textContent = `${coreTemperatureValue} °C`;
|
||||
document.getElementById('humidity-display').textContent = `${coreHumidityValue} %`;
|
||||
if (statusValue === false) {
|
||||
document.getElementById('status-display').textContent = `CLOSED`;
|
||||
setSignalColour("red")
|
||||
} else if (statusValue === true) {
|
||||
document.getElementById('status-display').textContent = `OPEN`;
|
||||
setSignalColour("green")
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error fetching SpaceAPI data:', error);
|
||||
document.getElementById('temperature-display').textContent = 'Failed to load data.';
|
||||
document.getElementById('humidity-display').textContent = 'Failed to load data.';
|
||||
document.getElementById('status-display').textContent = 'Failed to load state.';
|
||||
});
|
||||
const hasSensorsWidget = () =>
|
||||
document.getElementsByClassName("widget-sensors").length > 0;
|
||||
|
||||
async function randomDelay(min, max) {
|
||||
await new Promise(resolve => setTimeout(resolve, (Math.random() * (max - min) + min) * 1000));
|
||||
await new Promise((resolve) =>
|
||||
setTimeout(resolve, (Math.random() * (max - min) + min) * 1000),
|
||||
);
|
||||
}
|
||||
async function signalGlitch(minDelay, maxDelay) {
|
||||
while (true) {
|
||||
await randomDelay(3, 7);
|
||||
while (true) {
|
||||
await randomDelay(3, 7);
|
||||
|
||||
setSignalColour("yellow")
|
||||
await randomDelay(0.5, 1.5);
|
||||
setSignalColour(statusValue ? "green" : "red")
|
||||
}
|
||||
setSignalColour("yellow");
|
||||
await randomDelay(0.5, 1.5);
|
||||
setSignalColour(statusValue ? "green" : "red");
|
||||
}
|
||||
}
|
||||
signalGlitch();
|
||||
|
||||
window.onload = () => {
|
||||
fetch(spaceApiUrl)
|
||||
.then((response) => response.json()) // Parse the JSON from the response
|
||||
.then((data) => {
|
||||
const coreTemperatureValue = data.sensors.temperature[0].value;
|
||||
const coreHumidityValue = data.sensors.humidity[0].value;
|
||||
statusValue = data.state.open;
|
||||
|
||||
setSignalColour(statusValue === false ? "red" : "green");
|
||||
|
||||
if (hasSensorsWidget()) {
|
||||
document.getElementById("temperature-display").textContent =
|
||||
`${coreTemperatureValue} °C`;
|
||||
document.getElementById("humidity-display").textContent =
|
||||
`${coreHumidityValue} %`;
|
||||
document.getElementById("status-display").textContent =
|
||||
statusValue === false ? "CLOSED" : "OPEN";
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error fetching SpaceAPI data:", error);
|
||||
document.getElementById("temperature-display").textContent =
|
||||
"Failed to load data.";
|
||||
document.getElementById("humidity-display").textContent =
|
||||
"Failed to load data.";
|
||||
document.getElementById("status-display").textContent =
|
||||
"Failed to load state.";
|
||||
});
|
||||
|
||||
signalGlitch();
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue