-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestsMgr.cpp
More file actions
241 lines (211 loc) · 5.66 KB
/
RequestsMgr.cpp
File metadata and controls
241 lines (211 loc) · 5.66 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
/*
RequestsMgr.cpp - Support Library to manage http requests.
*/
#include "RequestsMgr.h"
#include <ESP8266WebServer.h>
#include <stdio.h>
// Constructors
RequestsMgr::RequestsMgr(uint8_t serverPort,
Controller *controller)
: _webServer(serverPort),
_controller(controller)
{
}
// Public
void RequestsMgr::setup()
{
_webServer.on("/", std::bind(&RequestsMgr::handleRoot, this));
_webServer.on("/info", std::bind(&RequestsMgr::handleInfo, this));
_webServer.on("/controlMode", std::bind(&RequestsMgr::handleControlMode, this));
_webServer.on("/schedule", std::bind(&RequestsMgr::handleSchedule, this));
_webServer.onNotFound(std::bind(&RequestsMgr::handleNotFound, this));
_webServer.begin();
}
void RequestsMgr::tick()
{
_webServer.handleClient();
}
// Private
void RequestsMgr::handleRoot()
{
_webServer.send(this->OK,
this->CONTENT_TYPE,
"Coffee Machine says hi!");
}
void RequestsMgr::handleInfo()
{
char res[100];
snprintf(res,
sizeof(res),
"Info:\n-Mode: %s\n-IsReady: %s",
_controller->showMode(),
_controller->isReady() ? "Yes" : "No");
_webServer.send(this->OK,
this->CONTENT_TYPE,
res);
}
void RequestsMgr::handleControlMode()
{
if (_webServer.method() == HTTP_GET)
{
getControlMode();
}
else if (_webServer.method() == HTTP_POST)
{
setControlMode();
}
else
{
_webServer.send(this->METHOD_NOT_ALLOWED,
this->CONTENT_TYPE,
"Expecting 'GET' or 'POST' verb.");
}
}
void RequestsMgr::handleSchedule()
{
if (_webServer.method() == HTTP_GET)
{
getSchedule();
}
else if (_webServer.method() == HTTP_POST)
{
setSchedule();
}
else
{
_webServer.send(this->METHOD_NOT_ALLOWED,
this->CONTENT_TYPE,
"Expecting 'GET' or 'POST' verb.");
}
}
void RequestsMgr::handleNotFound()
{
_webServer.send(this->NOT_FOUND,
this->CONTENT_TYPE,
"Woohaa horsy, coffee machine has no idea what you are on about!");
}
void RequestsMgr::getControlMode()
{
char res[100];
snprintf(res,
sizeof(res),
"ControlMode is '%s'",
_controller->showMode());
_webServer.send(this->OK,
this->CONTENT_TYPE,
res);
}
void RequestsMgr::setControlMode()
{
if (!_webServer.hasArg("controlMode"))
{
_webServer.send(this->BAD_REQUEST,
this->CONTENT_TYPE,
"Expecting a 'controlMode' argument.");
return;
}
const String controlModeArgValue = _webServer.arg("controlMode");
char requestedMode[200];
controlModeArgValue.toCharArray(requestedMode, sizeof(requestedMode));
ControlMode nextMode;
if (controlModeArgValue == "Off")
{
nextMode = ControlMode::off;
}
else if (controlModeArgValue == "Schedule")
{
nextMode = ControlMode::schedule_check;
}
else if (controlModeArgValue == "On")
{
if (!_controller->isReady())
{
_webServer.send(this->FORBIDDEN,
this->CONTENT_TYPE,
"Machine cannot be started if not physically flagged as ready.");
return;
}
nextMode = ControlMode::on;
}
else
{
char res[100];
snprintf(res,
sizeof(res),
"'%s' is not a valid 'controlMode' argument value.",
requestedMode);
_webServer.send(this->BAD_REQUEST,
this->CONTENT_TYPE,
res);
}
_controller->setMode(nextMode);
char res[100];
snprintf(res,
sizeof(res),
"ControlMode set to '%s'.",
requestedMode);
_webServer.send(this->OK,
this->CONTENT_TYPE,
res);
}
void RequestsMgr::getSchedule()
{
_webServer.send(this->OK,
this->CONTENT_TYPE,
_controller->getSchedule());
}
void RequestsMgr::setSchedule()
{
if (!_webServer.hasArg("hour"))
{
_webServer.send(this->BAD_REQUEST,
this->CONTENT_TYPE,
"Expecting a 'hour' argument.");
return;
}
if (!_webServer.hasArg("minute"))
{
_webServer.send(this->BAD_REQUEST,
this->CONTENT_TYPE,
"Expecting a 'minute' argument.");
return;
}
if (!_webServer.hasArg("keepWarmDuration"))
{
_webServer.send(this->BAD_REQUEST,
this->CONTENT_TYPE,
"Expecting a 'keepWarmDuration' argument.");
return;
}
const String hourArgValue = _webServer.arg("hour");
const String minuteArgValue = _webServer.arg("minute");
const String keepWarmDurationArgValue = _webServer.arg("keepWarmDuration");
if (hourArgValue.isEmpty())
{
_webServer.send(this->BAD_REQUEST,
this->CONTENT_TYPE,
"'hour' argument cannot be left empty.");
return;
}
if (minuteArgValue.isEmpty())
{
_webServer.send(this->BAD_REQUEST,
this->CONTENT_TYPE,
"'minute' argument cannot be left empty.");
return;
}
if (keepWarmDurationArgValue.isEmpty())
{
_webServer.send(this->BAD_REQUEST,
this->CONTENT_TYPE,
"'keepWarmDuration' argument cannot be left empty.");
return;
}
const uint8_t hour = (uint8_t)hourArgValue.toInt();
const uint8_t minute = (uint8_t)minuteArgValue.toInt();
const uint8_t keepWarmDuration = (uint8_t)keepWarmDurationArgValue.toInt();
_controller->setSchedule(hour, minute, keepWarmDuration);
_webServer.send(this->OK,
this->CONTENT_TYPE,
"New schedule applied");
}