GlobalConfig.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. #include "GlobalConfig.h"
  2. #include <QSettings>
  3. #include "spdlog/spdlog.h"
  4. /* ====================================================================================
  5. * ************************** GlobalConfig成员函数 ******************************
  6. * ====================================================================================*/
  7. GlobalConfig::GlobalConfig()
  8. {
  9. ThreadSleepMS = 300;
  10. }
  11. /* 读取配置文件 */
  12. bool GlobalConfig::readConfig(const QString& strConfigFile)
  13. {
  14. if(strConfigFile.isEmpty())
  15. {
  16. SPDLOG_ERROR("读取配置文件失败! 配置文件名为空");
  17. return false;
  18. }
  19. SPDLOG_DEBUG("读取配置文件: {}", strConfigFile.toStdString());
  20. QSettings settings(strConfigFile, QSettings::IniFormat);
  21. settings.setIniCodec("UTF-8");
  22. settings.beginGroup("System");
  23. AppUpdateOnWorkTimeInterval_Time = settings.value("WorkOnInfoSecond", 600).toInt(); /* 更新在岗信息的时间间隔 */
  24. AppPeopleOnWork_Time = settings.value("APPPEPOLEONWORK", 300).toInt(); /* 离岗时间 */
  25. AppContraband_Time = settings.value("APPBADTHING", 50).toInt(); /* 违禁物品出现的时间 */
  26. AppBadMan = settings.value("APPBADMAN", 50).toInt(); /* 非法入侵 */
  27. AppTired_Time = settings.value("APPTIRED", 50).toInt(); /* 疲劳检测时间 */
  28. AppPeopleCont = settings.value("APPPEPOLECONT", 50).toInt(); /* 人员聚集时间 */
  29. AppPlayPhone = settings.value("APPPLAYPHONE", 50).toInt(); /* 玩手机识别 */
  30. AppMouse = settings.value("APPMOUSE", 50).toInt(); /* 手势识别 */
  31. AppMask = settings.value("APPMASK", 5).toInt(); /* 戴口罩识别 */
  32. CheckSet = settings.value("CHECKSET", 300).toInt(); /* 服务端多久检测一次配置 */
  33. EventTimeValid = settings.value("EventTimeValid", 300).toInt(); /* 事件时间有效期 */
  34. Key = settings.value("Key").toString().toStdString(); /* Key */
  35. Secret = settings.value("Secret").toString().toStdString(); /* Secret */
  36. settings.endGroup();
  37. if(Key.empty() || Secret.empty())
  38. {
  39. SPDLOG_ERROR("读取配置文件失败! Key或Secret为空");
  40. return false;
  41. }
  42. return true;
  43. }
  44. /* 打印读取到的值 */
  45. void GlobalConfig::printValue()
  46. {
  47. SPDLOG_INFO("APPPEPOLEONWORK: {}", AppPeopleOnWork_Time);
  48. SPDLOG_INFO("APPBADTHING: {}", AppContraband_Time);
  49. SPDLOG_INFO("APPBADMAN: {}", AppBadMan);
  50. SPDLOG_INFO("APPTIRED: {}", AppTired_Time);
  51. SPDLOG_INFO("APPPEOPLECONT: {}", AppPeopleCont);
  52. SPDLOG_INFO("APPPLAYPHONE: {}", AppPlayPhone);
  53. SPDLOG_INFO("APPMOUSE: {}", AppMouse);
  54. SPDLOG_INFO("APPMASK: {}", AppMask);
  55. SPDLOG_INFO("CHECKSET: {}", CheckSet);
  56. SPDLOG_INFO("EventTimeValid: {}", EventTimeValid);
  57. }
  58. /* 添加通道信息 */
  59. void GlobalConfig::setChannelInfo(std::map<int, std::string> mapChannelName)
  60. {
  61. m_rwLockChnInfo.lockForWrite();
  62. m_mapChannelName.clear();
  63. m_mapChannelName = mapChannelName;
  64. m_rwLockChnInfo.unlock();
  65. }
  66. /* 清空通道信息 */
  67. void GlobalConfig::clearChannelInfo()
  68. {
  69. m_rwLockChnInfo.lockForWrite();
  70. m_mapChannelName.clear();
  71. m_rwLockChnInfo.unlock();
  72. }
  73. /* 获取通道名称 */
  74. std::string GlobalConfig::getChannelName(int ChannelID)
  75. {
  76. m_rwLockChnInfo.lockForRead();
  77. std::string strName = "";
  78. auto it = m_mapChannelName.find(ChannelID);
  79. if(it != m_mapChannelName.end())
  80. {
  81. strName = it->second;
  82. }
  83. m_rwLockChnInfo.unlock();
  84. return strName;
  85. }
  86. /* 添加摄像机信息 */
  87. void GlobalConfig::setCameraInfo(std::map<int, std::string> mapCameraName)
  88. {
  89. m_rwLockCamInfo.lockForWrite();
  90. m_mapCameraName.clear();
  91. m_mapCameraName = mapCameraName;
  92. m_rwLockCamInfo.unlock();
  93. }
  94. /* 获取摄像机名称 */
  95. std::string GlobalConfig::getCameraName(int CameraID)
  96. {
  97. m_rwLockCamInfo.lockForRead();
  98. std::string strName = "";
  99. auto it = m_mapCameraName.find(CameraID);
  100. if(it != m_mapCameraName.end())
  101. {
  102. strName = it->second;
  103. }
  104. m_rwLockCamInfo.unlock();
  105. return strName;
  106. }
  107. /* ====================================================================================
  108. * ************************ GlobalThreadInfo成员函数 *****************************
  109. * ====================================================================================*/
  110. /* 获取线程运行标志位 */
  111. bool GlobalThreadInfo::getRunning() const
  112. {
  113. return m_bRunning;
  114. }
  115. /* 设置线程运行标志位 */
  116. void GlobalThreadInfo::setRunning(bool bRunning)
  117. {
  118. m_bRunning = bRunning;
  119. }
  120. /* 手动给功能块列表加锁 */
  121. void GlobalThreadInfo::lockRunFAI()
  122. {
  123. m_mutexRunFAI.lock();
  124. }
  125. /* 手动解锁 */
  126. void GlobalThreadInfo::unlockRunFAI()
  127. {
  128. m_mutexRunFAI.unlock();
  129. }
  130. /**
  131. * @brief 添加应用信息,一个应用功能在一个频道内只有一个实例
  132. * 这里是添加应用功能和时间段信息
  133. *
  134. * @param func
  135. * @return true
  136. * @return false
  137. */
  138. bool GlobalThreadInfo::addFuncActionInfo(const AppAndTimeInfo& func)
  139. {
  140. if(func.AppType == 0)
  141. {
  142. return false;
  143. }
  144. /* 解出这条信息里包含几个App,AppType按位计算,总共8个应用信息 */
  145. for(int i = 0; i < 8; ++i)
  146. {
  147. if(func.AppType & 0x01)
  148. {
  149. /* 查找有没有这个应用 */
  150. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_OnWork);
  151. if(pFuncActionInfo != nullptr)
  152. {
  153. /* 更新时间信息 */
  154. pFuncActionInfo->StartTime = func.StartTime;
  155. pFuncActionInfo->EndTime = func.EndTime;
  156. continue;
  157. }
  158. FuncActionInfo* fa = new FuncActionInfo;
  159. fa->ChannelID = func.ChannelID;
  160. fa->appFunction = AppFunction::APP_OnWork;
  161. fa->strFunctionName = "人员在岗识别";
  162. fa->StartTime = func.StartTime;
  163. fa->EndTime = func.EndTime;
  164. m_listFuncActionInfo.push_back(fa);
  165. }
  166. else if(func.AppType & 0x02)
  167. {
  168. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_Contraband);
  169. if(pFuncActionInfo != nullptr)
  170. {
  171. /* 更新时间信息 */
  172. pFuncActionInfo->StartTime = func.StartTime;
  173. pFuncActionInfo->EndTime = func.EndTime;
  174. continue;
  175. }
  176. FuncActionInfo* fa = new FuncActionInfo;
  177. fa->ChannelID = func.ChannelID;
  178. fa->appFunction = AppFunction::APP_Contraband;
  179. fa->strFunctionName = "违禁品识别";
  180. fa->StartTime = func.StartTime;
  181. fa->EndTime = func.EndTime;
  182. m_listFuncActionInfo.push_back(fa);
  183. }
  184. else if (func.AppType & 0x04)
  185. {
  186. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_Illegal);
  187. if(pFuncActionInfo != nullptr)
  188. {
  189. /* 更新时间信息 */
  190. pFuncActionInfo->StartTime = func.StartTime;
  191. pFuncActionInfo->EndTime = func.EndTime;
  192. continue;
  193. }
  194. FuncActionInfo* fa = new FuncActionInfo;
  195. fa->ChannelID = func.ChannelID;
  196. fa->appFunction = AppFunction::APP_Illegal;
  197. fa->strFunctionName = "非法入侵检测";
  198. fa->StartTime = func.StartTime;
  199. fa->EndTime = func.EndTime;
  200. m_listFuncActionInfo.push_back(fa);
  201. }
  202. else if (func.AppType & 0x08)
  203. {
  204. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_Fatigue);
  205. if(pFuncActionInfo != nullptr)
  206. {
  207. /* 更新时间信息 */
  208. pFuncActionInfo->StartTime = func.StartTime;
  209. pFuncActionInfo->EndTime = func.EndTime;
  210. continue;
  211. }
  212. FuncActionInfo* fa = new FuncActionInfo;
  213. fa->ChannelID = func.ChannelID;
  214. fa->appFunction = AppFunction::APP_Fatigue;
  215. fa->strFunctionName = "疲劳检测";
  216. fa->StartTime = func.StartTime;
  217. fa->EndTime = func.EndTime;
  218. m_listFuncActionInfo.push_back(fa);
  219. }
  220. else if (func.AppType & 0x10)
  221. {
  222. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_Regional);
  223. if(pFuncActionInfo != nullptr)
  224. {
  225. /* 更新时间信息 */
  226. pFuncActionInfo->StartTime = func.StartTime;
  227. pFuncActionInfo->EndTime = func.EndTime;
  228. continue;
  229. }
  230. FuncActionInfo* fa = new FuncActionInfo;
  231. fa->ChannelID = func.ChannelID;
  232. fa->appFunction = AppFunction::APP_Regional;
  233. fa->strFunctionName = "区域人员检测";
  234. fa->StartTime = func.StartTime;
  235. fa->EndTime = func.EndTime;
  236. m_listFuncActionInfo.push_back(fa);
  237. }
  238. else if (func.AppType & 0x20)
  239. {
  240. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_Mouse);
  241. if(pFuncActionInfo != nullptr)
  242. {
  243. /* 更新时间信息 */
  244. pFuncActionInfo->StartTime = func.StartTime;
  245. pFuncActionInfo->EndTime = func.EndTime;
  246. continue;
  247. }
  248. FuncActionInfo* fa = new FuncActionInfo;
  249. fa->ChannelID = func.ChannelID;
  250. fa->appFunction = AppFunction::APP_Mouse;
  251. fa->strFunctionName = "老鼠识别";
  252. fa->StartTime = func.StartTime;
  253. fa->EndTime = func.EndTime;
  254. m_listFuncActionInfo.push_back(fa);
  255. }
  256. else if (func.AppType & 0x40)
  257. {
  258. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_PlayPhone);
  259. if(pFuncActionInfo != nullptr)
  260. {
  261. /* 更新时间信息 */
  262. pFuncActionInfo->StartTime = func.StartTime;
  263. pFuncActionInfo->EndTime = func.EndTime;
  264. continue;
  265. }
  266. FuncActionInfo* fa = new FuncActionInfo;
  267. fa->ChannelID = func.ChannelID;
  268. fa->appFunction = AppFunction::APP_PlayPhone;
  269. fa->strFunctionName = "玩手机识别";
  270. fa->StartTime = func.StartTime;
  271. fa->EndTime = func.EndTime;
  272. m_listFuncActionInfo.push_back(fa);
  273. }
  274. else if (func.AppType & 0x80)
  275. {
  276. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_NoMask);
  277. if(pFuncActionInfo != nullptr)
  278. {
  279. /* 更新时间信息 */
  280. pFuncActionInfo->StartTime = func.StartTime;
  281. pFuncActionInfo->EndTime = func.EndTime;
  282. continue;
  283. }
  284. FuncActionInfo* fa = new FuncActionInfo;
  285. fa->ChannelID = func.ChannelID;
  286. fa->appFunction = AppFunction::APP_NoMask;
  287. fa->strFunctionName = "未戴口罩识别";
  288. fa->StartTime = func.StartTime;
  289. fa->EndTime = func.EndTime;
  290. m_listFuncActionInfo.push_back(fa);
  291. }
  292. else if (func.AppType & 0x0100)
  293. {
  294. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_AllDown);
  295. if(pFuncActionInfo != nullptr)
  296. {
  297. /* 更新时间信息 */
  298. pFuncActionInfo->StartTime = func.StartTime;
  299. pFuncActionInfo->EndTime = func.EndTime;
  300. continue;
  301. }
  302. FuncActionInfo* fa = new FuncActionInfo;
  303. fa->ChannelID = func.ChannelID;
  304. fa->appFunction = AppFunction::APP_AllDown;
  305. fa->strFunctionName = "摔倒识别";
  306. fa->StartTime = func.StartTime;
  307. fa->EndTime = func.EndTime;
  308. m_listFuncActionInfo.push_back(fa);
  309. }
  310. }
  311. return true;
  312. }
  313. /**
  314. * @brief 添加算法信息,根据传进来的算法ID,将其加入到对应的功能中
  315. * 根据功能需要,将算法ID加入到对应的功能中
  316. *
  317. * @param info
  318. * @return true
  319. * @return false
  320. */
  321. bool GlobalThreadInfo::addActionInfo(const ActionInfo& info)
  322. {
  323. if(info.ActionID.empty())
  324. {
  325. return false;
  326. }
  327. /* 人脸识别算法(人员在岗识别、非法入侵检测需要) */
  328. if(info.ActionID == g_actionList.ActFace)
  329. {
  330. /* 人员在岗识别 */
  331. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_OnWork);
  332. if(pFuncActionInfo != nullptr)
  333. {
  334. pFuncActionInfo->addActionInfo(info);
  335. }
  336. /* 非法入侵检测 */
  337. pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_Illegal);
  338. if(pFuncActionInfo != nullptr)
  339. {
  340. pFuncActionInfo->addActionInfo(info);
  341. }
  342. }
  343. /* 人员计数 */
  344. else if (info.ActionID == g_actionList.ActPersonNumber)
  345. {
  346. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_Regional);
  347. if(pFuncActionInfo != nullptr)
  348. {
  349. pFuncActionInfo->addActionInfo(info);
  350. }
  351. }
  352. /* 违禁物品 */
  353. else if (info.ActionID == g_actionList.ActContraband)
  354. {
  355. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_Contraband);
  356. if(pFuncActionInfo != nullptr)
  357. {
  358. pFuncActionInfo->addActionInfo(info);
  359. }
  360. }
  361. /* 玩手机 */
  362. else if (info.ActionID == g_actionList.ActPlayPhone)
  363. {
  364. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_PlayPhone);
  365. if(pFuncActionInfo != nullptr)
  366. {
  367. pFuncActionInfo->addActionInfo(info);
  368. }
  369. }
  370. /* 睡岗识别 */
  371. else if (info.ActionID == g_actionList.ActSleep)
  372. {
  373. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_Fatigue);
  374. if(pFuncActionInfo != nullptr)
  375. {
  376. pFuncActionInfo->addActionInfo(info);
  377. }
  378. }
  379. /* 疲劳检测 */
  380. else if(info.ActionID == g_actionList.ActFatigueDetection)
  381. {
  382. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_Fatigue);
  383. if(pFuncActionInfo != nullptr)
  384. {
  385. pFuncActionInfo->addActionInfo(info);
  386. }
  387. }
  388. /* 动物识别 */
  389. else if (info.ActionID == g_actionList.ActAnimalDetect)
  390. {
  391. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_Mouse);
  392. if(pFuncActionInfo != nullptr)
  393. {
  394. pFuncActionInfo->addActionInfo(info);
  395. }
  396. }
  397. /* 老鼠识别 */
  398. else if (info.ActionID == g_actionList.ActMouseDetect)
  399. {
  400. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_Mouse);
  401. if(pFuncActionInfo != nullptr)
  402. {
  403. pFuncActionInfo->addActionInfo(info);
  404. }
  405. }
  406. /* 口罩识别 */
  407. else if (info.ActionID == g_actionList.ActMask)
  408. {
  409. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_NoMask);
  410. if(pFuncActionInfo != nullptr)
  411. {
  412. pFuncActionInfo->addActionInfo(info);
  413. }
  414. }
  415. else {
  416. SPDLOG_WARN("未知的算法ID: {}", info.ActionID);
  417. return false;
  418. }
  419. return true;
  420. }
  421. /**
  422. * @brief 清空无用的功能信息
  423. * 摄像机和算法信息为空的,或者运行状态为RUN_STATE_EXITCOMPLET,都会被清理掉
  424. *
  425. */
  426. void GlobalThreadInfo::clearNoneFuncActionInfo()
  427. {
  428. for(auto it0 = m_listFuncActionInfo.begin(); it0 != m_listFuncActionInfo.end();)
  429. {
  430. if((*it0)->listRoomCamActInfo.empty() || ((*it0)->RunState == RunTimeState::RUN_STATE_EXITCOMPLET))
  431. {
  432. delete *it0;
  433. it0 = m_listFuncActionInfo.erase(it0);
  434. }else {
  435. ++it0;
  436. }
  437. }
  438. }
  439. /* 清空算法列表(直接清空房间信息) */
  440. void GlobalThreadInfo::clearActionList()
  441. {
  442. for(auto& it0 : m_listFuncActionInfo)
  443. {
  444. // for(auto& it1 : it0->listRoomCamActInfo)
  445. // {
  446. // it1.mapCameraAction.clear();
  447. // }
  448. it0->listRoomCamActInfo.clear();
  449. }
  450. }
  451. /* 设置没有配置摄像机的功能退出 */
  452. void GlobalThreadInfo::setNoneCameraFuncStop()
  453. {
  454. for(auto& it : m_listFuncActionInfo)
  455. {
  456. if(it->listRoomCamActInfo.empty())
  457. {
  458. it->RunState = RunTimeState::RUN_STATE_STOP;
  459. }
  460. }
  461. }
  462. /* 查找应用信息 */
  463. bool GlobalThreadInfo::findAppFunction(const AppFunction func)
  464. {
  465. for(const auto& it0 : m_listFuncActionInfo)
  466. {
  467. if(it0->appFunction == func)
  468. {
  469. return true;
  470. }
  471. }
  472. return false;
  473. }
  474. /* 根据频率和功能查找实例 */
  475. FuncActionInfo* GlobalThreadInfo::findAppFunction(const int ChannelID, const AppFunction func)
  476. {
  477. for(const auto& it0 : m_listFuncActionInfo)
  478. {
  479. if( (it0->appFunction == func) && (it0->ChannelID == ChannelID) )
  480. {
  481. return it0;
  482. }
  483. }
  484. return nullptr;
  485. }
  486. /**
  487. * @brief 查找这个应用信息
  488. *
  489. * @param func
  490. * @return FuncActionInfo*
  491. */
  492. FuncActionInfo* GlobalThreadInfo::findAppFunction(const FuncActionInfo& func)
  493. {
  494. for(const auto& it0 : m_listFuncActionInfo)
  495. {
  496. if(it0->ChannelID == func.ChannelID && it0->appFunction == func.appFunction)
  497. {
  498. return it0;
  499. }
  500. }
  501. return nullptr;
  502. }
  503. /* 设置线程状态 */
  504. void GlobalThreadInfo::setThreadState(FuncActionInfo* pInfo, RunTimeState state)
  505. {
  506. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  507. auto p = findAppFunction(*pInfo);
  508. if(p != nullptr)
  509. {
  510. p->RunState = state;
  511. }
  512. }
  513. void GlobalThreadInfo::setThreadState(FuncActionInfo& pInfo, RunTimeState state)
  514. {
  515. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  516. auto p = findAppFunction(pInfo);
  517. if(p != nullptr)
  518. {
  519. p->RunState = state;
  520. }
  521. }
  522. /**
  523. * @brief 功能线程更新功能信息,这里是从内存中的数组里获取
  524. *
  525. * @param pInfo
  526. * @return true
  527. * @return false
  528. */
  529. bool GlobalThreadInfo::updateFuncInfo(FuncActionInfo* pInfo)
  530. {
  531. pInfo->clearActionList();
  532. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  533. auto fa = findAppFunction(*pInfo);
  534. if(fa == nullptr)
  535. {
  536. return false;
  537. }
  538. *pInfo = *fa;
  539. return true;
  540. }
  541. bool GlobalThreadInfo::updateFuncInfo(FuncActionInfo& pInfo)
  542. {
  543. pInfo.clearActionList();
  544. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  545. auto fa = findAppFunction(pInfo);
  546. if(fa == nullptr)
  547. {
  548. return false;
  549. }
  550. pInfo = *fa;
  551. return true;
  552. }