2024-09-27 14:31:22 +08:00
|
|
|
|
#include "Logic_transfer.h"
|
|
|
|
|
|
#include "qmutex.h"
|
|
|
|
|
|
#include "qfuture.h"
|
|
|
|
|
|
#include "QtConcurrent/qtconcurrent"
|
|
|
|
|
|
#include "qmessagebox.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QMutex log_mutex;
|
|
|
|
|
|
void LogMsgOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg)
|
|
|
|
|
|
{
|
|
|
|
|
|
Q_UNUSED(context);
|
|
|
|
|
|
|
|
|
|
|
|
QMutexLocker locker(&log_mutex);
|
|
|
|
|
|
QByteArray localMsg = msg.toLocal8Bit();
|
|
|
|
|
|
QString log;
|
|
|
|
|
|
QString log_info;
|
|
|
|
|
|
QString nowTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
|
|
|
|
|
|
|
|
|
|
|
|
QString m_msg;
|
|
|
|
|
|
QString m_name;
|
|
|
|
|
|
QStringList msg_list;
|
|
|
|
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
|
|
{
|
|
|
|
|
|
case QtDebugMsg:
|
|
|
|
|
|
log.append(QString("[%1] [info] %2").arg(nowTime).arg(msg));
|
|
|
|
|
|
break;
|
|
|
|
|
|
case QtInfoMsg:
|
|
|
|
|
|
m_msg = msg;
|
|
|
|
|
|
msg_list = m_msg.split("/");
|
|
|
|
|
|
if (msg_list.size() >= 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_name = msg_list.at(1);
|
|
|
|
|
|
m_msg.remove(0, m_name.size() + 2);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
m_name = "null";
|
|
|
|
|
|
}
|
|
|
|
|
|
log_info.append(QString("[%1] [info] %2").arg(nowTime).arg(m_msg));
|
|
|
|
|
|
break;
|
|
|
|
|
|
case QtWarningMsg:
|
|
|
|
|
|
log.append(QString("[%1] [warning] %2").arg(nowTime).arg(msg));
|
|
|
|
|
|
break;
|
|
|
|
|
|
case QtCriticalMsg:
|
|
|
|
|
|
log.append(QString("[%1] [critical] %2").arg(nowTime).arg(msg));
|
|
|
|
|
|
break;
|
|
|
|
|
|
case QtFatalMsg:
|
|
|
|
|
|
log.append(QString("[%1] [fatal] %2").arg(nowTime).arg(msg));
|
|
|
|
|
|
abort();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QFile savefile;
|
|
|
|
|
|
QString logTime = QDateTime::currentDateTime().toString("yyyy-MM-dd");
|
|
|
|
|
|
|
|
|
|
|
|
if (log != nullptr)
|
|
|
|
|
|
{
|
|
|
|
|
|
QDir m_dir = QApplication::applicationDirPath();
|
|
|
|
|
|
QString m_path = m_dir.path();
|
|
|
|
|
|
QDir m_checkdir = m_path + "/log/";
|
|
|
|
|
|
if (!m_checkdir.exists())
|
|
|
|
|
|
{
|
|
|
|
|
|
m_checkdir.mkdir(m_checkdir.path());
|
|
|
|
|
|
}
|
|
|
|
|
|
m_path = m_path + "/log/" + QString("%1_HanddleAI.log").arg(logTime);
|
|
|
|
|
|
// m_path = m_path + "/log/" + QString("HanddleAI.log");
|
|
|
|
|
|
savefile.setFileName(m_path);
|
|
|
|
|
|
if (!savefile.open(QIODevice::ReadWrite | QIODevice::Append))
|
|
|
|
|
|
{
|
|
|
|
|
|
QString erinfo = savefile.errorString();
|
|
|
|
|
|
std::cout << " erinfo: " << erinfo.toStdString() << std::endl;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
QTextStream out(&savefile);
|
|
|
|
|
|
out << "\r" << log;
|
|
|
|
|
|
savefile.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
if (log_info != nullptr)
|
|
|
|
|
|
{
|
|
|
|
|
|
QDir m_dir = QApplication::applicationDirPath();
|
|
|
|
|
|
QString m_path = m_dir.path();
|
|
|
|
|
|
QDir m_checkdir = m_path + "/log/";
|
|
|
|
|
|
if (!m_checkdir.exists())
|
|
|
|
|
|
{
|
|
|
|
|
|
m_checkdir.mkdir(m_checkdir.path());
|
|
|
|
|
|
}
|
|
|
|
|
|
m_path = m_path + "/log/" + QString("%1_%2.log").arg(logTime).arg(m_name);
|
|
|
|
|
|
// m_path = m_path + "/log/" + QString("HanddleAI.log");
|
|
|
|
|
|
savefile.setFileName(m_path);
|
|
|
|
|
|
if (!savefile.open(QIODevice::ReadWrite | QIODevice::Append))
|
|
|
|
|
|
{
|
|
|
|
|
|
QString erinfo = savefile.errorString();
|
|
|
|
|
|
std::cout << " erinfo: " << erinfo.toStdString() << std::endl;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
QTextStream out(&savefile);
|
|
|
|
|
|
out << "\r" << log_info;
|
|
|
|
|
|
savefile.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
int k = 0;
|
|
|
|
|
|
// log_info.append(log);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QJsonObject Logic_transfer::load_camera_cfg()
|
|
|
|
|
|
{
|
|
|
|
|
|
QDir dir = QApplication::applicationDirPath();
|
|
|
|
|
|
currPath = dir.path();
|
|
|
|
|
|
json_cfg_file_path = currPath + "/config/camera_cfg.json";
|
|
|
|
|
|
|
|
|
|
|
|
QJsonObject json_object;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
QJsonParseError jsonError;
|
|
|
|
|
|
if (json_cfg_file_path.isEmpty())
|
|
|
|
|
|
{
|
|
|
|
|
|
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " JsonPath is null!";
|
|
|
|
|
|
return json_object;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QFile JsonFile;
|
|
|
|
|
|
JsonFile.setFileName(json_cfg_file_path);
|
|
|
|
|
|
if (!JsonFile.isReadable())
|
|
|
|
|
|
{
|
|
|
|
|
|
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " camera_cfg.json not isReadable!";
|
|
|
|
|
|
//return json_object;
|
|
|
|
|
|
}
|
|
|
|
|
|
JsonFile.open(QIODevice::ReadOnly);
|
|
|
|
|
|
|
|
|
|
|
|
QByteArray m_Byte = JsonFile.readAll();
|
|
|
|
|
|
if (m_Byte.isEmpty())
|
|
|
|
|
|
{
|
|
|
|
|
|
qDebug() << __FUNCTION__ << " line:" << __LINE__ << json_cfg_file_path + " Content is empty";
|
|
|
|
|
|
JsonFile.close();
|
|
|
|
|
|
return json_object;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QJsonDocument jsonDocument(QJsonDocument::fromJson(m_Byte, &jsonError));
|
|
|
|
|
|
|
|
|
|
|
|
if (!jsonDocument.isNull() && jsonError.error == QJsonParseError::NoError)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (jsonDocument.isObject())
|
|
|
|
|
|
{
|
|
|
|
|
|
json_object = jsonDocument.object();
|
|
|
|
|
|
JsonFile.close();
|
|
|
|
|
|
return json_object;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
qCritical() << __FUNCTION__ << " line:" << __LINE__ << json_cfg_file_path + " is error!";
|
|
|
|
|
|
}
|
|
|
|
|
|
JsonFile.close();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (QString ev)
|
|
|
|
|
|
{
|
|
|
|
|
|
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " ev:" << ev;
|
|
|
|
|
|
}
|
|
|
|
|
|
return json_object;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ifRuntransfer = false;
|
|
|
|
|
|
Logic_transfer::Logic_transfer()
|
|
|
|
|
|
{
|
|
|
|
|
|
showMutex = new QMutex;
|
|
|
|
|
|
ifRuntransfer = true;
|
2024-09-30 15:36:39 +08:00
|
|
|
|
//ͨ�����ӿ�
|
|
|
|
|
|
//������dll
|
2024-09-27 14:31:22 +08:00
|
|
|
|
QLibrary* m_lib = new QLibrary("Hd_LogicDll_module.dll");
|
|
|
|
|
|
bool flag = m_lib->load();
|
|
|
|
|
|
if (!m_lib->isLoaded())
|
|
|
|
|
|
{
|
|
|
|
|
|
qDebug() << __FUNCTION__ << __LINE__ << " Loaded error:" << m_lib->errorString();
|
|
|
|
|
|
delete m_lib;
|
|
|
|
|
|
m_lib = nullptr;
|
|
|
|
|
|
//return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
CreateFunction createFun = (CreateFunction)m_lib->resolve("create");
|
|
|
|
|
|
ReleaseFunction releaseFun = (ReleaseFunction)m_lib->resolve("destory");
|
|
|
|
|
|
if (createFun && releaseFun)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logic_ptr = nullptr;
|
|
|
|
|
|
Logic_ptr = createFun();
|
|
|
|
|
|
destory_Logic = releaseFun;
|
|
|
|
|
|
|
2024-09-30 15:36:39 +08:00
|
|
|
|
//���ز���������ļ���bin/camera_cfg.json
|
2024-09-27 14:31:22 +08:00
|
|
|
|
//m_jsonObj =load_camera_cfg();
|
|
|
|
|
|
//check_cmeraCfg();
|
|
|
|
|
|
//Logic_ptr->setJsonObject(m_jsonObj);
|
2024-09-30 15:36:39 +08:00
|
|
|
|
//����
|
2024-09-27 14:31:22 +08:00
|
|
|
|
if(Logic_ptr) {
|
2024-09-30 15:36:39 +08:00
|
|
|
|
//connect(Logic_ptr, SIGNAL(Logic_sendImage(QString, cv::Mat)), this, SLOT(base_Logic_sendImage(QString, cv::Mat))); // ����ͼƬ
|
|
|
|
|
|
connect(Logic_ptr, SIGNAL(Logic_writeData(QString, QString, QString, QByteArray, QString)), this, SLOT(base_Logic_writeData(QString, QString, QString, QByteArray, QString)));// ��������
|
2024-09-27 14:31:22 +08:00
|
|
|
|
QFuture<void>muf = QtConcurrent::run(this, &Logic_transfer::check_sendImgStruct);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
Sleep(50);
|
2024-09-30 15:36:39 +08:00
|
|
|
|
//�Զ�дLog��־���ܣ�bin/Log
|
2024-09-27 14:31:22 +08:00
|
|
|
|
qInstallMessageHandler(LogMsgOutput);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Logic_transfer::check_sendImgStruct()
|
|
|
|
|
|
{
|
|
|
|
|
|
while (ifRuntransfer)
|
|
|
|
|
|
{
|
|
|
|
|
|
QQueue< sendImgStruct> myS;
|
|
|
|
|
|
//if (Logic_ptr->queue_sendImgStruct.size() > 0)
|
|
|
|
|
|
if (Logic_ptr->get_sendImgStruct(myS))
|
|
|
|
|
|
{
|
2024-09-30 15:36:39 +08:00
|
|
|
|
//QMutexLocker locker(sendImgStructMutex); // ����
|
2024-09-27 14:31:22 +08:00
|
|
|
|
//sendImgStruct mS;
|
|
|
|
|
|
//mS.send_Mat = myL.send_Mat;
|
|
|
|
|
|
//mS.cameraKey_index = myL.cameraKey_index;
|
|
|
|
|
|
|
2024-09-30 15:36:39 +08:00
|
|
|
|
////�ѻ��ˣ�ֻ��ʾ���µ�
|
2024-09-27 14:31:22 +08:00
|
|
|
|
//for (int i = 0; i < queue_sendImgStruct.size(); i++)
|
|
|
|
|
|
// if (myL.cameraKey_index == queue_sendImgStruct[i].cameraKey_index)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// queue_sendImgStruct.removeAt(i);
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// }
|
|
|
|
|
|
//queue_sendImgStruct.enqueue(mS);
|
|
|
|
|
|
for (int i = 0; i < myS.size(); i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
base_Logic_sendImage(myS[i].cameraKey_index, myS[i].send_Mat);
|
|
|
|
|
|
}
|
|
|
|
|
|
myS.clear();
|
|
|
|
|
|
//Sleep(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
Sleep(10);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
bool Logic_transfer::getShowQImage(QQueue< showImgStruct>& my_showImgStruct)
|
|
|
|
|
|
{
|
2024-09-30 15:36:39 +08:00
|
|
|
|
QMutexLocker locker(showMutex); // ����
|
2024-09-27 14:31:22 +08:00
|
|
|
|
//showImgStruct mS;
|
|
|
|
|
|
if (queue_showImgStruct.size() > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
my_showImgStruct.append(queue_showImgStruct);// .dequeue();
|
|
|
|
|
|
queue_showImgStruct.clear();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Logic_transfer::base_Logic_sendImage(QString key, cv::Mat mtx)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!mtx.empty()) {
|
2024-09-30 15:36:39 +08:00
|
|
|
|
if ((mtx.dataend - mtx.datastart) != mtx.cols * mtx.rows * mtx.channels()) //�����ܷ���mtx�����쳣����
|
2024-09-27 14:31:22 +08:00
|
|
|
|
{
|
2024-09-30 15:36:39 +08:00
|
|
|
|
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " mtx.dataend - mtx.datastart err����:";
|
2024-09-27 14:31:22 +08:00
|
|
|
|
return;// false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cv::Mat rgb;
|
|
|
|
|
|
//while (mtx.cols > 2000 || mtx.rows > 2000)
|
2024-09-30 15:36:39 +08:00
|
|
|
|
// cv::resize(mtx, mtx, cv::Size(), 0.1, 0.1); //��cpu���ܣ�i7 8700��4800*4800��resize��ʱ100ms��QImage*��scaled���ţ���ʱ200ms
|
2024-09-27 14:31:22 +08:00
|
|
|
|
QImage* itemImage = nullptr;
|
|
|
|
|
|
if (mtx.type() == CV_8UC1)
|
|
|
|
|
|
{
|
2024-09-30 15:36:39 +08:00
|
|
|
|
rgb = mtx.clone(); //mtx.data����ֻ��2�����ݣ�clone����
|
2024-09-27 14:31:22 +08:00
|
|
|
|
itemImage = new QImage(QSize(rgb.cols, rgb.rows), QImage::Format_Grayscale8);
|
|
|
|
|
|
QImage qim((const unsigned char*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_Grayscale8);
|
|
|
|
|
|
*itemImage = qim.copy();
|
|
|
|
|
|
}
|
2024-09-30 15:36:39 +08:00
|
|
|
|
else if (mtx.type() == CV_16U) // ˫ͨ��ת��ͨ����ʾ
|
2024-09-27 14:31:22 +08:00
|
|
|
|
{
|
|
|
|
|
|
cv::normalize(mtx, mtx, 0, 255, cv::NORM_MINMAX);
|
|
|
|
|
|
cv::convertScaleAbs(mtx, rgb);
|
|
|
|
|
|
itemImage = new QImage(QSize(rgb.cols, rgb.rows), QImage::Format_Grayscale8);
|
|
|
|
|
|
QImage qim((const unsigned char*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_Grayscale8);
|
|
|
|
|
|
*itemImage = qim.copy();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (mtx.type() == CV_8UC3)
|
|
|
|
|
|
{
|
|
|
|
|
|
cv::cvtColor(mtx, rgb, cv::COLOR_BGR2RGB);
|
|
|
|
|
|
itemImage = new QImage(QSize(rgb.cols, rgb.rows), QImage::Format_RGB888);
|
|
|
|
|
|
QImage qim((const unsigned char*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_RGB888);
|
|
|
|
|
|
*itemImage = qim.copy();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (mtx.type() == CV_8UC4)
|
|
|
|
|
|
{
|
|
|
|
|
|
cv::cvtColor(mtx, rgb, cv::COLOR_BGRA2RGBA);
|
|
|
|
|
|
itemImage = new QImage(QSize(rgb.cols, rgb.rows), QImage::Format_ARGB32);
|
|
|
|
|
|
QImage qim((const unsigned char*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_ARGB32);
|
|
|
|
|
|
*itemImage = qim.copy();
|
|
|
|
|
|
}
|
|
|
|
|
|
//emit LogicTransfer_sendImage(key, itemImage);
|
|
|
|
|
|
|
2024-09-30 15:36:39 +08:00
|
|
|
|
QMutexLocker locker(showMutex); // ����
|
2024-09-27 14:31:22 +08:00
|
|
|
|
showImgStruct mS;
|
|
|
|
|
|
mS.show_QImage = itemImage;
|
|
|
|
|
|
mS.cameraKey_index = key;
|
|
|
|
|
|
|
2024-09-30 15:36:39 +08:00
|
|
|
|
////�ѻ��ˣ�ֻ��ʾ���µ�
|
2024-09-27 14:31:22 +08:00
|
|
|
|
//for (int i = 0; i < queue_showImgStruct.size(); i++)
|
|
|
|
|
|
// if (key == queue_showImgStruct[i].cameraKey_index)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// queue_showImgStruct.removeAt(i);
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// }
|
|
|
|
|
|
queue_showImgStruct.enqueue(mS);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (QString ev)
|
|
|
|
|
|
{
|
|
|
|
|
|
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " ev:" << ev;
|
|
|
|
|
|
return;// false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return;// true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Logic_transfer::~Logic_transfer()
|
|
|
|
|
|
{
|
|
|
|
|
|
ifRuntransfer = false;
|
|
|
|
|
|
Sleep(100);
|
|
|
|
|
|
|
2024-09-30 15:36:39 +08:00
|
|
|
|
//ͨ�����ӿ�
|
2024-09-27 14:31:22 +08:00
|
|
|
|
destory_Logic(Logic_ptr);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Logic_transfer::base_Logic_sendTcpStatus(bool flag)
|
|
|
|
|
|
{
|
|
|
|
|
|
emit LogicTransfer_sendTcpStatus(flag);
|
|
|
|
|
|
}
|
2024-09-30 15:36:39 +08:00
|
|
|
|
void Logic_transfer::base_Logic_writeData(QString key, QString cameraName, QString snInfo, QByteArray byteArray, QString sendNgInfo)
|
2024-09-27 14:31:22 +08:00
|
|
|
|
{
|
2024-09-30 15:36:39 +08:00
|
|
|
|
emit LogicTransfer_writeData(key, cameraName, snInfo, byteArray, sendNgInfo);
|
2024-09-27 14:31:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
void Logic_transfer::base_Logic_cameraDisconnect(QString msg, bool status)
|
|
|
|
|
|
{
|
|
|
|
|
|
emit LogicTransfer_cameraDisconnect(msg, status);
|
|
|
|
|
|
}
|
|
|
|
|
|
void Logic_transfer::base_Logic_CommunicationDisconnect(QString msg, bool status)
|
|
|
|
|
|
{
|
|
|
|
|
|
emit LogicTransfer_CommunicationDisconnect(msg, status);
|
|
|
|
|
|
}
|