MiLightHttpServer.h 2.2 KB

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