forked from shashwat001/hollow_clock_wifi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.cpp
More file actions
312 lines (296 loc) · 7.85 KB
/
Copy pathdashboard.cpp
File metadata and controls
312 lines (296 loc) · 7.85 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "dashboard.h"
#include <string>
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
:root {
--bg: #f3f6fb;
--bg-start: #ffffff;
--card: #ffffff;
--text: #1f2937;
--muted: #64748b;
--accent: #0f766e;
--accent-soft: #ccfbf1;
--border: #dbe4ee;
--danger: #b91c1c;
--danger-bg: #fee2e2;
--shadow: 0 10px 28px rgba(15, 23, 42, 0.08);
}
* {
box-sizing: border-box;
}
html {
font-family: "Segoe UI", "Noto Sans", "Helvetica Neue", sans-serif;
font-size: 16px;
color: var(--text);
background: radial-gradient(circle at top left, var(--bg-start) 0%, var(--bg) 55%);
}
body {
margin: 0;
padding: 1.2rem;
}
.page {
width: min(860px, 100%);
margin: 0 auto;
background: var(--card);
border: 1px solid var(--border);
border-radius: 16px;
box-shadow: var(--shadow);
overflow: hidden;
}
.header {
padding: 1.1rem 1.2rem;
background: linear-gradient(135deg, #0f766e 0%, #115e59 100%);
color: #f8fafc;
border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}
.header h2 {
margin: 0;
font-size: 1.25rem;
font-weight: 700;
letter-spacing: 0.02em;
}
.list {
padding: 0.85rem;
display: grid;
gap: 0.6rem;
}
.entry {
display: flex;
align-items: center;
gap: 0.55rem;
padding: 0.65rem 0.8rem;
border: 1px solid var(--border);
border-radius: 12px;
background: #ffffff;
min-height: 3rem;
line-height: 1.35;
}
.label {
display: inline-block;
min-width: 10rem;
color: var(--muted);
font-size: 0.86rem;
font-weight: 600;
letter-spacing: 0.03em;
text-transform: uppercase;
}
.value {
font-size: 1.05rem;
font-weight: 600;
color: var(--text);
word-break: break-word;
}
.static-value {
display: inline-flex;
align-items: center;
flex-wrap: wrap;
gap: 0.55rem;
color: var(--text);
}
.calibration-controls {
display: inline-flex;
align-items: center;
flex-wrap: wrap;
gap: 0.65rem;
}
.activity {
font-size: 0.72rem;
color: var(--accent);
min-width: 0.6rem;
}
.error {
background-color: var(--danger-bg);
color: var(--danger);
border-radius: 6px;
padding: 0.1rem 0.35rem;
}
form {
display: inline-flex;
align-items: center;
gap: 0.45rem;
margin: 0;
flex-wrap: wrap;
}
input[type='number'] {
width: 4.2rem;
height: 2rem;
border: 1px solid var(--border);
border-radius: 8px;
padding: 0 0.45rem;
font-size: 0.95rem;
font-weight: 600;
color: var(--text);
background: #f8fbff;
}
input[type='number']:focus {
outline: none;
border-color: var(--accent);
box-shadow: 0 0 0 2px rgba(15, 118, 110, 0.16);
background: #ffffff;
}
button {
height: 2rem;
border: none;
border-radius: 8px;
padding: 0 0.8rem;
background: var(--accent);
color: #f8fafc;
font-size: 0.9rem;
font-weight: 700;
cursor: pointer;
}
button:active {
transform: translateY(1px);
}
strong {
background: var(--accent-soft);
color: #115e59;
border-radius: 999px;
padding: 0.3rem 0.55rem;
font-size: 0.8rem;
font-weight: 700;
}
@media (max-width: 640px) {
body {
padding: 0.7rem;
}
.entry {
align-items: flex-start;
flex-direction: column;
}
.label {
min-width: 0;
}
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #0f172a;
--bg-start: #111827;
--card: #111827;
--text: #e5e7eb;
--muted: #9ca3af;
--accent: #14b8a6;
--accent-soft: #134e4a;
--border: #1f2937;
--danger: #fecaca;
--danger-bg: #7f1d1d;
--shadow: 0 12px 30px rgba(2, 6, 23, 0.65);
}
.entry {
background: #0b1220;
}
.header {
border-bottom-color: rgba(255, 255, 255, 0.08);
}
input[type='number'] {
background: #0f172a;
}
input[type='number']:focus {
background: #0b1220;
}
strong {
color: #99f6e4;
}
}
</style>
</head>
<body>
<div class='page'>
<div class='header'>
<h2>Hollow Clock Dashboard</h2>
</div>
<div class='list'>
%LIST%
</div>
</div>
</body>
<script>
function showActivity(id) {
document.getElementById('activity-' + id).innerHTML = '^';
setTimeout(function() {
document.getElementById('activity-' + id).innerHTML = '';
}, 500);
}
function getValue(id) {
var xhttp = new XMLHttpRequest();
xhttp.timeout = 10 * 1000;
xhttp.onload = function() {
document.getElementById("value-" + id).innerHTML = this.responseText;
document.getElementById("value-" + id).classList.remove('error');
showActivity(id);
};
let errorFn = function() {
document.getElementById("value-" + id).classList.add('error');
};
xhttp.onerror = errorFn;
xhttp.onabort = errorFn;
xhttp.ontimeout = errorFn;
xhttp.open("GET", "/get-" + id, true);
xhttp.send();
}
%SCRIPT%
</script>
</html>)rawliteral";
String list;
String script;
String processor(const String &var) {
if (var == "LIST") {
return list;
} else if (var == "SCRIPT") {
return script;
} else {
Serial.println("Warning: unhandled template string: " + var);
}
return String();
}
void Dashboard::Add(std::string name, std::function<std::string()> callback,
uint32_t refresh_millis) {
// Add element to template
std::ostringstream list_stream;
list_stream << "<div class='entry'><span class='label'>" << name
<< "</span><span id='value-" << std::dec << next_id_
<< "' class='value'></span><span class='activity' id='activity-"
<< next_id_ << "'></span></div>\n";
list += list_stream.str().c_str();
uint32_t initial_delay = 75U * static_cast<uint32_t>(next_id_);
std::ostringstream script_stream;
script_stream << "setTimeout(function() { getValue('" << std::dec << next_id_
<< "'); }, " << initial_delay << ");\n";
script_stream << "setTimeout(function poll" << std::dec << next_id_
<< "() { getValue('" << next_id_ << "'); setTimeout(poll"
<< next_id_ << ", " << refresh_millis << "); }, "
<< (initial_delay + refresh_millis) << ");\n";
script += script_stream.str().c_str();
// Register endpoint for getting value
String path = String("/get-") + String(next_id_);
server_->on(path.c_str(), HTTP_GET,
[callback, this](AsyncWebServerRequest *request) {
last_used_ = millis();
std::string value = callback();
request->send(200, "text/plain", value.c_str());
});
next_id_++;
}
void Dashboard::AddStaticHtml(std::string name, std::string html) {
std::ostringstream list_stream;
list_stream << "<div class='entry'><span class='label'>" << name
<< "</span><span class='static-value'>" << html
<< "</span></div>\n";
list += list_stream.str().c_str();
}
void Dashboard::Add(std::string name, bool &value_ptr,
uint32_t refresh_millis) {
Add(
name, [&value_ptr]() { return value_ptr ? "true" : "false"; },
refresh_millis);
}
uint32_t Dashboard::last_used() { return last_used_; }
Dashboard::Dashboard(AsyncWebServer *server) : server_(server) {
server->on("/", HTTP_GET, [this](AsyncWebServerRequest *request) {
last_used_ = millis();
request->send_P(200, "text/html", index_html, processor);
});
}