-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.ino
92 lines (76 loc) · 2.2 KB
/
api.ino
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 "headers.h"
void routeHealth() {
StaticJsonDocument<500> json;
String output;
int seconds = millis() / 1000;
int minutes = seconds / 60;
int hours = minutes / 60;
json["firmware"] = FIRMWARE_VERSION;
json["uptime"]["hours"] = hours;
json["uptime"]["minutes"] = minutes % 60;
json["uptime"]["seconds"] = seconds % 60;
serializeJson(json, output);
httpServer.send(200, "application/json", output);
output = String();
}
void routeSystemJs() {
char output[500];
int seconds = millis() / 1000;
int minutes = seconds / 60;
int hours = minutes / 60;
snprintf(
output,
500,
"window.ESPSystem = {\n\
uptime: \"%02d:%02d:%02d\",\n\
coreVersion: \"%s\",\n\
fullVersion: \"%s\",\n\
freeHeap: %d,\n\
cpuFreqMHz: %d,\n\
flashChipSize: %d,\n\
flashChipRealSize: %d,\n\
sketchSize: %d,\n\
};",
hours, minutes % 60, seconds % 60,
ESP.getCoreVersion().c_str(),
ESP.getFullVersion().c_str(),
ESP.getFreeHeap(),
ESP.getCpuFreqMHz(),
ESP.getFlashChipSize(),
ESP.getFlashChipRealSize(),
ESP.getSketchSize()
);
httpServer.send(200, "application/javascript", output);
}
void routeGetPinState() {
if (!httpServer.authenticate(AUTH_USERNAME, AUTH_PASSWORD)) {
return httpServer.requestAuthentication();
}
StaticJsonDocument<64> response;
String output;
response["state"] = !getPinState(LED_PIN) ? 1 : 0;
serializeJson(response, output);
httpServer.send(200, "application/json", output);
output = String();
}
void routeSetPinState() {
if (!httpServer.authenticate(AUTH_USERNAME, AUTH_PASSWORD)) {
return httpServer.requestAuthentication();
}
if (!httpServer.hasArg("value")) {
httpServer.send(400, "text/plain", "Argument \"value\" is required");
return;
}
byte value = httpServer.arg("value").toInt();
if (value != 0 && value != 1) {
httpServer.send(400, "text/plain", "The request must be in the format \"{\"value\" : 0}\" or \"{\"value\" : 1}\"");
return;
}
StaticJsonDocument<64> response;
String output;
response["state"] = value;
setPinState(LED_PIN, value);
serializeJson(response, output);
httpServer.send(200, "application/json", output);
output = String();
}