Skip to content

Commit

Permalink
Update data/main.js to fetch measurement interval update for the web …
Browse files Browse the repository at this point in the history
…page from the server (same as sensor measurement interval)
  • Loading branch information
melkati committed May 1, 2024
1 parent ae5f87e commit 6416d3d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 39 deletions.
105 changes: 66 additions & 39 deletions data/main.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,51 @@
setInterval(function () {
// Call a function repetatively with 15 Second interval
getCO2Data();
}, 15000); // 15000mS update rate
// Initialize intervals with default values
let co2Interval = 15000; // 15 seconds
let temperatureInterval = 60000; // 60 seconds
let humidityInterval = 60000; // 60 seconds

setInterval(function () {
// Call a function repetatively with 60 Second interval
getTemperatureData();
}, 60000); // 60000mS update rate
// Define timers for data update functions
let co2Timer = setInterval(getCO2Data, co2Interval);
let temperatureTimer = setInterval(getTemperatureData, temperatureInterval);
let humidityTimer = setInterval(getHumidityData, humidityInterval);

setInterval(function () {
// Call a function repetatively with 60 Second interval
getHumidityData();
}, 60000); // 60000mS update rate
// Function to set intervals based on values obtained from the server
function setUpdateIntervals() {
// Clear current intervals
clearInterval(co2Timer);
clearInterval(temperatureTimer);
clearInterval(humidityTimer);
// Set new intervals
co2Timer = setInterval(getCO2Data, co2Interval);
temperatureTimer = setInterval(getTemperatureData, temperatureInterval);
humidityTimer = setInterval(getHumidityData, humidityInterval);
}

window.onload = function() {
getCO2Data();
getTemperatureData();
getHumidityData();
};
// Function to fetch measurement interval from the server
function getMeasurementInterval() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
console.log('CO2 Measurement Interval', this.responseText);
co2Interval = parseInt(this.responseText) * 1000;
setUpdateIntervals();
}
};
xhttp.onerror = function () {
console.error('Error fetching measurement interval');
};
xhttp.open("GET", "getMeasurementInterval", true);
xhttp.send();
}

function getCO2Data() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if ((this.readyState == 4) && (this.status == 200)) {
document.querySelector('text#CO2Value').textContent=this.responseText;
// document.getElementById("text#CO2Value").textContent = this.responseText;
// document.getElementById("CO2Value").innerHTML =
// this.responseText;
}
if ((this.readyState == 4) && (this.status == 200)) {
document.querySelector('text#CO2Value').textContent = this.responseText;
}
};
xhttp.onerror = function () {
console.error('Error fetching CO2 data');
};
xhttp.open("GET", "readCO2", true);
xhttp.send();
Expand All @@ -36,10 +54,12 @@ function getCO2Data() {
function getTemperatureData() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("TempValue").innerHTML =
this.responseText;
}
if (this.readyState == 4 && this.status == 200) {
document.getElementById("TempValue").innerHTML = this.responseText;
}
};
xhttp.onerror = function () {
console.error('Error fetching temperature data');
};
xhttp.open("GET", "readTemperature", true);
xhttp.send();
Expand All @@ -48,26 +68,33 @@ function getTemperatureData() {
function getHumidityData() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("HumValue").innerHTML =
this.responseText;
}
if (this.readyState == 4 && this.status == 200) {
document.getElementById("HumValue").innerHTML = this.responseText;
}
};
xhttp.onerror = function () {
console.error('Error fetching humidity data');
};
xhttp.open("GET", "readHumidity", true);
xhttp.send();
}

const colorSwitch = document.querySelector("#iconBulb");
function changeTheme(ev){
function changeTheme(ev) {
let elementStyle = window.getComputedStyle(colorSwitch);
let elementColor = elementStyle.getPropertyValue('--icon-color');
//console.log(elementColor);
if(elementColor==' #6e6e6e') {
//console.log('cambiar a dark');
document.documentElement.setAttribute('theme', 'dark');
if (elementColor == ' #6e6e6e') {
document.documentElement.setAttribute('theme', 'dark');
} else {
//console.log('cambiar a light');
document.documentElement.setAttribute('theme', 'light');
document.documentElement.setAttribute('theme', 'light');
}
};
colorSwitch.addEventListener('click', changeTheme);
colorSwitch.addEventListener('click', changeTheme);

// Start the process by fetching the measurement interval when the page loads
window.onload = function () {
getCO2Data();
getTemperatureData();
getHumidityData();
getMeasurementInterval();
};
Binary file modified data/main.js.gz
Binary file not shown.

0 comments on commit 6416d3d

Please sign in to comment.