From 1c13d20b492a168ddb70ee949c53a0f35036fa22 Mon Sep 17 00:00:00 2001 From: Sam Hos Date: Thu, 28 Mar 2024 13:32:22 +0100 Subject: [PATCH] added location and nodename --- web/new website/GaugGroup.js | 6 +-- web/new website/main.js | 98 ++++++++++++++++++++---------------- 2 files changed, 59 insertions(+), 45 deletions(-) diff --git a/web/new website/GaugGroup.js b/web/new website/GaugGroup.js index eb795cf..fc214fc 100644 --- a/web/new website/GaugGroup.js +++ b/web/new website/GaugGroup.js @@ -1,17 +1,17 @@ class GaugeGroup { - constructor(nodeId, gaugesCount, maxGaugeValues, dataTypes) { + constructor(nodeId, Location, gaugesCount, maxGaugeValues, dataTypes) { this.nodeId = nodeId; this.gaugesCount = gaugesCount; this.maxGaugeValues = maxGaugeValues; // Maximum value the gauge can display this.dataTypes = dataTypes; // Array of data type names for each gauge - + this.location = Location; // Create a new div element this.element = document.createElement("div"); this.element.className = "gaugeGroup"; // Set the HTML of the new div this.element.innerHTML = ` -

Gauge Group for Node ${this.nodeId}

+

${this.nodeId} - ${this.location}

${Array(this.gaugesCount).fill().map((_, i) => `
diff --git a/web/new website/main.js b/web/new website/main.js index b877136..e3e855c 100644 --- a/web/new website/main.js +++ b/web/new website/main.js @@ -46,49 +46,52 @@ function openConnection() { openConnection(); -function handleIncomingData(data) { - if (!data.node || !data.Temp || !data.Humi || !data.eCO2 || !data.TVOC) { - console.error('Invalid data received:', data); - return; - } - - nodeAdressHandler(data.node, Object.keys(data).filter(key => key !== 'node')); - - nodeNumber = nodeDict[data.node]; - temperature = data.Temp; - humidity = data.Humi; - CO2 = data.eCO2; - TVOC = data.TVOC; - - // Update the gauges with the new data - sensorData[data.node].updateGauge(1, temperature); - sensorData[data.node].updateGauge(2, humidity); - sensorData[data.node].updateGauge(3, CO2); - sensorData[data.node].updateGauge(4, TVOC); +async function handleIncomingData(data) { + if (!data.node || !data.Temp || !data.Humi || !data.eCO2 || !data.TVOC) { + console.error('Invalid data received:', data); + return; + } + + await nodeAdressHandler(data.node, Object.keys(data).filter(key => key !== 'node')); + + let nodeName = nodeDict[data.node]; + let temperature = data.Temp; + let humidity = data.Humi; + let CO2 = data.eCO2; + let TVOC = data.TVOC; + + // Update the gauges with the new data + if (sensorData[nodeName]) { + sensorData[nodeName].updateGauge(1, temperature); + sensorData[nodeName].updateGauge(2, humidity); + sensorData[nodeName].updateGauge(3, CO2); + sensorData[nodeName].updateGauge(4, TVOC); + } else { + console.error('No sensor data for node:', nodeName); + } } -function nodeAdressHandler(node, dataTypes) { - if (!nodeArray.includes(node)) { - nodeArray.push(node); - nodeDict[node] = nodeArray.length; - - // Define the maximum values for each data type - let maxGaugeValues = dataTypes.map(dataType => { - switch (dataType) { - case 'Temp': return 50; - case 'Humi': return 100; - case 'eCO2': return 3000; - case 'TVOC': return 2200; - default: return 100; - } - }); - - // Create a new GaugeGroup for the node - let gaugeGroup = new GaugeGroup(node, dataTypes.length, maxGaugeValues, dataTypes); - - // Store the GaugeGroup in the sensorData object for later use - sensorData[node] = gaugeGroup; - } +async function nodeAdressHandler(node, dataTypes) { + let nodeInfo = await getNodeInfo(node); + let nodeName = nodeInfo.name; + let nodeLocation = nodeInfo.location; + if (!nodeArray.includes(nodeName)) { + nodeArray.push(nodeName); + nodeDict[node] = nodeName; + + let maxGaugeValues = dataTypes.map(dataType => { + switch (dataType) { + case 'Temp': return 50; + case 'Humi': return 100; + case 'eCO2': return 3000; + case 'TVOC': return 2200; + default: return 100; + } + }); + + let gaugeGroup = new GaugeGroup(nodeName, nodeLocation, dataTypes.length, maxGaugeValues, dataTypes); + sensorData[nodeName] = gaugeGroup; + } } function createGauge(node, dataType) { @@ -112,4 +115,15 @@ function updateGauge(nodeNumber, dataType, value) { // Update the gauge here let gauge = sensorData[nodeNumber][dataType]; // Get the gauge from the sensorData object gauge.update(value); // Assuming the Gauge class has an update method - } \ No newline at end of file + } + +function getNodeInfo(node){ + return fetch("http://145.92.8.114/flask?MAC=" + node) + .then(response => response.json()) + .then(data => { + return { + name: data[0].Name, + location: data[0].Location // Assuming the server returns a Location property + }; + }); +} \ No newline at end of file