Fork of the excellent esp8266-react - https://github.com/rjwats/esp8266-react
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.7 KiB

  1. #ifndef Service_h
  2. #define Service_h
  3. #if defined(ESP8266)
  4. #include <ESP8266WiFi.h>
  5. #include <ESPAsyncTCP.h>
  6. #elif defined(ESP_PLATFORM)
  7. #include <WiFi.h>
  8. #include <AsyncTCP.h>
  9. #endif
  10. #include <ESPAsyncWebServer.h>
  11. #include <ArduinoJson.h>
  12. #include <AsyncArduinoJson6.h>
  13. #include <AsyncJsonRequestWebHandler.h>
  14. /**
  15. * At the moment, not expecting services to have to deal with large JSON
  16. * files this could be made configurable fairly simply, it's exposed on
  17. * AsyncJsonRequestWebHandler with a setter.
  18. */
  19. #define MAX_SETTINGS_SIZE 1024
  20. /*
  21. * Abstraction of a service which reads and writes data from an endpoint.
  22. *
  23. * Not currently used, but indended for use by features which do not
  24. * require setting persistance.
  25. */
  26. class SimpleService {
  27. private:
  28. AsyncJsonRequestWebHandler _updateHandler;
  29. void fetchConfig(AsyncWebServerRequest *request){
  30. AsyncJsonResponse * response = new AsyncJsonResponse(MAX_SETTINGS_SIZE);
  31. JsonObject jsonObject = response->getRoot();
  32. writeToJsonObject(jsonObject);
  33. response->setLength();
  34. request->send(response);
  35. }
  36. void updateConfig(AsyncWebServerRequest *request, JsonDocument &jsonDocument){
  37. if (jsonDocument.is<JsonObject>()){
  38. JsonObject newConfig = jsonDocument.as<JsonObject>();
  39. readFromJsonObject(newConfig);
  40. // write settings back with a callback to reconfigure the wifi
  41. AsyncJsonCallbackResponse * response = new AsyncJsonCallbackResponse([this] () {onConfigUpdated();}, MAX_SETTINGS_SIZE);
  42. JsonObject jsonObject = response->getRoot();
  43. writeToJsonObject(jsonObject);
  44. response->setLength();
  45. request->send(response);
  46. } else {
  47. request->send(400);
  48. }
  49. }
  50. protected:
  51. // will serve setting endpoints from here
  52. AsyncWebServer* _server;
  53. // reads the local config from the
  54. virtual void readFromJsonObject(JsonObject& root){}
  55. virtual void writeToJsonObject(JsonObject& root){}
  56. // implement to perform action when config has been updated
  57. virtual void onConfigUpdated(){}
  58. public:
  59. SimpleService(AsyncWebServer* server, char const* servicePath):
  60. _server(server) {
  61. // configure fetch config handler
  62. _server->on(servicePath, HTTP_GET, std::bind(&SimpleService::fetchConfig, this, std::placeholders::_1));
  63. // configure update settings handler
  64. _updateHandler.setUri(servicePath);
  65. _updateHandler.setMethod(HTTP_POST);
  66. _updateHandler.setMaxContentLength(MAX_SETTINGS_SIZE);
  67. _updateHandler.onRequest(std::bind(&SimpleService::updateConfig, this, std::placeholders::_1, std::placeholders::_2));
  68. _server->addHandler(&_updateHandler);
  69. }
  70. virtual ~SimpleService() {}
  71. };
  72. #endif // end SimpleService