-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPwmControllerWebUserControl.h
111 lines (86 loc) · 3.33 KB
/
PwmControllerWebUserControl.h
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
#ifndef PWM_CONTROLLER_WEB_USER_CONTROL_H
#define PWM_CONTROLLER_WEB_USER_CONTROL_H
#include "WebUserControl.h"
class PwmControllerWebUserControl : public WebUserControl {
public:
PwmControllerWebUserControl() : WebUserControl(
endpoints,
methods,
callbacks,
4
) {
lastTimeValueRead = millis();
}
void iterate();
private:
static void getHomeCallback(AsyncWebServerRequest* request);
static void getValueCallback(AsyncWebServerRequest* request);
static void postHomeCallback(AsyncWebServerRequest* request);
static void postResetCallback(AsyncWebServerRequest* request);
static const char* endpoints[];
static const WebRequestMethod methods[];
static const ArRequestHandlerFunction callbacks[];
static String templateProcessor(const String&);
static char value;
static bool valueValid;
static unsigned long lastTimeValueRead;
static const int valueRequestDelay = 100;
};
char PwmControllerWebUserControl::value = 0;
bool PwmControllerWebUserControl::valueValid = 0;
unsigned long PwmControllerWebUserControl::lastTimeValueRead = 0;
const char* PwmControllerWebUserControl::endpoints[] = {"/", "/value", "/", "/reset"};
const WebRequestMethod PwmControllerWebUserControl::methods[] = {HTTP_GET, HTTP_GET, HTTP_POST, HTTP_POST};
const ArRequestHandlerFunction PwmControllerWebUserControl::callbacks[] = {getHomeCallback, getValueCallback, postHomeCallback, postResetCallback};
void PwmControllerWebUserControl::iterate() {
if(millis() >= lastTimeValueRead + valueRequestDelay) {
ReturnedValue err = executeCommand(CommandGetValue, nullptr, &value);
if(err) {
value = -1;
valueValid = false;
} else {
valueValid = true;
}
DBGLOG("Received value:");
DBGLOG(value);
lastTimeValueRead = millis();
}
}
String PwmControllerWebUserControl::templateProcessor(const String& var) {
if(var == "VALUE") {
return valueValid ? String(value) : "?";
}
return String();
}
void PwmControllerWebUserControl::getHomeCallback(AsyncWebServerRequest* request) {
request->send_P(200, "text/html", index_html, index_html_len, templateProcessor);
};
void PwmControllerWebUserControl::getValueCallback(AsyncWebServerRequest* request) {
request->send(200, "text/plain", String(value));
};
void PwmControllerWebUserControl::postHomeCallback(AsyncWebServerRequest* request) {
DBGLOG("Received Post Request");
int numberOfParams = request->params();
for(int i = 0; i < numberOfParams; i++) {
AsyncWebParameter* param = request->getParam(i);
if(param->isPost()) {
value = atoi(param->value().c_str());
webServer->executeCommand(CommandSetValue, &value, nullptr);
DBGLOG("Set value to");
DBGLOG(value);
}
}
request->send_P(200, "text/html", index_html, index_html_len, templateProcessor);
};
void PwmControllerWebUserControl::postResetCallback(AsyncWebServerRequest* request) {
WifiData wifiData;
wifiData.ssid[0] = '\0';
wifiData.password[0] = '\0';
wifiData.ip[0] = '\0';
wifiData.gateway[0] = '\0';
EEPROM.put(0, wifiData);
EEPROM.commit();
request->send(200, "text/plain", "Wifi Settings Reset. ESP will restart.");
ESP.restart();
};
#endif