webinterface.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "webinterface.h"
  2. #include <WiFi.h>
  3. #include <WiFiClient.h>
  4. #include <WebServer.h>
  5. #include <WiFiAP.h>
  6. #include <ESPmDNS.h>
  7. #include <Update.h>
  8. #include "parameters.h"
  9. #include "romfs.h"
  10. static WebServer server(80);
  11. /*
  12. serve files from ROMFS
  13. */
  14. class ROMFS_Handler : public RequestHandler
  15. {
  16. bool canHandle(HTTPMethod method, String uri) {
  17. if (uri == "/") {
  18. uri = "/index.html";
  19. }
  20. Serial.printf("canHandle: %s\n", uri.c_str());
  21. uri = "web" + uri;
  22. if (ROMFS::exists(uri.c_str())) {
  23. return true;
  24. }
  25. return false;
  26. }
  27. bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri) {
  28. if (requestUri == "/") {
  29. requestUri = "/index.html";
  30. }
  31. String uri = "web" + requestUri;
  32. Serial.printf("handle: '%s'\n", requestUri.c_str());
  33. const char *content_type = "text/html";
  34. if (uri.endsWith(".js")) {
  35. content_type = "text/javascript";
  36. }
  37. auto *f = ROMFS::find_stream(uri.c_str());
  38. if (f != nullptr) {
  39. server.sendHeader("Content-Encoding", "gzip");
  40. server.streamFile(*f, content_type);
  41. delete f;
  42. return true;
  43. }
  44. return false;
  45. }
  46. } ROMFS_Handler;
  47. /*
  48. init web server
  49. */
  50. void WebInterface::init(void)
  51. {
  52. Serial.printf("WAP start %s %s\n", g.wifi_ssid, g.wifi_password);
  53. WiFi.softAP(g.wifi_ssid, g.wifi_password);
  54. IPAddress myIP = WiFi.softAPIP();
  55. server.addHandler( &ROMFS_Handler );
  56. #if 0
  57. /*return index page which is stored in serverIndex */
  58. server.on("/", HTTP_GET, []() {
  59. server.sendHeader("Connection", "close");
  60. server.send(200, "text/html", ROMFS::find_string("web/login.html"));
  61. });
  62. server.on("/serverIndex", HTTP_GET, []() {
  63. server.sendHeader("Connection", "close");
  64. server.send(200, "text/html", ROMFS::find_string("web/uploader.html"));
  65. });
  66. server.on("/js/jquery.min.js", HTTP_GET, []() {
  67. server.sendHeader("Connection", "close");
  68. server.send(200, "text/html", ROMFS::find_string("web/js/jquery.min.js"));
  69. });
  70. #endif
  71. /*handling uploading firmware file */
  72. server.on("/update", HTTP_POST, []() {
  73. server.sendHeader("Connection", "close");
  74. server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
  75. ESP.restart();
  76. }, []() {
  77. HTTPUpload& upload = server.upload();
  78. if (upload.status == UPLOAD_FILE_START) {
  79. Serial.printf("Update: %s\n", upload.filename.c_str());
  80. if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
  81. Update.printError(Serial);
  82. }
  83. } else if (upload.status == UPLOAD_FILE_WRITE) {
  84. /* flashing firmware to ESP*/
  85. if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  86. Update.printError(Serial);
  87. }
  88. } else if (upload.status == UPLOAD_FILE_END) {
  89. if (Update.end(true)) { //true to set the size to the current progress
  90. Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  91. } else {
  92. Update.printError(Serial);
  93. }
  94. }
  95. });
  96. Serial.printf("WAP started\n");
  97. server.begin();
  98. }
  99. void WebInterface::update()
  100. {
  101. if (!initialised) {
  102. init();
  103. initialised = true;
  104. }
  105. server.handleClient();
  106. }