2018-02-26 00:11:31 +00:00
|
|
|
#include <OTASettingsService.h>
|
|
|
|
|
2019-07-14 21:13:26 +00:00
|
|
|
OTASettingsService::OTASettingsService(FS* fs, SecurityManager* securityManager) : AdminSettingsService(fs, securityManager, OTA_SETTINGS_SERVICE_PATH, OTA_SETTINGS_FILE) {
|
2018-11-11 17:47:44 +00:00
|
|
|
#if defined(ESP8266)
|
|
|
|
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1));
|
|
|
|
#elif defined(ESP_PLATFORM)
|
|
|
|
WiFi.onEvent(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
|
|
|
|
#endif
|
|
|
|
}
|
2018-02-26 00:11:31 +00:00
|
|
|
|
|
|
|
OTASettingsService::~OTASettingsService() {}
|
|
|
|
|
|
|
|
void OTASettingsService::loop() {
|
|
|
|
if (_enabled && _arduinoOTA){
|
|
|
|
_arduinoOTA->handle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OTASettingsService::onConfigUpdated() {
|
|
|
|
configureArduinoOTA();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OTASettingsService::readFromJsonObject(JsonObject& root) {
|
|
|
|
_enabled = root["enabled"];
|
|
|
|
_port = root["port"];
|
|
|
|
_password = root["password"] | DEFAULT_OTA_PASSWORD;
|
|
|
|
|
|
|
|
// provide defaults
|
|
|
|
if (_port < 0){
|
|
|
|
_port = DEFAULT_OTA_PORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OTASettingsService::writeToJsonObject(JsonObject& root) {
|
|
|
|
root["enabled"] = _enabled;
|
|
|
|
root["port"] = _port;
|
|
|
|
root["password"] = _password;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OTASettingsService::configureArduinoOTA() {
|
2018-03-06 22:36:55 +00:00
|
|
|
if (_arduinoOTA){
|
2019-06-25 22:31:58 +00:00
|
|
|
#if defined(ESP_PLATFORM)
|
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
|
|
|
}
|
2018-02-26 00:11:31 +00:00
|
|
|
if (_enabled) {
|
2018-11-11 17:47:44 +00:00
|
|
|
Serial.println("Starting OTA Update Service");
|
2018-02-26 00:11:31 +00:00
|
|
|
_arduinoOTA = new ArduinoOTAClass;
|
|
|
|
_arduinoOTA->setPort(_port);
|
|
|
|
_arduinoOTA->setPassword(_password.c_str());
|
|
|
|
_arduinoOTA->onStart([]() {
|
|
|
|
Serial.println("Starting");
|
|
|
|
});
|
|
|
|
_arduinoOTA->onEnd([]() {
|
|
|
|
Serial.println("\nEnd");
|
|
|
|
});
|
|
|
|
_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);
|
|
|
|
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");
|
|
|
|
});
|
|
|
|
_arduinoOTA->begin();
|
|
|
|
}
|
|
|
|
}
|
2018-11-11 17:47:44 +00:00
|
|
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
void OTASettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
|
|
|
|
configureArduinoOTA();
|
|
|
|
}
|
|
|
|
#elif defined(ESP_PLATFORM)
|
|
|
|
void OTASettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
|
|
|
|
configureArduinoOTA();
|
|
|
|
}
|
|
|
|
#endif
|