Files
sprocket-network-wifi/src/OtaTcpPlugin.cpp_
2019-03-22 09:30:00 +01:00

71 lines
2.2 KiB
Plaintext

#ifndef __OTA_CLASSIC_H__
#define __OTA_CLASSIC_H__
#include "TaskSchedulerDeclarations.h"
#include "ArduinoOTA.h"
#include "Plugin.h"
using namespace std;
using namespace std::placeholders;
struct OtaConfig {
int port;
const char* password;
};
class OtaTcpPlugin : public Plugin {
private:
OtaConfig config;
Task otaTask;
public:
OtaTcpPlugin(OtaConfig cfg){
config = cfg;
}
void enable() {
Serial.println("OTA enable");
ArduinoOTA.begin();
otaTask.enable();
}
void activate(Scheduler* userScheduler){
// setup task
// TOOD check if we can increase the time OTA needs to be handled
// FIXME make this configurable
otaTask.set(TASK_MILLISECOND * 1000, TASK_FOREVER, [](){
ArduinoOTA.handle();
});
userScheduler->addTask(otaTask);
// configure OTA
ArduinoOTA.setPort(config.port);
//ArduinoOTA.setHostname(HOSTNAME);
if(strlen(config.password) > 0){
ArduinoOTA.setPassword(config.password);
}
// setup callbacks
ArduinoOTA.onStart([]() {
Serial.println("OTA: Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("OTA: End");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("OTA: Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("OTA: Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("OTA: Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("OTA: Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("OTA: Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("OTA: Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("OTA: End Failed");
});
enable();
}
void disable(){
otaTask.disable();
}
};
#endif