You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
2.9 KiB
C++
122 lines
2.9 KiB
C++
#include "udpdatahandler.h"
|
|
#include <QHostAddress>
|
|
#include "common.h"
|
|
#include "EasySpdLog.h"
|
|
|
|
#include <QDebug>
|
|
|
|
UdpDataHandler::UdpDataHandler(QObject *parent)
|
|
: QObject(parent)
|
|
, m_workerThread(new QThread())
|
|
, m_udpWorker(nullptr)
|
|
, m_checkConnectTimer(new QTimer(this))
|
|
, m_connectStatus(false)
|
|
, m_isSending(false)
|
|
{
|
|
auto ipAddr = Common::GetInstance()->getIPAddr();
|
|
auto recvPort = Common::GetInstance()->getSrcPort();
|
|
m_hostAddr.setAddress(ipAddr);
|
|
m_udpWorker = new UdpSocketWorker(m_hostAddr, recvPort);
|
|
m_udpWorker->moveToThread(m_workerThread);
|
|
m_workerThread->start();
|
|
connect(m_udpWorker, &UdpSocketWorker::dataReceived, this, &UdpDataHandler::recvData);
|
|
connect(m_checkConnectTimer, &QTimer::timeout, this, &UdpDataHandler::checkConnectSlot);
|
|
m_checkConnectTimer->start(1000);
|
|
|
|
}
|
|
|
|
UdpDataHandler::~UdpDataHandler()
|
|
{
|
|
|
|
}
|
|
|
|
void UdpDataHandler::sendSnId(const QString &data)
|
|
{
|
|
QString snMsg = QString("5901%1%2").arg(data.length()).arg(data);
|
|
m_udpWorker->startSending("send_sn", snMsg.toLocal8Bit(), m_hostAddr, Common::GetInstance()->getDstPort());
|
|
}
|
|
|
|
void UdpDataHandler::sendStop()
|
|
{
|
|
m_isSending = false;
|
|
}
|
|
|
|
void UdpDataHandler::sendClose()
|
|
{
|
|
QString msg = QString("%1%2").arg(CONSTHEAD).arg("0301");
|
|
m_udpWorker->startSending("iecs_close", msg.toLocal8Bit(), m_hostAddr, Common::GetInstance()->getDstPort());
|
|
}
|
|
|
|
void UdpDataHandler::stopClose()
|
|
{
|
|
m_udpWorker->stopSending("iecs_close");
|
|
}
|
|
|
|
void UdpDataHandler::recvData(const QByteArray &data)
|
|
{
|
|
if (data.size() == 0)
|
|
return;
|
|
QString dataStr(data);
|
|
if (dataStr.mid(0, 2).toInt() != CONSTHEAD)
|
|
return;
|
|
int msgType = dataStr.mid(2, 2).toInt();
|
|
switch(msgType)
|
|
{
|
|
case SIGNAL_BEATSRC:
|
|
{
|
|
m_connectStatus = true;
|
|
int type = dataStr.mid(6, 1).toInt();
|
|
Common::GetInstance()->setCurrentWorkMode(type);
|
|
emit GetConnStatusSig(true);
|
|
break;
|
|
}
|
|
case SIGNAL_SN:
|
|
{
|
|
int status = dataStr.mid(4, 3).toInt();
|
|
if (status == 11)
|
|
{
|
|
m_udpWorker->stopSending("send_sn");
|
|
}
|
|
else
|
|
{
|
|
LWarn("Unknown SIGNAL_SN:{}", data);
|
|
}
|
|
break;
|
|
}
|
|
case SIGNAL_DONE:
|
|
{
|
|
int status = dataStr.mid(4, 2).toInt();
|
|
bool ret = dataStr.mid(6, 1).toInt();
|
|
if (status == 1)
|
|
{
|
|
emit GetDoneSig(ret);
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
LWarn("Unknow message:{}", data);
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
void UdpDataHandler::checkConnectSlot()
|
|
{
|
|
static int times = 0;
|
|
if (!m_connectStatus)
|
|
{
|
|
times++;
|
|
}
|
|
else
|
|
{
|
|
times = 0;
|
|
}
|
|
if (times >= 3)
|
|
{
|
|
Common::GetInstance()->setCurrentWorkMode(-1);
|
|
emit GetConnStatusSig(false);
|
|
times = 0;
|
|
}
|
|
m_connectStatus = false;
|
|
}
|