Files
J1B3-Sensor-boxes/web/classes.js
2024-03-26 11:01:16 +01:00

173 lines
4.2 KiB
JavaScript

class node {
// Constructor to initialize the node
constructor(nodeId) {
this.nodeId = nodeId;
this.temperature = 0;
this.humidity = 0;
this.eCO2 = 0;
this.TVOC = 0;
this.connected = false;
}
// Function to update the data
updateData(temperature, humidity, eCO2, TVOC) {
this.temperature = temperature;
this.humidity = humidity;
this.eCO2 = eCO2;
this.TVOC = TVOC;
}
// Function to update the connection status
updateConnection() {
if (connectedNodes[this.nodeId]) {
this.connected = true;
} else {
this.connected = false;
}
}
}
class feedbackNode extends node {
// Constructor to initialize the feedback node
constructor(nodeId) {
super(nodeId);
this.feedback = {};
this.answers = 0;
}
// Function to update the feedback
updateFeedback(feedback) {
this.feedback = feedback;
}
}
class liveGraph extends graph {
// Constructor to initialize the graph
constructor(id) {
super(id);
this.tempArray = [];
this.humiArray = [];
this.eco2Array = [];
this.tvocArray = [];
this.cnt = 0;
this.nodeId = "liveGraph" + id;
}
// Fuction to create a graph
makeGraph() {
// Create a new line for temperature
Plotly.plot(this.nodeId, [
{
x: this.timeArray, // Use timeArray as x values
y: this.tempArray,
mode: "lines",
line: { color: "#FF0000" },
name: "Temperature",
},
]);
// Create a new line for humidity
Plotly.plot(this.nodeId, [
{
x: this.timeArray, // Use timeArray as x values
y: this.humiArray,
mode: "lines",
line: { color: "#80CAF6" },
name: "Humidity",
},
]);
// Create a new line for eCO2
Plotly.plot(this.nodeId, [
{
x: this.timeArray, // Use timeArray as x values
y: this.eco2Array,
mode: "lines",
line: { color: "#FFA500" },
name: "eCO2 / 10",
},
]);
// Create a new line for TVOC
Plotly.plot(this.nodeId, [
{
x: this.timeArray, // Use timeArray as x values
y: this.tvocArray,
mode: "lines",
line: { color: "#000000" },
name: "TVOC / 10",
},
]);
}
// Function to update the graph with new values got from updateData function
updateGraph() {
let time = new Date();
this.timeArray.push(new Date());
let update = {
x: [[this.timeArray]],
y: [
[this.tempArray],
[this.humiArray],
[this.eco2Array],
[this.tvocArray],
],
};
let olderTime = time.setMinutes(time.getMinutes() - 1);
let futureTime = time.setMinutes(time.getMinutes() + 1);
let minuteView = {
xaxis: {
type: "date",
range: [olderTime, futureTime],
},
};
Plotly.relayout(this.nodeId, minuteView);
if (this.cnt === 10) clearInterval(interval);
}
// function to get the new data for graph
updateData(temperature, humidity, eCO2, TVOC) {
// Update the graph
this.tempArray.push(temperature);
this.humiArray.push(humidity);
this.eco2Array.push(eCO2 / 10);
this.tvocArray.push(TVOC / 10);
}
}
class graph {
// Constructor to initialize the graph
constructor(id) {
this.nodeId = "graph" + id;
this.timeArray = [];
}
// Function to create a graph
makeGraph(amountOfGraphs, array1, array2, array3, array4) {
for (let i = 0; i < amountOfGraphs; i++) {
// Create a new line for temperature
Plotly.plot(this.nodeId, [
{
x: this.timeArray, // Use timeArray as x values
y: array + i,
mode: "lines",
line: { color: "#FF0000" },
name: "Temperature",
},
]);
}
}
// Function to update the graph with new values got from updateData function
updateGraph(array1, array2, array3, array4) {
let time = new Date();
this.timeArray.push(new Date());
let update = {
x: [[this.timeArray]],
y: [array1, array2, array3, array4],
};
let olderTime = time.setMinutes(time.getMinutes() - 1);
let futureTime = time.setMinutes(time.getMinutes() + 1);
let minuteView = {
xaxis: {
type: "date",
range: [olderTime, futureTime],
},
};
}
}