/*! * * Copyright (c) 2019-2020 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 * WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. \n * * \file applicationcontroller.cpp * \date 2019/09/30 * \author Behrouz NematiPour * */ #include "applicationcontroller.h" // Qt // Project #include "maintimer.h" #include "messagedispatcher.h" #include "logger.h" #include "usbwatcher.h" #include "filehandler.h" /*! * \brief ApplicationController::ApplicationController * \details Constructor * \param parent - QObject parent owner object. * Qt handles the children destruction by their parent objects life-cycle. */ ApplicationController::ApplicationController(QObject *parent) : QObject(parent) { _applicationPost = new ApplicationPost(this); } /*! * \brief ApplicationController initializer */ bool ApplicationController::init() { if ( _init ) return false; _init = true; initConnections(); // coco begin validated: The class ApplicationPost has not been implemented Yet. if (!_applicationPost->init()) return false; // coco end LOG_EVENT(QObject::tr("%1 Initialized").arg(metaObject()->className())); return true; } /*! * \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 */ 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() { // coco begin validated: Application termination is not correctly done in coco!!! // it has been tested and works perfectly fine in normal run. quitThread(); // validated } // coco end /*! * \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(didActionTransmit(GuiActionType, const QVariantList &)), this , SLOT( onActionTransmit(GuiActionType, const QVariantList &))); // From HD/DG connect(&_MessageDispatcher, SIGNAL(didActionReceive(GuiActionType, const QVariantList &)), this , SLOT( onActionReceive(GuiActionType, const QVariantList &))); connect(&_MessageDispatcher, SIGNAL(didFailedTransmit(Sequence)), this , SLOT(onFailedTransmit(Sequence))); 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())); connect(&_GuiController , SIGNAL(didAdjustBloodDialysateFlows(quint32, quint32)), this , SLOT( onAdjustBloodDialysateFlows(quint32, quint32))); connect(&_GuiController , SIGNAL(didAdjustDuration(quint32)), this , SLOT( onAdjustDuration(quint32))); ACTION_RECEIVE_MODEL_BRIDGE_CONNECTIONS(_MessageDispatcher) } /*! * \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() { // coco begin validated: Application termination is not correctly done in coco!!! // it has been tested and works perfectly fine in normal run. if ( ! _thread ) return; // runs in thread moveToThread(qApp->thread()); // validated } // coco end /*! * \brief ApplicationController::onFailedTransmit * Called when we failed to get a response back from the HD * \param seq - The sequence that failed to send to the HD. */ void ApplicationController::onFailedTransmit(Sequence seq) { emit didFailedTransmit(seq); } /*! * \brief Process the requested action * \details Processes the requested action * \param vAction - User requested Action * \param vData - Action data to be transmitted. */ void ApplicationController::onActionTransmit(GuiActionType vAction, const QVariantList &vData) { emit didActionTransmit(vAction, vData); } /*! * \brief An action has been confirmed * \details GUI requested an action. * In response HD confirmed the action. * \param vAction * \param vData */ void ApplicationController::onActionReceive (GuiActionType vAction, const QVariantList &vData) { emit didActionReceive (vAction, vData); } /*! * \brief ApplicationController::onMainTimerTimeout * \details This slot is called by MainTimer::didTimeout each second * to call required methods like keepAlive */ void ApplicationController::onMainTimerTimeout() { keepAlive(); } /*! * \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::onUSBDriveMount () { // coco begin validated: This needs user interaction to plug-in USB device // has been tested manually emit didUSBDriveMount(); } // coco end /*! * \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() { // coco begin validated: This needs user interaction to plug-in USB device // has been tested manually emit didUSBDriveUmount(); } // coco end /*! * \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() { // coco begin validated: This needs user interaction to plug-in USB device // has been tested manually emit didUSBDriveRemove(); } // coco end /*! * \brief ApplicationController::onExportLog * \details the slot which will be called by UI to so the log export. */ void ApplicationController::onExportLog() { // coco begin validated: This needs user interaction to plug-in USB device // has been tested manually LOG_EXPORT; } // coco end /*! * \brief ApplicationController::onExport * \details the slot which will be called by logger is done exporting. */ void ApplicationController::onExport() { // coco begin validated: This needs user interaction to plug-in USB device // has been tested manually emit didExport(); } // coco end /*! * \brief ApplicationController::onAdjustBloodDialysateFlows * \details This method transmits the Blood/Dialysate Adjustment Denali message. * \param vBloodFlow - Blood Flow Rate * \param vDialysateFlow - Dialysate Flow Rate * \return void */ void ApplicationController::onAdjustBloodDialysateFlows(quint32 vBloodFlow, quint32 vDialysateFlow) { QVariantList mData; mData += vBloodFlow; mData += vDialysateFlow; onActionTransmit(GuiActionType::AdjustBloodDialysateReq, mData); } /*! * \brief ApplicationController::onAdjustDuration * \details This method transmits the Blood/Dialysate Adjustment Denali message. * \param vDuration - Treatment duration adjustment value in minuts * \return void */ void ApplicationController::onAdjustDuration(quint32 vDuration) { QVariantList mData; mData += vDuration; onActionTransmit(GuiActionType::AdjustDurationReq, mData); } /*! * \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; int mFakeDataLen = gFakeData.length(); // coco begin validated: This is a fake data generator for CANBus missing/swapped frames Testing // will never be executed on the product // has been tested manually if (mFakeDataLen) { if (gFakeSeqAtBegin) { createFakeSeqAtBeginLongMessage(mData, mFakeDataLen); } else { createFakeSequencedLongMessage (mData, mFakeDataLen); } } // coco end else { mData += static_cast(GuiActionData::NoData); } onActionTransmit(GuiActionType::KeepAlive, mData); #endif } /*! * \brief ApplicationController::createFakeSequencedLongMessage * \details This method is creating the fake message with frame sequence * which we use for Denali Message test * \param vFakeDataLen */ void ApplicationController::createFakeSequencedLongMessage(QVariantList &vData, const int vFakeDataLen) { // coco begin validated: This is a fake data generator for CANBus missing/swapped frames Testing // will never be executed on the product // has been tested manually QByteArray data; if (vFakeDataLen == 1 && gFakeData == QByteArray::fromHex(gFakeData_default)) { static quint16 txCount = 0; Types::U16 seq; quint8 dataBytesLeft = 0; const quint8 crcBytesLen = 2; for (int i = 0; i < 13; i++) { switch (i) { case 0: // First frame : HEAD seq.value = txCount; data += seq.bytes[0]; data += seq.bytes[1]; break; case 12: // Last frame with CRC dataBytesLeft = 8 - sizeof(seq) - crcBytesLen; for (int j = 0; j < dataBytesLeft; j++) { data += (char)(0); } seq.value = txCount; data += seq.bytes[0]; data += seq.bytes[1]; break; default: // Middle Frames dataBytesLeft = 8 - sizeof(seq); for (int j = 0; j < dataBytesLeft; j++) { data += (char)(0); } seq.value = txCount; data += seq.bytes[0]; data += seq.bytes[1]; break; } Types::safeIncrement(txCount); } vData += QByteArray::fromHex(data.toHex()); } else { vData += gFakeData; } } // coco end /*! * \brief ApplicationController::createFakeSequencedAtBeginLongMessage * \details This method is creating the fake message with frame sequence * which we use for Denali Message test * \param vFakeDataLen */ void ApplicationController::createFakeSeqAtBeginLongMessage(QVariantList &vData, const int vFakeDataLen) { // coco begin validated: This is a fake data generator for CANBus missing/swapped frames Testing // will never be executed on the product // has been tested manually QByteArray data; if (vFakeDataLen == 1 && gFakeData == QByteArray::fromHex(gFakeData_default)) { static quint32 txCount = 0; Types::U32 seq; quint8 dataBytesLeft = 0; const quint8 crcBytesLen = 2; for (int i = 0; i < 13; i++) { switch (i) { case 0: // First frame : HEAD seq.value = txCount; data += seq.bytes[0]; data += seq.bytes[1]; //data += seq.bytes[3]; // Chopped off //data += seq.bytes[4]; // Chopped off break; case 12: // Last frame with CRC seq.value = txCount; data += seq.bytes[0]; data += seq.bytes[1]; data += seq.bytes[2]; data += seq.bytes[3]; dataBytesLeft = 8 - sizeof(seq) - crcBytesLen; for (int j = 0; j < dataBytesLeft; j++) { data += (char)(0); } break; default: // Middle Frames dataBytesLeft = 8 - sizeof(seq); seq.value = txCount; data += seq.bytes[0]; data += seq.bytes[1]; data += seq.bytes[2]; data += seq.bytes[3]; for (int j = 0; j < dataBytesLeft; j++) { data += (char)(0); } break; } Types::safeIncrement(txCount); } vData += QByteArray::fromHex(data.toHex()); } else { vData += gFakeData; } } // coco end