-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebServer.cpp
118 lines (97 loc) · 3.73 KB
/
WebServer.cpp
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
#include "WebServer.h"
#include <ESP8266WiFi.h>
#include "FS.h"
#include "libraries\\ArduinoJson.h"
WebServer::WebServer(String deviceId) : webServer(80) {
this->deviceId = deviceId;
}
void WebServer::start() {
this->setupRoutes();
this->webServer.begin();
}
void WebServer::handle() {
this->webServer.handleClient();
}
void WebServer::setupRoutes() {
this->webServer.on("/", HTTP_GET, [&]() { this->handleRootRedirect(); });
this->webServer.on("/api/status", HTTP_GET, [&]() { this->handleStatus(); });
this->webServer.on("/api/wifi/scan", HTTP_GET, [&]() { this->handleWifiScan(); });
this->webServer.on("/api/wifi/status", HTTP_GET, [&]() { this->handleWifiStatus(); });
this->webServer.serveStatic("/", SPIFFS, "/");
this->webServer.onNotFound([&]() { this->handleRootRedirect(); });
}
void WebServer::handleRootRedirect() {
this->webServer.sendHeader("Location", "/index.html", true);
this->webServer.send(302, "text/plain", "");
}
void WebServer::handleStatus() {
String jsonString;
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root.set("deviceId", this->deviceId);
root.set("auth", true);
root.set("success", true);
JsonObject& response = root.createNestedObject("response");
response.set("calentando", true); //llenoDeAgua();
response.set("temperatura", 123.45); //getTemperaturaDelAgua();
root.printTo(jsonString);
this->webServer.send(200, "application/json", jsonString);
}
void WebServer::handleWifiScan() {
String jsonString;
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root.set("deviceId", this->deviceId);
root.set("auth", true);
root.set("success", true);
JsonObject& response = root.createNestedObject("response");
JsonArray& networks = response.createNestedArray("networks");
int found = WiFi.scanNetworks();
for (int i = 0; i < found; i++) {
JsonObject& networkObj = jsonBuffer.createObject();
networkObj.set("ssid", WiFi.SSID(i));
networkObj.set("rssi", WiFi.RSSI(i));
networkObj.set("encType", WiFi.encryptionType(i));
networks.add(networkObj);
}
root.printTo(jsonString);
this->webServer.send(200, "application/json", jsonString);
}
void WebServer::handleWifiStatus() {
String jsonString;
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root.set("deviceId", this->deviceId);
root.set("auth", true);
root.set("success", true);
JsonObject& response = root.createNestedObject("response");
JsonObject& station = response.createNestedObject("station");
station.set("connected", WiFi.isConnected());
station.set("ssid", WiFi.SSID());
station.set("ip", WiFi.localIP().toString());
station.set("subnetMask", WiFi.subnetMask().toString());
station.set("gateway", WiFi.gatewayIP().toString());
station.set("dns1", WiFi.dnsIP().toString());
station.set("dns2", WiFi.dnsIP(1).toString());
station.set("macAddress", String(WiFi.macAddress().c_str()));
JsonObject& accessPoint = response.createNestedObject("accessPoint");
accessPoint.set("ssid", this->deviceId);
accessPoint.set("ip", WiFi.softAPIP().toString());
accessPoint.set("clients", WiFi.softAPgetStationNum());
// station.set("subnetMask", WiFi.subnetMask());
// station.set("gateway", WiFi.gatewayIP());
// station.set("DNS 1", WiFi.dnsIP());
// station.set("DNS 2", WiFi.dnsIP(1));
accessPoint.set("macAddress", String(WiFi.softAPmacAddress().c_str()));
root.printTo(jsonString);
this->webServer.send(200, "application/json", jsonString);
}
// void sendFile(String path) {
// File f = SPIFFS.open(path, "r");
// if (!f) {
// server.send(200, "text/html", "index not found! :(");
// } else {
// server.streamFile(f, "text/html");
// }
// f.close();
// }