58 lines
1.4 KiB
C++
58 lines
1.4 KiB
C++
#include "DmsAsyncAction.h"
|
|
#include "dms_mq.h"
|
|
|
|
#include <QTimer>
|
|
|
|
namespace
|
|
{
|
|
const int TIMEOUT_MSEC = 500;
|
|
}
|
|
|
|
DmsAsyncAction::DmsAsyncAction(int aServerId, int aActionId, QObject* aObject, const QString& aResponseSignal, QObject* aParent)
|
|
: QObject(aParent)
|
|
, mServerId(aServerId)
|
|
, mActionId(aActionId)
|
|
, mTimer(new QTimer(this))
|
|
, mObject(aObject)
|
|
, mResponseSignal(aResponseSignal)
|
|
, mSendData()
|
|
{
|
|
mTimer->setSingleShot(true);
|
|
mTimer->setInterval(TIMEOUT_MSEC);
|
|
connect(mTimer, &QTimer::timeout, this, &DmsAsyncAction::sendTimeoutSignal);
|
|
connect(mObject, ("2" + mResponseSignal).toStdString().c_str(), mTimer, SLOT(stop()));
|
|
}
|
|
|
|
DmsAsyncAction::~DmsAsyncAction()
|
|
{
|
|
disconnect(mTimer, &QTimer::timeout, this, &DmsAsyncAction::sendTimeoutSignal);
|
|
disconnect(mObject, ("2" + mResponseSignal).toStdString().c_str(), mTimer, SLOT(stop()));
|
|
}
|
|
|
|
bool DmsAsyncAction::execute()
|
|
{
|
|
QByteArray byteArray = mSendData.toUtf8();
|
|
uint8_t* data = reinterpret_cast<uint8_t*>(byteArray.data());
|
|
if(dmsmq_send(mServerId, mActionId, data, byteArray.size()) < 0)
|
|
{
|
|
return false;
|
|
}
|
|
mTimer->start();
|
|
return true;
|
|
}
|
|
|
|
void DmsAsyncAction::sendTimeoutSignal()
|
|
{
|
|
emit timeout();
|
|
}
|
|
|
|
void DmsAsyncAction::setTimeoutInterval(int aMsec)
|
|
{
|
|
mTimer->setInterval(aMsec);
|
|
}
|
|
|
|
void DmsAsyncAction::responsed()
|
|
{
|
|
mTimer->stop();
|
|
}
|