Added code for handeling websocket data, And made the html code fariable in a js function

This commit is contained in:
Dano van den Bosch
2024-02-29 16:59:04 +01:00
parent 9238d9841e
commit d1714fabec
3 changed files with 229 additions and 217 deletions

View File

@@ -1,5 +1,6 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://cdn.plot.ly/plotly-latest.min.js" charset="utf-8"></script>
@@ -23,8 +24,11 @@
</nav>
</header>
<div id="nodeDataLocation">
</div>
<!-- Make a new flex container for the live data -->
<div class="nodeData">
<!-- <div class="nodeData">
<div class="flex-NodeData">
<p>Node 1</p>
@@ -51,120 +55,13 @@
</div>
</div>
</div>
<!-- Make a new flexcontainer for the graphs and API request time -->
<div class="flex-graph">
<div>
<p>Live graph:</p>
<div id="liveGraph"></div>
</div>
</div>
</div>
<div class="flex-NodeData">
<p>Node 2</p>
<div class="flex-LiveData">
<div>
<div>Temperatuur: <p id="temperature">Not connected</p>
</div>
<div class="statusElement">
<p class="statusText" id="tempStatus">Not connected</p>
</div>
</div>
<div>
<div>Luchtvochtigheid: <p id="humidity">Not connected</p>
</div>
<div class="statusElement">
<p class="statusText" id="humidStatus">Not connected</p>
</div>
</div>
<div>
<div>Lichtintensiteit: <p id="lightIntensity">Not connected</p>
</div>
<div class="statusElement">
<p class="statusText" id="lightIntensityStatus">Not connected</p>
</div>
</div>
</div>
<!-- Make a new flexcontainer for the graphs and API request time -->
<div class="flex-graph">
<div>
<p>Live graph:</p>
<div id="liveGraph2"></div>
</div>
</div>
</div>
</div> <!-- class="nodeData" -->
<div class="nodeData">
<div class="flex-NodeData">
<p>Node 3</p>
<div class="flex-LiveData">
<div>
<div>Temperatuur: <p id="temperature">Not connected</p>
</div>
<div class="statusElement">
<p class="statusText" id="tempStatus">Not connected</p>
</div>
</div>
<div>
<div>Luchtvochtigheid: <p id="humidity">Not connected</p>
</div>
<div class="statusElement">
<p class="statusText" id="humidStatus">Not connected</p>
</div>
</div>
<div>
<div>Lichtintensiteit: <p id="lightIntensity">Not connected</p>
</div>
<div class="statusElement">
<p class="statusText" id="lightIntensityStatus">Not connected</p>
</div>
</div>
</div>
<!-- Make a new flexcontainer for the graphs and API request time -->
<div class="flex-graph">
<div>
<p>Live graph:</p>
<div id="liveGraph3"></div>
</div>
</div>
</div> <div class="flex-NodeData">
<p>Node 4</p>
<div class="flex-LiveData">
<div>
<div>Temperatuur: <p id="temperature">Not connected</p>
</div>
<div class="statusElement">
<p class="statusText" id="tempStatus">Not connected</p>
</div>
</div>
<div>
<div>Luchtvochtigheid: <p id="humidity">Not connected</p>
</div>
<div class="statusElement">
<p class="statusText" id="humidStatus">Not connected</p>
</div>
</div>
<div>
<div>Lichtintensiteit: <p id="lightIntensity">Not connected</p>
</div>
<div class="statusElement">
<p class="statusText" id="lightIntensityStatus">Not connected</p>
</div>
</div>
</div>
<!-- Make a new flexcontainer for the graphs and API request time -->
<div class="flex-graph">
<div>
<p>Live graph:</p>
<div id="liveGraph4"></div>
</div>
</div>
</div>
</div> <!-- class="nodeData" -->
</div> -->
<!-- Include the js file -->
<script src="main.js"></script>

View File

