239 lines
5.5 KiB
C++
239 lines
5.5 KiB
C++
#include "Hd_SocketModule_MES.h"
|
|
#include "qcoreapplication.h"
|
|
#include <qtimer.h>
|
|
|
|
static QQueue<QByteArray> WritePLCPack;
|
|
//static QQueue<PLCPack> ReadPack;
|
|
static QQueue<QByteArray> ReadPLCPack;
|
|
|
|
short* v_uint16 = nullptr;
|
|
unsigned char* v_uint8 = nullptr;
|
|
char* v_char8 = nullptr;
|
|
|
|
QByteArray readResult = "";
|
|
QMutex* r_Mutex = new QMutex();
|
|
QMutex* write_Mutex = new QMutex();
|
|
QMutex* read_Mutex = new QMutex();
|
|
QMutex* recive_Mutex = new QMutex();
|
|
|
|
bool connectStatus = true;
|
|
|
|
static bool ifrun = false;
|
|
|
|
Hd_SocketModule_MES* create()
|
|
{
|
|
qDebug() << "[INFO] " << "Hd_SocketModule_MES create success!";
|
|
return new Hd_SocketModule_MES;
|
|
}
|
|
|
|
void destory(Hd_SocketModule_MES* ptr)
|
|
{
|
|
if (ptr)
|
|
{
|
|
delete ptr;
|
|
ptr = nullptr;
|
|
}
|
|
qDebug() << "[INFO] " << " Hd_SocketModule_MES destory success!";
|
|
}
|
|
|
|
Hd_SocketModule_MES::Hd_SocketModule_MES()
|
|
{
|
|
m_Mutex = new QMutex;
|
|
m_Thread = new MyThread(this);
|
|
}
|
|
|
|
Hd_SocketModule_MES::~Hd_SocketModule_MES()
|
|
{
|
|
ifrun = false;
|
|
m_Thread->wait();
|
|
m_Thread->quit();
|
|
if (m_Thread)
|
|
{
|
|
qDebug() << "[INFO] "<< "~m_Thread()";
|
|
delete m_Thread;
|
|
m_Thread = nullptr;
|
|
}
|
|
|
|
qDebug() << "[INFO] " << "~Hd_SocketModule_MES success!";
|
|
}
|
|
//从类中读数据到实例对象
|
|
bool Hd_SocketModule_MES::readData(std::vector<cv::Mat>& vec_mat, QByteArray& data)
|
|
{
|
|
Q_UNUSED(vec_mat);
|
|
|
|
/*if (ReadPack.isEmpty())
|
|
return true;*/
|
|
|
|
// 发送数据包
|
|
/*QMutexLocker locker(m_Mutex);
|
|
QByteArray m_byte = ReadPack.dequeue();*/
|
|
QByteArray m_byte = "";
|
|
|
|
int times = 0;
|
|
while (m_byte == "")
|
|
{
|
|
times++;
|
|
if (times > 2000)
|
|
{
|
|
qDebug() << "[Hd_SocketModule_MES] " << "Hd_SocketModule_MES readCmd fail";
|
|
return false;
|
|
}
|
|
read_Mutex->lock();
|
|
m_byte = readResult;
|
|
if (m_byte != "")
|
|
readResult = "";
|
|
read_Mutex->unlock();
|
|
Sleep(5);
|
|
}
|
|
|
|
/*m_byte.remove(0, 30);*/
|
|
data = m_byte;
|
|
//qDebug() << __FUNCTION__ << "line:" << __LINE__ << "result:" << data.size();
|
|
return true;
|
|
}
|
|
//实例对象把数据写入到类
|
|
bool Hd_SocketModule_MES::writeData(std::vector<cv::Mat>& vec_mat, QByteArray& data)
|
|
{
|
|
Q_UNUSED(vec_mat);
|
|
write_Mutex->lock();
|
|
//QString str = QString(data);
|
|
if (data.isEmpty())
|
|
{
|
|
qCritical() << "[Hd_SocketModule_MES] " << " str is null";
|
|
write_Mutex->unlock();
|
|
return false;
|
|
}
|
|
|
|
//QList<QByteArray> m_list = data.split("MESURL_");
|
|
|
|
//QString m_value;
|
|
//QString m_ip_lastpos;
|
|
if (data.contains("http:"))
|
|
{
|
|
//"http://ot-dbvwmeqapi.mflex.com.cn:8081/api/Equipments/saveresultpanel",
|
|
//QString commond = m_list.at(1);
|
|
//myUrl = QUrl(commond);
|
|
m_Thread->setUrl(data);
|
|
}
|
|
else
|
|
{
|
|
WritePLCPack.enqueue(data);
|
|
}
|
|
write_Mutex->unlock();
|
|
return true;
|
|
}
|
|
//初始化
|
|
bool Hd_SocketModule_MES::initParas(QByteArray& data)
|
|
{
|
|
qDebug() << "[Hd_SocketModule_MES] " << "DATA: " << data;
|
|
m_Thread->start();
|
|
return true;
|
|
}
|
|
|
|
bool Hd_SocketModule_MES::run()
|
|
{
|
|
//qDebug() << "[info] " << __FUNCTION__ << " line:" << __LINE__ << " success!";
|
|
if (connectStatus == false)
|
|
connectStatus = true;
|
|
return connectStatus;
|
|
}
|
|
//检查异常标志
|
|
bool Hd_SocketModule_MES::checkStatus()
|
|
{
|
|
read_Mutex->lock();
|
|
bool connectStatus1 = connectStatus;
|
|
read_Mutex->unlock();
|
|
return connectStatus1;
|
|
}
|
|
|
|
MyThread::~MyThread()
|
|
{
|
|
//if (tcpsocket) {
|
|
// tcpsocket->abort();
|
|
//} // tcp通信的socket
|
|
//if (m_mutex_lock) {
|
|
// delete m_mutex_lock;
|
|
// m_mutex_lock = nullptr;
|
|
//}
|
|
if (net_mgr) {
|
|
delete net_mgr;
|
|
net_mgr = nullptr;
|
|
}
|
|
ifrun = false;
|
|
}
|
|
|
|
void MyThread::setUrl(QString qurl)
|
|
{
|
|
net_request.setUrl(QUrl(qurl));
|
|
//要发送json格式的数据必须要在header里设置,不然不会成功的
|
|
net_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
|
|
|
|
////百度测试地址
|
|
//net_request.setHeader(QNetworkRequest::UserAgentHeader,
|
|
// "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.38");
|
|
//net_request.setUrl(QUrl("http://www.baidu.com:80"));
|
|
}
|
|
|
|
|
|
|
|
//线程 run 中初始化 socket
|
|
void MyThread::run()
|
|
{
|
|
qDebug() << "[Hd_SocketModule_MES] " << " run start";
|
|
ifrun = true;
|
|
net_mgr = new QNetworkAccessManager(this);
|
|
net_mgr->setNetworkAccessible(QNetworkAccessManager::Accessible); //设置状态,必须要
|
|
connect(net_mgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
|
|
|
|
static bool ifmytimerStart = false;
|
|
mytimer = new QTimer();
|
|
connect(mytimer, &QTimer::timeout, [=]() {
|
|
connectStatus = false;
|
|
ifmytimerStart = false;
|
|
});
|
|
|
|
while (ifrun)
|
|
{
|
|
write_Mutex->lock();
|
|
if (WritePLCPack.size() > 0)
|
|
{
|
|
for (int i = 0; i < WritePLCPack.size(); i++)
|
|
{
|
|
QByteArray Writedata = WritePLCPack.dequeue();//json数据
|
|
//net_mgr->post(net_request, "");
|
|
net_mgr->post(net_request, Writedata);
|
|
if (ifmytimerStart == false)
|
|
{
|
|
mytimer->stop();
|
|
mytimer->start(30000);
|
|
ifmytimerStart = true;
|
|
}
|
|
}
|
|
}
|
|
qApp->processEvents();
|
|
write_Mutex->unlock();
|
|
}
|
|
}
|
|
|
|
//mes收到回复信号
|
|
void MyThread::finishedSlot(QNetworkReply* reply)
|
|
{
|
|
if (reply->error() == QNetworkReply::NoError)
|
|
{
|
|
connectStatus = true;
|
|
QByteArray bytes = reply->readAll();
|
|
//qDebug() << "----------------------------" + bytes;
|
|
|
|
// 下边我们将收到的body部分解析为json格式,其中有个key是status
|
|
/*QJsonDocument jsonDocument = QJsonDocument::fromJson(bytes);
|
|
QJsonObject jsonObject = jsonDocument.object();
|
|
QString value = jsonObject.value("data").toString();*/
|
|
//qDebug() << "----------------------------" + value;
|
|
|
|
read_Mutex->lock();
|
|
readResult = bytes;
|
|
read_Mutex->unlock();
|
|
}
|
|
reply->deleteLater();
|
|
}
|