Index: sources/applicationcontroller.cpp =================================================================== diff -u -rde2f87e15fa05b1c45581cfedd8f1af0c47c2b48 -r415f3e16ff6e572c8ce7e7b3576e82ce8b44c6ce --- sources/applicationcontroller.cpp (.../applicationcontroller.cpp) (revision de2f87e15fa05b1c45581cfedd8f1af0c47c2b48) +++ sources/applicationcontroller.cpp (.../applicationcontroller.cpp) (revision 415f3e16ff6e572c8ce7e7b3576e82ce8b44c6ce) @@ -1,6 +1,6 @@ /*! * - * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. + * Copyright (c) 2019-2019 Diality Inc. - All Rights Reserved. * \copyright \n * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, \n * IN PART OR IN WHOLE, \n @@ -17,108 +17,216 @@ // Project #include "guiglobals.h" +#include "maintimer.h" #include "guicontroller.h" -#include "messagehandler.h" +#include "messagedispatcher.h" +#include "logger.h" +#include "usbwatcher.h" +#include "filehandler.h" -// Singleton -SINGLETON_INIT(ApplicationController) - /*! * \brief ApplicationController Constructor * \param parent */ ApplicationController::ApplicationController(QObject *parent) : QObject(parent) { - _fileHandler = new Storage::FileHandler (this); _applicationPost = new ApplicationPost(this); - } /*! * \brief ApplicationController initializer */ bool ApplicationController::init() { - if (!_fileHandler ->init()) return false; + if ( _init ) return false; + _init = true; + + initConnections(); if (!_applicationPost->init()) return false; - connection(); + + LOG_EVENT(QObject::tr("%1 Initialized").arg(metaObject()->className())); + return true; } /*! - * \brief GUI Controller connections definition + * \brief ApplicationController::init + * \details Initialized the Class by calling the init() method first + * And initializes the thread vThread by calling initThread + * on success init(). + * \param vThread - the thread + * \return returns the return value of the init() method */ -void ApplicationController::connection() +bool ApplicationController::init(QThread &vThread) { + if ( ! init() ) return false; + initThread(vThread); + return true; +} + +/*! + * \brief ApplicationController::quit + * \details quits the class + * Calls quitThread + */ +void ApplicationController::quit() +{ + quitThread(); +} + +/*! + * \brief ApplicationController::initConnections + * \details Initializes the required signal/slot connection between this class and other objects + * to be able to communicate. + */ +void ApplicationController::initConnections() +{ + connect(&_MainTimer , SIGNAL( didTimeout()), + this , SLOT(onMainTimerTimeout())); + // From GUI - connect(_GuiController , SIGNAL(didActionRequest(GuiActionType)), - this , SLOT( onActionRequest(GuiActionType))); - // From GUI - connect(_GuiController , SIGNAL(didActionPerform(GuiActionType,GuiActionInfo)), - this , SLOT( onActionPerform(GuiActionType, GuiActionInfo))); + connect(&_GuiController , SIGNAL(didActionTransmit(GuiActionType, const QVariantList &)), + this , SLOT( onActionTransmit(GuiActionType, const QVariantList &))); // From HD/DG - connect(_MessageHandler, SIGNAL(didActionCommand(GuiActionType)), - this , SLOT( onActionCommand(GuiActionType))); - // From HD/DG - connect(_MessageHandler, SIGNAL(didActionConfirm(GuiActionType,GuiActionInfo)), - this , SLOT( onActionConfirm(GuiActionType, GuiActionInfo))); + connect(&_MessageDispatcher, SIGNAL(didActionReceive(GuiActionType, const QVariantList &)), + this , SLOT( onActionReceive(GuiActionType, const QVariantList &))); + + connect(&_GuiController , SIGNAL(didUSBDriveUmount()), + this , SLOT( onUSBDriveUmount())); + connect(&_USBWatcher , SIGNAL(didUSBDriveMount ()), + this , SLOT( onUSBDriveMount ())); + connect(&_USBWatcher , SIGNAL(didUSBDriveRemove()), + this , SLOT( onUSBDriveRemove())); + + connect(&_GuiController , SIGNAL(didExportLog()), + this , SLOT( onExportLog())); + + connect(&_Logger , SIGNAL(didExport()), + this , SLOT( onExport())); } +/*! + * \brief ApplicationController::initThread + * \details Moves this object into the thread vThread. + * And checks that this method is called from main thread. + * Also connects quitThread to application aboutToQuit. + * \param vThread - the thread + */ +void ApplicationController::initThread(QThread &vThread) +{ + // runs in main thread + Q_ASSERT_X(QThread::currentThread() == qApp->thread() , __func__, "The Class initialization must be done in Main Thread" ); + _thread = &vThread; + _thread->setObjectName(QString("%1_Thread").arg(metaObject()->className())); + connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(quit())); + _thread->start(); + moveToThread(_thread); +} /*! + * \brief ApplicationController::quitThread + * \details Moves this object to main thread to be handled by QApplicaiton + * And to be destroyed there. + */ +void ApplicationController::quitThread() +{ + if ( ! _thread ) return; + + // runs in thread + moveToThread(qApp->thread()); +} + +/*! * \brief Process the requested action * \details Processes the requested action * \param vAction - User requested Action */ -void ApplicationController::onActionRequest(GuiActionType vAction) +void ApplicationController::onActionTransmit(GuiActionType vAction, const QVariantList &vData) { - // qDebug() << "ApplicationController.actionRequested : " << vAction; - // Process the requested action by GUI - // Process ... - emit didActionRequest(vAction); + emit didActionTransmit(vAction, vData); } /*! - * \brief Process the performed action - * \details An action which has been commanded by HD has been performed by GUI.\n - * GUI notifies that the action has been performed + * \brief An action has been confirmed + * \details GUI requested an action. + * In response HD confirmed the action. * \param vAction - * \param vInfo + * \param vData */ -void ApplicationController::onActionPerform(GuiActionType vAction, GuiActionInfo vInfo) +void ApplicationController::onActionReceive (GuiActionType vAction, const QVariantList &vData) { - // qDebug() << "ApplicationController.actionPerformed : " << vAction << vInfo; - // Process the performed action by GUI - // Process ... - emit didActionPerform(vAction, vInfo); + emit didActionReceive (vAction, vData); } /*! - * \brief Action commanded by HD - * \details An action has been commanded by HD, - * GUI requires to be notified to perform the action. - * \param vAction + * \brief ApplicationController::onMainTimerTimeout + * \details This slot is called by MainTimer::didTimeout each second + * to call required methods like keepAlive */ -void ApplicationController::onActionCommand(GuiActionType vAction) +void ApplicationController::onMainTimerTimeout() { - // qDebug() << "ApplicationController.actionCommanded : " << vAction; - // Process the command and notify GUI Controller - // Process ... - emit didActionCommand(vAction); + keepAlive(); } /*! - * \brief An action has been confirmed - * \details GUI requested an action. - * In response HD confirmed the action. - * \param vAction - * \param vInfo + * \brief ApplicationController::onUSBDriveMount + * \details This is the slot which connects to the _USBWatcher didUSBDriveMount signal + * and notifies the other classes (GuiController) by emitting its signal didUSBDriveMount */ -void ApplicationController::onActionConfirm(GuiActionType vAction, GuiActionInfo vInfo) +void ApplicationController::onUSBDriveMount () { - // qDebug() << "ApplicationController.actionConfirmed : " << vAction; - // Process the command and notify GUI Controller - // Process ... - emit didActionConfirm(vAction, vInfo); + emit didUSBDriveMount(); } + +/*! + * \brief ApplicationController::onUSBDriveRemove + * \details This is the slot which connects to the _GuiController didUSBDriveUmount signal + * and notifies the other classes (USBWatcher) by emitting its signal didUSBDriveUmount + */ +void ApplicationController::onUSBDriveUmount() +{ + emit didUSBDriveUmount(); +} + +/*! + * \brief ApplicationController::onUSBDriveRemove + * \details This is the slot which connects to the _USBWatcher didUSBDriveRemove signal + * and notifies the other classes (GuiController) by emitting its signal didUSBDriveRemove + */ +void ApplicationController::onUSBDriveRemove() +{ + emit didUSBDriveRemove(); +} + +/*! + * \brief ApplicationController::onExportLog + * \details the slot which will be called by UI to so the log export. + */ +void ApplicationController::onExportLog() +{ + LOG_EXPORT; +} + +/*! + * \brief ApplicationController::onExport + * \details the slot which will be called by logger is done exporting. + */ +void ApplicationController::onExport() +{ + emit didExport(); +} + +/*! + * \brief ApplicationController::keepAlive + * \details This is the message which has to be send over the CANBUS + * as an monitor for other nodes on the bus to notify UI is alive + */ +void ApplicationController::keepAlive() +{ +#ifndef DISABLE_KEEP_ALIVE + QVariantList mData; + mData += static_cast(GuiActionData::NoData); + onActionTransmit(GuiActionType::KeepAlive, mData); +#endif +}