Repository navigation
Expand file tree
/
Copy pathtest_handlers.cpp
More file actions
92 lines (73 loc) · 1.87 KB
/
Copy pathtest_handlers.cpp
File metadata and controls
92 lines (73 loc) · 1.87 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
#include <Arduino.h>
#include "api/commands.h"
#include "display/oled.h"
#include "http/handlers.h"
#include "http/request.h"
#include "http/response.h"
namespace {
JsonDocument testReply(const char* test) {
JsonDocument reply;
reply["ok"] = true;
reply["test"] = test;
return reply;
}
void finishTest(WebServer& server, const JsonDocument& reply) {
showIdleScreen();
sendJson(server, 200, reply);
}
void runNamedTest(WebServer& server, const char* name) {
const ApiResult result = apiRunHardwareTest(name);
if (!result.ok) {
showIdleScreen();
sendError(server, result.code, result.error);
return;
}
finishTest(server, testReply(name));
}
} // namespace
void handleAudioTest(WebServer& server) {
runNamedTest(server, "audio");
}
void handleBellTest(WebServer& server) {
runNamedTest(server, "bell");
}
void handleScreenTest(WebServer& server) {
runNamedTest(server, "screen");
}
void handleMovementTest(WebServer& server) {
runNamedTest(server, "movement");
}
void handleLedTest(WebServer& server) {
runNamedTest(server, "led");
}
void handleServoTest(WebServer& server) {
JsonDocument request;
if (!readJsonBody(server, request)) {
return;
}
const std::optional<ServoMove> move = ServoMove::read(server, request.as<JsonObjectConst>());
if (!move) {
return;
}
const ApiResult result = apiMoveServo(move->index, move->angle);
if (!result.ok) {
showIdleScreen();
sendError(server, result.code, result.error);
return;
}
JsonDocument reply = testReply("servo");
move->addTo(reply);
finishTest(server, reply);
}
void handleServosPark(WebServer& server) {
const ApiResult result = apiParkServos();
if (!result.ok) {
sendError(server, result.code, result.error);
return;
}
JsonDocument reply;
reply["ok"] = true;
reply["park"] = "servos";
reply["hold"] = true;
sendJson(server, 200, reply);
}