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(status) { this.connected = status; } } class liveGraph { // Constructor to initialize the graph constructor(id) { this.timeArray = []; 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); } }