GlobalFuncThread.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. #include "GlobalFuncThread.h"
  2. #include "GlobalConfig.h"
  3. #include "UniversalFunc.h"
  4. #include "spdlog/spdlog.h"
  5. FuncThreadInfo::FuncThreadInfo()
  6. {
  7. ChannelID = -1;
  8. strChannelName = "";
  9. appFunction = AppFunction::APP_NONE;
  10. RunState = RunTimeState::RUN_STATE_NONE;
  11. strFunctionName = "";
  12. StartTime = QDateTime::currentDateTime();
  13. EndTime = QDateTime::currentDateTime();
  14. listRoomCamActInfo.clear();
  15. mapCameraName.clear();
  16. }
  17. FuncThreadInfo::~FuncThreadInfo()
  18. {
  19. }
  20. FuncThreadInfo& FuncThreadInfo::operator=(FuncThreadInfo& other)
  21. {
  22. if(this != &other)
  23. {
  24. ChannelID = other.ChannelID;
  25. strChannelName = other.strChannelName;
  26. appFunction = other.appFunction;
  27. RunState = other.RunState;
  28. strFunctionName = other.strFunctionName;
  29. StartTime = other.StartTime;
  30. EndTime = other.EndTime;
  31. listRoomCamActInfo = other.listRoomCamActInfo;
  32. mapCameraName = other.mapCameraName;
  33. }
  34. return *this;
  35. }
  36. /**
  37. * @brief 添加算法信息,这里不能直接创建房间,需要先判断有没有已有的房间,因为有的应用功能可能会包含多个房间
  38. * 已经提前被创建过了
  39. *
  40. * @param info
  41. * @return true
  42. * @return false
  43. */
  44. bool FuncThreadInfo::addActionInfo(const ActionInfo& info)
  45. {
  46. /* 根据此类的功能,添加算法信息 */
  47. if(appFunction == AppFunction::APP_NONE)
  48. {
  49. return false;
  50. }
  51. /* 将其添加到对应的房间 */
  52. bool isFind = false;
  53. for(auto& roomInfo : listRoomCamActInfo)
  54. {
  55. if((roomInfo.RoomID == info.RoomID) && (roomInfo.RoomType == info.RoomType))
  56. {
  57. isFind = true;
  58. // roomInfo.mapCameraAction.insert(std::make_pair(info.CameraID, info.ActionID));
  59. roomInfo.addCameraAction(info.CameraID, info.ActionID);
  60. break;
  61. }
  62. }
  63. /* 没找到这个房间,就创建 */
  64. if(!isFind)
  65. {
  66. RoomCamActInfo roomCamActInfo;
  67. roomCamActInfo.RoomID = info.RoomID;
  68. roomCamActInfo.RoomType = info.RoomType;
  69. roomCamActInfo.strRoomName = info.strRoomName;
  70. roomCamActInfo.addCameraAction(info.CameraID, info.ActionID);
  71. listRoomCamActInfo.push_back(roomCamActInfo);
  72. }
  73. return true;
  74. }
  75. /* 清空算法信息 */
  76. void FuncThreadInfo::clearActionList()
  77. {
  78. listRoomCamActInfo.clear();
  79. }
  80. /* 获取摄像机名称 */
  81. std::string FuncThreadInfo::getCameraName(int CameraID)
  82. {
  83. auto it = mapCameraName.find(CameraID);
  84. if(it != mapCameraName.end())
  85. {
  86. return it->second;
  87. }
  88. return "";
  89. }
  90. /**
  91. * @brief 添加应用信息,一个应用功能在一个频道内只有一个实例
  92. * 这里是添加应用功能和时间段信息
  93. *
  94. * @param func
  95. * @return true
  96. * @return false
  97. */
  98. bool ListFuncActInfo::addFuncThreadInfo(const AppAndTimeInfo& func)
  99. {
  100. if(func.AppType == 0)
  101. {
  102. return false;
  103. }
  104. /* 解出这条信息里包含几个App,AppType按位计算,总共8个应用信息 */
  105. for(int i = 0; i < 8; ++i)
  106. {
  107. if(func.AppType & 0x01)
  108. {
  109. /* 查找有没有这个应用 */
  110. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_OnWork);
  111. if(pFuncThreadInfo != nullptr)
  112. {
  113. /* 更新时间信息 */
  114. pFuncThreadInfo->StartTime = func.StartTime;
  115. pFuncThreadInfo->EndTime = func.EndTime;
  116. continue;
  117. }
  118. FuncThreadInfo* fa = new FuncThreadInfo;
  119. fa->ChannelID = func.ChannelID;
  120. fa->appFunction = AppFunction::APP_OnWork;
  121. fa->strFunctionName = "人员在岗识别";
  122. fa->StartTime = func.StartTime;
  123. fa->EndTime = func.EndTime;
  124. listFuncThreadInfo.push_back(fa);
  125. }
  126. else if(func.AppType & 0x02)
  127. {
  128. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_Contraband);
  129. if(pFuncThreadInfo != nullptr)
  130. {
  131. /* 更新时间信息 */
  132. pFuncThreadInfo->StartTime = func.StartTime;
  133. pFuncThreadInfo->EndTime = func.EndTime;
  134. continue;
  135. }
  136. FuncThreadInfo* fa = new FuncThreadInfo;
  137. fa->ChannelID = func.ChannelID;
  138. fa->appFunction = AppFunction::APP_Contraband;
  139. fa->strFunctionName = "违禁品识别";
  140. fa->StartTime = func.StartTime;
  141. fa->EndTime = func.EndTime;
  142. listFuncThreadInfo.push_back(fa);
  143. }
  144. else if (func.AppType & 0x04)
  145. {
  146. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_Illegal);
  147. if(pFuncThreadInfo != nullptr)
  148. {
  149. /* 更新时间信息 */
  150. pFuncThreadInfo->StartTime = func.StartTime;
  151. pFuncThreadInfo->EndTime = func.EndTime;
  152. continue;
  153. }
  154. FuncThreadInfo* fa = new FuncThreadInfo;
  155. fa->ChannelID = func.ChannelID;
  156. fa->appFunction = AppFunction::APP_Illegal;
  157. fa->strFunctionName = "非法入侵检测";
  158. fa->StartTime = func.StartTime;
  159. fa->EndTime = func.EndTime;
  160. listFuncThreadInfo.push_back(fa);
  161. }
  162. else if (func.AppType & 0x08)
  163. {
  164. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_Fatigue);
  165. if(pFuncThreadInfo != nullptr)
  166. {
  167. /* 更新时间信息 */
  168. pFuncThreadInfo->StartTime = func.StartTime;
  169. pFuncThreadInfo->EndTime = func.EndTime;
  170. continue;
  171. }
  172. FuncThreadInfo* fa = new FuncThreadInfo;
  173. fa->ChannelID = func.ChannelID;
  174. fa->appFunction = AppFunction::APP_Fatigue;
  175. fa->strFunctionName = "疲劳检测";
  176. fa->StartTime = func.StartTime;
  177. fa->EndTime = func.EndTime;
  178. listFuncThreadInfo.push_back(fa);
  179. }
  180. else if (func.AppType & 0x10)
  181. {
  182. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_Regional);
  183. if(pFuncThreadInfo != nullptr)
  184. {
  185. /* 更新时间信息 */
  186. pFuncThreadInfo->StartTime = func.StartTime;
  187. pFuncThreadInfo->EndTime = func.EndTime;
  188. continue;
  189. }
  190. FuncThreadInfo* fa = new FuncThreadInfo;
  191. fa->ChannelID = func.ChannelID;
  192. fa->appFunction = AppFunction::APP_Regional;
  193. fa->strFunctionName = "区域人员检测";
  194. fa->StartTime = func.StartTime;
  195. fa->EndTime = func.EndTime;
  196. listFuncThreadInfo.push_back(fa);
  197. }
  198. else if (func.AppType & 0x20)
  199. {
  200. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_Mouse);
  201. if(pFuncThreadInfo != nullptr)
  202. {
  203. /* 更新时间信息 */
  204. pFuncThreadInfo->StartTime = func.StartTime;
  205. pFuncThreadInfo->EndTime = func.EndTime;
  206. continue;
  207. }
  208. FuncThreadInfo* fa = new FuncThreadInfo;
  209. fa->ChannelID = func.ChannelID;
  210. fa->appFunction = AppFunction::APP_Mouse;
  211. fa->strFunctionName = "老鼠识别";
  212. fa->StartTime = func.StartTime;
  213. fa->EndTime = func.EndTime;
  214. listFuncThreadInfo.push_back(fa);
  215. }
  216. else if (func.AppType & 0x40)
  217. {
  218. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_PlayPhone);
  219. if(pFuncThreadInfo != nullptr)
  220. {
  221. /* 更新时间信息 */
  222. pFuncThreadInfo->StartTime = func.StartTime;
  223. pFuncThreadInfo->EndTime = func.EndTime;
  224. continue;
  225. }
  226. FuncThreadInfo* fa = new FuncThreadInfo;
  227. fa->ChannelID = func.ChannelID;
  228. fa->appFunction = AppFunction::APP_PlayPhone;
  229. fa->strFunctionName = "玩手机识别";
  230. fa->StartTime = func.StartTime;
  231. fa->EndTime = func.EndTime;
  232. listFuncThreadInfo.push_back(fa);
  233. }
  234. else if (func.AppType & 0x80)
  235. {
  236. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_NoMask);
  237. if(pFuncThreadInfo != nullptr)
  238. {
  239. /* 更新时间信息 */
  240. pFuncThreadInfo->StartTime = func.StartTime;
  241. pFuncThreadInfo->EndTime = func.EndTime;
  242. continue;
  243. }
  244. FuncThreadInfo* fa = new FuncThreadInfo;
  245. fa->ChannelID = func.ChannelID;
  246. fa->appFunction = AppFunction::APP_NoMask;
  247. fa->strFunctionName = "未戴口罩识别";
  248. fa->StartTime = func.StartTime;
  249. fa->EndTime = func.EndTime;
  250. listFuncThreadInfo.push_back(fa);
  251. }
  252. else if (func.AppType & 0x0100)
  253. {
  254. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_AllDown);
  255. if(pFuncThreadInfo != nullptr)
  256. {
  257. /* 更新时间信息 */
  258. pFuncThreadInfo->StartTime = func.StartTime;
  259. pFuncThreadInfo->EndTime = func.EndTime;
  260. continue;
  261. }
  262. FuncThreadInfo* fa = new FuncThreadInfo;
  263. fa->ChannelID = func.ChannelID;
  264. fa->appFunction = AppFunction::APP_AllDown;
  265. fa->strFunctionName = "摔倒识别";
  266. fa->StartTime = func.StartTime;
  267. fa->EndTime = func.EndTime;
  268. listFuncThreadInfo.push_back(fa);
  269. }
  270. }
  271. return true;
  272. }
  273. /**
  274. * @brief 添加算法信息,根据传进来的算法ID,将其加入到对应的功能中
  275. *
  276. * @param info
  277. * @return true
  278. * @return false
  279. */
  280. bool ListFuncActInfo::addActionInfo(const ActionInfo& info)
  281. {
  282. if(info.ActionID.empty())
  283. {
  284. return false;
  285. }
  286. /* 人脸识别算法(人员在岗识别、非法入侵检测需要) */
  287. if(info.ActionID == GVariable.ActFaceIdentify)
  288. {
  289. /* 人员在岗识别 */
  290. auto pFuncThreadInfo = findAppFunction(info.ChannelID, AppFunction::APP_OnWork);
  291. if(pFuncThreadInfo != nullptr)
  292. {
  293. pFuncThreadInfo->addActionInfo(info);
  294. }
  295. /* 非法入侵检测 */
  296. pFuncThreadInfo = findAppFunction(info.ChannelID, AppFunction::APP_Illegal);
  297. if(pFuncThreadInfo != nullptr)
  298. {
  299. pFuncThreadInfo->addActionInfo(info);
  300. }
  301. }
  302. /* 人员计数 */
  303. else if (info.ActionID == GVariable.ActPersonCount)
  304. {
  305. auto pFuncThreadInfo = findAppFunction(info.ChannelID, AppFunction::APP_Regional);
  306. if(pFuncThreadInfo != nullptr)
  307. {
  308. pFuncThreadInfo->addActionInfo(info);
  309. }
  310. }
  311. /* 违禁物品 */
  312. else if (info.ActionID == GVariable.ActContraband)
  313. {
  314. auto pFuncThreadInfo = findAppFunction(info.ChannelID, AppFunction::APP_Contraband);
  315. if(pFuncThreadInfo != nullptr)
  316. {
  317. pFuncThreadInfo->addActionInfo(info);
  318. }
  319. }
  320. /* 玩手机 */
  321. else if (info.ActionID == GVariable.ActPlayPhone)
  322. {
  323. auto pFuncThreadInfo = findAppFunction(info.ChannelID, AppFunction::APP_PlayPhone);
  324. if(pFuncThreadInfo != nullptr)
  325. {
  326. pFuncThreadInfo->addActionInfo(info);
  327. }
  328. }
  329. /* 睡岗识别 */
  330. else if (info.ActionID == GVariable.ActSleep)
  331. {
  332. auto pFuncThreadInfo = findAppFunction(info.ChannelID, AppFunction::APP_Fatigue);
  333. if(pFuncThreadInfo != nullptr)
  334. {
  335. pFuncThreadInfo->addActionInfo(info);
  336. }
  337. }
  338. /* 疲劳检测 */
  339. else if(info.ActionID == GVariable.ActFatigueDetection)
  340. {
  341. auto pFuncThreadInfo = findAppFunction(info.ChannelID, AppFunction::APP_Fatigue);
  342. if(pFuncThreadInfo != nullptr)
  343. {
  344. pFuncThreadInfo->addActionInfo(info);
  345. }
  346. }
  347. /* 动物识别 */
  348. else if (info.ActionID == GVariable.ActAnimalDetect)
  349. {
  350. auto pFuncThreadInfo = findAppFunction(info.ChannelID, AppFunction::APP_Mouse);
  351. if(pFuncThreadInfo != nullptr)
  352. {
  353. pFuncThreadInfo->addActionInfo(info);
  354. }
  355. }
  356. /* 老鼠识别 */
  357. else if (info.ActionID == GVariable.ActMouseDetect)
  358. {
  359. auto pFuncThreadInfo = findAppFunction(info.ChannelID, AppFunction::APP_Mouse);
  360. if(pFuncThreadInfo != nullptr)
  361. {
  362. pFuncThreadInfo->addActionInfo(info);
  363. }
  364. }
  365. /* 口罩识别 */
  366. else if (info.ActionID == GVariable.ActNoMask)
  367. {
  368. auto pFuncThreadInfo = findAppFunction(info.ChannelID, AppFunction::APP_NoMask);
  369. if(pFuncThreadInfo != nullptr)
  370. {
  371. pFuncThreadInfo->addActionInfo(info);
  372. }
  373. }
  374. else {
  375. SPDLOG_WARN("未知的算法ID: {}", info.ActionID);
  376. return false;
  377. }
  378. return true;
  379. }
  380. /**
  381. * @brief 清空无用的功能信息
  382. * 摄像机和算法信息为空的,或者运行状态为STOP,都会被清理掉
  383. *
  384. */
  385. void ListFuncActInfo::clearNoneFuncThreadInfo()
  386. {
  387. for(auto it0 = listFuncThreadInfo.begin(); it0 != listFuncThreadInfo.end();)
  388. {
  389. if((*it0)->listRoomCamActInfo.empty() || ((*it0)->RunState == RunTimeState::RUN_STATE_STOP))
  390. {
  391. delete *it0;
  392. it0 = listFuncThreadInfo.erase(it0);
  393. }else {
  394. ++it0;
  395. }
  396. }
  397. }
  398. /* 清空算法列表 */
  399. void ListFuncActInfo::clearActionList()
  400. {
  401. for(auto& it0 : listFuncThreadInfo)
  402. {
  403. it0->listRoomCamActInfo.clear();
  404. }
  405. }
  406. /* 查找应用信息 */
  407. bool ListFuncActInfo::findAppFunction(const AppFunction func)
  408. {
  409. for(const auto& it0 : listFuncThreadInfo)
  410. {
  411. if(it0->appFunction == func)
  412. {
  413. return true;
  414. }
  415. }
  416. return false;
  417. }
  418. /* 根据频率和功能查找实例 */
  419. FuncThreadInfo* ListFuncActInfo::findAppFunction(const int ChannelID, const AppFunction func)
  420. {
  421. for(const auto& it0 : listFuncThreadInfo)
  422. {
  423. if( (it0->appFunction == func) && (it0->ChannelID == ChannelID) )
  424. {
  425. return it0;
  426. }
  427. }
  428. return nullptr;
  429. }
  430. /**
  431. * @brief 查找这个应用信息
  432. *
  433. * @param func
  434. * @return FuncThreadInfo*
  435. */
  436. FuncThreadInfo* ListFuncActInfo::findAppFunction(const FuncThreadInfo& func)
  437. {
  438. for(const auto& it0 : listFuncThreadInfo)
  439. {
  440. if(it0->ChannelID == func.ChannelID && it0->appFunction == func.appFunction)
  441. {
  442. return it0;
  443. }
  444. }
  445. return nullptr;
  446. }
  447. /* ====================================================================================
  448. * ************************ GlobalThreadInfo成员函数 *****************************
  449. * ====================================================================================*/
  450. /* 获取线程运行标志位 */
  451. bool GlobalThreadInfo::getRunning() const
  452. {
  453. return m_bRunning;
  454. }
  455. /* 设置线程运行标志位 */
  456. void GlobalThreadInfo::setRunning(bool bRunning)
  457. {
  458. m_bRunning = bRunning;
  459. }
  460. /* 手动给功能块列表加锁 */
  461. void GlobalThreadInfo::lockRunFAI()
  462. {
  463. m_mutexRunFAI.lock();
  464. }
  465. /* 手动解锁 */
  466. void GlobalThreadInfo::unlockRunFAI()
  467. {
  468. m_mutexRunFAI.unlock();
  469. }
  470. /**
  471. * @brief 添加应用信息,一个应用功能在一个频道内只有一个实例
  472. * 这里是添加应用功能,检测时间段在每个功能线程内部自己获取
  473. *
  474. * @param func
  475. * @return true
  476. * @return false
  477. */
  478. bool GlobalThreadInfo::addFuncThreadInfo(const AppAndTimeInfo& func)
  479. {
  480. if(func.AppType == 0)
  481. {
  482. return false;
  483. }
  484. /* 解出这条信息里包含几个App,AppType按位计算,总共8个应用信息 */
  485. for(int i = 0; i < 8; ++i)
  486. {
  487. FuncThreadInfo* fa = new FuncThreadInfo;
  488. fa->ChannelID = func.ChannelID;
  489. fa->StartTime = func.StartTime;
  490. fa->EndTime = func.EndTime;
  491. fa->RunState = RunTimeState::RUN_STATE_INIT;
  492. AppFunction appFunc = getAppFunctionID(func.AppType);
  493. if(AppFunction::APP_OnWork == appFunc)
  494. {
  495. /* 查找有没有这个应用 */
  496. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_OnWork);
  497. if(pFuncThreadInfo != nullptr)
  498. {
  499. /* 更新时间信息 */
  500. // pFuncThreadInfo->StartTime = func.StartTime;
  501. // pFuncThreadInfo->EndTime = func.EndTime;
  502. continue;
  503. }
  504. fa->appFunction = appFunc;
  505. fa->strFunctionName = "人员在岗识别";
  506. m_listFuncThreadInfo.push_back(fa);
  507. }
  508. else if(AppFunction::APP_Contraband == appFunc)
  509. {
  510. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_Contraband);
  511. if(pFuncThreadInfo != nullptr)
  512. {
  513. continue;
  514. }
  515. fa->appFunction = appFunc;
  516. fa->strFunctionName = "违禁品识别";
  517. m_listFuncThreadInfo.push_back(fa);
  518. }
  519. else if (AppFunction::APP_Illegal == appFunc)
  520. {
  521. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_Illegal);
  522. if(pFuncThreadInfo != nullptr)
  523. {
  524. continue;
  525. }
  526. fa->appFunction = appFunc;
  527. fa->strFunctionName = "非法入侵检测";
  528. m_listFuncThreadInfo.push_back(fa);
  529. }
  530. else if (AppFunction::APP_Fatigue == appFunc)
  531. {
  532. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_Fatigue);
  533. if(pFuncThreadInfo != nullptr)
  534. {
  535. continue;
  536. }
  537. fa->appFunction = appFunc;
  538. fa->strFunctionName = "疲劳检测";
  539. m_listFuncThreadInfo.push_back(fa);
  540. }
  541. else if (AppFunction::APP_Regional == appFunc)
  542. {
  543. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_Regional);
  544. if(pFuncThreadInfo != nullptr)
  545. {
  546. continue;
  547. }
  548. fa->appFunction = appFunc;
  549. fa->strFunctionName = "区域人员检测";
  550. m_listFuncThreadInfo.push_back(fa);
  551. }
  552. else if (AppFunction::APP_Mouse == appFunc)
  553. {
  554. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_Mouse);
  555. if(pFuncThreadInfo != nullptr)
  556. {
  557. continue;
  558. }
  559. fa->appFunction = appFunc;
  560. fa->strFunctionName = "老鼠识别";
  561. m_listFuncThreadInfo.push_back(fa);
  562. }
  563. else if (AppFunction::APP_PlayPhone == appFunc)
  564. {
  565. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_PlayPhone);
  566. if(pFuncThreadInfo != nullptr)
  567. {
  568. continue;
  569. }
  570. fa->appFunction = appFunc;
  571. fa->strFunctionName = "玩手机识别";
  572. m_listFuncThreadInfo.push_back(fa);
  573. }
  574. else if (AppFunction::APP_NoMask == appFunc)
  575. {
  576. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_NoMask);
  577. if(pFuncThreadInfo != nullptr)
  578. {
  579. continue;
  580. }
  581. fa->appFunction = appFunc;
  582. fa->strFunctionName = "未戴口罩识别";
  583. m_listFuncThreadInfo.push_back(fa);
  584. }
  585. else if (AppFunction::APP_AllDown == appFunc)
  586. {
  587. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_AllDown);
  588. if(pFuncThreadInfo != nullptr)
  589. {
  590. continue;
  591. }
  592. fa->appFunction = appFunc;
  593. fa->strFunctionName = "摔倒识别";
  594. m_listFuncThreadInfo.push_back(fa);
  595. }
  596. else {
  597. SPDLOG_WARN("未知的算法ID: {}", func.AppType);
  598. delete fa;
  599. return false;
  600. }
  601. }
  602. return true;
  603. }
  604. /**
  605. * @brief 添加算法信息,根据传进来的算法ID,将其加入到对应的功能中
  606. * 根据功能需要,将算法ID加入到对应的功能中
  607. *
  608. * @param info
  609. * @return true
  610. * @return false
  611. */
  612. bool GlobalThreadInfo::addActionInfo(const ActionInfo& info)
  613. {
  614. if(info.ActionID.empty())
  615. {
  616. return false;
  617. }
  618. /* 人脸识别算法(人员在岗识别、非法入侵检测需要) */
  619. if(info.ActionID == GVariable.ActFaceIdentify)
  620. {
  621. /* 人员在岗识别 */
  622. setAppFunction(info, AppFunction::APP_OnWork);
  623. /* 非法入侵检测 */
  624. setAppFunction(info, AppFunction::APP_Illegal);
  625. }
  626. /* 人员计数 */
  627. else if (info.ActionID == GVariable.ActPersonCount)
  628. {
  629. /* 区域人员检测(人员计数?) */
  630. setAppFunction(info, AppFunction::APP_Regional);
  631. /* 非法入侵检测 */
  632. setAppFunction(info, AppFunction::APP_Illegal);
  633. /* 人员在岗识别 */
  634. setAppFunction(info, AppFunction::APP_OnWork);
  635. }
  636. /* 违禁物品 */
  637. else if (info.ActionID == GVariable.ActContraband)
  638. {
  639. setAppFunction(info, AppFunction::APP_Contraband);
  640. }
  641. /* 玩手机 */
  642. else if (info.ActionID == GVariable.ActPlayPhone)
  643. {
  644. setAppFunction(info, AppFunction::APP_PlayPhone);
  645. }
  646. /* 睡岗识别 */
  647. else if (info.ActionID == GVariable.ActSleep)
  648. {
  649. setAppFunction(info, AppFunction::APP_Fatigue);
  650. }
  651. /* 疲劳检测 */
  652. else if(info.ActionID == GVariable.ActFatigueDetection)
  653. {
  654. setAppFunction(info, AppFunction::APP_Fatigue);
  655. }
  656. /* 动物识别 */
  657. else if (info.ActionID == GVariable.ActAnimalDetect)
  658. {
  659. setAppFunction(info, AppFunction::APP_Mouse);
  660. }
  661. /* 老鼠识别 */
  662. else if (info.ActionID == GVariable.ActMouseDetect)
  663. {
  664. setAppFunction(info, AppFunction::APP_Mouse);
  665. }
  666. /* 口罩识别 */
  667. else if (info.ActionID == GVariable.ActNoMask)
  668. {
  669. setAppFunction(info, AppFunction::APP_NoMask);
  670. }
  671. else {
  672. SPDLOG_WARN("未知的算法ID: {}", info.ActionID);
  673. return false;
  674. }
  675. return true;
  676. }
  677. /**
  678. * @brief 清空无用的功能信息
  679. * 摄像机和算法信息为空的,或者运行状态为RUN_STATE_EXITCOMPLET,都会被清理掉
  680. *
  681. */
  682. void GlobalThreadInfo::clearNoneFuncThreadInfo()
  683. {
  684. for(auto it0 = m_listFuncThreadInfo.begin(); it0 != m_listFuncThreadInfo.end();)
  685. {
  686. if((*it0)->listRoomCamActInfo.empty() || ((*it0)->RunState == RunTimeState::RUN_STATE_EXITCOMPLET))
  687. {
  688. delete *it0;
  689. it0 = m_listFuncThreadInfo.erase(it0);
  690. }else {
  691. ++it0;
  692. }
  693. }
  694. }
  695. /* 清空算法列表(直接清空房间信息) */
  696. void GlobalThreadInfo::clearActionList()
  697. {
  698. for(auto& it0 : m_listFuncThreadInfo)
  699. {
  700. // for(auto& it1 : it0->listRoomCamActInfo)
  701. // {
  702. // it1.mapCameraAction.clear();
  703. // }
  704. it0->listRoomCamActInfo.clear();
  705. }
  706. }
  707. /* 设置没有配置摄像机的功能退出 */
  708. void GlobalThreadInfo::setNoneCameraFuncStop()
  709. {
  710. for(auto& it : m_listFuncThreadInfo)
  711. {
  712. if(it->listRoomCamActInfo.empty())
  713. {
  714. if(it->RunState == RunTimeState::RUN_STATE_NONE)
  715. {
  716. SPDLOG_INFO("频道:{:<15} 功能:{:<20}房间信息为空,无需开启线程",it->strChannelName, it->strFunctionName);
  717. }
  718. else if(it->RunState == RunTimeState::RUN_STATE_RUN)
  719. {
  720. SPDLOG_INFO("频道:{:<10}功能:{:<20}房间信息为空,停止线程",it->strChannelName, it->strFunctionName);
  721. }
  722. it->RunState = RunTimeState::RUN_STATE_STOP;
  723. }
  724. }
  725. }
  726. /* 查找应用信息 */
  727. bool GlobalThreadInfo::findAppFunction(const AppFunction func)
  728. {
  729. for(const auto& it0 : m_listFuncThreadInfo)
  730. {
  731. if(it0->appFunction == func)
  732. {
  733. return true;
  734. }
  735. }
  736. return false;
  737. }
  738. /* 根据频率和功能查找实例 */
  739. FuncThreadInfo* GlobalThreadInfo::findAppFunction(const int ChannelID, const AppFunction func)
  740. {
  741. for(const auto& it0 : m_listFuncThreadInfo)
  742. {
  743. if( (it0->appFunction == func) && (it0->ChannelID == ChannelID) )
  744. {
  745. return it0;
  746. }
  747. }
  748. return nullptr;
  749. }
  750. /**
  751. * @brief 查找这个应用信息
  752. *
  753. * @param func
  754. * @return FuncThreadInfo*
  755. */
  756. FuncThreadInfo* GlobalThreadInfo::findAppFunction(const FuncThreadInfo& func)
  757. {
  758. for(const auto& it0 : m_listFuncThreadInfo)
  759. {
  760. if(it0->ChannelID == func.ChannelID && it0->appFunction == func.appFunction)
  761. {
  762. return it0;
  763. }
  764. }
  765. return nullptr;
  766. }
  767. /* 设置应用信息 */
  768. void GlobalThreadInfo::setAppFunction(const ActionInfo& info, const AppFunction func)
  769. {
  770. auto pFuncThreadInfo = findAppFunction(info.ChannelID, func);
  771. if(pFuncThreadInfo != nullptr)
  772. {
  773. pFuncThreadInfo->addActionInfo(info);
  774. }
  775. }
  776. /* 设置线程状态 */
  777. void GlobalThreadInfo::setThreadState(FuncThreadInfo* pInfo, RunTimeState state)
  778. {
  779. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  780. auto p = findAppFunction(*pInfo);
  781. if(p != nullptr)
  782. {
  783. p->RunState = state;
  784. }
  785. }
  786. void GlobalThreadInfo::setThreadState(FuncThreadInfo& pInfo, RunTimeState state)
  787. {
  788. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  789. auto p = findAppFunction(pInfo);
  790. if(p != nullptr)
  791. {
  792. pInfo.RunState = state;
  793. p->RunState = state;
  794. }
  795. }
  796. /**
  797. * @brief 功能线程更新功能信息,这里是从内存中的数组里获取
  798. *
  799. * @param pInfo
  800. * @return true
  801. * @return false
  802. */
  803. bool GlobalThreadInfo::updateFuncInfo(FuncThreadInfo* pInfo)
  804. {
  805. pInfo->clearActionList();
  806. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  807. auto fa = findAppFunction(*pInfo);
  808. if(fa == nullptr)
  809. {
  810. return false;
  811. }
  812. *pInfo = *fa;
  813. return true;
  814. }
  815. bool GlobalThreadInfo::updateFuncInfo(FuncThreadInfo& pInfo)
  816. {
  817. /* 清空算法列表 */
  818. pInfo.clearActionList();
  819. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  820. auto fa = findAppFunction(pInfo);
  821. if(fa == nullptr)
  822. {
  823. return false;
  824. }
  825. pInfo = *fa;
  826. return true;
  827. }
  828. /* 更新频率信息 */
  829. // void GlobalThreadInfo::setChannelInfo(std::map<int, std::string> mapChannelName)
  830. // {
  831. // }
  832. /* 设置摄像机信息 */
  833. void GlobalThreadInfo::setCameraInfo(std::map<int, std::string> mapCameraName)
  834. {
  835. for(auto& it : m_listFuncThreadInfo)
  836. {
  837. it->mapCameraName.clear();
  838. it->mapCameraName = mapCameraName;
  839. }
  840. }