MiLightHttpServer.h 2.7 KB

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