Added a new OTAManager class which wraps the OTAUpdater class and configures it using parameters read from a cfg file

This commit is contained in:
anschrammh 2022-09-27 07:26:12 +02:00
parent ab493ef6d8
commit f547c8fc07
2 changed files with 116 additions and 0 deletions

89
src/app/OTAManager.cpp Normal file
View File

@ -0,0 +1,89 @@
#include "OTAManager.h"
#include "definition.h"
#define DEBUG_OTA_MANAGER(...) do {} while(0)
//#define DEBUG_OTA_MANAGER(...) do { Serial.printf(__VA_ARGS__); Serial.println();} while(0)
OTAManager::OTAManager(SDCardManager &sdCardManager, const BoardConfig &boardConfig) : _sdCardManager(&sdCardManager), _boardConfig(&boardConfig)
{ }
OTAManager::~OTAManager()
{ }
boolean OTAManager::init(void)
{
//If the SDCardManager is not available, then we disable the OTA service
if(!_sdCardManager)
{
DEBUG_OTA_MANAGER("SDCardMng is NULL !");
return true;
}
//If the SDCardManager is available, then we try to read the config file
CFGDictionary<CFGParameterValue> *otaCfg = _sdCardManager->getCFGFile(OTA_CFG_FILE);
boolean toReturn(true);
//If we did not find the file
if(!otaCfg)
{
DEBUG_OTA_MANAGER("otaCfg is NULL !");
return false;
}
if((*otaCfg)("ENABLED") != nullptr)
{
if((*otaCfg)("ENABLED")->booleanValue())
{
DEBUG_OTA_MANAGER("ENABLED : %s", (*otaCfg)("ENABLED")->stringValue());
if((*otaCfg)("OTA_SERVER_ADDRESS") != nullptr) //This is the only required parameter
{
_isServiceEnabled = true;
_otaUpdater.setLedPin(_boardConfig->getOnBoard_LED(), LOW);
_otaUpdater.setServerAddress((*otaCfg)("OTA_SERVER_ADDRESS")->stringValue());
if((*otaCfg)("OTA_SERVER_PORT") != nullptr)
{
DEBUG_OTA_MANAGER("OTA_SERVER_PORT : %s", (*otaCfg)("OTA_SERVER_PORT")->stringValue());
_otaUpdater.setPort((*otaCfg)("OTA_SERVER_PORT")->uintValue());
}
if((*otaCfg)("OTA_SERVICE_PATH") != nullptr)
{
DEBUG_OTA_MANAGER("OTA_SERVICE_PATH : %s", (*otaCfg)("OTA_SERVICE_PATH")->stringValue());
_otaUpdater.setPath((*otaCfg)("OTA_SERVICE_PATH")->stringValue());
}
if((*otaCfg)("OTA_SERVICE_AUTH_KEY") != nullptr)
{
DEBUG_OTA_MANAGER("OTA_SERVICE_AUTH_KEY : %s", (*otaCfg)("OTA_SERVICE_AUTH_KEY")->stringValue());
_otaUpdater.setOtaAuthKey((*otaCfg)("OTA_SERVICE_AUTH_KEY")->stringValue());
}
}
else
{
DEBUG_OTA_MANAGER("OTA_SERVER_ADDRESS is NULL !");
toReturn = false;
}
}
}
else
{
DEBUG_OTA_MANAGER("ENABLED is NULL !");
toReturn = false;
}
delete otaCfg;
return toReturn;
}
boolean OTAManager::isEnabled(void) const
{
return _isServiceEnabled;
}
OTAUpdater& OTAManager::getOTAUpdater(void)
{
return _otaUpdater;
}

27
src/app/OTAManager.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef OTAMANAGER_H
#define OTAMANAGER_H
#include "OTAUpdater.h"
#include "SDCardManager.h"
#include "BoardConfig.h"
class OTAManager
{
friend class SAB;
public:
~OTAManager();
boolean init(void);
boolean isEnabled(void) const;
OTAUpdater& getOTAUpdater(void);
protected:
OTAManager(SDCardManager &sdCardManager, const BoardConfig &boardConfig);
OTAManager();
private:
OTAUpdater _otaUpdater;
SDCardManager *_sdCardManager = nullptr;
const BoardConfig * _boardConfig = nullptr;
boolean _isServiceEnabled = false;
};
#endif //OTAMANAGER_H