-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-module.h
81 lines (75 loc) · 2.17 KB
/
web-module.h
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
#include <FirmwareModule.h>
#include <Arduino.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
#include <server-routes.h>
// #include <websocket-impl.h>
// #include <ArduinoJson.h>
// #include <WebSocket.h>
// #include <web-controls.h>
/**
* One server per module providing services on specific port:
* http server routes, web sockets, OTA service
*
* WebServer<port>
* -> List of available server routes
* -> List of Websockets implemented
* -> OTA service
*/
class WebModule : public FirmwareModule
{
AsyncWebServer *server; //@todo customize port from constructor
// std::vector<AsyncWebSocket *> *webSockets;
public:
WebModule(int serverPort = 80) : FirmwareModule("WebServer")
{
server = new AsyncWebServer(serverPort);
// webSockets = new std::vector<AsyncWebSocket *>();
}
void loadServerRoutes()
{
Serial.println("[WebServer] loading server routes");
// @todo provide routes as argument
initServerRoutes(server);
Serial.println("[WebServer] Done");
}
void addWebSocket(AsyncWebSocket *ws)
{
Serial.println("[WebServer] add web socket");
// AsyncWebSocket ws(wsRoute);
// // bind websocket event handler
// ws.onEvent(eventHandler);
// bind websocket to webserver
server->addHandler(ws);
// webSockets->push_back(&ws);
Serial.println("[WebServer] Done");
}
void startOTAService()
{
Serial.println("[WebServer] Starting OTA Update service");
AsyncElegantOTA.begin(server);
Serial.println("[WebServer] Ok");
}
void setup()
{
// Serial.println("");
// Loading services and routes
loadServerRoutes();
startOTAService();
// @todo create and assign websockets externally
// AsyncWebSocket ws("/ws");
// addWebSocket("/ws");
// Start server
Serial.println("[WebServer] Starting Server");
server->begin();
Serial.println("[WebServer] running!");
}
void loop()
{
// for (auto ws : *webSockets)
// {
// ws->cleanupClients();
// }
}
};