Add new website files and classes
This commit is contained in:
129
web/newWebsite/main.js
Normal file
129
web/newWebsite/main.js
Normal file
@@ -0,0 +1,129 @@
|
||||
// Description: Main JavaScript file for the web application.
|
||||
// arrays and stuff
|
||||
const sensorData = {};
|
||||
let liveGraphs = [];
|
||||
let nodeArray = [];
|
||||
let nodeDict = {};
|
||||
|
||||
// letiables
|
||||
let intervalDelay = 5000;
|
||||
let amountOfNodes = 3;
|
||||
|
||||
const socket = new WebSocket("ws://145.92.8.114/ws");
|
||||
function openConnection() {
|
||||
// Open connection
|
||||
socket.addEventListener("open", (event) => {
|
||||
console.log("Connected to the WebSocket server");
|
||||
});
|
||||
|
||||
// Error handling
|
||||
socket.addEventListener('error', (event) => {
|
||||
console.error('WebSocket error:', event);
|
||||
// Attempt to reconnect
|
||||
setTimeout(openConnection, 1000); // Retry after 1 second
|
||||
});
|
||||
|
||||
// Message handling
|
||||
socket.addEventListener("message", (event) => {
|
||||
try {
|
||||
const jsonData = JSON.parse(event.data);
|
||||
// Use the parsed JSON data as needed
|
||||
handleIncomingData(jsonData);
|
||||
|
||||
} catch (error) {
|
||||
console.error("Error parsing JSON:", error);
|
||||
}
|
||||
});
|
||||
|
||||
// Close handling
|
||||
socket.addEventListener('close', (event) => {
|
||||
console.log('Connection closed');
|
||||
// Attempt to reconnect
|
||||
setTimeout(openConnection, 1000); // Retry after 1 second
|
||||
});
|
||||
console.log("Connected to the WebSocket server");
|
||||
}
|
||||
|
||||
openConnection();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
// Create a new gauge here
|
||||
let gauge = new GaugeGroup(node, dataType); // Assuming Gauge is the name of the class
|
||||
sensorData[node][dataType] = gauge; // Store the gauge in the sensorData object for later use
|
||||
}
|
||||
|
||||
|
||||
//function for making the html elements for the following html code
|
||||
function nodeData(data) {
|
||||
let nodeData = document.createElement("div");
|
||||
nodeData.innerHTML = data;
|
||||
// nodeData.setAttribute("id", "node" + node);
|
||||
document.body.appendChild(nodeData);
|
||||
// console.log("Hello World");
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
function getNodeInfo(node){
|
||||
return fetch("http://145.92.8.114/getNodeInfo?macAdress=" + node)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
return {
|
||||
name: data[0].Name,
|
||||
location: data[0].Location // Assuming the server returns a Location property
|
||||
};
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user