2024-05-21 23:54:22 +08:00
|
|
|
#include <threadPoolManager.h>
|
2024-05-18 23:53:26 +08:00
|
|
|
#include <FrontendBackendBridge.h>
|
2024-05-23 18:12:43 +08:00
|
|
|
#include <algo/algorithmThread.h>
|
|
|
|
|
#include <algo/pinAioEvalute.h>
|
2024-05-18 23:53:26 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 检查目标文件夹是否存在,不存在则创建
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path"></param>
|
2024-05-22 16:47:14 +08:00
|
|
|
using namespace std;
|
2024-05-18 23:53:26 +08:00
|
|
|
void createDirectoryIfNotExists_2(const std::string& path) {
|
|
|
|
|
// 检查路径是否存在
|
|
|
|
|
if (!std::filesystem::exists(path)) {
|
|
|
|
|
// 创建路径
|
|
|
|
|
|
|
|
|
|
std::filesystem::create_directories(path);
|
|
|
|
|
//std::cout << "已创建文件夹: " << path << std::endl;
|
|
|
|
|
}
|
|
|
|
|
else {/*
|
|
|
|
|
std::cout << "文件夹已存在: " << path << std::endl;*/
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
std::string getCurrentDateTime_2() {
|
|
|
|
|
// 获取当前时间点
|
|
|
|
|
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
|
|
|
|
|
|
|
|
|
|
// 转换为字符串
|
|
|
|
|
std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
|
|
|
|
|
std::tm* localTime = std::localtime(¤tTime);
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
ss << std::put_time(localTime, "%H-%M-%S");
|
|
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
|
}
|
|
|
|
|
int threadAmount = 0;
|
|
|
|
|
int maxThread = 16;
|
|
|
|
|
|
2024-05-21 23:54:22 +08:00
|
|
|
////////////THREAD//Manager////////////////////////
|
2024-05-22 01:32:43 +08:00
|
|
|
threadManager::threadManager(QObject* parent) : QThread(parent)
|
2024-05-18 23:53:26 +08:00
|
|
|
{
|
|
|
|
|
//sql初始化
|
2024-05-21 23:54:22 +08:00
|
|
|
sql = new sqlManager;
|
2024-05-18 23:53:26 +08:00
|
|
|
sql->initPara();
|
|
|
|
|
//辅助线程初始化
|
2024-05-21 18:11:13 +08:00
|
|
|
//oriSaveThread->oriThread = true;
|
|
|
|
|
//oriSaveThread->Init(this);
|
2024-05-23 18:12:43 +08:00
|
|
|
this->start();
|
|
|
|
|
}
|
2024-05-18 23:53:26 +08:00
|
|
|
|
2024-05-23 18:12:43 +08:00
|
|
|
void threadManager::process()
|
|
|
|
|
{
|
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
|
2024-05-24 00:55:08 +08:00
|
|
|
#pragma region 等待图片满足要求后异步执行算法函数
|
|
|
|
|
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;
|
|
|
|
|
if (ptr->imgs.size() >= ptr->imgAmountMin)
|
2024-05-23 18:12:43 +08:00
|
|
|
{
|
2024-05-24 00:55:08 +08:00
|
|
|
futures.insert(std::make_pair(ptr->timeCode, std::async(std::launch::async, pinAioEvalute, ptr)));
|
|
|
|
|
it = waitImgData.erase(it);
|
|
|
|
|
processData.push_back(ptr);
|
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;
|
|
|
|
|
futrueIte = futures.find(ptr->timeCode);
|
|
|
|
|
if (futrueIte->second.valid())
|
|
|
|
|
{
|
|
|
|
|
it = processData.erase(it); // 删除元素后,更新迭代器
|
|
|
|
|
futures.erase(futrueIte);
|
|
|
|
|
std::async(std::launch::async, &threadManager::resultHandle, this, ptr);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
++it;
|
2024-05-18 23:53:26 +08:00
|
|
|
}
|
2024-05-23 18:12:43 +08:00
|
|
|
#pragma endregion
|
2024-05-18 23:53:26 +08:00
|
|
|
|
2024-05-24 00:55:08 +08:00
|
|
|
}
|
|
|
|
|
void threadManager::resultHandle(std::shared_ptr <detData>)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2024-05-22 01:32:43 +08:00
|
|
|
void threadManager::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-05-22 16:47:14 +08:00
|
|
|
void threadManager::addTask(std::shared_ptr <detData> data)
|
2024-05-18 23:53:26 +08:00
|
|
|
{
|
2024-05-23 18:12:43 +08:00
|
|
|
//{
|
|
|
|
|
// CameraListCls::datastruct _struct = bridge.back.json.camera.getData(data->cameraKeyName);
|
|
|
|
|
// data->oriShowIndex = _struct.showIndex;
|
|
|
|
|
//}
|
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();
|
|
|
|
|
//this->start();
|
|
|
|
|
}
|
2024-05-22 01:32:43 +08:00
|
|
|
threadManager::~threadManager()
|
2024-05-18 23:53:26 +08:00
|
|
|
{
|
2024-05-24 00:55:08 +08:00
|
|
|
runFlag = false;
|
|
|
|
|
this->stop();
|
|
|
|
|
this->terminate();
|
|
|
|
|
processData.clear();
|
|
|
|
|
futures.clear();
|
2024-05-22 16:47:14 +08:00
|
|
|
}
|
2024-05-18 23:53:26 +08:00
|
|
|
////////////////DET//DATA//////////////////
|
|
|
|
|
sncodeCls::sncodeCls()
|
|
|
|
|
{
|
|
|
|
|
tcpStartPos = bridge.back.json.tcp.startPos;
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
void sncodeCls::checkSnFromWaittingVec(QByteArray byte)
|
|
|
|
|
{
|
|
|
|
|
while (clearSameSnRequestData() == false);
|
|
|
|
|
for (int i = 0; i < snWaittingVec.size(); i++)
|
|
|
|
|
{//第一位是hash,第二位是index
|
|
|
|
|
string SN = readSnFromQByteArray(byte, snWaittingVec[i].startpos, false, true, 0);
|
|
|
|
|
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
|
|
|
|
|
|
|
|
//FINSEDIT
|
2024-05-18 23:53:26 +08:00
|
|
|
string sncodeCls::readSnFromQByteArray(QByteArray byte, int dmPos,
|
|
|
|
|
bool highLowConversion, bool clearAfterRead, long long timeCode, int Size)
|
|
|
|
|
{
|
|
|
|
|
int triggerPos = (dmPos - tcpStartPos) * 2;
|
|
|
|
|
if (triggerPos < 0)
|
|
|
|
|
{
|
|
|
|
|
cout << "Read SN error,trigger is DM:" << dmPos << ",but read start at DM:" << tcpStartPos << endl;
|
|
|
|
|
return snErrorsnError;
|
|
|
|
|
}
|
|
|
|
|
QString str;
|
|
|
|
|
if (byte.at(triggerPos + 1) == 0 && byte.at(triggerPos) == 0)
|
|
|
|
|
return snErrorsnError;
|
|
|
|
|
int snCount = 0;
|
|
|
|
|
while (byte.at(snCount + triggerPos) != 0 || byte.at(snCount + triggerPos + 1) != 0)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (byte.at(snCount + triggerPos) == 0)
|
|
|
|
|
{
|
|
|
|
|
snCount++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
str += QChar(byte.at(snCount + triggerPos));
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
string sncodeCls::getData(unsigned long long timeCode)
|
|
|
|
|
{
|
|
|
|
|
for (int i = snResList.size() - 1; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
if (snResList[i].timecode == timeCode)
|
|
|
|
|
return snResList[i].sn;
|
|
|
|
|
}
|
|
|
|
|
return snErrorsnError;
|
|
|
|
|
}
|
|
|
|
|
string sncodeCls::waitForSn(unsigned long long timeCode, int timeOut)
|
|
|
|
|
{
|
|
|
|
|
int cnt = 0;
|
|
|
|
|
string sn = getData(timeCode);
|
|
|
|
|
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;
|
|
|
|
|
}
|