This repository has been archived by the owner on Jan 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
WebServer.hpp
74 lines (58 loc) · 2.24 KB
/
WebServer.hpp
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
/*
Basecamp - ESP32 library to simplify the basics of IoT projects
Written by Merlin Schumacher ([email protected]) for c't magazin für computer technik (https://www.ct.de)
Licensed under GPLv3. See LICENSE for details.
*/
#ifndef WebServer_h
#define WebServer_h
#include "debug.hpp"
#include <map>
#include <vector>
#include <SPIFFS.h>
#include <ESPAsyncWebServer.h>
#include <AsyncJson.h>
#include "data.hpp"
#include "Configuration.hpp"
#include "WebInterface.hpp"
#ifdef BASECAMP_USEDNS
#ifdef DNSServer_h
#include "CaptiveRequestHandler.hpp"
#endif
#endif
class WebServer {
public:
WebServer();
~WebServer() = default;
void begin(Configuration &configuration, std::function<void()> submitFunc = 0);
bool addURL(const char* url, const char* content, const char* mimetype);
// Remark: The server should be stopped before any changes to the interface elements are done to avoid inconsistent results if a request comes in at that very moment.
// However, ESPAsyncWebServer does not support any kind of end() function or something like that in the moment.
void addInterfaceElement(const String &id, String element, String content, String parent = "#configform", String configvariable = "");
// Sets "key" to "value" in element with id "id" if exists.
void setInterfaceElementAttribute(const String &id, const String &key, String value);
// Removes all interface elements
void reset();
struct cmp_str
{
bool operator()(String a, String b)
{
return strcmp(a.c_str(), b.c_str()) < 0;
}
};
AsyncWebServer server;
private:
static void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len);
std::map<const char*, const char* > _URLList;
bool handleFileRead(char* path);
char* getContentType(char* filename);
// Print "request" to serial console for debugging purposes.
void debugPrintRequest(AsyncWebServerRequest *request);
int _interfaceElementCounter = 0;
static std::map<String, String, cmp_str> _interfaceMeta;
int _typeof(String a){ return 0; };
int _typeof(int a){ return 1; };
int _typeof(std::map<String, String, cmp_str> a){ return 2; };
AsyncEventSource events;
std::vector<InterfaceElement> interfaceElements;
};
#endif