Index: sources/cloudsync/CloudSyncController.cpp =================================================================== diff -u -r9ae3b0d6624904693329309aaf8ff02784c17184 -re60d099fd4fbada632a974b9fc95d4d2c9652b1a --- sources/cloudsync/CloudSyncController.cpp (.../CloudSyncController.cpp) (revision 9ae3b0d6624904693329309aaf8ff02784c17184) +++ sources/cloudsync/CloudSyncController.cpp (.../CloudSyncController.cpp) (revision e60d099fd4fbada632a974b9fc95d4d2c9652b1a) @@ -20,7 +20,6 @@ // Project #include "MainTimer.h" #include "MessageDispatcher.h" -#include "Logger.h" #include "GuiController.h" #include "DeviceController.h" #include "FileHandler.h" @@ -33,7 +32,9 @@ * Qt handles the children destruction by their parent objects life-cycle. */ CloudSyncController::CloudSyncController(QObject *parent) : QObject(parent) { - addWatch(); + checkDate(); + sendOutBuff("Ready"); + startTimer(_interval); } /*! @@ -76,21 +77,6 @@ // it has been tested and works perfectly fine in normal run. quitThread(); // validated } - -/*! - * \brief CloudSyncController::onWatchFileChange - * \details This slot will be called when the Device Controller identifies any changes in the watched files. - * \param vFile - watched file - * \note The DeviceController will emit the signal on any watched file update, it's up to the CloudSyncController to filter the result. - */ -void CloudSyncController::onWatchFileChange(const QString &vFile) -{ - if ( QFileInfo(vFile).fileName() != Storage::CloudSync_Out_File ) return; // ignore unwanted file updates. - - QString content; - Storage::FileHandler::read(vFile, content); - interpreter(content); -} // coco end /*! @@ -141,12 +127,46 @@ } // coco end +/*! + * \brief CloudSyncController::timerEvent + * \details The timer event handler which currently is triggered on each second to check for the date change, + * Which caused the watched file change and needs to updated the watched list. + * The check-in (watch dog) also needs to be here. + */ +void CloudSyncController::timerEvent(QTimerEvent *) +{ + // TODO: touch the inp file to as a check-in for CloudSync to know we are up + // a simple touch or a check-in message? + checkDate(); +} + +/*! + * \brief CloudSyncController::onWatchFileChange + * \details This slot will be called when the Device Controller identifies any changes in the watched files. + * \param vFile - watched file + * \note The DeviceController will emit the signal on any watched file update, it's up to the CloudSyncController to filter the result. + */ +void CloudSyncController::onWatchFileChange(const QString &vFile) +{ + if ( vFile != _date_out_File ) return; // ignore unwanted file updates. + QString content; + Storage::FileHandler::read(vFile, content); + interpreter(content); +} + +/*! + * \brief CloudSyncController::addWatch + * \details + */ void CloudSyncController::addWatch() { - bool ok = Storage::FileHandler::makeFolder(Storage::SDCard_Base_Path_Name + _location); + // QString outBuff; + bool ok = Storage::FileHandler::makeFolder(_location); if ( ok ) { + _date_out_File = _location + // The location + _dateFormatted + _dateSeparator + _out_File; // The file name // watching for the cloud sync output file buffer. - _DeviceController.doAddWatch(Storage::SDCard_Base_Path_Name + _location + Storage::CloudSync_Out_File); + _DeviceController.doAddWatch(_date_out_File); } else { LOG_DEBUG(tr("the CloudSync log folder cannot be created.")); @@ -161,31 +181,69 @@ */ bool CloudSyncController::interpreter(const QString &vContent) { - qDebug() << QString("~~~CloudSync Message received [%1]").arg(vContent); + LOG_DEBUG(QString("~~~CloudSync Message received [%1]").arg(vContent)); // TODO: messages have to have a sequence. // if the seq is duplicate it will be ignored. // seq, id, payload return true; } /*! + * \brief CloudSyncController::checkDate + * \details Checks the date and updated the watched file in case the date changed. + */ +void CloudSyncController::checkDate() +{ + _datetime = QDateTime::currentDateTime(); + _timeFormatted = _datetime.toString(_timeFormat); + QString dateFormatted = _datetime.toString(_dateFormat); + if (_dateFormatted != dateFormatted) { + _dateFormatted = dateFormatted; + // TODO: do we need to remove current watch? + addWatch(); + } +} + +bool CloudSyncController::sendOutBuff(const QString &vData) +{ + QString inpBuff; + _date_inp_File = _location + // The location + _dateFormatted + _dateSeparator + _inp_File; // The file name + inpBuff = _timeFormatted; + inpBuff += _separator + QString::number(_seq++); + inpBuff += _separator + vData; + // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + // writing the message into the buffer. + if ( ! Storage::FileHandler::write(_date_inp_File, inpBuff) ) { + LOG_DEBUG(tr("Error writing to the CloudSync Input file.")); + return false; + } + return true; +} + +/*! * \brief CloudSyncController::onActionReceive * \details The slot which will be called when a CANBus message is received, and will be sent to CloudSync if related. * \param vAction - The message action * \param vData - The message data */ void CloudSyncController::onActionReceive(GuiActionType vAction, const QVariantList &vData) { + QString inpBuff; switch (vAction) { case GuiActionType::ID_HDOperationModeData: case GuiActionType::ID_PreTreatmentStates : case GuiActionType::ID_TreatmentStates : case GuiActionType::ID_PostTreatmentStates: case GuiActionType::ID_DisinfectStates : - qDebug() << "~ update for CloudSync" << vAction << vData; - if ( ! Storage::FileHandler::write(Storage::SDCard_Base_Path_Name + _location + Storage::CloudSync_Inp_File, enumString(vAction)) ) - LOG_DEBUG(tr("Error writing to the CloudSync Input file.")); - break; + // TODO: This section is the translation/mapping section + // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv + // preparing the message + inpBuff = Format::toHexString/*QString::number*//*enumString*/(vAction); + for (auto var : vData) + { inpBuff += _separator + var.toString(); } + inpBuff += '\n'; + if ( ! sendOutBuff(inpBuff) ) break; default: break; }