MiLightHttpServer.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. bool serveFile(const char* file, const char* contentType = "text/html");
  36. ESP8266WebServer::THandlerFunction handleUpdateFile(const char* filename);
  37. ESP8266WebServer::THandlerFunction handleServe_P(const char* data, size_t length);
  38. void applySettings(Settings& settings);
  39. void handleUpdateSettings();
  40. void handleGetRadioConfigs();
  41. void handleAbout();
  42. void handleSystemPost();
  43. void handleListenGateway(const UrlTokenBindings* urlBindings);
  44. void handleSendRaw(const UrlTokenBindings* urlBindings);
  45. void handleUpdateGroup(const UrlTokenBindings* urlBindings);
  46. void handleGetGroup(const UrlTokenBindings* urlBindings);
  47. void handleRequest(const JsonObject& request);
  48. void handleWsEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length);
  49. File updateFile;
  50. WebServer server;
  51. WebSocketsServer wsServer;
  52. Settings& settings;
  53. MiLightClient*& milightClient;
  54. GroupStateStore& stateStore;
  55. SettingsSavedHandler settingsSavedHandler;
  56. size_t numWsClients;
  57. };
  58. #endif