forked from shashwat001/hollow_clock_wifi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.h
More file actions
68 lines (55 loc) · 1.8 KB
/
Copy pathdashboard.h
File metadata and controls
68 lines (55 loc) · 1.8 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
#ifndef DASHBOARD
#define DASHBOARD
#include <ESPAsyncWebSrv.h>
#include <functional>
#include <sstream>
class Dashboard {
public:
Dashboard(AsyncWebServer *server);
// Adds a dashboard entry. The callback is called to get the value to
// display. The javascript refreshes the value every refresh_millis.
void Add(std::string name, std::function<std::string()> callback,
uint32_t refresh_millis);
void AddStaticHtml(std::string name, std::string html);
// Convenience functions that use standard conversions.
void Add(std::string name, bool &value_ptr, uint32_t refresh_millis);
template <typename Type>
void Add(std::string name, Type &value_ptr, uint32_t refresh_millis);
template <typename Type>
void Add(std::string name, std::function<Type()> callback,
uint32_t refresh_millis);
// Returns the last time (in millis since startup) that the server was
// used. If wifi sleep is enabled, the server may be very slow to respond.
// You can use this to disable wifi sleep, WiFi.setSleep(false) when the
// dashboard is being used.
uint32_t last_used();
private:
int next_id_ = 0;
uint32_t last_used_ = 0;
AsyncWebServer *const server_;
};
template <typename Type>
void Dashboard::Add(std::string name, Type &value_ptr,
uint32_t refresh_millis) {
Add(
name,
[&value_ptr]() {
std::ostringstream stream;
stream << value_ptr;
return stream.str();
},
refresh_millis);
}
template <typename Type>
void Dashboard::Add(std::string name, std::function<Type()> callback,
uint32_t refresh_millis) {
Add(
name,
[callback]() {
std::ostringstream stream;
stream << callback();
return stream.str();
},
refresh_millis);
}
#endif // DASHBOARD