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.

79 lines
2.5 KiB

  1. #include <OTASettingsService.h>
  2. OTASettingsService::OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, OTA_SETTINGS_SERVICE_PATH, OTA_SETTINGS_FILE) {
  3. #if defined(ESP8266)
  4. _onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1));
  5. #elif defined(ESP_PLATFORM)
  6. WiFi.onEvent(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
  7. #endif
  8. }
  9. OTASettingsService::~OTASettingsService() {}
  10. void OTASettingsService::loop() {
  11. if (_enabled && _arduinoOTA){
  12. _arduinoOTA->handle();
  13. }
  14. }
  15. void OTASettingsService::onConfigUpdated() {
  16. configureArduinoOTA();
  17. }
  18. void OTASettingsService::readFromJsonObject(JsonObject& root) {
  19. _enabled = root["enabled"];
  20. _port = root["port"];
  21. _password = root["password"] | DEFAULT_OTA_PASSWORD;
  22. // provide defaults
  23. if (_port < 0){
  24. _port = DEFAULT_OTA_PORT;
  25. }
  26. }
  27. void OTASettingsService::writeToJsonObject(JsonObject& root) {
  28. root["enabled"] = _enabled;
  29. root["port"] = _port;
  30. root["password"] = _password;
  31. }
  32. void OTASettingsService::configureArduinoOTA() {
  33. if (_arduinoOTA){
  34. delete _arduinoOTA;
  35. _arduinoOTA = nullptr;
  36. }
  37. if (_enabled) {
  38. Serial.println("Starting OTA Update Service");
  39. _arduinoOTA = new ArduinoOTAClass;
  40. _arduinoOTA->setPort(_port);
  41. _arduinoOTA->setPassword(_password.c_str());
  42. _arduinoOTA->onStart([]() {
  43. Serial.println("Starting");
  44. });
  45. _arduinoOTA->onEnd([]() {
  46. Serial.println("\nEnd");
  47. });
  48. _arduinoOTA->onProgress([](unsigned int progress, unsigned int total) {
  49. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  50. });
  51. _arduinoOTA->onError([](ota_error_t error) {
  52. Serial.printf("Error[%u]: ", error);
  53. if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  54. else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  55. else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  56. else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  57. else if (error == OTA_END_ERROR) Serial.println("End Failed");
  58. });
  59. _arduinoOTA->begin();
  60. }
  61. }
  62. #if defined(ESP8266)
  63. void OTASettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
  64. configureArduinoOTA();
  65. }
  66. #elif defined(ESP_PLATFORM)
  67. void OTASettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
  68. configureArduinoOTA();
  69. }
  70. #endif