MiLightHttpServer.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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(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(BulbId& bulbId, GroupState* state);
  41. void handleUpdateSettings();
  42. void handleUpdateSettingsPost();
  43. void handleGetRadioConfigs();
  44. void handleAbout();
  45. void handleSystemPost();
  46. void handleFirmwareUpload();
  47. void handleFirmwarePost();
  48. void handleListenGateway(const UrlTokenBindings* urlBindings);
  49. void handleSendRaw(const UrlTokenBindings* urlBindings);
  50. void handleUpdateGroup(const UrlTokenBindings* urlBindings);
  51. void handleGetGroup(const UrlTokenBindings* urlBindings);
  52. void handleRequest(const JsonObject& request);
  53. void handleWsEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length);
  54. File updateFile;
  55. WebServer server;
  56. WebSocketsServer wsServer;
  57. Settings& settings;
  58. MiLightClient*& milightClient;
  59. GroupStateStore*& stateStore;
  60. SettingsSavedHandler settingsSavedHandler;
  61. size_t numWsClients;
  62. ESP8266WebServer::THandlerFunction _handleRootPage;
  63. };
  64. #endif