diff --git a/Sources/Chalet/ChaletDataLogger.cpp b/Sources/Chalet/ChaletDataLogger.cpp index aa661c1..81b604e 100644 --- a/Sources/Chalet/ChaletDataLogger.cpp +++ b/Sources/Chalet/ChaletDataLogger.cpp @@ -1,6 +1,88 @@ #include "ChaletDataLogger.h" +#include +#include + CChaletDataLogger::CChaletDataLogger(QObject *parent) : QObject(parent) { + bool create = false; + mChaletLogFile = new QFile(CHALET_LORA_DATA_LOG_FILENAME); + if(QFile::exists(CHALET_LORA_DATA_LOG_FILENAME) == false) + create = true; + if(mChaletLogFile->open(QIODevice::ReadWrite|QIODevice::Append/*|QIODevice::Text*/) == false) + { + delete mChaletLogFile; + mChaletLogFile = 0; + qDebug("Cannot open chalet LORA log file..."); + return; + } + + if(create) + { + qDebug("Creating new chalet LORA log file..."); + QString Header("Date;Heure;Courant Batterie;Tension Batterie;Inverter;Wifi\n"); + int bytes = mChaletLogFile->write(qPrintable(Header)); + mChaletLogFile->flush(); + + } +// mChaletLogFile->close(); + + qDebug("Chalet LORA log file created successfully"); +} + +CChaletDataLogger::~CChaletDataLogger() +{ + if(mChaletLogFile) + { + mChaletLogFile->close(); + delete mChaletLogFile; + } +} + +bool CChaletDataLogger::LogChaletLORAData(CChaletMainStatus *Data) +{ + if(mChaletLogFile == 0) + { + qDebug("Invalid LORA log file handle"); + return false; + } + +// if(mChaletLogFile->open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text) == false) +// { +// delete mChaletLogFile; +// mChaletLogFile = 0; +// qDebug("Cannot open chalet LORA log file..."); +// return false; +// } + + QString CSVData;// = QString("%1;%2;%3;%4;%5;%6\n").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd").arg(QDateTime::currentDateTime().toString("hh:mm:ss")).arg(Data->mBatteryCurrent).arg(Data->mBatteryVoltage).arg(Data->mInverterRelayStatus).arg(Data->mWiFiModuleStatus)); + CSVData.clear(); + CSVData.append(QDateTime::currentDateTime().toString("yyyy-MM-dd")); + CSVData.append(";"); + CSVData.append(QDateTime::currentDateTime().toString("hh:mm:ss")); + CSVData.append(";"); + CSVData.append(QString("%1;").arg(Data->mBatteryCurrent)); + CSVData.append(QString("%1;").arg(Data->mBatteryVoltage)); + if(Data->mInverterRelayStatus == 1) + CSVData.append(QString("ON;")); + else + CSVData.append(QString("OFF;")); + + //CSVData.append(QString("%1;").arg(Data->mInverterRelayStatus)); + if(Data->mWiFiModuleStatus == 0) + CSVData.append(QString("OFF\n")); + else + CSVData.append(QString("ON\n")); + // CSVData.append(QString("%1\n").arg(Data->mWiFiModuleStatus)); + + + + + mChaletLogFile->write(qPrintable(CSVData)); + mChaletLogFile->flush(); + + // mChaletLogFile->close(); + + return true; } diff --git a/Sources/Chalet/ChaletDataLogger.h b/Sources/Chalet/ChaletDataLogger.h index 16c12dd..2d6e927 100644 --- a/Sources/Chalet/ChaletDataLogger.h +++ b/Sources/Chalet/ChaletDataLogger.h @@ -2,16 +2,29 @@ #define CHALETDATALOGGER_H #include +#include +#include "ChaletData.h" + +//#define CHALET_LORA_DATA_LOG_FILENAME "./ChaletLogs/ChaletLora.csv" +#define CHALET_LORA_DATA_LOG_FILENAME "D:/Main/Chalet/LoraLogs/ChaletLora.csv" + +class CChaletMainStatus; class CChaletDataLogger : public QObject { Q_OBJECT public: explicit CChaletDataLogger(QObject *parent = 0); + ~CChaletDataLogger(); + + bool LogChaletLORAData(CChaletMainStatus *Data); + + QFile *mChaletLogFile; + signals: public slots: }; -#endif // CHALETDATALOGGER_H \ No newline at end of file +#endif // CHALETDATALOGGER_H diff --git a/Sources/Chalet/ChaletLoraDevice.cpp b/Sources/Chalet/ChaletLoraDevice.cpp index 040cdf8..a05667a 100644 --- a/Sources/Chalet/ChaletLoraDevice.cpp +++ b/Sources/Chalet/ChaletLoraDevice.cpp @@ -75,6 +75,8 @@ int CChaletLoraDevice::NewDeviceFrameReceived(int DeviceID, int DeviceAddress, i qDebug("Current: %d",mChaletMainStatus.mBatteryCurrent); qDebug("SOC: %d",mChaletMainStatus.mBatterySOC); + mChaletDataLogger.LogChaletLORAData(&mChaletMainStatus); + mBlynkInterface.UpdateChaletCurrent(mChaletMainStatus.mBatteryCurrent); mBlynkInterface.UpdateChaletVoltage(mChaletMainStatus.mBatteryVoltage); // mBlynkInterface.UpdateChaletWifiStatus((int)mChaletMainStatus.mInverterRelayStatus); @@ -86,7 +88,7 @@ int CChaletLoraDevice::NewDeviceFrameReceived(int DeviceID, int DeviceAddress, i case CHALET_AC_POWER_STATE_STATUS_RESPONSE: { mChaletMainStatus.mInverterRelayStatus = Data[0]; - CmdResponseReceived(CHALET_AC_POWER_SET_STATE_RESPONSE); + CmdResponseReceived(CHALET_AC_POWER_STATE_STATUS_REQUEST); break; } case CHALET_AC_POWER_SET_STATE_RESPONSE: @@ -291,7 +293,7 @@ int CChaletLoraDevice::CmdResponseReceived(int CmdID) if(mPendingNetworkMsgList.size() == 0) //If no message is left pending... schedule status request { ScheduleChaletStatusRequest(); - mChaletStatusTimer->start(1000);//Next status request will be sent in 1000ms + mChaletStatusTimer->start(LORA_NORMAL_REQUEST_TIMEOUT);//Next status request will be sent within this timeout } else { diff --git a/Sources/Chalet/ChaletLoraDevice.h b/Sources/Chalet/ChaletLoraDevice.h index 34d8202..520a080 100644 --- a/Sources/Chalet/ChaletLoraDevice.h +++ b/Sources/Chalet/ChaletLoraDevice.h @@ -10,12 +10,13 @@ #include "ChaletNetworkMessage.h" #include #include "BlynkCloudClient.h" +#include "ChaletDataLogger.h" #define LORA_MAGIC_WORD 0xBAADCAFE -#define LORA_NORMAL_REQUEST_TIMEOUT 1000 +#define LORA_NORMAL_REQUEST_TIMEOUT 3000 #define LORA_IMMEDIATE_REQUEST_TIMEOUT 300 -#define LORA_RETRY_REQUEST_TIMEOUT 1000 -#define LORA_RESPONSE_TIME_TIMEOUT 5000 +#define LORA_RETRY_REQUEST_TIMEOUT 2000 +#define LORA_RESPONSE_TIME_TIMEOUT 3000 #define LORA_CHALET_STATUS_POWER_RELAY_MASK 0x01 #define LORA_CHALET_STATUS_CUR_SENSOR_MASK 0x02 @@ -39,6 +40,7 @@ public: CChaletMainStatus mChaletMainStatus; QList mPendingNetworkMsgList; + CChaletDataLogger mChaletDataLogger; int SendWiFiModuleSetState(bool State); int SendInverterPowerRelayState(bool State);