GlobalConfig.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 = settings.value("WorkOnInfoSecond", 600).toInt(); /* 更新在岗信息的时间间隔 */
  24. AppPeopleOnWork = settings.value("APPPEPOLEONWORK", 300).toInt(); /* 离岗时间 */
  25. Contraband = settings.value("APPBADTHING", 50).toInt(); /* 违禁物品出现的时间 */
  26. AppBadMan = settings.value("APPBADMAN", 50).toInt(); /* 非法入侵 */
  27. AppTired = 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);
  48. SPDLOG_INFO("APPBADTHING: {}", Contraband);
  49. SPDLOG_INFO("APPBADMAN: {}", AppBadMan);
  50. SPDLOG_INFO("APPTIRED: {}", AppTired);
  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. * ************************ GlobalThreadInfo成员函数 *****************************
  60. * ====================================================================================*/
  61. /* 获取线程运行标志位 */
  62. bool GlobalThreadInfo::getRunning() const
  63. {
  64. return m_bRunning;
  65. }
  66. /* 设置线程运行标志位 */
  67. void GlobalThreadInfo::setRunning(bool bRunning)
  68. {
  69. m_bRunning = bRunning;
  70. }
  71. /* 手动给功能块列表加锁 */
  72. void GlobalThreadInfo::lockRunFAI()
  73. {
  74. m_mutexRunFAI.lock();
  75. }
  76. /* 手动解锁 */
  77. void GlobalThreadInfo::unlockRunFAI()
  78. {
  79. m_mutexRunFAI.unlock();
  80. }
  81. /**
  82. * @brief 添加应用信息,一个应用功能在一个频道内只有一个实例
  83. * 这里是添加应用功能和时间段信息
  84. *
  85. * @param func
  86. * @return true
  87. * @return false
  88. */
  89. bool GlobalThreadInfo::addFuncActionInfo(const AppAndTimeInfo& func)
  90. {
  91. if(func.AppType == 0)
  92. {
  93. return false;
  94. }
  95. /* 解出这条信息里包含几个App,AppType按位计算,总共8个应用信息 */
  96. for(int i = 0; i < 8; ++i)
  97. {
  98. if(func.AppType & 0x01)
  99. {
  100. /* 查找有没有这个应用 */
  101. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_OnWork);
  102. if(pFuncActionInfo != nullptr)
  103. {
  104. /* 更新时间信息 */
  105. pFuncActionInfo->StartTime = func.StartTime;
  106. pFuncActionInfo->EndTime = func.EndTime;
  107. continue;
  108. }
  109. FuncActionInfo* fa = new FuncActionInfo;
  110. fa->ChannelID = func.ChannelID;
  111. fa->appFunction = AppFunction::APP_OnWork;
  112. fa->strFunctionName = "人员在岗识别";
  113. fa->StartTime = func.StartTime;
  114. fa->EndTime = func.EndTime;
  115. m_listFuncActionInfo.push_back(fa);
  116. }
  117. else if(func.AppType & 0x02)
  118. {
  119. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_Contraband);
  120. if(pFuncActionInfo != nullptr)
  121. {
  122. /* 更新时间信息 */
  123. pFuncActionInfo->StartTime = func.StartTime;
  124. pFuncActionInfo->EndTime = func.EndTime;
  125. continue;
  126. }
  127. FuncActionInfo* fa = new FuncActionInfo;
  128. fa->ChannelID = func.ChannelID;
  129. fa->appFunction = AppFunction::APP_Contraband;
  130. fa->strFunctionName = "违禁品识别";
  131. fa->StartTime = func.StartTime;
  132. fa->EndTime = func.EndTime;
  133. m_listFuncActionInfo.push_back(fa);
  134. }
  135. else if (func.AppType & 0x04)
  136. {
  137. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_Illegal);
  138. if(pFuncActionInfo != nullptr)
  139. {
  140. /* 更新时间信息 */
  141. pFuncActionInfo->StartTime = func.StartTime;
  142. pFuncActionInfo->EndTime = func.EndTime;
  143. continue;
  144. }
  145. FuncActionInfo* fa = new FuncActionInfo;
  146. fa->ChannelID = func.ChannelID;
  147. fa->appFunction = AppFunction::APP_Illegal;
  148. fa->strFunctionName = "非法入侵检测";
  149. fa->StartTime = func.StartTime;
  150. fa->EndTime = func.EndTime;
  151. m_listFuncActionInfo.push_back(fa);
  152. }
  153. else if (func.AppType & 0x08)
  154. {
  155. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_Fatigue);
  156. if(pFuncActionInfo != nullptr)
  157. {
  158. /* 更新时间信息 */
  159. pFuncActionInfo->StartTime = func.StartTime;
  160. pFuncActionInfo->EndTime = func.EndTime;
  161. continue;
  162. }
  163. FuncActionInfo* fa = new FuncActionInfo;
  164. fa->ChannelID = func.ChannelID;
  165. fa->appFunction = AppFunction::APP_Fatigue;
  166. fa->strFunctionName = "疲劳检测";
  167. fa->StartTime = func.StartTime;
  168. fa->EndTime = func.EndTime;
  169. m_listFuncActionInfo.push_back(fa);
  170. }
  171. else if (func.AppType & 0x10)
  172. {
  173. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_Regional);
  174. if(pFuncActionInfo != nullptr)
  175. {
  176. /* 更新时间信息 */
  177. pFuncActionInfo->StartTime = func.StartTime;
  178. pFuncActionInfo->EndTime = func.EndTime;
  179. continue;
  180. }
  181. FuncActionInfo* fa = new FuncActionInfo;
  182. fa->ChannelID = func.ChannelID;
  183. fa->appFunction = AppFunction::APP_Regional;
  184. fa->strFunctionName = "区域人员检测";
  185. fa->StartTime = func.StartTime;
  186. fa->EndTime = func.EndTime;
  187. m_listFuncActionInfo.push_back(fa);
  188. }
  189. else if (func.AppType & 0x20)
  190. {
  191. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_Mouse);
  192. if(pFuncActionInfo != nullptr)
  193. {
  194. /* 更新时间信息 */
  195. pFuncActionInfo->StartTime = func.StartTime;
  196. pFuncActionInfo->EndTime = func.EndTime;
  197. continue;
  198. }
  199. FuncActionInfo* fa = new FuncActionInfo;
  200. fa->ChannelID = func.ChannelID;
  201. fa->appFunction = AppFunction::APP_Mouse;
  202. fa->strFunctionName = "老鼠识别";
  203. fa->StartTime = func.StartTime;
  204. fa->EndTime = func.EndTime;
  205. m_listFuncActionInfo.push_back(fa);
  206. }
  207. else if (func.AppType & 0x40)
  208. {
  209. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_PlayPhone);
  210. if(pFuncActionInfo != nullptr)
  211. {
  212. /* 更新时间信息 */
  213. pFuncActionInfo->StartTime = func.StartTime;
  214. pFuncActionInfo->EndTime = func.EndTime;
  215. continue;
  216. }
  217. FuncActionInfo* fa = new FuncActionInfo;
  218. fa->ChannelID = func.ChannelID;
  219. fa->appFunction = AppFunction::APP_PlayPhone;
  220. fa->strFunctionName = "玩手机识别";
  221. fa->StartTime = func.StartTime;
  222. fa->EndTime = func.EndTime;
  223. m_listFuncActionInfo.push_back(fa);
  224. }
  225. else if (func.AppType & 0x80)
  226. {
  227. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_NoMask);
  228. if(pFuncActionInfo != nullptr)
  229. {
  230. /* 更新时间信息 */
  231. pFuncActionInfo->StartTime = func.StartTime;
  232. pFuncActionInfo->EndTime = func.EndTime;
  233. continue;
  234. }
  235. FuncActionInfo* fa = new FuncActionInfo;
  236. fa->ChannelID = func.ChannelID;
  237. fa->appFunction = AppFunction::APP_NoMask;
  238. fa->strFunctionName = "未戴口罩识别";
  239. fa->StartTime = func.StartTime;
  240. fa->EndTime = func.EndTime;
  241. m_listFuncActionInfo.push_back(fa);
  242. }
  243. else if (func.AppType & 0x0100)
  244. {
  245. auto pFuncActionInfo = findAppFunction(func.ChannelID, AppFunction::APP_AllDown);
  246. if(pFuncActionInfo != nullptr)
  247. {
  248. /* 更新时间信息 */
  249. pFuncActionInfo->StartTime = func.StartTime;
  250. pFuncActionInfo->EndTime = func.EndTime;
  251. continue;
  252. }
  253. FuncActionInfo* fa = new FuncActionInfo;
  254. fa->ChannelID = func.ChannelID;
  255. fa->appFunction = AppFunction::APP_AllDown;
  256. fa->strFunctionName = "摔倒识别";
  257. fa->StartTime = func.StartTime;
  258. fa->EndTime = func.EndTime;
  259. m_listFuncActionInfo.push_back(fa);
  260. }
  261. }
  262. return true;
  263. }
  264. /**
  265. * @brief 添加算法信息,根据传进来的算法ID,将其加入到对应的功能中
  266. *
  267. * @param info
  268. * @return true
  269. * @return false
  270. */
  271. bool GlobalThreadInfo::addActionInfo(const ActionInfo& info)
  272. {
  273. if(info.ActionID.empty())
  274. {
  275. return false;
  276. }
  277. /* 人脸识别算法(人员在岗识别、非法入侵检测需要) */
  278. if(info.ActionID == g_actionList.ActFace)
  279. {
  280. /* 人员在岗识别 */
  281. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_OnWork);
  282. if(pFuncActionInfo != nullptr)
  283. {
  284. pFuncActionInfo->addActionInfo(info);
  285. }
  286. /* 非法入侵检测 */
  287. pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_Illegal);
  288. if(pFuncActionInfo != nullptr)
  289. {
  290. pFuncActionInfo->addActionInfo(info);
  291. }
  292. }
  293. /* 人员计数 */
  294. else if (info.ActionID == g_actionList.ActPersonNumber)
  295. {
  296. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_Regional);
  297. if(pFuncActionInfo != nullptr)
  298. {
  299. pFuncActionInfo->addActionInfo(info);
  300. }
  301. }
  302. /* 违禁物品 */
  303. else if (info.ActionID == g_actionList.ActContraband)
  304. {
  305. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_Contraband);
  306. if(pFuncActionInfo != nullptr)
  307. {
  308. pFuncActionInfo->addActionInfo(info);
  309. }
  310. }
  311. /* 玩手机 */
  312. else if (info.ActionID == g_actionList.ActPlayPhone)
  313. {
  314. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_PlayPhone);
  315. if(pFuncActionInfo != nullptr)
  316. {
  317. pFuncActionInfo->addActionInfo(info);
  318. }
  319. }
  320. /* 睡岗识别 */
  321. else if (info.ActionID == g_actionList.ActSleep)
  322. {
  323. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_Fatigue);
  324. if(pFuncActionInfo != nullptr)
  325. {
  326. pFuncActionInfo->addActionInfo(info);
  327. }
  328. }
  329. /* 疲劳检测 */
  330. else if(info.ActionID == g_actionList.ActFatigueDetection)
  331. {
  332. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_Fatigue);
  333. if(pFuncActionInfo != nullptr)
  334. {
  335. pFuncActionInfo->addActionInfo(info);
  336. }
  337. }
  338. /* 动物识别 */
  339. else if (info.ActionID == g_actionList.ActAnimalDetect)
  340. {
  341. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_Mouse);
  342. if(pFuncActionInfo != nullptr)
  343. {
  344. pFuncActionInfo->addActionInfo(info);
  345. }
  346. }
  347. /* 老鼠识别 */
  348. else if (info.ActionID == g_actionList.ActMouseDetect)
  349. {
  350. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_Mouse);
  351. if(pFuncActionInfo != nullptr)
  352. {
  353. pFuncActionInfo->addActionInfo(info);
  354. }
  355. }
  356. /* 口罩识别 */
  357. else if (info.ActionID == g_actionList.ActMask)
  358. {
  359. auto pFuncActionInfo = findAppFunction(info.ChannelID, AppFunction::APP_NoMask);
  360. if(pFuncActionInfo != nullptr)
  361. {
  362. pFuncActionInfo->addActionInfo(info);
  363. }
  364. }
  365. else {
  366. SPDLOG_WARN("未知的算法ID: {}", info.ActionID);
  367. return false;
  368. }
  369. return true;
  370. }
  371. /**
  372. * @brief 清空无用的功能信息
  373. * 摄像机和算法信息为空的,或者运行状态为STOP,都会被清理掉
  374. *
  375. */
  376. void GlobalThreadInfo::clearNoneFuncActionInfo()
  377. {
  378. for(auto it0 = m_listFuncActionInfo.begin(); it0 != m_listFuncActionInfo.end();)
  379. {
  380. if((*it0)->listRoomCamActInfo.empty() || ((*it0)->RunState == RunTimeState::RUN_STATE_STOP))
  381. {
  382. delete *it0;
  383. it0 = m_listFuncActionInfo.erase(it0);
  384. }else {
  385. ++it0;
  386. }
  387. }
  388. }
  389. /* 清空算法列表 */
  390. void GlobalThreadInfo::clearActionList()
  391. {
  392. for(auto& it0 : m_listFuncActionInfo)
  393. {
  394. it0->listRoomCamActInfo.clear();
  395. }
  396. }
  397. /* 设置没有配置摄像机的功能退出 */
  398. void GlobalThreadInfo::setNoneCameraFuncStop()
  399. {
  400. for(auto& it : m_listFuncActionInfo)
  401. {
  402. if(it->listRoomCamActInfo.empty())
  403. {
  404. it->RunState = RunTimeState::RUN_STATE_STOP;
  405. }
  406. }
  407. }
  408. /* 查找应用信息 */
  409. bool GlobalThreadInfo::findAppFunction(const AppFunction func)
  410. {
  411. for(const auto& it0 : m_listFuncActionInfo)
  412. {
  413. if(it0->appFunction == func)
  414. {
  415. return true;
  416. }
  417. }
  418. return false;
  419. }
  420. /* 根据频率和功能查找实例 */
  421. FuncActionInfo* GlobalThreadInfo::findAppFunction(const int ChannelID, const AppFunction func)
  422. {
  423. for(const auto& it0 : m_listFuncActionInfo)
  424. {
  425. if( (it0->appFunction == func) && (it0->ChannelID == ChannelID) )
  426. {
  427. return it0;
  428. }
  429. }
  430. return nullptr;
  431. }
  432. /**
  433. * @brief 查找这个应用信息
  434. *
  435. * @param func
  436. * @return FuncActionInfo*
  437. */
  438. FuncActionInfo* GlobalThreadInfo::findAppFunction(const FuncActionInfo& func)
  439. {
  440. for(const auto& it0 : m_listFuncActionInfo)
  441. {
  442. if(it0->ChannelID == func.ChannelID && it0->appFunction == func.appFunction)
  443. {
  444. return it0;
  445. }
  446. }
  447. return nullptr;
  448. }
  449. /* 设置线程状态 */
  450. void GlobalThreadInfo::setThreadState(FuncActionInfo* pInfo, RunTimeState state)
  451. {
  452. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  453. auto p = findAppFunction(*pInfo);
  454. if(p != nullptr)
  455. {
  456. p->RunState = state;
  457. }
  458. }
  459. /**
  460. * @brief 功能线程更新功能信息,这里是从内存中的数组里获取
  461. *
  462. * @param pInfo
  463. * @return true
  464. * @return false
  465. */
  466. bool GlobalThreadInfo::updateFuncInfo(FuncActionInfo* pInfo)
  467. {
  468. pInfo->clearActionList();
  469. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  470. auto fa = findAppFunction(*pInfo);
  471. if(fa == nullptr)
  472. {
  473. return false;
  474. }
  475. *pInfo = *fa;
  476. return true;
  477. }