MiLightHttpServer.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <WebServer.h>
  2. #include <MiLightClient.h>
  3. #include <Settings.h>
  4. #ifndef _MILIGHT_HTTP_SERVER
  5. #define _MILIGHT_HTTP_SERVER
  6. #define MAX_DOWNLOAD_ATTEMPTS 3
  7. typedef std::function<void(void)> SettingsSavedHandler;
  8. const char DEFAULT_INDEX_PAGE[] PROGMEM
  9. = "Web app not installed. Click <a href=\"/download_update/web\">here</a> to attempt to download it from GitHub.";
  10. const char TEXT_PLAIN[] PROGMEM = "text/plain";
  11. const char APPLICATION_JSON[] PROGMEM = "application/json";
  12. class MiLightHttpServer {
  13. public:
  14. MiLightHttpServer(Settings& settings, MiLightClient*& milightClient)
  15. : server(WebServer(80)),
  16. milightClient(milightClient),
  17. settings(settings)
  18. {
  19. this->applySettings(settings);
  20. }
  21. void begin();
  22. void handleClient();
  23. void onSettingsSaved(SettingsSavedHandler handler);
  24. void on(const char* path, HTTPMethod method, ESP8266WebServer::THandlerFunction handler);
  25. WiFiClient client();
  26. protected:
  27. ESP8266WebServer::THandlerFunction handleServeFile(
  28. const char* filename,
  29. const char* contentType,
  30. const char* defaultText = NULL);
  31. bool serveFile(const char* file, const char* contentType = "text/html");
  32. ESP8266WebServer::THandlerFunction handleUpdateFile(const char* filename);
  33. ESP8266WebServer::THandlerFunction handleServe_P(const char* data, size_t length);
  34. void applySettings(Settings& settings);
  35. void handleUpdateSettings();
  36. void handleGetRadioConfigs();
  37. void handleAbout();
  38. void handleGetLatestRelease();
  39. void handleSystemPost();
  40. void handleListenGateway(const UrlTokenBindings* urlBindings);
  41. void handleSendRaw(const UrlTokenBindings* urlBindings);
  42. void handleUpdateGroup(const UrlTokenBindings* urlBindings);
  43. void handleDownloadUpdate(const UrlTokenBindings* urlBindings);
  44. void handleRequest(const JsonObject& request);
  45. File updateFile;
  46. WebServer server;
  47. Settings& settings;
  48. MiLightClient*& milightClient;
  49. SettingsSavedHandler settingsSavedHandler;
  50. };
  51. #endif