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.

82 lines
2.6 KiB

  1. #include <OTASettingsService.h>
  2. OTASettingsService::OTASettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(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. #if defined(ESP_PLATFORM)
  35. _arduinoOTA->end();
  36. #endif
  37. delete _arduinoOTA;
  38. _arduinoOTA = nullptr;
  39. }
  40. if (_enabled) {
  41. Serial.println("Starting OTA Update Service");
  42. _arduinoOTA = new ArduinoOTAClass;
  43. _arduinoOTA->setPort(_port);
  44. _arduinoOTA->setPassword(_password.c_str());
  45. _arduinoOTA->onStart([]() {
  46. Serial.println("Starting");
  47. });
  48. _arduinoOTA->onEnd([]() {
  49. Serial.println("\nEnd");
  50. });
  51. _arduinoOTA->onProgress([](unsigned int progress, unsigned int total) {
  52. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  53. });
  54. _arduinoOTA->onError([](ota_error_t error) {
  55. Serial.printf("Error[%u]: ", error);
  56. if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  57. else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  58. else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  59. else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  60. else if (error == OTA_END_ERROR) Serial.println("End Failed");
  61. });
  62. _arduinoOTA->begin();
  63. }
  64. }
  65. #if defined(ESP8266)
  66. void OTASettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
  67. configureArduinoOTA();
  68. }
  69. #elif defined(ESP_PLATFORM)
  70. void OTASettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
  71. configureArduinoOTA();
  72. }
  73. #endif