-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondWebpage.cpp
More file actions
143 lines (127 loc) · 6.49 KB
/
SecondWebpage.cpp
File metadata and controls
143 lines (127 loc) · 6.49 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <ESPAsyncWebServer.h>
#include "SecondWebpage.h"
#include <ArduinoJson.h> // Make sure you have this library for JSON handling
extern AsyncWebServer server; // Ensure server is declared somewhere globally
// Updated HTML and CSS for the second page with improved layout
const char* secondPageHtml = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<link rel="icon" type="image/png" sizes="48x48" href="/favicon.png">
<title>ESP Web Server/Logs</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: Arial, sans-serif; text-align: center; }
#updateAllButton { margin-bottom: 20px; }
#buttonContainer { text-align: center; }
#filesContainer, #downloadFilesButton { display: none; margin-top: 10px; }
#pumpGrid { display: grid; grid-template-columns: repeat(5, 1fr); text-align: left; margin: auto; max-width: 800px; }
.grid-header { font-weight: bold; padding: 10px 0; }
.grid-cell { padding: 5px 0; }
</style>
</head>
<body>
<h2>****Pump Runtimes****</h2>
<button id="updateAllButton">Update All</button>
<div id="pumpGrid">
<div class="grid-header">Pump</div>
<div class="grid-header">Today</div>
<div class="grid-header">Current Month</div>
<div class="grid-header">Current Year</div>
<div class="grid-header">Total</div>
</div>
<div id="buttonContainer">
<button id="listFilesButton">List Files</button><br>
<button id="downloadFilesButton">Download Files</button>
</div>
<div id="filesContainer"></div> <!-- Container for the file list -->
<script>
function formatRuntime(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = seconds % 60;
return `${hours}h ${minutes}m ${secs}s`;
}
document.addEventListener('DOMContentLoaded', function () {
const ws = new WebSocket('ws://' + window.location.hostname + '/ws');
const pumps = 10; // Number of pumps
const pumpGrid = document.getElementById('pumpGrid');
ws.onopen = function() {
console.log('WebSocket connection established');
ws.send('updateAllRuntimes'); // Automatically update all runtimes upon connection
};
ws.onmessage = function(event) {
console.log('WebSocket message received:', event.data);
if (event.data.startsWith("JSON:")) {
const data = JSON.parse(event.data.substring(5));
data.data.forEach(pump => {
document.getElementById(`pump${pump.pumpIndex}-day`).textContent = formatRuntime(pump.day);
document.getElementById(`pump${pump.pumpIndex}-month`).textContent = formatRuntime(pump.month);
document.getElementById(`pump${pump.pumpIndex}-year`).textContent = formatRuntime(pump.year);
document.getElementById(`pump${pump.pumpIndex}-total`).textContent = formatRuntime(pump.total);
});
}
};
for (let i = 1; i <= pumps; i++) {
const rowHTML = `<div class="grid-cell">Pump ${i}</div>
<div class="grid-cell" id="pump${i}-day">--</div>
<div class="grid-cell" id="pump${i}-month">--</div>
<div class="grid-cell" id="pump${i}-year">--</div>
<div class="grid-cell" id="pump${i}-total">--</div>`;
pumpGrid.insertAdjacentHTML('beforeend', rowHTML);
}
document.getElementById('updateAllButton').addEventListener('click', function() {
console.log("Updating all runtimes...");
ws.send('updateAllRuntimes');
});
document.getElementById('listFilesButton').addEventListener('click', function() {
const container = document.getElementById('filesContainer');
const downloadButton = document.getElementById('downloadFilesButton');
// Toggle visibility
if (container.style.display === 'none' || container.style.display === '') {
fetchFileList(); // Call fetchFileList to update the file list
container.style.display = 'block';
downloadButton.style.display = 'inline'; // Show download button when listing files
} else {
container.style.display = 'none';
downloadButton.style.display = 'none'; // Hide download button when not listing files
}
});
function fetchFileList() {
fetch('/list-logs')
.then(response => response.json())
.then(files => {
const list = document.getElementById('filesContainer');
list.innerHTML = ''; // Clear current list
files.forEach(file => {
const label = document.createElement('label');
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.value = file;
label.appendChild(checkbox);
label.appendChild(document.createTextNode(file));
list.appendChild(label);
list.appendChild(document.createElement('br'));
});
});
}
document.getElementById('downloadFilesButton').addEventListener('click', function() {
downloadSelected();
});
function downloadSelected() {
const checkboxes = document.querySelectorAll('#filesContainer input[type="checkbox"]:checked');
checkboxes.forEach(checkbox => {
const file = checkbox.value;
window.open('/download-log?file=' + encodeURIComponent(file), '_blank');
});
}
});
</script>
</body>
</html>
)rawliteral";
void setupSecondPageRoutes() {
server.on("/second-page", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/html", secondPageHtml);
});
}