-
Notifications
You must be signed in to change notification settings - Fork 1
/
WebService.cpp
178 lines (155 loc) · 7.19 KB
/
WebService.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
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
#include "WebService.h"
WebService::WebService(WiFiManager* _wifiManager)
{
this->wifiManager = _wifiManager;
this->server = new ESP8266WebServer (80);
}
void WebService::init()
{
String menu;
menu += "<div>";
menu += "<a href='/'>index</a> ";
menu += "<a href='/toggle-mqtt'>toggle-mqtt</a> ";
menu += "<a href='/config'>config</a> ";
menu += "<a href='/restart'>restart</a> ";
menu += "<a href='/logout'>logout</a> ";
menu += "</div><hr>";
this->server->on("/", [this, menu](){
String str = "";
str += menu;
str += "<pre>";
str += String() + " Temperature IN: " + TemperatureService::instance->getTemperatureByAddress(TemperatureService::ADDRESS_IN) + " ºC \n";
str += String() + " Temperature OUT: " + TemperatureService::instance->getTemperatureByAddress(TemperatureService::ADDRESS_OUT) + " ºC \n";
str += String() + " Temperature BRD: " + TemperatureService::instance->getTemperatureByAddress(TemperatureService::ADDRESS_BOARD) + " ºC \n";
str += "\n";
str += String() + " Firmware Version: " + Globals::appVersion + " \n";
str += String() + " Uptime: " + (millis() / 1000) + " \n";
str += String() + " FullVersion: " + ESP.getFullVersion() + " \n";
str += String() + " ESP Chip ID: " + ESP.getChipId() + " \n";
str += String() + " CpuFreqMHz: " + ESP.getCpuFreqMHz() + " \n";
str += String() + " VCC: " + ESP.getVcc() + " \n";
str += String() + " FreeHeap: " + ESP.getFreeHeap() + " \n";
str += String() + " SketchSize: " + ESP.getSketchSize() + " \n";
str += String() + " FreeSketchSpace: " + ESP.getFreeSketchSpace() + " \n";
str += String() + " FlashChipSize: " + ESP.getFlashChipSize() + " \n";
str += String() + "FlashChipRealSize: " + ESP.getFlashChipRealSize() + " \n";
str += "</pre>";
server->send(200, "text/html; charset=utf-8", str);
});
// Edit config
this->server->on("/config", [this, menu](){
if(this->server->method() == HTTP_POST){
String newContent = this->server->arg("content");
saveConfig("/config.json", newContent);
server->sendHeader("Location", String("/config"), true);
server->send ( 302, "text/plain", "");
return;
}
loadConfig("/config.json", [this, menu](DynamicJsonDocument json){
String content;
serializeJson(json, content);
String output = "";
output += menu;
output += "<form method='post'>";
output += "<textarea name='content' cols=80 rows=10>";
output += content;
output += "</textarea>";
output += "</br>";
output += "<input type='submit'>";
output += "</form>";
server->send(200, "text/html", output);
});
});
// Toggle MQTT listening (On/Off)
this->server->on("/toggle-mqtt", [this, menu](){
if(this->server->method() == HTTP_POST){
Globals::mqttEnabled = !Globals::mqttEnabled;
this->server->send(200, "text/html", menu + String("OK! ") + (Globals::mqttEnabled? "enabled" : "disabled"));
}
else{
String output = "";
output += menu;
output += "MQTT is ";
output += (Globals::mqttEnabled ? "enabled" : "disabled");
output += "<form method='post'><button>Toggle</button></form>";
this->server->send(400, "text/html", output);
}
});
this->server->on("/restart", [this, menu](){
if(this->server->method() == HTTP_POST){
this->server->sendHeader("Location", "/",true); //Redirect to index
this->server->send(200, "text/html", "<script> setTimeout(()=> document.location = '/', 5000) </script> restarting ESP ...");
ESP.restart();
}
else{
String output = "";
output += menu;
output += "<form method='post'><button>Restart ESP</button></form>";
this->server->send(200, "text/html", output);
}
});
// Logout (reset wifi settings)
this->server->on("/logout", [this, menu](){
if(this->server->method() == HTTP_POST){
this->server->send(200, "text/html", "OK");
this->wifiManager->resetSettings();
ESP.reset();
}
else{
String output = "";
output += menu;
output += String() + "<pre>";
output += String() + "Wifi network: " + WiFi.SSID() + " \n";
output += String() + " RSSI: " + WiFi.RSSI() + " \n";
output += String() + " hostname: " + WiFi.hostname() + " \n";
output += String() + "</pre>";
output += "<form method='post'><button>Forget</button></form>";
this->server->send(400, "text/html", output);
}
});
// Trying to read file (if route not found)
this->server->onNotFound( [this](){
// check if the file exists in the flash memory (SPIFFS), if so, send it
if(!this->handleFileRead(this->server->uri())){
// Otherwise send 404 error
String message = "File Not Found\n\n";
message += "URI: ";
message += this->server->uri();
message += "\nMethod: ";
message += (this->server->method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += this->server->args();
message += "\n";
for (uint8_t i=0; i < this->server->args(); i++){
message += " " + this->server->argName(i) + ": " + this->server->arg(i) + "\n";
}
this->server->send(404, "text/html", message);
}
} );
this->server->begin();
Serial.println("HTTP server started at ip " + WiFi.localIP().toString() );
}
bool WebService::handleFileRead(String path)
{
SPIFFS.begin();
Serial.println("handleFileRead: " + path);
if (path.endsWith("/")) path += "index.html"; // If a folder is requested, send the index file
String contentType = getContentType(path); // Get the MIME type
String pathWithGz = path + ".gz";
if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) { // If the file exists, either as a compressed archive, or normal
Serial.println(String("\tFile exists: ") + path);
if (SPIFFS.exists(pathWithGz)) // If there's a compressed version available
path += ".gz"; // Use the compressed verion
File file = SPIFFS.open(path, "r"); // Open the file
size_t sent = this->server->streamFile(file, contentType); // Send it to the client
file.close(); // Close the file again
Serial.println(String("\tSent file: ") + path);
return true;
}
Serial.println(String("\tFile Not Found: ") + path); // If the file doesn't exist, return false
return false;
}
void WebService::loop()
{
this->server->handleClient();
}