added location and nodename

This commit is contained in:
2024-03-28 13:32:22 +01:00
parent c6f3c7c018
commit 1c13d20b49
2 changed files with 59 additions and 45 deletions

View File

@@ -1,17 +1,17 @@
class GaugeGroup { class GaugeGroup {
constructor(nodeId, gaugesCount, maxGaugeValues, dataTypes) { constructor(nodeId, Location, gaugesCount, maxGaugeValues, dataTypes) {
this.nodeId = nodeId; this.nodeId = nodeId;
this.gaugesCount = gaugesCount; this.gaugesCount = gaugesCount;
this.maxGaugeValues = maxGaugeValues; // Maximum value the gauge can display this.maxGaugeValues = maxGaugeValues; // Maximum value the gauge can display
this.dataTypes = dataTypes; // Array of data type names for each gauge this.dataTypes = dataTypes; // Array of data type names for each gauge
this.location = Location;
// Create a new div element // Create a new div element
this.element = document.createElement("div"); this.element = document.createElement("div");
this.element.className = "gaugeGroup"; this.element.className = "gaugeGroup";
// Set the HTML of the new div // Set the HTML of the new div
this.element.innerHTML = ` this.element.innerHTML = `
<h2 class="groupTitle">Gauge Group for Node ${this.nodeId}</h2> <h2 class="groupTitle">${this.nodeId} - ${this.location}</h2>
<div class="Node"> <div class="Node">
${Array(this.gaugesCount).fill().map((_, i) => ` ${Array(this.gaugesCount).fill().map((_, i) => `
<div class="Sensorvalues"> <div class="Sensorvalues">

View File

@@ -46,49 +46,52 @@ function openConnection() {
openConnection(); openConnection();
function handleIncomingData(data) { async function handleIncomingData(data) {
if (!data.node || !data.Temp || !data.Humi || !data.eCO2 || !data.TVOC) { if (!data.node || !data.Temp || !data.Humi || !data.eCO2 || !data.TVOC) {
console.error('Invalid data received:', data); console.error('Invalid data received:', data);
return; return;
} }
nodeAdressHandler(data.node, Object.keys(data).filter(key => key !== 'node')); await nodeAdressHandler(data.node, Object.keys(data).filter(key => key !== 'node'));
nodeNumber = nodeDict[data.node]; let nodeName = nodeDict[data.node];
temperature = data.Temp; let temperature = data.Temp;
humidity = data.Humi; let humidity = data.Humi;
CO2 = data.eCO2; let CO2 = data.eCO2;
TVOC = data.TVOC; let TVOC = data.TVOC;
// Update the gauges with the new data // Update the gauges with the new data
sensorData[data.node].updateGauge(1, temperature); if (sensorData[nodeName]) {
sensorData[data.node].updateGauge(2, humidity); sensorData[nodeName].updateGauge(1, temperature);
sensorData[data.node].updateGauge(3, CO2); sensorData[nodeName].updateGauge(2, humidity);
sensorData[data.node].updateGauge(4, TVOC); sensorData[nodeName].updateGauge(3, CO2);
sensorData[nodeName].updateGauge(4, TVOC);
} else {
console.error('No sensor data for node:', nodeName);
}
} }
function nodeAdressHandler(node, dataTypes) { async function nodeAdressHandler(node, dataTypes) {
if (!nodeArray.includes(node)) { let nodeInfo = await getNodeInfo(node);
nodeArray.push(node); let nodeName = nodeInfo.name;
nodeDict[node] = nodeArray.length; let nodeLocation = nodeInfo.location;
if (!nodeArray.includes(nodeName)) {
// Define the maximum values for each data type nodeArray.push(nodeName);
let maxGaugeValues = dataTypes.map(dataType => { nodeDict[node] = nodeName;
switch (dataType) {
case 'Temp': return 50; let maxGaugeValues = dataTypes.map(dataType => {
case 'Humi': return 100; switch (dataType) {
case 'eCO2': return 3000; case 'Temp': return 50;
case 'TVOC': return 2200; case 'Humi': return 100;
default: 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);
let gaugeGroup = new GaugeGroup(nodeName, nodeLocation, dataTypes.length, maxGaugeValues, dataTypes);
// Store the GaugeGroup in the sensorData object for later use sensorData[nodeName] = gaugeGroup;
sensorData[node] = gaugeGroup; }
}
} }
function createGauge(node, dataType) { function createGauge(node, dataType) {
@@ -112,4 +115,15 @@ function updateGauge(nodeNumber, dataType, value) {
// Update the gauge here // Update the gauge here
let gauge = sensorData[nodeNumber][dataType]; // Get the gauge from the sensorData object let gauge = sensorData[nodeNumber][dataType]; // Get the gauge from the sensorData object
gauge.update(value); // Assuming the Gauge class has an update method gauge.update(value); // Assuming the Gauge class has an update method
} }
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
};
});
}