Files
handleaiuinew/HanddleAI/HandleAiUi-new/back/threadPoolManager.cpp
T

210 lines
5.2 KiB
C++
Raw Normal View History

2024-05-21 23:54:22 +08:00
#include <threadPoolManager.h>
2024-08-07 09:16:35 +00:00
#include <appConfig/sqlManager/sqlManager.h>
2024-05-24 18:05:29 +08:00
#include <imgSaveThreadManager.h>
2024-05-27 00:22:42 +08:00
#include <triggerManager.h>
#include <algo/ALGO_MAIN.h>
2024-08-07 09:16:35 +00:00
#include <appCore.hpp>
#include <handleAiModel.h>
#include <detProcess.hpp>
#include <appCore.hpp>
2024-05-18 23:53:26 +08:00
2024-05-21 23:54:22 +08:00
////////////THREAD//Manager////////////////////////
2024-08-07 09:16:35 +00:00
detThreadManager::detThreadManager()
2024-05-18 23:53:26 +08:00
{
2024-05-23 18:12:43 +08:00
}
2024-05-18 23:53:26 +08:00
2024-08-07 09:16:35 +00:00
void detThreadManager::initialization(detProcess* _appCore)
2024-05-24 18:05:29 +08:00
{
2024-08-07 09:16:35 +00:00
_imgSaveThreadManager = &_appCore->_imgSaveThreadManager;
_triggerManager = &_appCore->_triggerManager;
_handleAiModelManager = &_appCore->_handleAiModelManager;
initFlag = true;
2024-08-07 09:16:35 +00:00
this->start();
2024-05-24 18:05:29 +08:00
}
2024-08-07 09:16:35 +00:00
void detThreadManager::process()
2024-05-23 18:12:43 +08:00
{
2024-05-24 00:55:08 +08:00
2024-05-23 18:12:43 +08:00
#pragma region 检查队列是否有新的数据
queueMutex.lock();
while (insertQueue.size() > 0)
2024-05-24 00:55:08 +08:00
waitImgData.push_back(insertQueue.dequeue());
2024-05-23 18:12:43 +08:00
queueMutex.unlock();
#pragma endregion
#pragma region 等待图片满足要求后,储存原图,异步执行算法函数
2024-05-24 00:55:08 +08:00
for (auto it = waitImgData.begin(); it != waitImgData.end();)
2024-05-18 23:53:26 +08:00
{
2024-05-24 00:55:08 +08:00
std::shared_ptr<detData> ptr = *it;
2024-05-24 18:05:29 +08:00
if (ptr->oriImgs.size() >= ptr->imgAmountMin)
2024-05-23 18:12:43 +08:00
{
2024-08-07 09:16:35 +00:00
std::async(std::launch::async, &imgSaveThreadManager::saveOri,_imgSaveThreadManager, ptr);
2024-05-27 00:22:42 +08:00
algoFutures.insert(std::make_pair(ptr->timeCode, std::async(std::launch::async, processAlgo, ptr,_handleAiModelManager)));
2024-05-24 00:55:08 +08:00
processData.push_back(ptr);
2024-05-24 18:05:29 +08:00
it = waitImgData.erase(it);
2024-05-23 18:12:43 +08:00
}
2024-05-24 00:55:08 +08:00
else
++it;
}
#pragma endregion
#pragma region 执行完毕后异步获取储存使用结果
for (auto it = processData.begin(); it != processData.end();)
{
std::shared_ptr<detData> ptr = *it;
2024-05-24 18:05:29 +08:00
futrueIte = algoFutures.find(ptr->timeCode);
2024-05-27 00:22:42 +08:00
if (futrueIte->second.wait_for(zeroSec) == std::future_status::ready)
2024-05-24 00:55:08 +08:00
{
2024-08-07 09:16:35 +00:00
std::async(std::launch::async, &detThreadManager::resultHandle, this, ptr);
2024-05-24 18:05:29 +08:00
it = processData.erase(it);
algoFutures.erase(futrueIte);
}
else
++it;
}
#pragma endregion
2024-05-18 23:53:26 +08:00
2024-05-24 00:55:08 +08:00
}
2024-08-07 09:16:35 +00:00
void detThreadManager::resultHandle(std::shared_ptr <detData> data)
2024-05-24 00:55:08 +08:00
{
2024-08-27 10:46:43 +08:00
//for (auto func : resultHandleCallbacks)
// func(data);
if (data->algoThreadIndex < resultHandleCallbacks.size())
resultHandleCallbacks[data->algoThreadIndex](data);
2024-05-24 00:55:08 +08:00
}
2024-08-07 09:16:35 +00:00
void detThreadManager::run()
2024-05-18 23:53:26 +08:00
{
2024-05-24 00:55:08 +08:00
while (runFlag == true)
2024-05-23 18:12:43 +08:00
{
process();
QThread::msleep(1);
}
2024-05-18 23:53:26 +08:00
}
2024-08-07 09:16:35 +00:00
void detThreadManager::addTask(std::shared_ptr <detData> data)
2024-05-18 23:53:26 +08:00
{
insertQueueMutex.lock();
2024-05-23 18:12:43 +08:00
insertQueue.enqueue(data);
2024-05-18 23:53:26 +08:00
insertQueueMutex.unlock();
}
2024-08-07 09:16:35 +00:00
detThreadManager::~detThreadManager()
2024-05-18 23:53:26 +08:00
{
2024-05-24 00:55:08 +08:00
runFlag = false;
this->stop();
this->terminate();
processData.clear();
2024-05-24 18:05:29 +08:00
algoFutures.clear();
2024-05-22 16:47:14 +08:00
}
2024-05-18 23:53:26 +08:00
////////////////DET//DATA//////////////////
sncodeCls::sncodeCls()
{
2024-08-07 09:16:35 +00:00
tcpStartPos = _core._appConfig._triggerStruct.startPos;
2024-05-18 23:53:26 +08:00
}
bool sncodeCls::clearSameSnRequestData()
{
std::unordered_map< size_t, size_t >tempMap;
for (int i = snWaittingVec.size() - 1; i >= 0; i--)
{
auto it = tempMap.find(snWaittingVec[i].startpos);
if (it == tempMap.end()) {
tempMap.insert({ snWaittingVec[i].startpos,i });
}
else
{
snWaittingVec.removeAt(i);
return false;
}
}
return true;
}
2024-05-27 00:22:42 +08:00
void sncodeCls::checkSnFromWaittingVec(QByteArray plcByte)
2024-05-18 23:53:26 +08:00
{
while (clearSameSnRequestData() == false);
for (int i = 0; i < snWaittingVec.size(); i++)
{//第一位是hash,第二位是index
2024-05-27 00:22:42 +08:00
std::string SN = readSnFromQByteArray(plcByte, snWaittingVec[i].startpos, false, true, 0);
2024-05-18 23:53:26 +08:00
if (SN.compare(snErrorsnError))//不为空
{
snResList.push_back({ snWaittingVec[i].timecode,SN,snWaittingVec[i].startpos });
while (snResList.size() > 100)
snResList.pop_front();
snWaittingVec.removeAt(i);
}
}
}
2024-05-22 16:47:14 +08:00
2024-08-07 09:16:35 +00:00
//FINS EDIT
2024-05-27 00:22:42 +08:00
std::string sncodeCls::readSnFromQByteArray(QByteArray plcByte, int dmPos,
2024-05-18 23:53:26 +08:00
bool highLowConversion, bool clearAfterRead, long long timeCode, int Size)
{
int triggerPos = (dmPos - tcpStartPos) * 2;
if (triggerPos < 0)
{
2024-05-27 00:22:42 +08:00
std::cout << "Read SN error,trigger is DM:" << dmPos << ",but read start at DM:" << tcpStartPos << endl;
2024-05-18 23:53:26 +08:00
return snErrorsnError;
}
QString str;
2024-05-27 00:22:42 +08:00
if (plcByte.at(triggerPos + 1) == 0 && plcByte.at(triggerPos) == 0)
2024-05-18 23:53:26 +08:00
return snErrorsnError;
int snCount = 0;
2024-05-27 00:22:42 +08:00
while (plcByte.at(snCount + triggerPos) != 0 || plcByte.at(snCount + triggerPos + 1) != 0)
2024-05-18 23:53:26 +08:00
{
2024-05-27 00:22:42 +08:00
if (plcByte.at(snCount + triggerPos) == 0)
2024-05-18 23:53:26 +08:00
{
snCount++;
continue;
}
2024-05-27 00:22:42 +08:00
str += QChar(plcByte.at(snCount + triggerPos));
2024-05-18 23:53:26 +08:00
snCount++;
}
if (highLowConversion == true)
{
snHex(str);
}
if (str.size() < Size && Size != -1)
return snErrorsnError;
2024-05-22 16:47:14 +08:00
//if (clearAfterRead == true)
// //for(int i=0;i< snCount;i++)
// bridge.back.sendFinsMsg(
//QString::fromStdString("writeALL_DM" + to_string(dmPos) + "_" + to_string(snCount) + "_0"));
2024-05-18 23:53:26 +08:00
return str.toStdString();
}
QString sncodeCls::snHex(QString sn)
{
QString resultStr = "";
for (int i = 0; i < sn.size() - 1; i = i + 2)
{
resultStr = resultStr + sn.at(i + 1) + sn.at(i);
}
return resultStr;
}
2024-05-27 00:22:42 +08:00
std::string sncodeCls::getData(unsigned long long timeCode)
2024-05-18 23:53:26 +08:00
{
for (int i = snResList.size() - 1; i >= 0; i--)
{
if (snResList[i].timecode == timeCode)
return snResList[i].sn;
}
return snErrorsnError;
}
2024-05-27 00:22:42 +08:00
std::string sncodeCls::waitForSn(unsigned long long timeCode, int timeOut)
2024-05-18 23:53:26 +08:00
{
int cnt = 0;
2024-05-27 00:22:42 +08:00
std::string sn = getData(timeCode);
2024-05-18 23:53:26 +08:00
if (!sn.compare(snErrorsnError))
{
while (cnt < timeOut)
{
sn = getData(timeCode);
if (sn.compare(snErrorsnError))
{
return sn;
}
cnt += 10;
QThread::msleep(10);
}
}
else
return sn;
return snErrorsnError;
}