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.

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