MiLightHttpServer.h 2.6 KB

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