diff options
Diffstat (limited to 'webserver.cpp')
| -rw-r--r-- | webserver.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/webserver.cpp b/webserver.cpp new file mode 100644 index 0000000..36b5a14 --- /dev/null +++ b/webserver.cpp | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | #include "webserver.h" | ||
| 2 | |||
| 3 | |||
| 4 | WebServer::WebServer(Controller *controller) { | ||
| 5 | this->controller = controller; | ||
| 6 | this->server = new ESP8266WebServer(80); | ||
| 7 | |||
| 8 | if(!setupST()) { | ||
| 9 | Serial.println("Cos sie zjebalo!!"); | ||
| 10 | return; | ||
| 11 | } | ||
| 12 | |||
| 13 | if(!LittleFS.begin()){ | ||
| 14 | Serial.println("An Error has occurred while mounting LittleFS"); | ||
| 15 | return; | ||
| 16 | } | ||
| 17 | setupRoutes(); | ||
| 18 | } | ||
| 19 | |||
| 20 | WebServer::~WebServer() { | ||
| 21 | delete server; | ||
| 22 | } | ||
| 23 | |||
| 24 | void WebServer::setupRoutes() { | ||
| 25 | server->serveStatic("/", LittleFS, "/index.html", "max-age=43200"); | ||
| 26 | server->serveStatic("/script.js", LittleFS, "/script.js", "max-age=43200"); | ||
| 27 | server->serveStatic("/style.css", LittleFS, "/style.css", "max-age=43200"); | ||
| 28 | |||
| 29 | server->on("/next", [this]() { | ||
| 30 | this->server->send(200, "text/plain", "Next ok!"); | ||
| 31 | this->controller->next(); | ||
| 32 | }); | ||
| 33 | |||
| 34 | server->on("/previous", [this]() { | ||
| 35 | this->server->send(200, "text/plain", "Previous ok!"); | ||
| 36 | this->controller->previous(); | ||
| 37 | }); | ||
| 38 | |||
| 39 | server->on("/nexts", [this]() { | ||
| 40 | this->server->send(200, "text/plain", "Next_S ok!"); | ||
| 41 | this->controller->nextS(); | ||
| 42 | }); | ||
| 43 | |||
| 44 | server->on("/previouss", [this]() { | ||
| 45 | this->server->send(200, "text/plain", "Previous_S ok!"); | ||
| 46 | this->controller->previousS(); | ||
| 47 | }); | ||
| 48 | |||
| 49 | server->begin(); | ||
| 50 | } | ||
| 51 | |||
| 52 | void WebServer::loop() { | ||
| 53 | server->handleClient(); | ||
| 54 | } | ||
| 55 | |||
| 56 | bool WebServer::setupAP () { | ||
| 57 | WiFi.softAP(controller->getAPSSID(), controller->getAPPassword()); | ||
| 58 | wifiStatus = WiFi.status(); | ||
| 59 | return wifiStatus; | ||
| 60 | } | ||
| 61 | |||
| 62 | bool WebServer::setupST () { | ||
| 63 | Serial.println("Station connecting: "); | ||
| 64 | |||
| 65 | WiFi.begin("OpenWrt2"); | ||
| 66 | while (WiFi.status() != WL_CONNECTED) | ||
| 67 | { | ||
| 68 | delay(500); | ||
| 69 | Serial.print("."); | ||
| 70 | } | ||
| 71 | Serial.println(); | ||
| 72 | |||
| 73 | Serial.print("Connected, IP address: "); | ||
| 74 | Serial.println(WiFi.localIP()); | ||
| 75 | |||
| 76 | wifiStatus = WiFi.status(); | ||
| 77 | return wifiStatus; | ||
| 78 | } | ||
