diff --git a/web/main.js b/web/main.js index aa8568b..ac77024 100644 --- a/web/main.js +++ b/web/main.js @@ -4,7 +4,7 @@ const sensorData = {}; let liveGraphs = []; let nodeArray = []; let nodeDict = {}; - +let funny; // letiables let intervalDelay = 5000; let amountOfNodes = 3; @@ -47,9 +47,9 @@ function openConnection() { openConnection(); function handleIncomingData(data) { - nodeAdressHandler(data.Node); - - nodeNumber = nodeDict[data.Node]; + nodeEventHandler(data.node); + funny = data.node; + nodeNumber = nodeDict[data.node]; temperature = data.Temp; humidity = data.Humi; CO2 = data.eCO2; @@ -58,13 +58,23 @@ function handleIncomingData(data) { updateNodeData(nodeNumber, temperature, humidity, CO2, TVOC); } -function nodeAdressHandler(node) { +function nodeEventHandler(node) { if (!nodeArray.includes(node)) { nodeArray.push(node); nodeDict[node] = nodeArray.length; + makeLiveGraph(nodeArray.length); } } +function makeLiveGraph(node) { + createNodeData(node); + liveGraphs.push(new liveGraph(node)); + + liveGraphs.forEach((graph) => { + graph.makeGraph(); + }); +} + //function for making the html elements for the following html code function nodeData(data, node) { let nodeData = document.createElement("div"); @@ -175,19 +185,13 @@ function updateNodeData(node, temperature, humidity, eCO2, TVOC) { document.getElementById("TVOCStatus").textContent = "Connected"; // Update the graph - liveGraphs[0].updateData(temperature, humidity, eCO2, TVOC); - liveGraphs[0].updateGraph(); + liveGraphs[node -1].updateData(temperature, humidity, eCO2, TVOC); + liveGraphs[node - 1].updateGraph(); - console.log(nodeDict[node]); + console.log(nodeDict); console.log(nodeArray); } -// Call the function to create the HTML structure -for (let i = 1; i <= amountOfNodes; i++) { - createNodeData(i); - liveGraphs.push(new liveGraph(i)); -} - // make the graphs liveGraphs.forEach((graph) => { graph.makeGraph();