Repository navigation
Expand file tree
/
Copy pathLastPart.js
More file actions
116 lines (99 loc) · 4.33 KB
/
Copy pathLastPart.js
File metadata and controls
116 lines (99 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Import the external CSS file
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'LastPart.css'; // Ensure this file exists in the correct directory
document.head.appendChild(link);
// WebSocket connection to your Python server
const socket = new WebSocket('ws://127.0.0.1:5001');
document.addEventListener("DOMContentLoaded", function () {
const ctx = document.getElementById('temperatureChart').getContext('2d');
let temperatureData = []; // Stores the temperature values
let timeData = []; // Stores the timestamps or X-axis labels
const maxDataPoints = 10; // Maximum number of points to display
let isPaused = false; // Pause state variable
const tempChart = new Chart(ctx, {
type: 'line', // Line chart for temperature data
data: {
labels: timeData, // X-axis labels (timestamps)
datasets: [{
label: 'Temperature (°C)',
data: temperatureData, // Y-axis data (temperature values)
fill: false,
borderColor: 'rgb(75, 192, 192)', // Line color
tension: 0.1
}]
},
options: {
responsive: true,
animation: {
duration: 500 // Smooth transitions
},
scales: {
x: {
type: 'category', // Treat X-axis as categorical (timestamps)
position: 'bottom'
},
y: {
min: 0, // Set min temperature to 0 (or adjust as needed)
max: 50 // Set max temperature (based on your sensor range)
}
}
}
});
// WebSocket event handlers
socket.onopen = function () {
console.log('WebSocket connection established');
};
socket.onmessage = function (event) {
if (!isPaused) {
console.log("Received data:", event.data);
const temperature = parseFloat(event.data); // Convert the received string to a float number
if (!isNaN(temperature)) {
const timestamp = new Date().toLocaleTimeString();
temperatureData.push(temperature);
timeData.push(timestamp);
if (temperatureData.length > maxDataPoints) {
temperatureData.shift(); // Remove the oldest temperature
timeData.shift(); // Remove the oldest timestamp
}
tempChart.data.labels = timeData;
tempChart.data.datasets[0].data = temperatureData;
tempChart.update();
// Update temperature display with white color and position
const tempDisplay = document.getElementById('data');
tempDisplay.innerText = `${temperature}°C`;
tempDisplay.style.color = "#FFFFFF";
tempDisplay.style.position = "absolute";
tempDisplay.style.top = "20px";
tempDisplay.style.left = "20px";
tempDisplay.style.fontSize = "2rem";
tempDisplay.style.fontWeight = "bold";
tempDisplay.style.zIndex = "999"; // Ensure it stays above other elements
} else {
console.warn("Invalid data received:", event.data);
}
}
};
socket.onerror = function (event) {
console.error('WebSocket error:', event);
};
socket.onclose = function () {
console.warn('WebSocket connection closed');
document.getElementById('data').innerText = "Connection lost. Reconnecting...";
setTimeout(() => {
location.reload();
}, 5000);
};
// Create and style the pause/resume button dynamically
const pauseButton = document.createElement('div');
pauseButton.id = 'pauseButton';
pauseButton.innerHTML = `<i class="fa fa-pause"></i>`; // Initial icon is pause
// Toggle pause/resume on click
pauseButton.addEventListener('click', function () {
isPaused = !isPaused;
pauseButton.innerHTML = isPaused ? `<i class="fa fa-play"></i>` : `<i class="fa fa-pause"></i>`;
console.log(isPaused ? "Chart updates paused" : "Chart updates resumed");
});
// Append button to the body once
document.body.appendChild(pauseButton);
});