WebPanelServer.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #pragma once
  2. #include <stddef.h>
  3. #if defined(ESP_PLATFORM)
  4. #include <WiFi.h>
  5. #if !defined(WITH_WEB_PANEL)
  6. #define WITH_WEB_PANEL 1
  7. #endif
  8. #if WITH_WEB_PANEL
  9. #include <esp_https_server.h>
  10. #endif
  11. #endif
  12. class WebPanelCommandRunner {
  13. public:
  14. virtual ~WebPanelCommandRunner() = default;
  15. virtual void runWebCommand(const char* command, char* reply, size_t reply_size) = 0;
  16. virtual const char* getWebAdminPassword() const = 0;
  17. };
  18. class WebPanelServer {
  19. public:
  20. WebPanelServer();
  21. void setCommandRunner(WebPanelCommandRunner* runner);
  22. bool start();
  23. void stop();
  24. bool isRunning() const;
  25. bool hasSessionToken() const;
  26. bool shouldAutoLock(unsigned long now_ms) const;
  27. void lockSession();
  28. private:
  29. #if defined(ESP_PLATFORM) && WITH_WEB_PANEL
  30. struct RouteContext {
  31. WebPanelServer* self;
  32. };
  33. WebPanelCommandRunner* _runner;
  34. httpd_handle_t _server;
  35. char _token[33];
  36. unsigned long _last_activity_ms;
  37. RouteContext _route_context;
  38. static esp_err_t handleIndex(httpd_req_t* req);
  39. static esp_err_t handleApp(httpd_req_t* req);
  40. static esp_err_t handleLogin(httpd_req_t* req);
  41. static esp_err_t handleCommand(httpd_req_t* req);
  42. static esp_err_t handleStats(httpd_req_t* req);
  43. bool readRequestBody(httpd_req_t* req, char* buffer, size_t buffer_size) const;
  44. void refreshToken();
  45. bool isAuthorized(httpd_req_t* req) const;
  46. void noteActivity();
  47. #else
  48. WebPanelCommandRunner* _runner;
  49. #endif
  50. };