You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

129 lines
5.1 KiB

#ifndef USMODULE_H
#define USMODULE_H
#include <QObject>
#include "type/CommandRequest.h"
#include "type/CommandResponse.h"
#include "UsEventReceiver.h"
#include "UsEventSender.h"
#include "logger/Logger.h"
#include "UsHelper.h"
#include "UsPacket.h"
#include "utils/Utils.h"
//forward decleration
class ApplicationStarter;
//macro defenition
//Place this in each and every class that inherits from UsModule
//so that you dont have to type all this again
#define US_MODULE \
protected: \
virtual void commandHandler(const CommandRequest& request) override; \
virtual void packetHandler(const UsPacket& packet) override; \
virtual void init() override; \
virtual void start() override; \
virtual void afterThreadStart() override; \
private: \
template<class T> \
void connectCommandPath() \
{ \
connect(T::getInstance()->getEventReceiver(), \
&UsEventReceiver::commandResponse, \
_eventSender, \
&UsEventSender::newCommandResponse, Qt::QueuedConnection); \
connect(_eventSender, \
&UsEventSender::commandRequest, \
T::getInstance()->getEventReceiver(), \
&UsEventReceiver::newCommandRequest, Qt::QueuedConnection); \
} \
template<class T> \
void connectDataPath() \
{ \
connect(this, SIGNAL(packetReady(const UsPacket&)), \
T::getInstance(), SLOT(newPacket(const UsPacket&))); \
} \
/*************************************************************************************************/
/**
* @brief The UsModule class
* @details This class is the bone of our ultrasound framework(if we can call it), it includes all
* the basic functionalities that we expect from a UsModule and provides the developers tools
* and helpers to ease the development of UsModules,
* @note UsModule in its finest defenition, is just a wrapper around some hidden internal
* functionalities, this wrapper provides means of communication(data and command), utilities
* such as logger and helpers. Each UsModule runs in its own thread, has some lifecycle hooks and
* is a singleton
* Some of the features, for example having a special folder layout, was only forced
* to ease further development and readability and was not part of first design goals
* @author Hessamoddin Hediyehloo(H-4nd-H)
* @date 2019/12/15(1398/9/24)
*/
/*************************************************************************************************/
class UsModule : public QObject
{
Q_OBJECT
US_EVENT_SENDER(protected)
US_EVENT_RECEIVER(protected)
private:
void initialize();
protected:
virtual void afterThreadStart() = 0; //hook after object is moved to destination thread
virtual void init() = 0; //hook after all USMs created and moved to their threads
virtual void start() = 0; //hook after all USMs gone under init
virtual void commandHandler(const CommandRequest& request) = 0;
virtual void packetHandler(const UsPacket& packet) = 0;
void log(ESeverityLevel::eSeverityLevel severity,
ELogID::eLogID logID, QString message);
void fail(const CommandRequest& request);
void fail(const CommandRequest& request, QList<QVariant> args);
void pass(const CommandRequest& request);
void pass(const CommandRequest& request, QList<QVariant> args);
void sendAsyncCommand(ECommandType::eCommandType commandType, QString receiver);
void sendAsyncCommand(ECommandType::eCommandType commandType,
QString receiver, QList<QVariant> args);
void sendAsyncCommand(ECommandType::eCommandType commandType, QString receiver,
std::function<void (QList<QVariant>)> successCallback,
std::function<void (QList<QVariant>)> failureCallback);
void sendAsyncCommand(ECommandType::eCommandType commandType, QString receiver,
QList<QVariant> args,
std::function<void (QList<QVariant>)> successCallback,
std::function<void (QList<QVariant>)> failureCallback);
CommandResult sendSyncCommand(ECommandType::eCommandType commandType, QString receiver);
CommandResult sendSyncCommand(ECommandType::eCommandType commandType,
QString receiver, QList<QVariant> args);
void sendData(const UsPacket& data);
public:
explicit UsModule(QObject*);
virtual ~UsModule()
{
}
signals:
//To notify the thread to finish
void finished();
//Will be called when object is moved to destination thread
void ThreadStartUpRoutineFinished();
//Called to data passing
void packetReady(const UsPacket& packet);
//uncrustify off
public slots:
void threadStarted();
void newCommandRequest(const CommandRequest& request);
void newPacket(const UsPacket& packet);
//uncrustify on
};
#endif //USMODULE_H