GlobalFuncThread.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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_NONE;
  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. /* 更新时间信息 */
  514. pFuncThreadInfo->StartTime = func.StartTime;
  515. pFuncThreadInfo->EndTime = func.EndTime;
  516. continue;
  517. }
  518. fa->appFunction = appFunc;
  519. fa->strFunctionName = "违禁品识别";
  520. m_listFuncThreadInfo.push_back(fa);
  521. }
  522. else if (AppFunction::APP_Illegal == appFunc)
  523. {
  524. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_Illegal);
  525. if(pFuncThreadInfo != nullptr)
  526. {
  527. /* 更新时间信息 */
  528. pFuncThreadInfo->StartTime = func.StartTime;
  529. pFuncThreadInfo->EndTime = func.EndTime;
  530. continue;
  531. }
  532. fa->appFunction = appFunc;
  533. fa->strFunctionName = "非法入侵检测";
  534. m_listFuncThreadInfo.push_back(fa);
  535. }
  536. else if (AppFunction::APP_Fatigue == appFunc)
  537. {
  538. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_Fatigue);
  539. if(pFuncThreadInfo != nullptr)
  540. {
  541. /* 更新时间信息 */
  542. pFuncThreadInfo->StartTime = func.StartTime;
  543. pFuncThreadInfo->EndTime = func.EndTime;
  544. continue;
  545. }
  546. fa->appFunction = appFunc;
  547. fa->strFunctionName = "疲劳检测";
  548. m_listFuncThreadInfo.push_back(fa);
  549. }
  550. else if (AppFunction::APP_Regional == appFunc)
  551. {
  552. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_Regional);
  553. if(pFuncThreadInfo != nullptr)
  554. {
  555. /* 更新时间信息 */
  556. pFuncThreadInfo->StartTime = func.StartTime;
  557. pFuncThreadInfo->EndTime = func.EndTime;
  558. continue;
  559. }
  560. fa->appFunction = appFunc;
  561. fa->strFunctionName = "区域人员检测";
  562. m_listFuncThreadInfo.push_back(fa);
  563. }
  564. else if (AppFunction::APP_Mouse == appFunc)
  565. {
  566. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_Mouse);
  567. if(pFuncThreadInfo != nullptr)
  568. {
  569. /* 更新时间信息 */
  570. pFuncThreadInfo->StartTime = func.StartTime;
  571. pFuncThreadInfo->EndTime = func.EndTime;
  572. continue;
  573. }
  574. fa->appFunction = appFunc;
  575. fa->strFunctionName = "老鼠识别";
  576. m_listFuncThreadInfo.push_back(fa);
  577. }
  578. else if (AppFunction::APP_PlayPhone == appFunc)
  579. {
  580. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_PlayPhone);
  581. if(pFuncThreadInfo != nullptr)
  582. {
  583. /* 更新时间信息 */
  584. pFuncThreadInfo->StartTime = func.StartTime;
  585. pFuncThreadInfo->EndTime = func.EndTime;
  586. continue;
  587. }
  588. fa->appFunction = appFunc;
  589. fa->strFunctionName = "玩手机识别";
  590. m_listFuncThreadInfo.push_back(fa);
  591. }
  592. else if (AppFunction::APP_NoMask == appFunc)
  593. {
  594. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_NoMask);
  595. if(pFuncThreadInfo != nullptr)
  596. {
  597. /* 更新时间信息 */
  598. pFuncThreadInfo->StartTime = func.StartTime;
  599. pFuncThreadInfo->EndTime = func.EndTime;
  600. continue;
  601. }
  602. fa->appFunction = appFunc;
  603. fa->strFunctionName = "未戴口罩识别";
  604. m_listFuncThreadInfo.push_back(fa);
  605. }
  606. else if (AppFunction::APP_AllDown == appFunc)
  607. {
  608. auto pFuncThreadInfo = findAppFunction(func.ChannelID, AppFunction::APP_AllDown);
  609. if(pFuncThreadInfo != nullptr)
  610. {
  611. /* 更新时间信息 */
  612. pFuncThreadInfo->StartTime = func.StartTime;
  613. pFuncThreadInfo->EndTime = func.EndTime;
  614. continue;
  615. }
  616. fa->appFunction = appFunc;
  617. fa->strFunctionName = "摔倒识别";
  618. m_listFuncThreadInfo.push_back(fa);
  619. }
  620. }
  621. return true;
  622. }
  623. /**
  624. * @brief 添加算法信息,根据传进来的算法ID,将其加入到对应的功能中
  625. * 根据功能需要,将算法ID加入到对应的功能中
  626. *
  627. * @param info
  628. * @return true
  629. * @return false
  630. */
  631. bool GlobalThreadInfo::addActionInfo(const ActionInfo& info)
  632. {
  633. if(info.ActionID.empty())
  634. {
  635. return false;
  636. }
  637. /* 人脸识别算法(人员在岗识别、非法入侵检测需要) */
  638. if(info.ActionID == GVariable.ActFaceIdentify)
  639. {
  640. /* 人员在岗识别 */
  641. setAppFunction(info, AppFunction::APP_OnWork);
  642. /* 非法入侵检测 */
  643. setAppFunction(info, AppFunction::APP_Illegal);
  644. }
  645. /* 人员计数 */
  646. else if (info.ActionID == GVariable.ActPersonCount)
  647. {
  648. /* 区域人员检测(人员计数?) */
  649. setAppFunction(info, AppFunction::APP_Regional);
  650. /* 非法入侵检测 */
  651. setAppFunction(info, AppFunction::APP_Illegal);
  652. /* 人员在岗识别 */
  653. setAppFunction(info, AppFunction::APP_OnWork);
  654. }
  655. /* 违禁物品 */
  656. else if (info.ActionID == GVariable.ActContraband)
  657. {
  658. setAppFunction(info, AppFunction::APP_Contraband);
  659. }
  660. /* 玩手机 */
  661. else if (info.ActionID == GVariable.ActPlayPhone)
  662. {
  663. setAppFunction(info, AppFunction::APP_PlayPhone);
  664. }
  665. /* 睡岗识别 */
  666. else if (info.ActionID == GVariable.ActSleep)
  667. {
  668. setAppFunction(info, AppFunction::APP_Fatigue);
  669. }
  670. /* 疲劳检测 */
  671. else if(info.ActionID == GVariable.ActFatigueDetection)
  672. {
  673. setAppFunction(info, AppFunction::APP_Fatigue);
  674. }
  675. /* 动物识别 */
  676. else if (info.ActionID == GVariable.ActAnimalDetect)
  677. {
  678. setAppFunction(info, AppFunction::APP_Mouse);
  679. }
  680. /* 老鼠识别 */
  681. else if (info.ActionID == GVariable.ActMouseDetect)
  682. {
  683. setAppFunction(info, AppFunction::APP_Mouse);
  684. }
  685. /* 口罩识别 */
  686. else if (info.ActionID == GVariable.ActNoMask)
  687. {
  688. setAppFunction(info, AppFunction::APP_NoMask);
  689. }
  690. else {
  691. SPDLOG_WARN("未知的算法ID: {}", info.ActionID);
  692. return false;
  693. }
  694. return true;
  695. }
  696. /**
  697. * @brief 清空无用的功能信息
  698. * 摄像机和算法信息为空的,或者运行状态为RUN_STATE_EXITCOMPLET,都会被清理掉
  699. *
  700. */
  701. void GlobalThreadInfo::clearNoneFuncThreadInfo()
  702. {
  703. for(auto it0 = m_listFuncThreadInfo.begin(); it0 != m_listFuncThreadInfo.end();)
  704. {
  705. if((*it0)->listRoomCamActInfo.empty() || ((*it0)->RunState == RunTimeState::RUN_STATE_EXITCOMPLET))
  706. {
  707. delete *it0;
  708. it0 = m_listFuncThreadInfo.erase(it0);
  709. }else {
  710. ++it0;
  711. }
  712. }
  713. }
  714. /* 清空算法列表(直接清空房间信息) */
  715. void GlobalThreadInfo::clearActionList()
  716. {
  717. for(auto& it0 : m_listFuncThreadInfo)
  718. {
  719. // for(auto& it1 : it0->listRoomCamActInfo)
  720. // {
  721. // it1.mapCameraAction.clear();
  722. // }
  723. it0->listRoomCamActInfo.clear();
  724. }
  725. }
  726. /* 设置没有配置摄像机的功能退出 */
  727. void GlobalThreadInfo::setNoneCameraFuncStop()
  728. {
  729. for(auto& it : m_listFuncThreadInfo)
  730. {
  731. if(it->listRoomCamActInfo.empty())
  732. {
  733. if(it->RunState == RunTimeState::RUN_STATE_NONE)
  734. {
  735. SPDLOG_INFO("频道:{:<15} 功能:{:<20}房间信息为空,无需开启线程",it->strChannelName, it->strFunctionName);
  736. }
  737. else if(it->RunState == RunTimeState::RUN_STATE_RUN)
  738. {
  739. SPDLOG_INFO("频道:{:<10}功能:{:<20}房间信息为空,停止线程",it->strChannelName, it->strFunctionName);
  740. }
  741. it->RunState = RunTimeState::RUN_STATE_STOP;
  742. }
  743. }
  744. }
  745. /* 查找应用信息 */
  746. bool GlobalThreadInfo::findAppFunction(const AppFunction func)
  747. {
  748. for(const auto& it0 : m_listFuncThreadInfo)
  749. {
  750. if(it0->appFunction == func)
  751. {
  752. return true;
  753. }
  754. }
  755. return false;
  756. }
  757. /* 根据频率和功能查找实例 */
  758. FuncThreadInfo* GlobalThreadInfo::findAppFunction(const int ChannelID, const AppFunction func)
  759. {
  760. for(const auto& it0 : m_listFuncThreadInfo)
  761. {
  762. if( (it0->appFunction == func) && (it0->ChannelID == ChannelID) )
  763. {
  764. return it0;
  765. }
  766. }
  767. return nullptr;
  768. }
  769. /**
  770. * @brief 查找这个应用信息
  771. *
  772. * @param func
  773. * @return FuncThreadInfo*
  774. */
  775. FuncThreadInfo* GlobalThreadInfo::findAppFunction(const FuncThreadInfo& func)
  776. {
  777. for(const auto& it0 : m_listFuncThreadInfo)
  778. {
  779. if(it0->ChannelID == func.ChannelID && it0->appFunction == func.appFunction)
  780. {
  781. return it0;
  782. }
  783. }
  784. return nullptr;
  785. }
  786. /* 设置应用信息 */
  787. void GlobalThreadInfo::setAppFunction(const ActionInfo& info, const AppFunction func)
  788. {
  789. auto pFuncThreadInfo = findAppFunction(info.ChannelID, func);
  790. if(pFuncThreadInfo != nullptr)
  791. {
  792. pFuncThreadInfo->addActionInfo(info);
  793. pFuncThreadInfo->RunState = RunTimeState::RUN_STATE_INIT;
  794. }
  795. }
  796. /* 设置线程状态 */
  797. void GlobalThreadInfo::setThreadState(FuncThreadInfo* pInfo, RunTimeState state)
  798. {
  799. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  800. auto p = findAppFunction(*pInfo);
  801. if(p != nullptr)
  802. {
  803. p->RunState = state;
  804. }
  805. }
  806. void GlobalThreadInfo::setThreadState(FuncThreadInfo& pInfo, RunTimeState state)
  807. {
  808. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  809. auto p = findAppFunction(pInfo);
  810. if(p != nullptr)
  811. {
  812. p->RunState = state;
  813. }
  814. }
  815. /**
  816. * @brief 功能线程更新功能信息,这里是从内存中的数组里获取
  817. *
  818. * @param pInfo
  819. * @return true
  820. * @return false
  821. */
  822. bool GlobalThreadInfo::updateFuncInfo(FuncThreadInfo* pInfo)
  823. {
  824. pInfo->clearActionList();
  825. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  826. auto fa = findAppFunction(*pInfo);
  827. if(fa == nullptr)
  828. {
  829. return false;
  830. }
  831. *pInfo = *fa;
  832. return true;
  833. }
  834. bool GlobalThreadInfo::updateFuncInfo(FuncThreadInfo& pInfo)
  835. {
  836. /* 清空算法列表 */
  837. pInfo.clearActionList();
  838. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  839. auto fa = findAppFunction(pInfo);
  840. if(fa == nullptr)
  841. {
  842. return false;
  843. }
  844. pInfo = *fa;
  845. return true;
  846. }
  847. /* 更新频率信息 */
  848. // void GlobalThreadInfo::setChannelInfo(std::map<int, std::string> mapChannelName)
  849. // {
  850. // }
  851. /* 设置摄像机信息 */
  852. void GlobalThreadInfo::setCameraInfo(std::map<int, std::string> mapCameraName)
  853. {
  854. for(auto& it : m_listFuncThreadInfo)
  855. {
  856. it->mapCameraName.clear();
  857. it->mapCameraName = mapCameraName;
  858. }
  859. }