therminator/data/script.js

148 lines
3.5 KiB
JavaScript
Raw Normal View History

2023-12-14 18:27:07 +00:00
const canvasA = document.getElementById("chartA");
const canvasB = document.getElementById("chartB");
const maxValsSelect = document.getElementById("maxVals");
let maxVals = maxValsSelect.options[maxValsSelect.selectedIndex].text;
2023-12-14 18:27:07 +00:00
const options = {
plugins: {
legend: {
labels: {
color: "rgba(250, 250, 250, 1)",
},
},
},
scales: {
y: {
grid: {
color: "rgba(250, 250, 250, 0.5)",
},
ticks: {
color: "rgba(250, 250, 250, 0.5)",
},
},
x: {
grid: {
color: "rgba(250, 250, 250, 0.5)",
},
ticks: {
color: "rgba(250, 250, 250, 0.5)",
},
},
},
};
2023-12-14 18:27:07 +00:00
const chartA = new Chart(canvasA, {
type: "line",
data: {
labels: [],
datasets: [
{
label: "Channel A temperature",
backgroundColor: "red",
borderColor: "red",
data: [],
},
],
},
options: options,
});
const chartB = new Chart(canvasB, {
type: "line",
data: {
labels: [],
datasets: [
{
label: "Channel B temperature",
backgroundColor: "blue",
borderColor: "blue",
data: [],
},
],
},
options: options,
});
2023-12-13 16:32:12 +00:00
var gateway = `ws://${window.location.hostname}/ws`;
var websocket;
// Init web socket when the page loads
2023-12-13 18:30:06 +00:00
window.addEventListener("load", onload);
2023-12-13 16:32:12 +00:00
function onload(event) {
2023-12-13 18:30:06 +00:00
initWebSocket();
2023-12-13 16:32:12 +00:00
}
2023-12-13 18:30:06 +00:00
function getReadings() {
websocket.send("getReadings");
2023-12-13 16:32:12 +00:00
}
function initWebSocket() {
2023-12-13 18:30:06 +00:00
console.log("Trying to open a WebSocket connection...");
websocket = new WebSocket(gateway);
websocket.onopen = onOpen;
websocket.onclose = onClose;
websocket.onmessage = onMessage;
2023-12-13 16:32:12 +00:00
}
// When websocket is established, call the getReadings() function
function onOpen(event) {
2023-12-13 18:30:06 +00:00
console.log("Connection opened");
getReadings();
2023-12-13 16:32:12 +00:00
}
function onClose(event) {
2023-12-13 18:30:06 +00:00
console.log("Connection closed");
setTimeout(initWebSocket, 2000);
2023-12-13 16:32:12 +00:00
}
2023-12-14 18:27:07 +00:00
function addData(chart, label, data) {
chart.data.labels.push(label);
chart.data.datasets.forEach((dataset) => {
dataset.data.push(data);
});
chart.update();
}
function removeFirstData(chart) {
chart.data.labels.splice(0, 1);
chart.data.datasets.forEach((dataset) => {
dataset.data.shift();
});
}
function changeUpdateInterval() {
const updateSelect = document.getElementById("updateInterval");
let updateInt = updateSelect.options[updateSelect.selectedIndex].text;
websocket.send("u" + updateInt);
location.reload();
}
2023-12-13 16:32:12 +00:00
// Function that receives the message from the ESP32 with the readings
function onMessage(event) {
2023-12-13 18:30:06 +00:00
console.log(event.data);
2023-12-14 18:27:07 +00:00
var data = JSON.parse(event.data);
2023-12-13 18:30:06 +00:00
var today = new Date();
var date =
today.getFullYear() + "-" + (today.getMonth() + 1) + "-" + today.getDate();
var time =
today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date + " " + time;
document.getElementById("time").innerHTML = dateTime;
2023-12-14 18:27:07 +00:00
if (chartA.data.labels.length > maxVals) {
removeFirstData(chartA);
}
if (chartB.data.labels.length > maxVals) {
removeFirstData(chartB);
}
if (isNaN(data["tempA"])) {
document.getElementById("error-tempA").innerHTML =
"<h3>" + data["tempA"] + "</h3>";
} else {
document.getElementById("error-tempA").innerText = "";
}
if (isNaN(data["tempB"])) {
document.getElementById("error-tempB").innerHTML =
"<h3>" + data["tempB"] + "</h3>";
} else {
document.getElementById("error-tempB").innerText = "";
}
addData(chartA, data["time"] / 1000, data["tempA"]);
addData(chartB, data["time"] / 1000, data["tempB"]);
2023-12-13 18:30:06 +00:00
}