1347 lines
38 KiB
C++
1347 lines
38 KiB
C++
#include "processtask.h"
|
|
#include "hpromptwidget.h"
|
|
#include "FrontendBackendBridge.h"
|
|
#include "fileManager.h"
|
|
QList<MyThread*> threadlist;
|
|
QList<MyIoThread*> myIoThreadList;
|
|
QMap<QString, HStationData*> station_list;
|
|
ResultSnData* resultSnData = nullptr;
|
|
|
|
ProcessTask::ProcessTask(QObject* parent) : QObject(parent)
|
|
{
|
|
m_mutex = new QMutex;
|
|
|
|
resultSnData = new ResultSnData();
|
|
//初始化IO线程
|
|
m_iomutex = new QMutex;
|
|
|
|
//初始化io读取包的线程
|
|
m_readThread = new ReadPackThread(this);
|
|
m_readThread->setMutex(m_mutex);
|
|
|
|
deletePicTimer = new QTimer;
|
|
deletePicTimer->setInterval(200);
|
|
autoDeleteThread=new autoDelete(bridge.back.json.imgSavePath+"//yuantu",autoDelete::sizeOver, deletePicTimer);
|
|
deletePicTimer->start();
|
|
|
|
}
|
|
|
|
ProcessTask::~ProcessTask()
|
|
{
|
|
clearTask();
|
|
|
|
if (m_readThread)
|
|
{
|
|
m_readThread->deleteLater();
|
|
m_readThread = nullptr;
|
|
}
|
|
|
|
if (m_mutex)
|
|
{
|
|
delete m_mutex;
|
|
m_mutex = nullptr;
|
|
}
|
|
if (m_iomutex)
|
|
{
|
|
delete m_iomutex;
|
|
m_iomutex = nullptr;
|
|
}
|
|
if (deletePicTimer)
|
|
{
|
|
delete deletePicTimer;
|
|
deletePicTimer = nullptr;
|
|
}
|
|
qDebug() <<"[INFO] " << " ~ProcessTask success";
|
|
}
|
|
|
|
void ProcessTask::setJsonObject(QJsonObject obj)
|
|
{
|
|
if (obj.isEmpty())
|
|
{
|
|
qCritical() << "[Error] " << " obj is null! ";
|
|
return;
|
|
}
|
|
cameraObj = obj;
|
|
}
|
|
|
|
// 任务初始化
|
|
int ProcessTask::initTask()
|
|
{
|
|
try
|
|
{
|
|
//tcplist_array = cameraObj.value("tcplist").toArray();
|
|
//stationlist_array = cameraObj.value("stationlist").toArray();
|
|
//if (stationlist_array.isEmpty() || tcplist_array.isEmpty())
|
|
//{
|
|
// qCritical() << "[Error] " << QString::fromUtf8("配置文件属性缺失tcplist,stationlist!");
|
|
// //return -1;
|
|
//}
|
|
// 加载所有模块
|
|
int res = loadAllModule();
|
|
if (res ==OK)
|
|
{
|
|
qDebug() << "[INFO] " << QString::fromUtf8("所有模块加载成功!");
|
|
setLoadFlag(1);
|
|
}
|
|
else
|
|
{
|
|
qCritical() << "[Error] " << QString::fromUtf8(" 模块加载失败!");
|
|
setLoadFlag(0);
|
|
return res;
|
|
}
|
|
res = initAllModule();
|
|
if (res == OK)
|
|
{
|
|
qDebug() << "[INFO] " << QString::fromUtf8("所有模块初始化成功!");
|
|
setInitFlag(1);
|
|
}
|
|
else
|
|
{
|
|
qCritical() << "[Error] " << QString::fromUtf8("模块初始化失败!");
|
|
setInitFlag(0);
|
|
return res;
|
|
}
|
|
// 创建线程
|
|
res = createModuleThread();
|
|
if (res == OK)
|
|
{
|
|
qDebug() << "[INFO] " << QString::fromUtf8("模块线程创建成功!");
|
|
setCreateThread(1);
|
|
}
|
|
else
|
|
{
|
|
qCritical() << "[Error] " << QString::fromUtf8("模块线程创建失败!");
|
|
setCreateThread(0);
|
|
return res;
|
|
}
|
|
}
|
|
catch (QString ev)
|
|
{
|
|
qCritical() << "[Error] " << " ev:" << ev;
|
|
return -1;
|
|
}
|
|
qDebug() << "[INFO] " << QString::fromUtf8("软件初始化成功!");
|
|
return 0;
|
|
}
|
|
|
|
QList<MyModule> ProcessTask::getModuleList(int thread_index,QString stationKey)
|
|
{
|
|
QList<MyModule> m_module_list;
|
|
for (int i = 0; i < module_list.size(); i++)
|
|
{
|
|
QString task_thread_index = module_list.at(i).task_thread_index;
|
|
QString m_stationKey = module_list.at(i).m_stationKy;
|
|
if (task_thread_index.toInt() == thread_index && m_stationKey == stationKey)
|
|
m_module_list.append(module_list.at(i));
|
|
}
|
|
return m_module_list;
|
|
}
|
|
|
|
QJsonArray ProcessTask::getTaskAlgorithmObject(int thread_index, QJsonArray m_array)
|
|
{
|
|
QJsonArray array;
|
|
for (int i = 0; i < m_array.size(); i++)
|
|
{
|
|
QJsonObject obj = m_array.at(i).toObject();
|
|
QString task_thread_index = obj.value("task_thread_index").toString();
|
|
if (task_thread_index.toInt() == thread_index)
|
|
array.append(obj);
|
|
}
|
|
return array;
|
|
}
|
|
|
|
void ProcessTask::clearData()
|
|
{
|
|
QStringList keys = station_list.keys();
|
|
for (int i = 0; i < keys.size(); i++)
|
|
{
|
|
station_list.value(keys[i])->clearData();
|
|
}
|
|
}
|
|
|
|
QJsonObject ProcessTask::getFinsTcpObject(QJsonArray array)
|
|
{
|
|
QJsonObject m_obj;
|
|
for (int i = 0; i < array.size(); i++)
|
|
{
|
|
QJsonObject obj = array.at(i).toObject();
|
|
QString key = obj.value("key").toString();
|
|
if (key == "finstcp")
|
|
{
|
|
return obj;
|
|
}
|
|
}
|
|
return m_obj;
|
|
}
|
|
|
|
int ProcessTask::createModuleThread()
|
|
{
|
|
try
|
|
{
|
|
if (modulethread_flag == 1)
|
|
{
|
|
qDebug() << "[INFO] " << " all module thread start ! ";
|
|
return OK;
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////
|
|
bridge.back.setFins(finstcp_ptr);
|
|
//SETTING.json
|
|
////////////////////相机初始化///////////////////////////
|
|
for (auto val = 0; val < bridge.back.json.camera.cameraList.size(); val++)
|
|
{
|
|
QString connectionName = bridge.back.json.camera.cameraList[val].connectionName;
|
|
QString serialNum = bridge.back.json.camera.cameraList[val].serialNum;
|
|
QString triggermode = bridge.back.json.camera.cameraList[val].triggermode;
|
|
if (connectionName.isEmpty() || triggermode.isEmpty())
|
|
{
|
|
qCritical() << "[Error] " << " key or type is null";
|
|
return THREAD_CAMERA_FAILURE;
|
|
}
|
|
// 设置 MyModule
|
|
MyModule camera_Module = getModule(connectionName); // 获取加载好的module
|
|
|
|
// 创建相机线程
|
|
MyThread* m_thread = new MyThread(this);
|
|
//m_thread->setSaveImage(saveImage);
|
|
m_thread->setModule("", connectionName, triggermode, camera_Module);
|
|
m_thread->setMutex(m_mutex); // 设置锁
|
|
m_thread->showIndex = val;
|
|
threadlist.append(m_thread);
|
|
connect(m_thread, &MyThread::mvDisconnect, [=]() {
|
|
emit mvDisconnect(connectionName);//0106 相机断连弹窗
|
|
});
|
|
}
|
|
#if 1
|
|
MyIoThread* m_IoThread = new MyIoThread();
|
|
m_IoThread->setIOMutex(m_iomutex);
|
|
connect(m_IoThread, &MyIoThread::SendStationLabel, this, &ProcessTask::SendStationLabel);
|
|
myIoThreadList.append(m_IoThread);
|
|
|
|
MyIoThread* m_IoThreadHeart = new MyIoThread();
|
|
//m_IoThread->setIOMutex(m_iomutex);
|
|
//m_IoThread->setheartCmd(m_heartCmd);
|
|
m_IoThreadHeart->setType("Heart");
|
|
myIoThreadList.append(m_IoThreadHeart);
|
|
#endif
|
|
#if 0
|
|
//CMAERA_CFG
|
|
///////////////////////////////////////////////////////////////////
|
|
for (int i = 0; i < stationlist_array.size(); i++)
|
|
{
|
|
|
|
QJsonObject station_obj = stationlist_array.at(i).toObject();
|
|
QString station_name = station_obj.value("key").toString();
|
|
#if 0
|
|
// 设置相机
|
|
QJsonArray cameramodulelist = station_obj.value("cameramodulelist").toArray();
|
|
for (auto val = 0; val < cameramodulelist.size(); val++)
|
|
{
|
|
QJsonObject camera_obj = cameramodulelist.at(val).toObject();
|
|
QString key = camera_obj.value("key").toString();
|
|
QString type = camera_obj.value("type").toString();
|
|
if (key.isEmpty() || type.isEmpty())
|
|
{
|
|
qCritical() << "[Error] " << " key or type is null";
|
|
return false;
|
|
}
|
|
// 设置 MyModule
|
|
MyModule camera_Module = getModule(key); // 获取加载好的module
|
|
|
|
// 创建相机线程
|
|
MyThread* m_thread = new MyThread(this);
|
|
m_thread->setSaveImage(saveImage);
|
|
//m_thread->setModule(station_name, key, type, camera_Module, camera_obj);
|
|
m_thread->setMutex(m_mutex); // 设置锁
|
|
m_thread->showIndex = val;
|
|
//m_thread->setSerialPorts(QSerialPorts);
|
|
threadlist.append(m_thread);
|
|
connect(m_thread, &MyThread::mvDisconnect, [=]() {
|
|
emit mvDisconnect(station_name+key);//0106 相机断连弹窗
|
|
});
|
|
}
|
|
#endif
|
|
// 创建黑匣子线程
|
|
|
|
#if 0
|
|
QJsonArray algorithmlist = station_obj.value("algorithmlist").toArray();
|
|
for (auto val = 0; val < algorithmlist.size(); val++)
|
|
{
|
|
QJsonObject algorithm_obj = algorithmlist.at(val).toObject();
|
|
QString key = algorithm_obj.value("key").toString();
|
|
QJsonArray trigger_conditions = algorithm_obj.value("trigger_conditions").toArray();
|
|
for (int m = 0; m < trigger_conditions.size(); m++)
|
|
{
|
|
QJsonObject trigger_condition = trigger_conditions.at(m).toObject();
|
|
QJsonArray triggerCondition = trigger_condition.value("trigger_condition").toArray();
|
|
HDataIntegration* dataIntegrationThread = new HDataIntegration();
|
|
dataIntegrationThread->setSavePath(m_savePath);
|
|
dataIntegrationThread->setStationKey(station_name);
|
|
dataIntegrationThread->setAlgorithmKey(key);
|
|
dataIntegrationThread->setTriggerCondition(triggerCondition);
|
|
dataIntegrationThread->init(station_obj);
|
|
dataIntegrationThread_list.append(dataIntegrationThread);
|
|
}
|
|
|
|
}
|
|
|
|
// 创建任务线程
|
|
int task_thread_num = station_obj.value("task_thread_num").toString().toInt();
|
|
for (int val = 0; val < task_thread_num; val++)
|
|
{
|
|
QJsonArray m_array = getTaskAlgorithmObject(val, algorithmlist);
|
|
QList<MyModule> m_mymodule = getModuleList(val, station_name);
|
|
|
|
taskThread* m_task = new taskThread(this);
|
|
m_task->setFinsTcpPtr(finstcp_ptr);
|
|
m_task->initTaskThread(val, station_name, m_array, m_mymodule);
|
|
m_task->setMutex(m_mutex);
|
|
|
|
task_thread_list.append(m_task);
|
|
}
|
|
#endif
|
|
///创建IO线程
|
|
if (station_obj.contains("plcread_area"))
|
|
{
|
|
MyIoThread* m_IoThread = new MyIoThread();
|
|
m_IoThread->setIOMutex(m_iomutex);
|
|
m_IoThread->setStationKey(station_name);
|
|
m_IoThread->setStationObject(cameraObj);
|
|
//m_IoThread->setFinsTcpPtr(finstcp_ptr);
|
|
connect(m_IoThread, &MyIoThread::SendStationLabel, this, &ProcessTask::SendStationLabel);
|
|
myIoThreadList.append(m_IoThread);
|
|
}
|
|
}
|
|
|
|
//心跳线程
|
|
MyIoThread* m_IoThread = new MyIoThread();
|
|
m_IoThread->setIOMutex(m_iomutex);
|
|
m_IoThread->setStationObject(cameraObj);
|
|
//m_IoThread->setFinsTcpPtr(finstcp_ptr);
|
|
m_IoThread->setheartCmd(m_heartCmd);
|
|
m_IoThread->setType("Heart");
|
|
myIoThreadList.append(m_IoThread);
|
|
|
|
// 设置IOThread的读取指令
|
|
//QJsonObject finstcp_obj = tcplist_array.first().toObject();
|
|
//QString readAll_cmd = finstcp_obj.value("finstcp_readAllCmd").toString();
|
|
//// QVector<QString> trigger_camera_vector = getCameraTriggerCmdArray(cameraObj);
|
|
//if (!readAll_cmd.isEmpty())
|
|
//{
|
|
// //m_IoThread->setReadAllCmd(readAll_cmd);
|
|
// //m_IoThread->setTriggerCmd(cameraObj); //设置所有相机的指令
|
|
//}
|
|
#endif
|
|
}
|
|
catch (QString ev) {
|
|
qCritical() << "[Error] " << " ev:" << ev;
|
|
return ERROR_createModuleThread;
|
|
}
|
|
|
|
qDebug() << "[INFO] " << " threadlist size:" << threadlist.size();
|
|
return OK;
|
|
}
|
|
|
|
// 获取 相处对应的 任务模块名称
|
|
MyModule ProcessTask::getModule(QString keyIN)
|
|
{
|
|
MyModule m_mod;
|
|
for (int i = 0; i < module_list.size(); i++)
|
|
{
|
|
QString key = module_list.at(i).key;
|
|
QString moduleName = module_list.at(i).moduleName;
|
|
if (!key.compare(keyIN))
|
|
return module_list.at(i);
|
|
}
|
|
return m_mod;
|
|
}
|
|
|
|
bool ProcessTask::startTask()
|
|
{
|
|
if (isStartThread)
|
|
return false;
|
|
qDebug() << "[INFO] " << " start all thread!";
|
|
|
|
//io线程
|
|
//m_IoThread->start();
|
|
//m_readThread->start();
|
|
//开启IO线程
|
|
foreach(MyIoThread * m_thread, myIoThreadList)
|
|
{
|
|
qDebug() << "[INFO] " << " start io thread!";
|
|
m_thread->start();
|
|
}
|
|
|
|
Sleep(2000);
|
|
clearData();
|
|
|
|
// 开启所有线程
|
|
foreach(MyThread * m_thread, threadlist)
|
|
{
|
|
if (m_thread->getType().contains("algorithm"))
|
|
{
|
|
qDebug() << "[INFO] " << "-- threadname:" << m_thread->getName();
|
|
m_thread->start(); // 运行所有线程
|
|
}
|
|
else if (m_thread->getType().contains("camera") && !m_thread->getTriggerModel().contains("software"))
|
|
{
|
|
m_thread->start();
|
|
}
|
|
}
|
|
|
|
Sleep(2000);
|
|
clearData();
|
|
|
|
// 开启任务线程
|
|
foreach(taskThread* m_thread, task_thread_list)
|
|
{
|
|
m_thread->start();
|
|
}
|
|
|
|
// 开启黑匣子线程
|
|
//foreach(HDataIntegration * m_thread, dataIntegrationThread_list)
|
|
//{
|
|
// m_thread->start();
|
|
//}
|
|
|
|
isStartThread = true;
|
|
|
|
return true;
|
|
}
|
|
|
|
int ProcessTask::initAllModule()
|
|
{
|
|
qDebug() << "[INFO] " << " initAllModule start!";
|
|
try
|
|
{
|
|
if (init_flag == 1)
|
|
{
|
|
qDebug() << "[Error] " << " all module inited!";
|
|
return OK;
|
|
}
|
|
|
|
std::vector<cv::Mat> vec_mat;
|
|
QJsonObject finstcp_obj = tcplist_array.first().toObject();
|
|
//QString ip_port = finstcp_obj.value("finstcp_init").toString();
|
|
//m_heartCmd = finstcp_obj.value("heartCmd").toString();
|
|
QString ip_port = bridge.back.json.tcp.finstcp_init;
|
|
m_heartCmd = bridge.back.json.tcp.heartCmd;
|
|
if (ip_port.isEmpty())
|
|
{
|
|
qCritical() << "[TCP] " << " ip_port is null ! ";
|
|
return INIT_FINS_FAILURE;
|
|
}
|
|
QByteArray mm_byte = ip_port.toUtf8();
|
|
if (!finstcp_ptr->reciveData(vec_mat, mm_byte))
|
|
{
|
|
qCritical() << "[TCP] " << " finstcp reciveData error!";
|
|
return INIT_FINS_FAILURE;
|
|
}
|
|
|
|
QByteArray init_byte;
|
|
//QString finstcp_sleeptime = finstcp_obj.value("finstcp_sleeptime").toString();
|
|
//QString finstcp_readnum = finstcp_obj.value("finstcp_readAllCmd").toString(); //读取包的数量
|
|
//QString finstcp_writeType = finstcp_obj.value("writetype").toString(); // 写入类型 ushort 还是 short
|
|
QString finstcp_sleeptime = bridge.back.json.tcp.finstcp_sleeptime;
|
|
QString finstcp_readnum = bridge.back.json.tcp.finstcp_readAllCmd; //读取包的数量
|
|
QString finstcp_writeType = bridge.back.json.tcp.writetype; // 写入类型 ushort 还是 short
|
|
if (finstcp_sleeptime.isEmpty() || finstcp_readnum.isEmpty())
|
|
{
|
|
qCritical() << "[TCP] " << " finstcp_sleeptime or finstcp_readnum is null!";
|
|
return INIT_FINS_FAILURE;
|
|
}
|
|
else
|
|
{
|
|
QStringList list = finstcp_readnum.split("_");
|
|
if (list.size() != 3)
|
|
{
|
|
qCritical() << "[Error] " << " list size is error! size :" << list.size();
|
|
return INIT_FINS_FAILURE;
|
|
}
|
|
else
|
|
{
|
|
qDebug() << "[INFO] " << " list:" << list;
|
|
QString initdata = "sleeptime_" + finstcp_sleeptime + "_readnum_" + list.last() + "_writetype_" + finstcp_writeType;
|
|
init_byte = initdata.toUtf8();
|
|
//m_IoThread->setTcpInitPara(init_byte);
|
|
if (finstcp_ptr->initParas(init_byte))
|
|
{
|
|
qDebug() << "[TCP] " << " finstcp initParas success!";
|
|
//isInitTcp = false;
|
|
}
|
|
else
|
|
{
|
|
qCritical() << "[TCP] " << " finstcp initParas failed!";
|
|
return INIT_FINS_FAILURE;
|
|
//Sleep(500);
|
|
//continue;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
// 初始化所有模块
|
|
QByteArray mvDevices("m_stDevList");
|
|
for (auto i = 0; i < module_list.size(); i++)
|
|
{
|
|
MyModule m_module = module_list.at(i);
|
|
QString moduleName = m_module.moduleName;
|
|
QString key = m_module.key;
|
|
//传递初始化参数
|
|
QByteArray initByte = m_module.m_initByte;
|
|
std::vector<cv::Mat> vec_moduleMat;
|
|
QByteArray m_keyByte = key.toUtf8();
|
|
//遍历是否有相同key的相机,如果有直接复制指针
|
|
auto startTime = std::chrono::high_resolution_clock::now();
|
|
for(int count=0;count<module_list.size();count++)
|
|
{
|
|
if (!module_list[count].key.compare(m_module.key) && count < i)
|
|
{
|
|
m_module.module_ptr = module_list[count].module_ptr;
|
|
auto endTime = std::chrono::high_resolution_clock::now();
|
|
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
|
|
std::cout << moduleName.toStdString() << " time: " << duration.count() << " ms model has copyed" << std::endl;
|
|
//return true;
|
|
}
|
|
}
|
|
|
|
if (moduleName.contains("Camera"))
|
|
{
|
|
m_module.module_ptr->reciveData(vec_moduleMat, m_keyByte); //初始化相机
|
|
if(mvDevices!="m_stDevList"&&moduleName.contains("MvCamera"))
|
|
m_module.module_ptr->reciveData(vec_moduleMat, mvDevices);
|
|
}
|
|
if (m_module.module_ptr->initParas(initByte))
|
|
{
|
|
qDebug() << "[INFO] " << " initParas success!";
|
|
}
|
|
else
|
|
{
|
|
qCritical() << "[Error] " << " moduleName:" << m_module.moduleName << " initParas failed!";
|
|
return INIT_CAMERA_ALGO_FAILURE;
|
|
}
|
|
if (mvDevices == "m_stDevList" && moduleName.contains("MvCamera"))
|
|
m_module.module_ptr->sendData(vec_moduleMat, mvDevices);
|
|
auto endTime = std::chrono::high_resolution_clock::now();
|
|
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
|
|
std::cout << moduleName.toStdString() <<" time: " << duration.count() << " ms" << std::endl;
|
|
}
|
|
|
|
}
|
|
catch (QString ev)
|
|
{
|
|
qDebug() << "[Error] " << " ev:" << ev;
|
|
return ERROR_initAllModule;
|
|
}
|
|
qDebug() << "[INFO] " << " initAllModule success! ";
|
|
return OK;
|
|
}
|
|
|
|
void ProcessTask::setLoadFlag(int index)
|
|
{
|
|
load_flag = index;
|
|
}
|
|
|
|
void ProcessTask::setInitFlag(int index)
|
|
{
|
|
init_flag = index;
|
|
}
|
|
|
|
void ProcessTask::setRunFlag(int index)
|
|
{
|
|
run_flag = index;
|
|
}
|
|
|
|
bool ProcessTask::clearTask()
|
|
{
|
|
qDebug() << "[INFO] " << " clearTask--start";
|
|
try
|
|
{
|
|
//if (m_IoThread)
|
|
//{
|
|
// m_IoThread->setRunStop(); // 设置停止运行;
|
|
// m_IoThread->wait();
|
|
// m_IoThread->quit();
|
|
// delete m_IoThread;
|
|
// m_IoThread = nullptr;
|
|
//}
|
|
//qDebug() << __FUNCTION__ << "line:" << __LINE__ << " clear m_IoThread success! ";
|
|
// 清理 finstcp
|
|
|
|
for (int i = 0; i < myIoThreadList.size(); i++)
|
|
{
|
|
myIoThreadList.at(i)->setRunStop(); // 设置停止运行;
|
|
myIoThreadList.at(i)->wait();
|
|
myIoThreadList.at(i)->quit();
|
|
myIoThreadList[i]->deleteLater();
|
|
myIoThreadList[i] = nullptr;
|
|
}
|
|
myIoThreadList.clear();
|
|
qDebug() << "[INFO] " << " clear IOthreadlist success ! ";
|
|
|
|
// 清理线程
|
|
for (int i = 0; i < threadlist.size(); i++)
|
|
{
|
|
delete threadlist[i];
|
|
threadlist[i] = nullptr;
|
|
}
|
|
threadlist.clear();
|
|
qDebug() << "[INFO] " << " clear threadlist success ! ";
|
|
|
|
// 模块清理指针
|
|
for (int i = 0; i < module_list.size(); i++)
|
|
{
|
|
//QString modelName = module_list.at(i).moduleName;
|
|
Base* base_ptr = const_cast<Base*>(module_list.at(i).module_ptr);
|
|
module_list.at(i).destory_ptr(base_ptr);
|
|
//delete module_list.at(i).module_ptr;
|
|
}
|
|
module_list.clear();
|
|
qDebug() << "[INFO] " << " clear module_list success ! ";
|
|
|
|
if (finstcp_ptr)
|
|
{
|
|
destory_finstcp(finstcp_ptr);
|
|
}
|
|
qDebug() << "[INFO] " << " clear finstcp_ptr success! ";
|
|
|
|
//// 清理 dataIntegrationThread_list
|
|
//for (auto val = 0; val < dataIntegrationThread_list.size(); val++)
|
|
//{
|
|
// if (dataIntegrationThread_list.at(val))
|
|
// {
|
|
// delete dataIntegrationThread_list.at(val);
|
|
// }
|
|
//}
|
|
//dataIntegrationThread_list.clear();
|
|
//qDebug() << "[INFO] " << " clear dataIntegrationThread_list success ! ";
|
|
|
|
// 清理任务线程
|
|
for (auto val = 0; val < task_thread_list.size(); val++)
|
|
{
|
|
if (task_thread_list.at(val))
|
|
{
|
|
delete task_thread_list.at(val);
|
|
}
|
|
}
|
|
task_thread_list.clear();
|
|
qDebug() << "[INFO] " << " clear task_thread_list success ! ";
|
|
|
|
// 清理 station_list
|
|
while (!station_list.isEmpty())
|
|
{
|
|
QString m_key = station_list.firstKey();
|
|
delete station_list.first();
|
|
station_list.remove(m_key);
|
|
}
|
|
qDebug() << "[INFO] " << " clear station_list success ! ";
|
|
}
|
|
catch (QString ev)
|
|
{
|
|
qCritical() << "[Error] " << " ev:" << ev;
|
|
return false;
|
|
}
|
|
qDebug() << "[INFO] " << " clearTask success! ";
|
|
return true;
|
|
}
|
|
|
|
int ProcessTask::loadAllModule()
|
|
{
|
|
if (load_flag == 1)
|
|
{
|
|
qDebug() << "[INFO] " << " all module loaded! ";
|
|
return OK;
|
|
}
|
|
|
|
qDebug() << "[INFO] " << " loadAllModule start! ";
|
|
auto time1 = std::chrono::high_resolution_clock::now();//delete
|
|
// 加载通信模块,通信模块不开线程
|
|
//if (!loadModule_cameraCfg("finstcp", finstcpObj, "",""))
|
|
if (!loadModule_setting("finstcp", bridge.back.json.tcp.moduleName,""))
|
|
{
|
|
qCritical() << "[TCP] " << " load finstcp module failed!";
|
|
return LOAD_FINS_FAILURE;
|
|
}
|
|
auto time2 = std::chrono::high_resolution_clock::now();//delete
|
|
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(time2 - time1);//delete
|
|
std::cout << "fins module loaded in: " << duration.count() << " ms" << std::endl;//delete
|
|
// 加载所有的相机模块和算法模块
|
|
if (!loadCameraAndAlgorithmModule_setting())
|
|
{
|
|
qCritical() << "[Error] " << " loadCameraAndAlgorithmModule is failed!";
|
|
return LOAD_CAMERA_ALGO_FAILURE;
|
|
}
|
|
time1 = std::chrono::high_resolution_clock::now();//delete
|
|
duration = std::chrono::duration_cast<std::chrono::milliseconds>(time1 - time2);//delete
|
|
std::cout << "loadCameraAndAlgorithmModule module loaded in: " << duration.count() << " ms" << std::endl;//delete
|
|
qDebug() << "[INFO] " << " loadAllModule success!";
|
|
return OK;
|
|
}
|
|
#if 1
|
|
bool ProcessTask::loadCameraAndAlgorithmModule_cameracfg(QJsonArray stationlist_array)
|
|
{
|
|
for (int i = 0; i < stationlist_array.size(); i++)
|
|
{
|
|
QJsonObject station_obj = stationlist_array.at(i).toObject();
|
|
QString station_key = station_obj.value("key").toString();
|
|
|
|
QJsonObject plcread_areaObject = station_obj.value("plcread_area").toObject();
|
|
QJsonObject plcwrite_areaObject = station_obj.value("plcwrite_area").toObject();
|
|
|
|
HStationData* stationPtr = new HStationData;
|
|
stationPtr->setStationKey(station_key);
|
|
|
|
HSnData* snData = new HSnData();
|
|
|
|
QMap<QString, int> camreaPicNumMap;
|
|
QMap<QString, QVector<QString>> cameraProductSurfaceMap;
|
|
QJsonArray cameramodulelist = station_obj.value("cameramodulelist").toArray();
|
|
for (auto val = 0; val < cameramodulelist.size(); val++)
|
|
{
|
|
|
|
QJsonObject camera_obj = cameramodulelist.at(val).toObject();
|
|
QString camera_key = camera_obj.value("key").toString();
|
|
QString moduleName = camera_obj.value("moduleName").toString(); // 相机模块名
|
|
QString image_queue_num = camera_obj.value("image_queue_num").toString();
|
|
QJsonArray camera_productSurface = camera_obj.value("productSurface").toArray();
|
|
// 根据相机编号单独处理工位五
|
|
QString cameraName = camera_obj.value("eName").toString();
|
|
//if (cameraName == "Camera1")
|
|
// snData->setCamera1Key(camera_key);
|
|
//if (cameraName == "Camera3")
|
|
// snData->setCamera3Key(camera_key);
|
|
//if (cameraName == "Camera7")
|
|
// snData->setCamera7Key(camera_key);
|
|
|
|
QVector<QString> surfaceVec;
|
|
for (int i = 0; i < camera_productSurface.size(); i++)
|
|
surfaceVec.push_back(camera_productSurface[i].toString());
|
|
|
|
camreaPicNumMap.insert(camera_key, image_queue_num.toInt());
|
|
cameraProductSurfaceMap.insert(camera_key, surfaceVec);
|
|
QByteArray init_Byte;
|
|
// 加载相机模块
|
|
if (!loadModule_cameraCfg(camera_key, camera_obj, init_Byte, station_key))
|
|
{
|
|
qCritical() << "[Error] " << " moduleName:" << moduleName << " load camera module failed! ";
|
|
return false;
|
|
}
|
|
|
|
// 此处创建保存图片数据的容器
|
|
HCameraData* cameraPtr = new HCameraData;
|
|
if (!cameraPtr->initCameraVectorQueue(image_queue_num.toInt()))
|
|
{
|
|
qCritical() << "[Error] " << " initCameraVectorQueue is error!";
|
|
if (cameraPtr)
|
|
{
|
|
delete cameraPtr;
|
|
cameraPtr = nullptr;
|
|
}
|
|
return false;
|
|
}
|
|
cameraPtr->setCameraKey(camera_key);
|
|
stationPtr->addCameraData(cameraPtr);
|
|
}
|
|
|
|
snData->setcamreaPicNumMap(camreaPicNumMap);
|
|
snData->setcameraProductSurface(cameraProductSurfaceMap);
|
|
stationPtr->setsnInfoData(snData);
|
|
#if 0
|
|
QJsonArray algorithmlist = station_obj.value("algorithmlist").toArray();
|
|
for (auto val = 0; val < algorithmlist.size(); val++)
|
|
{
|
|
QJsonObject algorithm_obj = algorithmlist.at(val).toObject(); // 算法模块obj
|
|
QString algorithm_key = algorithm_obj.value("key").toString(); // 算法名称
|
|
QString moduleName = algorithm_obj.value("moduleName").toString(); // 算法模块名
|
|
QString algorithm_config = algorithm_obj.value("algorithm_config").toString(); // 算法所需要的配置文件路径
|
|
QJsonArray algorithm_init_array = algorithm_obj.value("algorithm_init").toArray(); // 基准坐标
|
|
QJsonArray delete_result_index_array = algorithm_obj.value("delete_result_index").toArray(); // 删除算法结果数据
|
|
if (algorithm_key.isEmpty() || moduleName.isEmpty())
|
|
{
|
|
qCritical() << "[Error] " << " algorithm_key or moduleName is null! ";
|
|
return false;
|
|
}
|
|
|
|
// 加载算法模块
|
|
QByteArray init_Byte = encodeAlgorithmData(algorithm_init_array, algorithm_config);
|
|
if (!loadModule(algorithm_key, algorithm_obj, init_Byte, station_key))
|
|
{
|
|
qCritical() << "[Error] " << " camerakey load module fail! ";
|
|
return false;
|
|
}
|
|
|
|
// 此处创建保存算法结果数据的容器
|
|
HAlgorithmData* algorithmPtr = new HAlgorithmData;
|
|
if (!delete_result_index_array.isEmpty())
|
|
{
|
|
QMap<QString, Algorithm_Result_Data_index> mm_map;
|
|
for (int kkkk = 0; kkkk < delete_result_index_array.size(); kkkk++)
|
|
{
|
|
QJsonObject delete_obj = delete_result_index_array.at(kkkk).toObject();
|
|
|
|
QString delete_key = delete_obj.value("key").toString();
|
|
QJsonArray index_array = delete_obj.value("index").toArray();
|
|
QString delete_num = delete_obj.value("delete_num").toString();
|
|
if (!index_array.isEmpty())
|
|
{
|
|
for (int yyyyy = 0; yyyyy < index_array.size(); yyyyy++)
|
|
{
|
|
QString index_key = index_array.at(yyyyy).toString();
|
|
QString map_camera_key = delete_key + "_" + index_key;
|
|
|
|
Algorithm_Result_Data_index index_pack;
|
|
index_pack.delete_Result_index = delete_num.toInt();
|
|
mm_map.insert(map_camera_key, index_pack);
|
|
}
|
|
}
|
|
}
|
|
algorithmPtr->setDeleteResultMap(mm_map);
|
|
}
|
|
algorithmPtr->setAlgorithmKey(algorithm_key);
|
|
|
|
// 此处创建保存算法执行数据的容器
|
|
HAlgorithmExecData* algorithmExecPtr = new HAlgorithmExecData();
|
|
algorithmExecPtr->setAlgorithmName(algorithm_key);
|
|
|
|
stationPtr->addAlgorithmData(algorithmPtr);
|
|
stationPtr->addAlgorithmExecData(algorithmExecPtr);
|
|
}
|
|
// 添加站中算法数据
|
|
station_list.insert(station_key, stationPtr);
|
|
#endif
|
|
}
|
|
|
|
return true;
|
|
}
|
|
bool ProcessTask::loadModule_cameraCfg(QString name, QJsonObject module_obj, QByteArray initByte, QString stationKey)
|
|
{
|
|
QString moduleName = module_obj.value("moduleName").toString();
|
|
QString task_thread_index = module_obj.value("task_thread_index").toString();
|
|
//qDebug() << "[INFO] " << " name:" << name << " moduleName:" << moduleName;
|
|
try
|
|
{
|
|
//QString module_path = JsonPath + "/plugin/"+moduleName; // 暂时做修改
|
|
QString module_path = JsonPath + "/" + moduleName;
|
|
qDebug() << "[INFO] " << " module_path:" << module_path;
|
|
|
|
/////// 自动拷贝重用dll
|
|
if (QString(module_path.at(module_path.size() - 1)).toInt() > 0) {
|
|
//命名格式: 模块名+1/2/3...
|
|
QString path_model = module_path.mid(0, module_path.size() - 1);
|
|
if (QFile::exists(path_model + ".dll")) {
|
|
QFile::remove(module_path + ".dll");
|
|
QFile::copy(path_model + ".dll", module_path + ".dll");
|
|
|
|
qDebug() << "[INFO] " << "copy dll " << module_path;
|
|
}
|
|
|
|
}
|
|
/// 动态加载dll
|
|
|
|
QLibrary* m_lib = new QLibrary(module_path);
|
|
lib_list.append(m_lib);
|
|
bool flag = m_lib->load();
|
|
if (!m_lib->isLoaded())
|
|
{
|
|
|
|
qDebug() << "[Error] " << " Loaded error:" << m_lib->errorString();
|
|
return false;
|
|
}
|
|
|
|
if (name == "finstcp")
|
|
{
|
|
if (flag)
|
|
{
|
|
CreateFunction createFun = (CreateFunction)m_lib->resolve("create");
|
|
ReleaseFunction releaseFun = (ReleaseFunction)m_lib->resolve("destory");
|
|
if (createFun && releaseFun)
|
|
{
|
|
finstcp_ptr = createFun();
|
|
destory_finstcp = releaseFun;
|
|
}
|
|
else
|
|
{
|
|
delete m_lib;
|
|
m_lib = nullptr;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (flag)
|
|
{
|
|
|
|
CreateFunction createFun = (CreateFunction)m_lib->resolve("create");
|
|
ReleaseFunction releaseFun = (ReleaseFunction)m_lib->resolve("destory");
|
|
if (createFun && releaseFun)
|
|
{
|
|
MyModule m_module;
|
|
m_module.m_stationKy = stationKey;
|
|
m_module.key = name;
|
|
m_module.m_initByte = initByte;
|
|
m_module.moduleName = moduleName;
|
|
m_module.module_ptr = createFun();
|
|
m_module.destory_ptr = releaseFun;
|
|
|
|
if (!task_thread_index.isEmpty())
|
|
m_module.task_thread_index = task_thread_index;
|
|
|
|
module_list.append(m_module);
|
|
}
|
|
else
|
|
{
|
|
delete m_lib;
|
|
m_lib = nullptr;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (QString event)
|
|
{
|
|
qCritical() << "[Error] " << " event:" << event;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
#endif
|
|
bool ProcessTask::loadCameraAndAlgorithmModule_setting()
|
|
{
|
|
HSnData* snData = new HSnData();
|
|
|
|
QMap<QString, int> camreaPicNumMap;
|
|
//QList<CameraListCls::datastruct> cameraList;
|
|
QMap<QString, QVector<QString>> cameraProductSurfaceMap;
|
|
for (auto val = 0; val < bridge.back.json.camera.cameraList.size(); val++)
|
|
{
|
|
QString camera_key = bridge.back.json.camera.cameraList[val].connectionName;
|
|
QString moduleName = bridge.back.json.camera.cameraList[val].moduleName; // 相机模块名
|
|
QVector<QString> surfaceVec;
|
|
//for (int i = 0; i < camera_productSurface.size(); i++)
|
|
//surfaceVec.push_back(camera_productSurface[i].toString());
|
|
|
|
camreaPicNumMap.insert(camera_key, 0);
|
|
cameraProductSurfaceMap.insert(camera_key, surfaceVec);
|
|
QByteArray init_Byte;
|
|
// 加载相机模块
|
|
if (!loadModule_setting(camera_key,moduleName, init_Byte))
|
|
{
|
|
qCritical() << "[Error] " << " moduleName:" << moduleName << " load camera module failed! ";
|
|
return false;
|
|
}
|
|
|
|
// 此处创建保存图片数据的容器
|
|
HCameraData* cameraPtr = new HCameraData;
|
|
cameraPtr->setCameraKey(camera_key);
|
|
}
|
|
|
|
snData->setcamreaPicNumMap(camreaPicNumMap);
|
|
snData->setcameraProductSurface(cameraProductSurfaceMap);
|
|
|
|
return true;
|
|
}
|
|
QByteArray ProcessTask::encodeAlgorithmData(QJsonArray array, QString algorithm_config)
|
|
{
|
|
QByteArray byte;
|
|
if (array.isEmpty())
|
|
{
|
|
qCritical() << "[Error] " << " array is null! ";
|
|
// return byte;
|
|
}
|
|
|
|
if (algorithm_config.isEmpty())
|
|
{
|
|
// 当前参数未使用
|
|
//qCritical() << "[Error] " << " algorithm_config is null! ";
|
|
}
|
|
|
|
std::string sendMsg;
|
|
|
|
// 添加参数个数
|
|
int type = 3; // int
|
|
std::string m_type;
|
|
m_type.insert(0, 4, '1');
|
|
memcpy((void*)m_type.c_str(), (void*)&type, 4);
|
|
sendMsg.append(m_type);
|
|
|
|
// len
|
|
int len = 4;
|
|
std::string m_len;
|
|
m_len.insert(0, 4, '1');
|
|
memcpy((void*)m_len.c_str(), (void*)&len, 4);
|
|
sendMsg.append(m_len);
|
|
|
|
// num
|
|
int num;
|
|
if (algorithm_config.isEmpty())
|
|
num = array.size();
|
|
else
|
|
num = array.size() + 1; // 添加配置文件的信息
|
|
|
|
//qDebug() << __FUNCTION__ << " line:" << __LINE__ << " ------------------ num: " << num;
|
|
|
|
std::string m_num;
|
|
m_num.insert(0, 4, '1');
|
|
memcpy((void*)m_num.c_str(), (void*)&num, 4);
|
|
sendMsg.append(m_num);
|
|
|
|
//添加数据
|
|
for (int i = 0; i < array.size(); i++)
|
|
{
|
|
QJsonObject m_obj = array.at(i).toObject();
|
|
QString value = m_obj.value("value").toString();
|
|
if (value.isEmpty())
|
|
{
|
|
qCritical() << "[Error] " << "algorithm_init value is null!";
|
|
return "";
|
|
}
|
|
|
|
int type = 3; //int
|
|
std::string _type;
|
|
_type.insert(0, 4, '1');
|
|
memcpy((void*)_type.c_str(), (void*)&type, 4);
|
|
sendMsg.append(_type);
|
|
|
|
//len
|
|
int data_len = value.length();
|
|
std::string _len;
|
|
_len.insert(0, 4, '1');
|
|
memcpy((void*)_len.c_str(), (void*)&data_len, 4);
|
|
sendMsg.append(_len);
|
|
|
|
//data
|
|
sendMsg.append(value.toStdString());
|
|
}
|
|
|
|
// 添加配置文件的信息
|
|
if (!algorithm_config.isEmpty())
|
|
{
|
|
int type = 3; //int
|
|
std::string _type;
|
|
_type.insert(0, 4, '1');
|
|
memcpy((void*)_type.c_str(), (void*)&type, 4);
|
|
sendMsg.append(_type);
|
|
|
|
//len
|
|
int data_len = algorithm_config.length();
|
|
std::string _len;
|
|
_len.insert(0, 4, '1');
|
|
memcpy((void*)_len.c_str(), (void*)&data_len, 4);
|
|
sendMsg.append(_len);
|
|
|
|
//data
|
|
sendMsg.append(algorithm_config.toStdString());
|
|
}
|
|
QByteArray byteArray(sendMsg.c_str(), sendMsg.length());
|
|
return byteArray;
|
|
}
|
|
|
|
|
|
bool ProcessTask::loadModule_setting(QString keyName, QString moduleName ,QByteArray initByte)
|
|
{
|
|
QString task_thread_index = 0;
|
|
try
|
|
{
|
|
QString module_path = JsonPath + "/" + moduleName;
|
|
qDebug() << "[INFO] " << " module_path:" << module_path;
|
|
|
|
/////// 自动拷贝重用dll
|
|
if (QString(module_path.at(module_path.size() - 1)).toInt() > 0) {
|
|
//命名格式: 模块名+1/2/3...
|
|
QString path_model = module_path.mid(0, module_path.size() - 1);
|
|
if (QFile::exists(path_model + ".dll")) {
|
|
QFile::remove(module_path + ".dll");
|
|
QFile::copy(path_model + ".dll", module_path + ".dll");
|
|
|
|
qDebug() << "[INFO] " << "copy dll " << module_path;
|
|
}
|
|
|
|
}
|
|
/// 动态加载dll
|
|
|
|
QLibrary* m_lib = new QLibrary(module_path);
|
|
lib_list.append(m_lib);
|
|
bool flag = m_lib->load();
|
|
if (!m_lib->isLoaded())
|
|
{
|
|
|
|
qDebug() << "[Error] " << " Loaded error:" << m_lib->errorString();
|
|
return false;
|
|
}
|
|
|
|
if (keyName == "finstcp")
|
|
{
|
|
if (flag)
|
|
{
|
|
CreateFunction createFun = (CreateFunction)m_lib->resolve("create");
|
|
ReleaseFunction releaseFun = (ReleaseFunction)m_lib->resolve("destory");
|
|
if (createFun && releaseFun)
|
|
{
|
|
finstcp_ptr = createFun();
|
|
destory_finstcp = releaseFun;
|
|
}
|
|
else
|
|
{
|
|
delete m_lib;
|
|
m_lib = nullptr;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (flag)
|
|
{
|
|
|
|
CreateFunction createFun = (CreateFunction)m_lib->resolve("create");
|
|
ReleaseFunction releaseFun = (ReleaseFunction)m_lib->resolve("destory");
|
|
if (createFun && releaseFun)
|
|
{
|
|
MyModule m_module;
|
|
m_module.key = keyName;
|
|
m_module.m_initByte = initByte;
|
|
m_module.moduleName = moduleName;
|
|
m_module.module_ptr = createFun();
|
|
m_module.destory_ptr = releaseFun;
|
|
|
|
if (!task_thread_index.isEmpty())
|
|
m_module.task_thread_index = task_thread_index;
|
|
|
|
module_list.append(m_module);
|
|
}
|
|
else
|
|
{
|
|
delete m_lib;
|
|
m_lib = nullptr;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (QString event)
|
|
{
|
|
qCritical() << "[Error] " << " event:" << event;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool ProcessTask::stopTask()
|
|
{
|
|
qDebug() << "[INFO] " << " stopTask";
|
|
return true;
|
|
}
|
|
|
|
void ProcessTask::reciveImageData()
|
|
{
|
|
try {
|
|
QMutexLocker locker(m_mutex);
|
|
if (ImageDataPack.isEmpty())
|
|
{
|
|
qCritical() << "[Error] " << " ImageDataPack is null!";
|
|
return;
|
|
}
|
|
|
|
dataPack m_data = ImageDataPack.dequeue();
|
|
cv::Mat m_mat = m_data.mat;
|
|
QString name = m_data.name;
|
|
QByteArray m_byte = m_data.byte;
|
|
QString retFlag = m_data.retFlag;
|
|
int showIndex = m_data.showIndex;
|
|
|
|
//传递图片
|
|
if (retFlag == "camera")
|
|
decodePackage(name, m_mat, showIndex);
|
|
else if (retFlag == "algorithm")
|
|
{
|
|
if (!m_mat.empty())
|
|
decodePackage(name, m_mat, showIndex);
|
|
|
|
emit sendData(name, m_byte);
|
|
}
|
|
}
|
|
catch (QString err)
|
|
{
|
|
qCritical() << "[Error] " << "show image reeor!";
|
|
}
|
|
;
|
|
|
|
}
|
|
|
|
bool ProcessTask::decodePackage(QString name, cv::Mat mtx,int showindex)
|
|
{
|
|
//qDebug() << __FUNCTION__ << "line:" << __LINE__ << " start name:" << name;
|
|
try
|
|
{
|
|
if (!mtx.empty())
|
|
{
|
|
cv::Mat rgb;
|
|
QImage* itemImage = nullptr;
|
|
if (mtx.type() == CV_8UC1)
|
|
{
|
|
mtx.copyTo(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_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 sendImage(name, itemImage,showindex);
|
|
}
|
|
}
|
|
catch (QString ev)
|
|
{
|
|
qCritical() << "[Error] " << " ev:" << ev;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void ProcessTask::deletePicFolders()
|
|
{
|
|
QJsonObject json = loadCameraCfg();
|
|
QString Imagepath = json.value("imgSavePath").toString();
|
|
std::string saveImageDays_str = (json.value("save_imageDays").toString()).toStdString();
|
|
int saveImageDays=stoi(saveImageDays_str);
|
|
|
|
//std::stringstream str2Int;
|
|
//str2Int << saveImageDays_str;
|
|
//str2Int >> saveImageDays;
|
|
|
|
QDir dir(Imagepath);
|
|
QDateTime current_date_time = QDateTime::currentDateTime();
|
|
QFileInfoList dirList = dir.entryInfoList();
|
|
for (int i = 2; i < dirList.size(); i++)
|
|
{
|
|
QString ddd = dirList[i].fileName();
|
|
QFileInfoList dateDirList = QDir(Imagepath + '/' + dirList[i].fileName()).entryInfoList();
|
|
for (int j = 2; j < dateDirList.size(); j++)
|
|
{
|
|
QString folder = (Imagepath + '/' + dirList[i].fileName() + '/' + dateDirList[j].fileName());
|
|
QDateTime dt = dateDirList[j].created();
|
|
int days = dt.daysTo(current_date_time);
|
|
if (days > saveImageDays)
|
|
{
|
|
QDir dateDir(folder);
|
|
if (dateDir.exists())
|
|
{
|
|
dateDir.removeRecursively();
|
|
}
|
|
/*QFile file(folder);
|
|
file.remove();
|
|
if (!file.remove())
|
|
{
|
|
qDebug() << "remove error:" << file.errorString();
|
|
}*/
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void ProcessTask::getDiskSpace(QString strDisk, double& dTotalBytes, double& dFreeBytes)
|
|
{
|
|
LPCWSTR lpcwstrDriver = (LPCWSTR)strDisk.utf16();
|
|
ULARGE_INTEGER lFreeBytesAvailable, lTotalBytesTemp, lTotalFreeBytes;
|
|
if (!GetDiskFreeSpaceExW(lpcwstrDriver, &lFreeBytesAvailable, &lTotalBytesTemp, &lTotalFreeBytes)) {
|
|
dTotalBytes = -1;
|
|
dFreeBytes = -1;
|
|
return;
|
|
}
|
|
dTotalBytes = lTotalBytesTemp.QuadPart / 1024.0 / 1024 / 1024;
|
|
dFreeBytes = lTotalFreeBytes.QuadPart / 1024.0 / 1024 / 1024;
|
|
}
|
|
|
|
void ProcessTask::autoCheckDisk()
|
|
{
|
|
QJsonObject json = loadCameraCfg();
|
|
QString Imagepath = json.value("imgSavePath").toString();
|
|
QString diskPath = QString::fromStdString(Imagepath.toStdString().substr(0, Imagepath.toStdString().find_first_of("/") + 1));
|
|
double dTotalBytes, dFreeBytes;
|
|
getDiskSpace(diskPath, dTotalBytes, dFreeBytes);
|
|
if (dFreeBytes < 100 && dFreeBytes>0)
|
|
{
|
|
QDir dir(Imagepath);
|
|
QFileInfoList dirList = dir.entryInfoList();
|
|
for (int i = 0; i < dirList.size(); i++)
|
|
{
|
|
QString ddd = dirList[dirList.size()-1].fileName();
|
|
QDir dateDir(ddd);
|
|
dateDir.removeRecursively();
|
|
}
|
|
}
|
|
}
|
|
|
|
QJsonObject ProcessTask::loadCameraCfg()
|
|
{
|
|
QJsonObject json_object;
|
|
try
|
|
{
|
|
QJsonParseError jsonError;
|
|
QString json_cfg = "./config/setting.json";
|
|
if (json_cfg.isEmpty())
|
|
{
|
|
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " JsonPath is null!";
|
|
return json_object;
|
|
}
|
|
|
|
QFile JsonFile;
|
|
JsonFile.setFileName(json_cfg);
|
|
JsonFile.open(QIODevice::ReadOnly);
|
|
|
|
QByteArray m_Byte = JsonFile.readAll();
|
|
if (m_Byte.isEmpty())
|
|
{
|
|
qDebug() << __FUNCTION__ << " line:" << __LINE__ << json_cfg + " 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();
|
|
return json_object;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
qCritical() << __FUNCTION__ << " line:" << __LINE__ << json_cfg + " is error!";
|
|
}
|
|
|
|
JsonFile.close();
|
|
|
|
}
|
|
catch (QString ev)
|
|
{
|
|
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " ev:" << ev;
|
|
}
|
|
return json_object;
|
|
}
|
|
|