291 lines
8.4 KiB
C++
291 lines
8.4 KiB
C++
#include "SystemConfig.h"
|
|
#include "GeneralMessagesLogDispatcher.h"
|
|
|
|
CSystemConfig::CSystemConfig()
|
|
{
|
|
}
|
|
|
|
CSystemConfig::~CSystemConfig()
|
|
{
|
|
while(mCANDeviceConfigList.size() != 0)
|
|
{
|
|
delete mCANDeviceConfigList.takeAt(0);
|
|
}
|
|
while(mCloudLoggingParamsList.size() != 0)
|
|
{
|
|
delete mCloudLoggingParamsList.takeAt(0);
|
|
}
|
|
}
|
|
|
|
int CSystemConfig::LoadConfig()
|
|
{
|
|
|
|
CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Chargement de la configuration système..."),"CPCANInterface");
|
|
// if(DevicesList == 0)
|
|
// {
|
|
// qDebug("CSystemConfig: Trying to load a config into an invalid devices list");
|
|
// CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Échec de sauvegarde de la config système. Pointeur vers liste de devices invalide!!!"),true,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_ERROR_STATUS);
|
|
// return RET_GENERAL_ERROR;
|
|
// }
|
|
|
|
QFile* ConfigFile = new QFile("./Config/Station.cfg");
|
|
if(ConfigFile)
|
|
{
|
|
if(ConfigFile->open(QIODevice::ReadOnly | QIODevice::Unbuffered) == false)
|
|
{
|
|
qDebug("CSystemConfig: Failed to open config file in config loading");
|
|
CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Échec de sauvegarde de la config système. Impossible d'ouvrir le fichier /Config/Station.cfg"),"CPCANInterface",true,1,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_ERROR_STATUS);
|
|
return RET_GENERAL_ERROR;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Échec de sauvegarde de la config système. Pointeur vers QFile invalide !!!"),"CPCANInterface",true,1,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_ERROR_STATUS);
|
|
qDebug("CSystemConfig: Invalid QFile pointer in config loading...");
|
|
return RET_GENERAL_ERROR;
|
|
}
|
|
|
|
QDataStream * InputStream = new QDataStream(ConfigFile);
|
|
|
|
quint32 MagicNbr;// = 0xBAADCAFE;
|
|
quint32 FileVersion;
|
|
|
|
|
|
*InputStream >> MagicNbr;
|
|
if(MagicNbr != OTARCIK_CONFIG_FILE_MAGIC_NBR)
|
|
{
|
|
// CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Fichier /Config/Station.cfg corrompu. Création d'une config vide"),"CPCANInterface",true,1,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_WARNING_STATUS);
|
|
ConfigFile->close();
|
|
// CreateEmptyConfig();
|
|
return RET_GENERAL_ERROR;
|
|
}
|
|
|
|
*InputStream >> FileVersion;
|
|
|
|
//Load CAN devices configs
|
|
int NbDevices;
|
|
*InputStream >> NbDevices;
|
|
for(int i = 0; i < NbDevices; i++)
|
|
{
|
|
CCANDeviceConfig *NewDeviceConfig = new CCANDeviceConfig;
|
|
*InputStream >> *NewDeviceConfig;
|
|
//DevicesList->append(new CCANDevice(NewDeviceConfig));
|
|
mCANDeviceConfigList.append(NewDeviceConfig);
|
|
}
|
|
|
|
//Load Cloud logging settings
|
|
//TODO: load list size
|
|
int NbCloudParams = 1;
|
|
*InputStream >> NbCloudParams;
|
|
for(int i = 0; i < NbCloudParams; i++)
|
|
{
|
|
CCloudParams *NewBrokerParams = new CCloudParams;
|
|
*InputStream >> *NewBrokerParams;
|
|
mCloudLoggingParamsList.append(NewBrokerParams);
|
|
}
|
|
|
|
//Load general program settings
|
|
*InputStream >> mGeneralSystemParams;
|
|
|
|
//Load CAN Watchdog parameters
|
|
*InputStream >> mCANWatchdogParams;
|
|
|
|
//Load CPU Watchdog parameters
|
|
if(FileVersion > 1)
|
|
{
|
|
*InputStream >> mCPUWatchdogParams;
|
|
}
|
|
|
|
//Load the device devection reporting params
|
|
if(FileVersion > 1)
|
|
{
|
|
*InputStream >> mDeviceDetectionParams;
|
|
}
|
|
|
|
|
|
|
|
ConfigFile->close();
|
|
delete ConfigFile;
|
|
delete InputStream;
|
|
|
|
CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Configuration système chargée avec succès!"),"CPCANInterface",true,1,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_SUCCESS_STATUS);
|
|
return RET_OK;
|
|
}
|
|
|
|
int CSystemConfig::SaveConfig()
|
|
{
|
|
CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Sauvegarde de la configuration système..."),"CPCANInterface");
|
|
|
|
QFile *ConfigFile = new QFile("./Config/Station.cfg");
|
|
if(ConfigFile)
|
|
{
|
|
if(ConfigFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered) == false)
|
|
{
|
|
CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Échec de sauvegarde de la config système. Impossible d'ouvrir le fichier /Config/Station.cfg"),"CPCANInterface",true,1,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_ERROR_STATUS);
|
|
return RET_GENERAL_ERROR;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Échec de sauvegarde de la config système. Pointeur vers QFile invalide !!!"),"CPCANInterface",true,1,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_ERROR_STATUS);
|
|
return RET_GENERAL_ERROR;
|
|
}
|
|
|
|
|
|
QDataStream * OutputStream = new QDataStream(ConfigFile);
|
|
|
|
quint32 MagicNbr = OTARCIK_CONFIG_FILE_MAGIC_NBR;
|
|
quint32 FileVersion = OTARCIK_CURRENT_CONFIG_FILE_VERSION;
|
|
|
|
//Setup the config file header
|
|
*OutputStream << MagicNbr;
|
|
*OutputStream << FileVersion;
|
|
|
|
//Save the CAN devices config info
|
|
*OutputStream << mCANDeviceConfigList.size(); //Number of devices
|
|
for(int i = 0; i < mCANDeviceConfigList.size(); i++)
|
|
{
|
|
*OutputStream << *mCANDeviceConfigList.at(i); //Add each device info to config file
|
|
}
|
|
|
|
//Save the Cloud logging settings
|
|
*OutputStream << mCloudLoggingParamsList.size();
|
|
for(int i = 0; i < mCloudLoggingParamsList.size(); i++)
|
|
{
|
|
*OutputStream << *mCloudLoggingParamsList.at(i);
|
|
}
|
|
|
|
//Save the general system parameters
|
|
*OutputStream << mGeneralSystemParams;
|
|
|
|
//Save the CAN Watchdog params
|
|
*OutputStream << mCANWatchdogParams;
|
|
|
|
//Save the CPU Watchdog params
|
|
*OutputStream << mCPUWatchdogParams;
|
|
|
|
//Save the device detection reporting params
|
|
*OutputStream << mDeviceDetectionParams;
|
|
|
|
ConfigFile->flush();
|
|
ConfigFile->close();
|
|
|
|
delete ConfigFile;
|
|
delete OutputStream;
|
|
|
|
CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Configuration système sauvegardée avec succès."),"CPCANInterface",true,3,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_SUCCESS_STATUS);
|
|
|
|
return RET_OK;
|
|
}
|
|
|
|
int CSystemConfig::CreateEmptyConfig()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
QList<CCANDeviceConfig*> * CSystemConfig::GetCANDevicesConfigList()
|
|
{
|
|
return &mCANDeviceConfigList;
|
|
}
|
|
|
|
|
|
int CSystemConfig::SetCANDevicesConfigList(QList<CCANDeviceConfig *> *DevicesConfigList)
|
|
{
|
|
if(DevicesConfigList == 0)
|
|
return RET_GENERAL_ERROR;
|
|
|
|
while(mCANDeviceConfigList.size() != 0)
|
|
{
|
|
delete mCANDeviceConfigList.takeAt(0);
|
|
}
|
|
|
|
for(int i = 0; i < DevicesConfigList->size(); i++)
|
|
{
|
|
CCANDeviceConfig *NewDeviceConfig = new CCANDeviceConfig(*DevicesConfigList->at(i));
|
|
mCANDeviceConfigList.append(NewDeviceConfig);
|
|
}
|
|
|
|
return RET_OK;
|
|
}
|
|
|
|
QList<CCloudParams *> *CSystemConfig::GetCloudParams()
|
|
{
|
|
return &mCloudLoggingParamsList;
|
|
}
|
|
|
|
CCANWatchdogConfig *CSystemConfig::GetCANWatchdogConfig()
|
|
{
|
|
return &mCANWatchdogParams;
|
|
}
|
|
|
|
CGeneralSystemParams *CSystemConfig::GetGeneralSystemSettings()
|
|
{
|
|
return &mGeneralSystemParams;
|
|
}
|
|
|
|
CCPUWatchdogConfig *CSystemConfig::GetCPUWatchdogSettings()
|
|
{
|
|
return &mCPUWatchdogParams;
|
|
}
|
|
|
|
int CSystemConfig::SetCANWatchdogConfig(CCANWatchdogConfig *Config)
|
|
{
|
|
if(Config == 0)
|
|
return RET_GENERAL_ERROR;
|
|
|
|
mCANWatchdogParams = *Config;
|
|
}
|
|
|
|
int CSystemConfig::SetCloudParamsList(QList<CCloudParams *> *CloudParams)
|
|
{
|
|
if(CloudParams == 0)
|
|
return RET_GENERAL_ERROR;
|
|
|
|
while(mCloudLoggingParamsList.size() != 0)
|
|
{
|
|
delete mCloudLoggingParamsList.takeAt(0);
|
|
}
|
|
|
|
for(int i = 0; i < CloudParams->size(); i++)
|
|
{
|
|
CCloudParams *NewBrokerParams = new CCloudParams(*CloudParams->at(i));
|
|
mCloudLoggingParamsList.append(NewBrokerParams);
|
|
}
|
|
|
|
return RET_OK;
|
|
}
|
|
|
|
int CSystemConfig::SetGeneralSystemParams(CGeneralSystemParams *GeneralParams)
|
|
{
|
|
if(GeneralParams == 0)
|
|
return RET_GENERAL_ERROR;
|
|
|
|
mGeneralSystemParams = *GeneralParams;
|
|
return RET_OK;
|
|
}
|
|
|
|
int CSystemConfig::SetCPUWatchdogPArams(CCPUWatchdogConfig *CPUWatchdogParams)
|
|
{
|
|
if(CPUWatchdogParams == 0)
|
|
return RET_GENERAL_ERROR;
|
|
|
|
mCPUWatchdogParams = *CPUWatchdogParams;
|
|
return RET_OK;
|
|
}
|
|
|
|
int CSystemConfig::SetDeviceDetectionConfig(CDeviceDetectionConfig *DeviceDetectconfig)
|
|
{
|
|
if(DeviceDetectconfig == 0)
|
|
{
|
|
return RET_GENERAL_ERROR;
|
|
}
|
|
|
|
mDeviceDetectionParams = *DeviceDetectconfig;
|
|
}
|
|
|
|
CDeviceDetectionConfig* CSystemConfig::GetDeviceDetectionConfig()
|
|
{
|
|
return &mDeviceDetectionParams;
|
|
}
|