webinterface.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // work out content type
  34. const char *content_type = "text/html";
  35. const struct {
  36. const char *extension;
  37. const char *content_type;
  38. } extensions[] = {
  39. { ".js", "text/javascript" },
  40. { ".jpg", "image/jpeg" },
  41. { ".css", "text/css" },
  42. };
  43. for (const auto &e : extensions) {
  44. if (uri.endsWith(e.extension)) {
  45. content_type = e.content_type;
  46. break;
  47. }
  48. }
  49. auto *f = ROMFS::find_stream(uri.c_str());
  50. if (f != nullptr) {
  51. server.sendHeader("Content-Encoding", "gzip");
  52. server.streamFile(*f, content_type);
  53. delete f;
  54. return true;
  55. }
  56. return false;
  57. }
  58. } ROMFS_Handler;
  59. /*
  60. init web server
  61. */
  62. void WebInterface::init(void)
  63. {
  64. Serial.printf("WAP start %s %s\n", g.wifi_ssid, g.wifi_password);
  65. WiFi.softAP(g.wifi_ssid, g.wifi_password);
  66. IPAddress myIP = WiFi.softAPIP();
  67. server.addHandler( &ROMFS_Handler );
  68. #if 0
  69. /*return index page which is stored in serverIndex */
  70. server.on("/", HTTP_GET, []() {
  71. server.sendHeader("Connection", "close");
  72. server.send(200, "text/html", ROMFS::find_string("web/login.html"));
  73. });
  74. server.on("/serverIndex", HTTP_GET, []() {
  75. server.sendHeader("Connection", "close");
  76. server.send(200, "text/html", ROMFS::find_string("web/uploader.html"));
  77. });
  78. server.on("/js/jquery.min.js", HTTP_GET, []() {
  79. server.sendHeader("Connection", "close");
  80. server.send(200, "text/html", ROMFS::find_string("web/js/jquery.min.js"));
  81. });
  82. #endif
  83. /*handling uploading firmware file */
  84. server.on("/update", HTTP_POST, []() {
  85. server.sendHeader("Connection", "close");
  86. server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
  87. ESP.restart();
  88. }, []() {
  89. HTTPUpload& upload = server.upload();
  90. if (upload.status == UPLOAD_FILE_START) {
  91. Serial.printf("Update: %s\n", upload.filename.c_str());
  92. if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
  93. Update.printError(Serial);
  94. }
  95. } else if (upload.status == UPLOAD_FILE_WRITE) {
  96. /* flashing firmware to ESP*/
  97. if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  98. Update.printError(Serial);
  99. }
  100. } else if (upload.status == UPLOAD_FILE_END) {
  101. if (Update.end(true)) { //true to set the size to the current progress
  102. Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  103. } else {
  104. Update.printError(Serial);
  105. }
  106. }
  107. });
  108. Serial.printf("WAP started\n");
  109. server.begin();
  110. }
  111. void WebInterface::update()
  112. {
  113. if (!initialised) {
  114. init();
  115. initialised = true;
  116. }
  117. server.handleClient();
  118. }