init files
This commit is contained in:
+344
@@ -0,0 +1,344 @@
|
||||
#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;
|
||||
//通用逻辑接口
|
||||
//加载逻辑dll
|
||||
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;
|
||||
|
||||
//加载并检查配置文件:bin/camera_cfg.json
|
||||
//m_jsonObj =load_camera_cfg();
|
||||
//check_cmeraCfg();
|
||||
//Logic_ptr->setJsonObject(m_jsonObj);
|
||||
//信号与槽
|
||||
if(Logic_ptr) {
|
||||
//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)));// 传递数据
|
||||
QFuture<void>muf = QtConcurrent::run(this, &Logic_transfer::check_sendImgStruct);
|
||||
}
|
||||
}
|
||||
}
|
||||
Sleep(50);
|
||||
//自动写Log日志功能;bin/Log
|
||||
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))
|
||||
{
|
||||
//QMutexLocker locker(sendImgStructMutex); // 加锁
|
||||
//sendImgStruct mS;
|
||||
//mS.send_Mat = myL.send_Mat;
|
||||
//mS.cameraKey_index = myL.cameraKey_index;
|
||||
|
||||
////堆积了,只显示最新的
|
||||
//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)
|
||||
{
|
||||
QMutexLocker locker(showMutex); // 加锁
|
||||
//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()) {
|
||||
if ((mtx.dataend - mtx.datastart) != mtx.cols * mtx.rows * mtx.channels()) //尝试能否解决mtx内容异常问题
|
||||
{
|
||||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " mtx.dataend - mtx.datastart err!!:";
|
||||
return;// false;
|
||||
}
|
||||
|
||||
cv::Mat rgb;
|
||||
//while (mtx.cols > 2000 || mtx.rows > 2000)
|
||||
// cv::resize(mtx, mtx, cv::Size(), 0.1, 0.1); //吃cpu性能,i7 8700,4800*4800的resize耗时100ms;QImage*的scaled缩放,耗时200ms
|
||||
QImage* itemImage = nullptr;
|
||||
if (mtx.type() == CV_8UC1)
|
||||
{
|
||||
rgb = mtx.clone(); //mtx.data检查后,只有2个数据,clone崩溃
|
||||
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_16U) // 双通道转单通道显示
|
||||
{
|
||||
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);
|
||||
|
||||
QMutexLocker locker(showMutex); // 加锁
|
||||
showImgStruct mS;
|
||||
mS.show_QImage = itemImage;
|
||||
mS.cameraKey_index = key;
|
||||
|
||||
////堆积了,只显示最新的
|
||||
//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);
|
||||
|
||||
//通用逻辑接口
|
||||
destory_Logic(Logic_ptr);
|
||||
}
|
||||
|
||||
|
||||
void Logic_transfer::base_Logic_sendTcpStatus(bool flag)
|
||||
{
|
||||
emit LogicTransfer_sendTcpStatus(flag);
|
||||
}
|
||||
void Logic_transfer::base_Logic_writeData(QString key, QString cameraName, QString snInfo, QByteArray byte, QString sendNgInfo)
|
||||
{
|
||||
emit LogicTransfer_writeData(key, cameraName, snInfo, byte, sendNgInfo);
|
||||
}
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user