2022-10-25 22:43:57 +00:00
# include <esp_task_wdt.h>
2022-11-16 20:41:50 +00:00
# include <logger.h>
2021-03-12 22:21:45 +00:00
2021-01-04 22:10:23 +00:00
# include "Task.h"
2021-03-12 22:21:45 +00:00
# include "TaskOTA.h"
# include "project_configuration.h"
2021-01-03 21:43:35 +00:00
2021-03-12 22:21:45 +00:00
OTATask : : OTATask ( ) : Task ( TASK_OTA , TaskOta ) , _beginCalled ( false ) {
2021-01-03 21:43:35 +00:00
}
2021-03-12 22:21:45 +00:00
OTATask : : ~ OTATask ( ) {
2021-01-03 21:43:35 +00:00
}
2021-05-18 22:44:37 +00:00
bool OTATask : : setup ( System & system ) {
2021-05-21 21:06:13 +00:00
_ota . onStart ( [ & ] ( ) {
2021-03-12 22:21:45 +00:00
String type ;
2022-03-19 21:29:38 +00:00
if ( _ota . getCommand ( ) = = U_FLASH ) {
2021-03-12 22:21:45 +00:00
type = " sketch " ;
2022-03-19 21:29:38 +00:00
} else { // U_SPIFFS
2021-03-12 22:21:45 +00:00
type = " filesystem " ;
2022-03-19 21:29:38 +00:00
}
2022-11-16 22:10:06 +00:00
system . getLogger ( ) . log ( logging : : LoggerLevel : : LOGGER_LEVEL_INFO , getName ( ) , " Start updating %s. please wait, this process is taking some time! " , type . c_str ( ) ) ;
2021-03-12 22:21:45 +00:00
} )
2022-03-19 21:29:38 +00:00
. onEnd ( [ & ] ( ) {
system . getLogger ( ) . log ( logging : : LoggerLevel : : LOGGER_LEVEL_INFO , getName ( ) , " OTA End " ) ;
2021-03-12 22:21:45 +00:00
} )
2022-03-19 21:29:38 +00:00
. onError ( [ & ] ( ota_error_t error ) {
String error_str ;
if ( error = = OTA_AUTH_ERROR ) {
error_str = " Auth Failed " ;
} else if ( error = = OTA_BEGIN_ERROR ) {
error_str = " Begin Failed " ;
} else if ( error = = OTA_CONNECT_ERROR ) {
error_str = " Connect Failed " ;
} else if ( error = = OTA_RECEIVE_ERROR ) {
error_str = " Receive Failed " ;
} else if ( error = = OTA_END_ERROR ) {
error_str = " End Failed " ;
}
2022-03-20 09:56:06 +00:00
system . getLogger ( ) . log ( logging : : LoggerLevel : : LOGGER_LEVEL_ERROR , getName ( ) , " Error[%d]: %s " , error , error_str . c_str ( ) ) ;
2022-10-25 22:43:57 +00:00
} )
2022-11-16 19:22:03 +00:00
. onProgress ( [ & ] ( unsigned int received , unsigned int total_size ) {
2022-10-25 22:43:57 +00:00
esp_task_wdt_reset ( ) ;
2021-03-12 22:21:45 +00:00
} ) ;
2021-12-18 21:33:35 +00:00
if ( system . getUserConfig ( ) - > network . hostname . overwrite ) {
_ota . setHostname ( system . getUserConfig ( ) - > network . hostname . name . c_str ( ) ) ;
} else {
_ota . setHostname ( system . getUserConfig ( ) - > callsign . c_str ( ) ) ;
}
2021-03-12 22:21:45 +00:00
_stateInfo = " " ;
return true ;
2021-01-03 21:43:35 +00:00
}
2021-05-18 22:44:37 +00:00
bool OTATask : : loop ( System & system ) {
2021-03-12 22:21:45 +00:00
if ( ! _beginCalled ) {
2021-05-21 21:06:13 +00:00
_ota . begin ( ) ;
2021-03-12 22:21:45 +00:00
_beginCalled = true ;
}
2021-05-21 21:06:13 +00:00
_ota . handle ( ) ;
2021-03-12 22:21:45 +00:00
return true ;
2021-01-03 21:43:35 +00:00
}