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-14 22:23:45 +00:00
|
|
|
_httpEndpoint(OTASettings::serialize,
|
|
|
|
OTASettings::deserialize,
|
|
|
|
this,
|
|
|
|
server,
|
|
|
|
OTA_SETTINGS_SERVICE_PATH,
|
|
|
|
securityManager),
|
|
|
|
_fsPersistence(OTASettings::serialize, OTASettings::deserialize, this, fs, OTA_SETTINGS_FILE) {
|
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-14 22:23:45 +00:00
|
|
|
addUpdateHandler([&](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) {
|
|
|
|
Serial.println("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());
|
2019-12-03 23:16:06 +00:00
|
|
|
_arduinoOTA->onStart([]() { Serial.println("Starting"); });
|
|
|
|
_arduinoOTA->onEnd([]() { Serial.println("\nEnd"); });
|
2018-02-26 00:11:31 +00:00
|
|
|
_arduinoOTA->onProgress([](unsigned int progress, unsigned int total) {
|
|
|
|
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
|
|
|
|
});
|
|
|
|
_arduinoOTA->onError([](ota_error_t error) {
|
|
|
|
Serial.printf("Error[%u]: ", error);
|
2019-12-03 23:16:06 +00:00
|
|
|
if (error == OTA_AUTH_ERROR)
|
|
|
|
Serial.println("Auth Failed");
|
|
|
|
else if (error == OTA_BEGIN_ERROR)
|
|
|
|
Serial.println("Begin Failed");
|
|
|
|
else if (error == OTA_CONNECT_ERROR)
|
|
|
|
Serial.println("Connect Failed");
|
|
|
|
else if (error == OTA_RECEIVE_ERROR)
|
|
|
|
Serial.println("Receive Failed");
|
|
|
|
else if (error == OTA_END_ERROR)
|
|
|
|
Serial.println("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
|