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.

124 lines
3.0 KiB
C++

3 months ago
#include "udpdatathread.h"
#include <QHostAddress>
#include "common.h"
#include "EasySpdLog.h"
3 months ago
#include <QDebug>
UdpDataThread::UdpDataThread(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()->getHostAddress();
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, &UdpDataThread::recvData);
connect(m_checkConnectTimer, &QTimer::timeout, this, &UdpDataThread::checkConnectSlot);
m_checkConnectTimer->start(1000);
}
UdpDataThread::~UdpDataThread()
{
}
void UdpDataThread::sendSnId(const QString &data)
{
QString snMsg = QString("5901%1%2").arg(data.length()).arg(data);
//m_udpWorker->sendData(snMsg.toLocal8Bit(), m_hostAddr, Common::GetInstance()->getDstPort());
m_udpWorker->startSending("send_sn", snMsg.toLocal8Bit(), m_hostAddr, Common::GetInstance()->getDstPort());
3 months ago
}
void UdpDataThread::sendStop()
{
m_isSending = false;
}
void UdpDataThread::sendClose()
{
QString msg = QString("%1%2").arg(CONSTHEAD).arg("0301");
m_udpWorker->startSending("iecs_close", msg.toLocal8Bit(), m_hostAddr, Common::GetInstance()->getDstPort());
}
void UdpDataThread::stopClose()
{
m_udpWorker->stopSending("iecs_close");
}
void UdpDataThread::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();
3 months ago
Common::GetInstance()->setCurrentWorkMode(type);
emit connectStatus(true);
break;
}
case SIGNAL_SN:
{
int status = dataStr.mid(4, 3).toInt();
if (status == 11)
{
m_udpWorker->stopSending("send_sn");
}
else
{
qDebug()<<"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 registerRet(ret);
}
break;
}
3 months ago
default:
qDebug()<<"data:"<<data;
3 months ago
break;
}
}
void UdpDataThread::checkConnectSlot()
{
static int times = 0;
if (!m_connectStatus)
{
times++;
}
else
{
times = 0;
}
if (times >= 3)
{
Common::GetInstance()->setCurrentWorkMode(-1);
3 months ago
emit connectStatus(false);
qDebug()<<"disconnect";
times = 0;
}
m_connectStatus = false;
}