@@ -6,25 +6,105 @@ let newArrayTemp = [];
let newArrayHumid = [];
let newArrayLight = [];
let timeArray = []; // Array to store time values`
let dateArray = [1, 2, 3];
let valueArray = [1, 2, 3, 4, 5];
let valueArray2 = [1, 2, 3, 4, 5];
let valueArray3 = [1, 2, 3, 4, 5];
let valueArray4 = [1, 2, 3, 4, 5];
let newValueArray = [4, 5, 6];
let myValue = 1;
let intervalDelay = 50;
//const timeArray = [1,2,3,4];
//const dateArray = [5,6,7,8];
//const valueArray = [1,1,1,3];
// const socket = new WebSocket("ws://127.0.0.1:8001");
// Create WebSocket connection.
const socket = new WebSocket("ws://145.92.8.114/ws");
/*
function openConnection() {
// Connection opened
socket.addEventListener("open", (event) => {
console.log("Connected to the WebSocket server");
socket.send("Hello Server!");
});}
// Error handling
socket.addEventListener('error', (event) => {
console.error('WebSocket error:', event);
});
// Message handling
socket.addEventListener("message", (event) => {
console.log('Message from server:', event.data);
});
openConnection();
*//////////////////////
function openConnection() {
// Open connection
socket.addEventListener("open", (event) => {
console.log("Connected to the WebSocket server");
socket.send("Hello 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) => {
console.log('Message from server:', event.data);
});
// Close handling
socket.addEventListener('close', (event) => {
console.log('Connection closed');
// Attempt to reconnect
setTimeout(openConnection, 1000); // Retry after 1 second
});
}
// Open the connection
openConnection();
// socket.onopen = function(e) {
// alert("[open] Connection established");
// alert("Sending to server");
// socket.send("My name is John");
// };
// socket.onmessage = function(event) {
// // alert(`[message] Data received from server: ${event.data}`);
// console.log("Data received from server: ", event.data);
// };
// socket.addEventListener("message", (event) => {
// console.log(event.data);
// });
// function openPort() {
// socket.addEventListener('open', function (event) {
// console.log('Connected to the WebSocket server');
// });
// console.log("Connected to the WebSocket server");
// }
// socket.addEventListener('message', function (event) {
// console.log('Message from server:', event.data);
// // Here you can handle the received message and update your UI accordingly
// });
function pushArray(array) {
for (let i = 0; i < 10; i++) {
array.push(Math.random() * 10);
}
}
// Make lines in the graph of the live data
Plotly.plot("liveGraph", [
/*Plotly.plot("liveGraph", [
{
x: timeArray, // Use timeArray as x values
y: valueArray,
@@ -34,41 +114,6 @@ Plotly.plot("liveGraph", [
},
]);
// Make lines in the graph of the live data
Plotly.plot("liveGraph2", [
{
x: timeArray, // Use timeArray as x values
y: valueArray2,
mode: "lines",
line: { color: "#80CAF6" },
name: "Temperature",
},
]);
// Make lines in the graph of the live data
Plotly.plot("liveGraph3", [
{
x: timeArray, // Use timeArray as x values
y: valueArray3,
mode: "lines",
line: { color: "#80CAF6" },
name: "Temperature",
},
]);// Make lines in the graph of the live data
Plotly.plot("liveGraph4", [
{
x: timeArray, // Use timeArray as x values
y: valueArray4,
mode: "lines",
line: { color: "#80CAF6" },
name: "Temperature",
},
]);
// Make lines in the graph of the live data
let cnt = 0;
// Update the graph every 1 ms
@@ -97,80 +142,111 @@ let interval = setInterval(function () {
if (cnt === 10) clearInterval(interval);
}, intervalDelay);
// Update the graph every 1 ms
let interval2 = setInterval(function () {
var time = new Date();
timeArray.push(new Date());
*/
pushArray(valueArray2);
//function for making the html elements for the following html code
var update = {
x: [[time]],
y: [[newValueArray]],
};
function nodeData(data, node) {
let nodeData = document.createElement("div");
nodeData.innerHTML = data;
// nodeData.setAttribute("id", "node" + node);
document.body.appendChild(nodeData);
// console.log("Hello World");
}
var olderTime = time.setMinutes(time.getMinutes() - 1);
var futureTime = time.setMinutes(time.getMinutes() + 1);
// nodeData("Temperature: ", 1);
// nodeData("Humidity: ", 2);
var minuteView = {
xaxis: {
type: "date",
range: [olderTime, futureTime],
},
};
Plotly.relayout("liveGraph2", minuteView);
if (cnt === 1000) clearInterval(interval2);
}, intervalDelay);
function createNodeData(node) {
// Create main div
var nodeData = document.createElement("div");
nodeData.className = "nodeData";
// Update the graph every 1 ms
let interval3 = setInterval(function () {
var time = new Date();
timeArray.push(new Date());
// Create flex-NodeData div
var flexNodeData = document.createElement("div");
flexNodeData.className = "flex-NodeData";
pushArray(valueArray3);
// Create p element
var pNode = document.createElement("p");
pNode.textContent = "Node " + node;
var update = {
x: [[time]],
y: [[newValueArray]],
};
// Append p to flex-NodeData
flexNodeData.appendChild(pNode);
var olderTime = time.setMinutes(time.getMinutes() - 1);
var futureTime = time.setMinutes(time.getMinutes() + 1);
// Create flex-LiveData div
var flexLiveData = document.createElement("div");
flexLiveData.className = "flex-LiveData";
var minuteView = {
xaxis: {
type: "date",
range: [olderTime, futureTime],
},
};
Plotly.relayout("liveGraph3", minuteView);
// Create data divs (Temperature, Humidity, Light Intensity)
var dataTypes = ["Temperatuur", "Luchtvochtigheid", "Lichtintensiteit"];
var ids = ["temperature", "humidity", "lightIntensity"];
var statusIds = ["tempStatus", "humidStatus", "lightIntensityStatus"];
if (cnt === 1000) clearInterval(interval3);
}, intervalDelay);
for (var i = 0; i < dataTypes.length; i++) {
var dataDiv = document.createElement("div");
// Update the graph every 1 ms
let interval4 = setInterval(function () {
var time = new Date();
timeArray.push(new Date());
var dataTypeDiv = document.createElement("div");
dataTypeDiv.textContent = dataTypes[i] + ": ";
pushArray(valueArray4);
var pElement = document.createElement("p");
pElement.id = ids[i];
pElement.textContent = "Not connected";
var update = {
x: [[time]],
y: [[newValueArray]],
};
dataTypeDiv.appendChild(pElement);
dataDiv.appendChild(dataTypeDiv);
var olderTime = time.setMinutes(time.getMinutes() - 1);
var futureTime = time.setMinutes(time.getMinutes() + 1);
var statusElement = document.createElement("div");
statusElement.className = "statusElement";
var minuteView = {
xaxis: {
type: "date",
range: [olderTime, futureTime],
},
};
Plotly.relayout("liveGraph4", minuteView);
var statusText = document.createElement("p");
statusText.className = "statusText";
statusText.id = statusIds[i];
statusText.textContent = "Not connected";
if (cnt === 1000) clearInterval(interval4);
}, intervalDelay);
statusElement.appendChild(statusText);
dataDiv.appendChild(statusElement);
flexLiveData.appendChild(dataDiv);
}
// Append flex-LiveData to flex-NodeData
flexNodeData.appendChild(flexLiveData);
// Create flex-graph div
var flexGraph = document.createElement("div");
flexGraph.className = "flex-graph";
var graphDiv = document.createElement("div");
var graphP = document.createElement("p");
graphP.textContent = "Live graph:";
var liveGraph = document.createElement("div");
liveGraph.id = "liveGraph";
graphDiv.appendChild(graphP);
graphDiv.appendChild(liveGraph);
flexGraph.appendChild(graphDiv);
// Append flex-graph to flex-NodeData
flexNodeData.appendChild(flexGraph);
// Append flex-NodeData to main div
nodeData.appendChild(flexNodeData);
// Check if nodeDataLocation element exists
var nodeDataLocation = document.getElementById("nodeDataLocation");
if (nodeDataLocation) {
// Append main div to nodeDataLocation
nodeDataLocation.appendChild(nodeData);
} else {
console.error("Element with ID 'nodeDataLocation' does not exist.");
}
}
// Call the function to create the HTML structure
createNodeData(1);
createNodeData(2);
createNodeData(3);
createNodeData(4);

View File

@@ -43,21 +43,56 @@ p1 {
border: solid #1f82d3 2px;
border-radius: 10px;
width: 90%;
}
.statusText{
font-size: 20px;
}
/* body{
display: flex;
flex-direction: row;
justify-content: space-evenly;
align-content: center;
} */
#randomShit{
display: flex;
flex-direction: row;
justify-content: space-evenly;
align-content: center;
border: 2px solid purple;
}
#nodeDataLocation{
display: flex;
flex-direction: column;
align-items: center;
/* justify-content: center; */
/* align-content: center; */
border: 4px solid blue;
/* padding-bottom: 50%; */
}
.flex-NodeData {
display: flex;
margin-left: 1%;
margin-right: 1%;
width: 50%;
height: 40%;
justify-content: space-evenly;
/* height: 40%; */
flex-direction: column;
border: 3px solid #1f82d3;
border-radius: 20px;
/* border-radius: 20px; */
/* border: 2px solid red; */
/* margin-right: 90%; */
/* width: 150vh; */
/* padding-right: 40%; */
/* padding-left: 40%; */
}
.nodeData {
@@ -78,6 +113,8 @@ justify-content: left;
justify-content: space-evenly;
gap: 5px;
padding: 10px;
}
@@ -89,6 +126,8 @@ justify-content: left;
border-radius: 10px;
height: fit-content;
width: 30%;
}
.flex-graph {