2021-01-03 21:43:35 +00:00
|
|
|
#include <logger.h>
|
|
|
|
#include "project_configuration.h"
|
|
|
|
#include "TaskOTA.h"
|
2021-01-04 22:10:23 +00:00
|
|
|
#include "Task.h"
|
2021-01-03 21:43:35 +00:00
|
|
|
|
|
|
|
OTATask::OTATask()
|
2021-01-09 12:38:48 +00:00
|
|
|
: Task(TASK_OTA, TaskOta), _beginCalled(false)
|
2021-01-03 21:43:35 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
OTATask::~OTATask()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-04 22:10:23 +00:00
|
|
|
bool OTATask::setup(std::shared_ptr<Configuration> config, std::shared_ptr<BoardConfig> boardConfig)
|
2021-01-03 21:43:35 +00:00
|
|
|
{
|
|
|
|
_ota = std::shared_ptr<ArduinoOTAClass>(new ArduinoOTAClass());
|
|
|
|
_ota->onStart([&]()
|
|
|
|
{
|
|
|
|
String type;
|
|
|
|
if (_ota->getCommand() == U_FLASH)
|
|
|
|
type = "sketch";
|
|
|
|
else // U_SPIFFS
|
|
|
|
type = "filesystem";
|
|
|
|
logPrintlnI("Start updating " + type);
|
|
|
|
})
|
|
|
|
.onEnd([]()
|
|
|
|
{
|
|
|
|
logPrintlnI("");
|
|
|
|
logPrintlnI("OTA End");
|
|
|
|
})
|
|
|
|
.onProgress([](unsigned int progress, unsigned int total)
|
|
|
|
{
|
|
|
|
logPrintI("Progress: ");
|
|
|
|
logPrintI(String(progress / (total / 100)));
|
|
|
|
logPrintlnI("%");
|
|
|
|
})
|
|
|
|
.onError([](ota_error_t error)
|
|
|
|
{
|
|
|
|
logPrintE("Error[");
|
|
|
|
logPrintE(String(error));
|
|
|
|
logPrintE("]: ");
|
|
|
|
if (error == OTA_AUTH_ERROR) logPrintlnE("Auth Failed");
|
|
|
|
else if (error == OTA_BEGIN_ERROR) logPrintlnE("Begin Failed");
|
|
|
|
else if (error == OTA_CONNECT_ERROR) logPrintlnE("Connect Failed");
|
|
|
|
else if (error == OTA_RECEIVE_ERROR) logPrintlnE("Receive Failed");
|
|
|
|
else if (error == OTA_END_ERROR) logPrintlnE("End Failed");
|
|
|
|
});
|
|
|
|
_ota->setHostname(config->callsign.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OTATask::loop(std::shared_ptr<Configuration> config)
|
|
|
|
{
|
2021-01-05 22:57:59 +00:00
|
|
|
if(!_beginCalled)
|
|
|
|
{
|
|
|
|
_ota->begin();
|
|
|
|
_beginCalled = true;
|
|
|
|
}
|
2021-01-03 21:43:35 +00:00
|
|
|
_ota->handle();
|
|
|
|
return true;
|
|
|
|
}
|