MiLightHttpServer.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <WebServer.h>
  2. #include <MiLightClient.h>
  3. #include <Settings.h>
  4. #include <WebSocketsServer.h>
  5. #ifndef _MILIGHT_HTTP_SERVER
  6. #define _MILIGHT_HTTP_SERVER
  7. #define MAX_DOWNLOAD_ATTEMPTS 3
  8. typedef std::function<void(void)> SettingsSavedHandler;
  9. const char TEXT_PLAIN[] PROGMEM = "text/plain";
  10. const char APPLICATION_JSON[] = "application/json";
  11. class MiLightHttpServer {
  12. public:
  13. MiLightHttpServer(Settings& settings, MiLightClient*& milightClient)
  14. : server(WebServer(80)),
  15. wsServer(WebSocketsServer(81)),
  16. numWsClients(0),
  17. milightClient(milightClient),
  18. settings(settings)
  19. {
  20. this->applySettings(settings);
  21. }
  22. void begin();
  23. void handleClient();
  24. void onSettingsSaved(SettingsSavedHandler handler);
  25. void on(const char* path, HTTPMethod method, ESP8266WebServer::THandlerFunction handler);
  26. void handlePacketSent(uint8_t* packet, const MiLightRemoteConfig& config);
  27. WiFiClient client();
  28. protected:
  29. ESP8266WebServer::THandlerFunction handleServeFile(
  30. const char* filename,
  31. const char* contentType,
  32. const char* defaultText = NULL);
  33. bool serveFile(const char* file, const char* contentType = "text/html");
  34. ESP8266WebServer::THandlerFunction handleUpdateFile(const char* filename);
  35. ESP8266WebServer::THandlerFunction handleServe_P(const char* data, size_t length);
  36. void applySettings(Settings& settings);
  37. void handleUpdateSettings();
  38. void handleGetRadioConfigs();
  39. void handleAbout();
  40. void handleSystemPost();
  41. void handleListenGateway(const UrlTokenBindings* urlBindings);
  42. void handleSendRaw(const UrlTokenBindings* urlBindings);
  43. void handleUpdateGroup(const UrlTokenBindings* urlBindings);
  44. void handleRequest(const JsonObject& request);
  45. void handleWsEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length);
  46. File updateFile;
  47. WebServer server;
  48. WebSocketsServer wsServer;
  49. Settings& settings;
  50. MiLightClient*& milightClient;
  51. SettingsSavedHandler settingsSavedHandler;
  52. size_t numWsClients;
  53. };
  54. #endif