2018-02-26 00:11:31 +00:00
|
|
|
#include <OTASettingsService.h>
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
OTASettingsService::OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
|
2020-05-29 19:18:43 +00:00
|
|
|
_httpEndpoint(OTASettings::read, OTASettings::update, this, server, OTA_SETTINGS_SERVICE_PATH, securityManager),
|
|
|
|
_fsPersistence(OTASettings::read, OTASettings::update, this, fs, OTA_SETTINGS_FILE),
|
2020-05-20 17:51:04 +00:00
|
|
|
_arduinoOTA(nullptr) {
|
2019-12-24 11:19:19 +00:00
|
|
|
#ifdef ESP32
|
2019-12-03 23:16:06 +00:00
|
|
|
WiFi.onEvent(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2),
|
|
|
|
WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
|
2019-12-24 11:19:19 +00:00
|
|
|
#elif defined(ESP8266)
|
|
|
|
_onStationModeGotIPHandler =
|
|
|
|
WiFi.onStationModeGotIP(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1));
|
2018-11-11 17:47:44 +00:00
|
|
|
#endif
|
2020-05-21 07:42:21 +00:00
|
|
|
addUpdateHandler([&](const String& originId) { configureArduinoOTA(); }, false);
|
2018-11-11 17:47:44 +00:00
|
|
|
}
|
2018-02-26 00:11:31 +00:00
|
|
|
|
2020-05-14 22:23:45 +00:00
|
|
|
void OTASettingsService::begin() {
|
|
|
|
_fsPersistence.readFromFS();
|
|
|
|
configureArduinoOTA();
|
2019-12-03 23:16:06 +00:00
|
|
|
}
|
2018-02-26 00:11:31 +00:00
|
|
|
|
|
|
|
void OTASettingsService::loop() {
|
2020-05-14 22:23:45 +00:00
|
|
|
if (_state.enabled && _arduinoOTA) {
|
2018-02-26 00:11:31 +00:00
|
|
|
_arduinoOTA->handle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OTASettingsService::configureArduinoOTA() {
|
2019-12-03 23:16:06 +00:00
|
|
|
if (_arduinoOTA) {
|
2019-12-24 11:19:19 +00:00
|
|
|
#ifdef ESP32
|
2019-06-07 18:47:59 +00:00
|
|
|
_arduinoOTA->end();
|
2019-06-25 22:31:58 +00:00
|
|
|
#endif
|
2018-03-06 22:36:55 +00:00
|
|
|
delete _arduinoOTA;
|
2019-06-02 22:15:56 +00:00
|
|
|
_arduinoOTA = nullptr;
|
2018-03-06 22:36:55 +00:00
|
|
|
}
|
2020-05-14 22:23:45 +00:00
|
|
|
if (_state.enabled) {
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.println(F("Starting OTA Update Service..."));
|
2018-02-26 00:11:31 +00:00
|
|
|
_arduinoOTA = new ArduinoOTAClass;
|
2020-05-14 22:23:45 +00:00
|
|
|
_arduinoOTA->setPort(_state.port);
|
|
|
|
_arduinoOTA->setPassword(_state.password.c_str());
|
2020-05-21 07:42:21 +00:00
|
|
|
_arduinoOTA->onStart([]() { Serial.println(F("Starting")); });
|
|
|
|
_arduinoOTA->onEnd([]() { Serial.println(F("\r\nEnd")); });
|
2018-02-26 00:11:31 +00:00
|
|
|
_arduinoOTA->onProgress([](unsigned int progress, unsigned int total) {
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.printf_P(PSTR("Progress: %u%%\r\n"), (progress / (total / 100)));
|
2018-02-26 00:11:31 +00:00
|
|
|
});
|
|
|
|
_arduinoOTA->onError([](ota_error_t error) {
|
|
|
|
Serial.printf("Error[%u]: ", error);
|
2019-12-03 23:16:06 +00:00
|
|
|
if (error == OTA_AUTH_ERROR)
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.println(F("Auth Failed"));
|
2019-12-03 23:16:06 +00:00
|
|
|
else if (error == OTA_BEGIN_ERROR)
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.println(F("Begin Failed"));
|
2019-12-03 23:16:06 +00:00
|
|
|
else if (error == OTA_CONNECT_ERROR)
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.println(F("Connect Failed"));
|
2019-12-03 23:16:06 +00:00
|
|
|
else if (error == OTA_RECEIVE_ERROR)
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.println(F("Receive Failed"));
|
2019-12-03 23:16:06 +00:00
|
|
|
else if (error == OTA_END_ERROR)
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.println(F("End Failed"));
|
2018-02-26 00:11:31 +00:00
|
|
|
});
|
|
|
|
_arduinoOTA->begin();
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 22:23:45 +00:00
|
|
|
|
2019-12-24 11:19:19 +00:00
|
|
|
#ifdef ESP32
|
|
|
|
void OTASettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
|
2018-11-11 17:47:44 +00:00
|
|
|
configureArduinoOTA();
|
2019-12-03 23:16:06 +00:00
|
|
|
}
|
2019-12-24 11:19:19 +00:00
|
|
|
#elif defined(ESP8266)
|
|
|
|
void OTASettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
|
2018-11-11 17:47:44 +00:00
|
|
|
configureArduinoOTA();
|
2019-12-03 23:16:06 +00:00
|
|
|
}
|
2018-11-11 17:47:44 +00:00
|
|
|
#endif
|