SPAServer.cpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  1. #include "SPAServer.h"
  2. #include "spdlog/spdlog.h"
  3. // #include "CurlHttp.h"
  4. #include <filesystem>
  5. // #include "CurlFtp.h"
  6. #include "ThreadPool/ThreadPool.h"
  7. #include <QApplication>
  8. #include <QVector>
  9. SPAServer::SPAServer()
  10. {
  11. m_logger = spdlog::get("SPAServer");
  12. if(m_logger == nullptr)
  13. {
  14. SPDLOG_ERROR("APAServer logger is nullptr");
  15. return;
  16. }
  17. /* 读取全局的配置文件 */
  18. QString strConfigFile = QApplication::applicationDirPath() + "/config.ini";
  19. if(!g_config.readConfig(strConfigFile))
  20. {
  21. /* 读取配置文件失败,直接退出程序 */
  22. return;
  23. }
  24. g_config.printValue();
  25. m_threadRunning = true;
  26. /* 初始化WebAPI */
  27. m_toEQMDataBase.initWebApi("http://192.1.3.133:31000/v6/", "", "4c2f9fc91c22dd98331e47af2e2964f4");
  28. /* 模拟违禁品算法ID,后续需要动态调整 */
  29. g_actionList.ActContraband = "OD210_026_005246_001-IZRTKyEx";
  30. }
  31. SPAServer::~SPAServer()
  32. {
  33. }
  34. /* 启动服务 */
  35. void SPAServer::startServer()
  36. {
  37. /* 添加获取基础信息的线程 */
  38. // CPPTP.add_task(&SPAServer::threadFromSuperBrain, this);
  39. /* 测试Redis读取并解析数据线程 */
  40. CameraThreadInfo info;
  41. info.RedisIP = "172.16.36.80";
  42. info.RedisPort = 32222;
  43. info.RedisPWD = "Ff1z@TOFr^iwd%Ra";
  44. info.DeviceID = 117;
  45. info.vecAction.push_back("OD210_026_005246_001-IZRTKyEx");
  46. g_actionList.ActContraband = "OD210_026_005246_001-IZRTKyEx";
  47. // CPPTP.add_task(&SPAServer::threadFromRedis, this, info);
  48. // threadFromRedis(info);
  49. }
  50. /**
  51. * @brief 从基础平台获取算法信息和设备信息的线程函数
  52. *
  53. */
  54. void SPAServer::threadFromSuperBrain()
  55. {
  56. SPDLOG_LOGGER_INFO(m_logger, "开启 fromSuperBrainThread 线程");
  57. /* 创建变量 */
  58. std::vector<AlgorithmInfo> vecAlgNewInfo;
  59. std::vector<DeviceInfo> vecDevNewInfo;
  60. /* 获取一次token,后续失效了再获取 */
  61. m_fromSuperBrain.getToken();
  62. while (m_threadRunning)
  63. {
  64. SPDLOG_LOGGER_INFO(m_logger, "刷新算法和设备信息");
  65. /* 先更新数据库的信息,防止从其他地方更改了数据库,这里没有刷新本地缓存 */
  66. m_toEQMDataBase.getAlgorithmInfo(m_vecEqmAlgInfo);
  67. m_toEQMDataBase.getDeviceInfo(m_vecEqmDevInfo);
  68. m_toEQMDataBase.getDeviceAlgorithmInfo(m_vecEqmDevInfo, m_listDevIDDelete);
  69. /* 从超脑获取基础信息 */
  70. m_fromSuperBrain.getTaskTypeList(vecAlgNewInfo);
  71. m_fromSuperBrain.getDeviceList(vecDevNewInfo);
  72. /* 处理算法信息 */
  73. bool algIsUpdate = processAlgorithmInfo(vecAlgNewInfo);
  74. /* 处理设备信息 */
  75. bool devIsUpdate = processDeviceInfo(vecDevNewInfo);
  76. vecAlgNewInfo.clear();
  77. vecDevNewInfo.clear();
  78. /* 更新算法详细信息 */
  79. m_mutexActionInfo.lock();
  80. m_toEQMDataBase.getActionInfo(m_listActionInfo);
  81. m_mutexActionInfo.unlock();
  82. std::this_thread::sleep_for(std::chrono::seconds(10));
  83. }
  84. }
  85. /* 处理算法信息,返回值为true,说明有改变,需要重新读取 */
  86. bool SPAServer::processAlgorithmInfo(std::vector<AlgorithmInfo> vecNewAlgInfo)
  87. {
  88. std::vector<AlgorithmInfo> vecAlgUpdate;
  89. std::vector<AlgorithmInfo> vecAlgDelete;
  90. /* 对比数据库表格信息,这里只有插入和删除,没有更新 */
  91. compareAlgorithmInfo(vecNewAlgInfo, vecAlgUpdate, vecAlgDelete);
  92. /* 更新数据库,先删除,再写入刷新 */
  93. bool isUpdate = false;
  94. if(vecAlgDelete.size() > 0)
  95. {
  96. SPDLOG_LOGGER_DEBUG(m_logger, "删除算法信息");
  97. m_toEQMDataBase.deleteAlgorithmInfo(vecAlgDelete);
  98. isUpdate = true;
  99. }
  100. if(vecAlgUpdate.size() > 0)
  101. {
  102. SPDLOG_LOGGER_DEBUG(m_logger, "写入算法信息");
  103. m_toEQMDataBase.writeAlgorithmInfo(vecAlgUpdate);
  104. isUpdate = true;
  105. }
  106. return isUpdate;
  107. }
  108. /**
  109. * @brief 处理设备信息
  110. *
  111. * @param vecNewDevInfo 传入新获取到的值
  112. * @return true 需要重新读取数据库,获取新的数据
  113. * @return false 无需读取数据库
  114. */
  115. bool SPAServer::processDeviceInfo(std::vector<DeviceInfo> vecNewDevInfo)
  116. {
  117. std::vector<DeviceInfo> vecDevInsert;
  118. std::vector<DeviceInfo> vecDevUpdate;
  119. std::vector<DeviceInfo> vecDevDelete;
  120. /*-------------------------------------------------------------------------
  121. ****** 这里只对比设备信息,不对比设备的算法信息,算法信息在下面单独对比 *******
  122. *------------------------------------------------------------------------*/
  123. /* 如果本地缓存没有数据,那么就全部插入 */
  124. if(m_vecEqmDevInfo.size() > 0)
  125. {
  126. for(auto& DevInfo : vecNewDevInfo)
  127. {
  128. bool isExist = false;
  129. for(auto& it0 : m_vecEqmDevInfo)
  130. {
  131. if(DevInfo.DeviceID == it0.DeviceID)
  132. {
  133. isExist = true;
  134. /* 对比其他项是否相等,不相等就更新 */
  135. if(DevInfo == it0)
  136. {
  137. continue;
  138. }else {
  139. vecDevUpdate.push_back(DevInfo);
  140. }
  141. break;
  142. }
  143. }
  144. if(!isExist)
  145. {
  146. vecDevInsert.push_back(DevInfo);
  147. }
  148. }
  149. }else {
  150. vecDevInsert = vecNewDevInfo;
  151. }
  152. /* 获取删除列表 */
  153. if(vecNewDevInfo.size() > 0)
  154. {
  155. bool isExist = false;
  156. for(const auto& it : m_vecEqmDevInfo)
  157. {
  158. isExist = false;
  159. for(const auto& it0 : vecNewDevInfo)
  160. {
  161. if(it.DeviceID == it0.DeviceID)
  162. {
  163. isExist = true;
  164. break;
  165. }
  166. }
  167. if(!isExist)
  168. {
  169. vecDevDelete.push_back(it);
  170. }
  171. }
  172. }else {
  173. vecDevDelete = m_vecEqmDevInfo;
  174. }
  175. bool isUpdate = false;
  176. /* 先删除多余的数据 */
  177. if(vecDevDelete.size() > 0)
  178. {
  179. SPDLOG_LOGGER_DEBUG(m_logger, "删除设备信息");
  180. m_toEQMDataBase.deleteDeviceInfo(vecDevDelete);
  181. isUpdate = true;
  182. }
  183. /* 更新数据 */
  184. if(vecDevUpdate.size() > 0)
  185. {
  186. SPDLOG_LOGGER_DEBUG(m_logger, "更新设备信息");
  187. m_toEQMDataBase.updateDeviceInfo(vecDevUpdate);
  188. isUpdate = true;
  189. }
  190. /* 插入数据 */
  191. if(vecDevInsert.size() > 0)
  192. {
  193. SPDLOG_LOGGER_DEBUG(m_logger, "插入设备信息");
  194. m_toEQMDataBase.insertDeviceInfo(vecDevInsert);
  195. isUpdate = true;
  196. }
  197. /*-------------------------------------------------------------------------
  198. ************* 处理设备和算子关联的表格,单独对比设备的算法信息 *************
  199. *------------------------------------------------------------------------*/
  200. /* 插入新的设备信息 */
  201. if(vecDevInsert.size() > 0)
  202. {
  203. SPDLOG_LOGGER_DEBUG(m_logger, "插入设备和算法关联表");
  204. m_toEQMDataBase.insertDeviceAlgorithmInfo(vecDevInsert);
  205. isUpdate = true;
  206. }
  207. vecDevUpdate.clear();
  208. /* 对比现有的设备是否需要更新算法 */
  209. compareDeviceAlgorithmInfo(vecNewDevInfo, vecDevUpdate);
  210. if(vecDevUpdate.size() > 0)
  211. {
  212. SPDLOG_LOGGER_DEBUG(m_logger, "更新设备和算法关联表, 更新设备数目:{}", vecDevUpdate.size());
  213. m_toEQMDataBase.updateDeviceAlgorithmInfo(vecDevUpdate);
  214. }
  215. /* 删除tActionCamer表中消失的设备信息 */
  216. if(m_listDevIDDelete.size() > 0)
  217. {
  218. SPDLOG_LOGGER_DEBUG(m_logger, "删除消失的设备关联的算法");
  219. m_toEQMDataBase.deleteDeviceAlgorithmInfo(m_listDevIDDelete);
  220. isUpdate = true;
  221. }
  222. return isUpdate;
  223. }
  224. /* 对比现有的数据和新获取到的数据,取出要删除和添加的数据 */
  225. void SPAServer::compareAlgorithmInfo(const std::vector<AlgorithmInfo>& vecNewInfo, std::vector<AlgorithmInfo>& vecAlgUpdate, std::vector<AlgorithmInfo>& vecAlgDelete)
  226. {
  227. /* 取出要添加的,如果本地缓存是0,那么全部都要添加 */
  228. if(m_vecEqmAlgInfo.size() > 0)
  229. {
  230. for(const auto& it : vecNewInfo)
  231. {
  232. bool isExist = false;
  233. for(const auto& it0 : m_vecEqmAlgInfo)
  234. {
  235. /* 如果存在就退出循环 */
  236. if(it.ActionID == it0.ActionID)
  237. {
  238. isExist = true;
  239. break;
  240. }
  241. }
  242. if(!isExist)
  243. {
  244. vecAlgUpdate.push_back(it);
  245. }
  246. }
  247. }else {
  248. vecAlgUpdate = vecNewInfo;
  249. }
  250. /* 取出要删除的,如果新的数据是0,那么全部都要删除 */
  251. if(vecNewInfo.size() > 0)
  252. {
  253. bool isExist = false;
  254. for(const auto& it : m_vecEqmAlgInfo)
  255. {
  256. isExist = false;
  257. for(const auto& it0 : vecNewInfo)
  258. {
  259. if(it.ActionID == it0.ActionID)
  260. {
  261. isExist = true;
  262. break;
  263. }
  264. }
  265. if(!isExist)
  266. {
  267. vecAlgDelete.push_back(it);
  268. }
  269. }
  270. }else {
  271. vecAlgDelete = m_vecEqmAlgInfo;
  272. }
  273. }
  274. /**
  275. * @brief 对比设备和算法关联表是否需要更新
  276. * 对比规则:
  277. * 1、这里只对比已有的设备ID,需要删除的ID在获取到tActionCamer表是就已经取出来了
  278. * 2、如果设备ID相等,那么进一步对比算法信息是否相等
  279. * 3、如果设备ID相等,但是算法信息数目不相等,那么直接加入更新列表
  280. * 4、如果设备ID相等,算法信息数目相等,进一步对比算法信息
  281. *
  282. * @param vecNewInfo
  283. * @param vecDevUpdate
  284. */
  285. void SPAServer::compareDeviceAlgorithmInfo(const std::vector<DeviceInfo>& vecNewInfo, std::vector<DeviceInfo>& vecDevUpdate)
  286. {
  287. vecDevUpdate.clear();
  288. for(const auto& it0 : vecNewInfo)
  289. {
  290. for(const auto& it1 : m_vecEqmDevInfo)
  291. {
  292. if(it0.DeviceID == it1.DeviceID)
  293. {
  294. /* 设备的算法信息数目不相等,直接加入更新列表 */
  295. if(it0.vecAlgorithmInfo.size() != it1.vecAlgorithmInfo.size())
  296. {
  297. vecDevUpdate.push_back(it0);
  298. break;
  299. }
  300. /* 设备的算法信息数目相等,进一步对比算法信息 */
  301. bool isEquality = true;
  302. for(const auto& it2 : it0.vecAlgorithmInfo)
  303. {
  304. bool isEq2 = false;
  305. for(const auto& it3 : it1.vecAlgorithmInfo)
  306. {
  307. /* 这里只对比算法ID */
  308. if(it2.ActionID != it3.ActionID)
  309. {
  310. continue;
  311. }else {
  312. isEq2 = true;
  313. break;
  314. }
  315. }
  316. if(!isEq2)
  317. {
  318. isEquality = false;
  319. break;
  320. }
  321. }
  322. if(!isEquality)
  323. {
  324. vecDevUpdate.push_back(it0);
  325. break;
  326. }
  327. }
  328. }
  329. }
  330. }
  331. /**
  332. * @brief 从Redis获取数据线程函数,这个是摄像机线程
  333. * 一个设备一个线程,这个线程的相关变量只在这个线程中使用
  334. * (注意,这个函数未被使用,不从这里获取Redis数据)
  335. * @param info
  336. */
  337. void SPAServer::threadFromRedis(const CameraThreadInfo& info)
  338. {
  339. SPDLOG_LOGGER_INFO(m_logger, "开启 fromRedisThread 线程,设备ID:{}", info.DeviceID);
  340. FromRedis fromRedis;
  341. fromRedis.setRedisIPAndPort(info.RedisIP, info.RedisPort);
  342. fromRedis.setRedisPassword(info.RedisPWD);
  343. if(fromRedis.connectRedis())
  344. {
  345. SPDLOG_LOGGER_INFO(m_logger, "连接Redis成功");
  346. }else {
  347. SPDLOG_LOGGER_ERROR(m_logger, "连接Redis失败");
  348. return;
  349. }
  350. CameraThreadInfo threadInfo = info;
  351. // std::string strKey = "117:OD210_026_005246_001-IZRTKyEx";
  352. /* 取出该设备的Key */
  353. std::vector<std::string> vecKey;
  354. for(const auto& it : info.vecAction)
  355. {
  356. std::string strKey = std::to_string(info.DeviceID) + ":" + it;
  357. vecKey.push_back(strKey);
  358. }
  359. std::string strRetValue;
  360. /* 进入线程循环 */
  361. while (m_threadRunning)
  362. {
  363. /* 循环读取这个设备关联的算法信息 */
  364. for(const auto& it : vecKey)
  365. {
  366. SPDLOG_LOGGER_INFO(m_logger, "读取Redis信息, Key: {}", it);
  367. if( !fromRedis.getRedisString(it, strRetValue) )
  368. {
  369. continue;
  370. }
  371. SPDLOG_LOGGER_TRACE(m_logger, "Redis Value:\n{}", strRetValue);
  372. /* 解析数据 */
  373. AlarmInfo alarmInfo;
  374. alarmInfo.ActionID = it.substr(it.find(":") + 1);
  375. /* 解析数据 */
  376. parseRedisData(strRetValue, alarmInfo);
  377. /* 信息时间有的效性判断,主要是记录Redis数据的有效性,是否长时间没有变化,排查错误时用的
  378. * 如果数据长时间数据不变,那么超脑那里就挂了,写入日志 */
  379. isEventTimeVaild(alarmInfo.EventTime);
  380. /* 是否需要写入EQM数据库判断
  381. * 人员计数和区域人员检测需要多次判断,其他的直接写入即可 */
  382. }
  383. std::this_thread::sleep_for(std::chrono::seconds(2));
  384. }
  385. return;
  386. }
  387. /* 解析Redis基础数据 */
  388. void SPAServer::parseRedisData(const std::string& strData, AlarmInfo& alarmInfo)
  389. {
  390. try
  391. {
  392. nJson json0;
  393. json0 = nJson::parse(strData);
  394. alarmInfo.AlarmID = json0["alarmId"].get<int>();
  395. alarmInfo.ChannelID = json0["channel"].get<int>();
  396. alarmInfo.PicUrl = json0["picUrl"].get<std::string>();
  397. alarmInfo.ImageInfo = json0["imageInfo"].get<std::string>();
  398. /* 解析时间,需要将时间中的“T”换成空格 */
  399. alarmInfo.StartTime = json0["beginTime"].get<std::string>();
  400. std::replace(alarmInfo.StartTime.begin(), alarmInfo.StartTime.end(), 'T', ' ');
  401. alarmInfo.EndTime = json0["endTime"].get<std::string>();
  402. std::replace(alarmInfo.EndTime.begin(), alarmInfo.EndTime.end(), 'T', ' ');
  403. alarmInfo.EventTime = json0["eventTime"].get<std::string>();
  404. std::replace(alarmInfo.EventTime.begin(), alarmInfo.EventTime.end(), 'T', ' ');
  405. /* 判断bBoxes有无数据,有数据就解析,没数据就直接返回了 */
  406. nJson json1 = json0["bBoxes"];
  407. std::string labelList; /* 记录违禁品 */
  408. std::string bboxList; /* 记录bbox */
  409. if(!json1.empty())
  410. {
  411. for(auto& it0 : json1)
  412. {
  413. /* 如果status是true,就不是报警,直接跳过 */
  414. bool status = it0["status"].get<bool>();
  415. if(status)
  416. {
  417. continue;
  418. }
  419. /* 这一条Box数据报警了将其内容存储起来 */
  420. alarmInfo.Is_Alarm = true;
  421. /* 解析label,违禁品关键字,先判断这个是不是违禁品检测的算法ID */
  422. if(alarmInfo.ActionID == g_actionList.ActContraband)
  423. {
  424. /* 解析报警,取出报警类型 */
  425. nJson label = it0["label"];
  426. for(auto& it1 : label)
  427. {
  428. std::string strLabel = it1.get<std::string>();
  429. /* 检测是否已经加入到字符串中了 */
  430. strLabel += "|";
  431. if(labelList.find(strLabel) != std::string::npos)
  432. {
  433. continue;
  434. }
  435. labelList += strLabel;
  436. }
  437. }
  438. /* 解析bbox,貌似是在图像中的位置 */
  439. nJson bbox = it0["bbox"];
  440. std::string strBbox;
  441. for(auto& it1 : bbox)
  442. {
  443. strBbox += std::to_string(it1.get<int>()) + ",";
  444. }
  445. /* 去掉最后一个“,” */
  446. if(!strBbox.empty())
  447. {
  448. strBbox.pop_back();
  449. }
  450. bboxList += strBbox + "|";
  451. }
  452. /* 去掉最后一个“|” */
  453. if(!labelList.empty())
  454. {
  455. labelList.pop_back();
  456. }
  457. if(!bboxList.empty())
  458. {
  459. bboxList.pop_back();
  460. }
  461. SPDLOG_LOGGER_DEBUG(m_logger, "违禁品列表:{}", labelList);
  462. SPDLOG_LOGGER_DEBUG(m_logger, "bbox列表:{}", bboxList);
  463. }
  464. /* 如果有报警的Box */
  465. if(alarmInfo.Is_Alarm)
  466. {
  467. /* 添加报警信息的提示信息 */
  468. alarmInfo.BboxList = bboxList;
  469. if( (alarmInfo.ActionID == g_actionList.ActContraband) && !labelList.empty() )
  470. {
  471. alarmInfo.ActionDes = fmt::format("出现违禁品[{}]告警", labelList);
  472. SPDLOG_LOGGER_INFO(m_logger, "{}", alarmInfo.ActionDes);
  473. }else {
  474. alarmInfo.ActionDes = json0["actionDes"].get<std::string>();
  475. }
  476. /* 判断有没有报警数据 */
  477. if(alarmInfo.ImageInfo.empty())
  478. {
  479. SPDLOG_LOGGER_ERROR(m_logger, "有报警区域,但是没有图片信息");
  480. return;
  481. }
  482. /* 如果是人员报警,就存储人员报警信息 */
  483. if(alarmInfo.ActionID == g_actionList.ActFace)
  484. {
  485. nJson jsonArray = json0["personList"];
  486. for(auto& it : jsonArray)
  487. {
  488. PersonInfo personInfo;
  489. personInfo.PersonID = it["personId"].get<std::string>();
  490. personInfo.PersonName = it["personName"].get<std::string>();
  491. alarmInfo.vecPersonInfo.push_back(personInfo);
  492. }
  493. }
  494. }
  495. }
  496. catch (const nJson::parse_error& e)
  497. {
  498. SPDLOG_LOGGER_ERROR(m_logger, "解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  499. return;
  500. }
  501. catch (const nJson::type_error& e)
  502. {
  503. SPDLOG_LOGGER_ERROR(m_logger, "解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  504. return;
  505. }
  506. }
  507. /**
  508. * @brief 解析Redis的基础通用数据,不包含bBoxes数组数据
  509. *
  510. * @param strData Redis返回的源数据,JSON格式
  511. * @param alarmInfo 解析出来的数据
  512. */
  513. void SPAServer::parseRedisBaseData(const std::string& strData, AlarmInfo& alarmInfo)
  514. {
  515. try
  516. {
  517. nJson json0;
  518. json0 = nJson::parse(strData);
  519. alarmInfo.AlarmID = json0["alarmId"].get<int>();
  520. alarmInfo.ChannelID = json0["channel"].get<int>();
  521. alarmInfo.PicUrl = json0["picUrl"].get<std::string>();
  522. alarmInfo.ImageInfo = json0["imageInfo"].get<std::string>();
  523. /* 解析时间,需要将时间中的“T”换成空格 */
  524. alarmInfo.StartTime = json0["beginTime"].get<std::string>();
  525. std::replace(alarmInfo.StartTime.begin(), alarmInfo.StartTime.end(), 'T', ' ');
  526. alarmInfo.EndTime = json0["endTime"].get<std::string>();
  527. std::replace(alarmInfo.EndTime.begin(), alarmInfo.EndTime.end(), 'T', ' ');
  528. alarmInfo.EventTime = json0["eventTime"].get<std::string>();
  529. std::replace(alarmInfo.EventTime.begin(), alarmInfo.EventTime.end(), 'T', ' ');
  530. }
  531. catch (const nJson::parse_error& e)
  532. {
  533. SPDLOG_LOGGER_ERROR(m_logger, "解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  534. return;
  535. }
  536. catch (const nJson::type_error& e)
  537. {
  538. SPDLOG_LOGGER_ERROR(m_logger, "解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  539. return;
  540. }
  541. }
  542. /**
  543. * @brief 解析Redis的bBoxes数据,这个内容可能根据算法ID不同,内容不同
  544. *
  545. * @param strData
  546. * @param alarmInfo
  547. */
  548. void SPAServer::parseRedisBBoxesData(const std::string& strData, AlarmInfo& alarmInfo)
  549. {
  550. try
  551. {
  552. nJson json0;
  553. json0 = nJson::parse(strData);
  554. /* 判断bBoxes有无数据,有数据就解析,没数据就直接返回了 */
  555. nJson json1 = json0["bBoxes"];
  556. std::string labelList; /* 记录违禁品 */
  557. std::string bboxList; /* 记录bbox */
  558. if(!json1.empty())
  559. {
  560. for(auto& it0 : json1)
  561. {
  562. /* 如果status是true,就不是报警,直接跳过 */
  563. bool status = it0["status"].get<bool>();
  564. if(status)
  565. {
  566. continue;
  567. }
  568. /* 这一条Box数据报警了将其内容存储起来 */
  569. alarmInfo.Is_Alarm = true;
  570. /* 解析label,违禁品关键字,先判断这个是不是违禁品检测的算法ID */
  571. if(alarmInfo.ActionID == g_actionList.ActContraband)
  572. {
  573. /* 解析报警,取出报警类型 */
  574. nJson label = it0["label"];
  575. for(auto& it1 : label)
  576. {
  577. std::string strLabel = it1.get<std::string>();
  578. /* 检测是否已经加入到字符串中了 */
  579. strLabel += "|";
  580. if(labelList.find(strLabel) != std::string::npos)
  581. {
  582. continue;
  583. }
  584. labelList += strLabel;
  585. }
  586. }
  587. /* 解析bbox,貌似是在图像中的位置 */
  588. nJson bbox = it0["bbox"];
  589. std::string strBbox;
  590. for(auto& it1 : bbox)
  591. {
  592. strBbox += std::to_string(it1.get<int>()) + ",";
  593. }
  594. /* 去掉最后一个“,” */
  595. if(!strBbox.empty())
  596. {
  597. strBbox.pop_back();
  598. }
  599. bboxList += strBbox + "|";
  600. }
  601. /* 去掉最后一个“|” */
  602. if(!labelList.empty())
  603. {
  604. labelList.pop_back();
  605. }
  606. if(!bboxList.empty())
  607. {
  608. bboxList.pop_back();
  609. }
  610. SPDLOG_LOGGER_DEBUG(m_logger, "违禁品列表:{}", labelList);
  611. SPDLOG_LOGGER_DEBUG(m_logger, "bbox列表:{}", bboxList);
  612. }
  613. /* 如果有报警的Box,解析出报警的说明 */
  614. if(alarmInfo.Is_Alarm)
  615. {
  616. /* 添加报警信息的提示信息 */
  617. alarmInfo.BboxList = bboxList;
  618. /* 违禁品报警信息,违禁品列表不是空的,就添加补充的文字 */
  619. if( (alarmInfo.ActionID == g_actionList.ActContraband) && !labelList.empty() )
  620. {
  621. alarmInfo.ActionDes = fmt::format("出现违禁品[{}]告警", labelList);
  622. SPDLOG_LOGGER_INFO(m_logger, "{}", alarmInfo.ActionDes);
  623. }else {
  624. /* 其他报警信息,直接获取 */
  625. alarmInfo.ActionDes = json0["actionDes"].get<std::string>();
  626. }
  627. /* 判断有没有报警数据 */
  628. if(alarmInfo.ImageInfo.empty())
  629. {
  630. SPDLOG_LOGGER_WARN(m_logger, "有报警区域,但是没有图片信息");
  631. return;
  632. }
  633. /* 如果是人员报警,就存储人员报警信息 */
  634. if(alarmInfo.ActionID == g_actionList.ActFace)
  635. {
  636. nJson jsonArray = json0["personList"];
  637. for(auto& it : jsonArray)
  638. {
  639. PersonInfo personInfo;
  640. personInfo.PersonID = it["personId"].get<std::string>();
  641. personInfo.PersonName = it["personName"].get<std::string>();
  642. alarmInfo.vecPersonInfo.push_back(personInfo);
  643. }
  644. }
  645. }
  646. }
  647. catch (const nJson::parse_error& e)
  648. {
  649. SPDLOG_LOGGER_ERROR(m_logger, "解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  650. return;
  651. }
  652. catch (const nJson::type_error& e)
  653. {
  654. SPDLOG_LOGGER_ERROR(m_logger, "解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  655. return;
  656. }
  657. }
  658. /**
  659. * @brief 判断时间是否长时间没有更新,默认的是600秒,超过这个时间Redis还未更新,可能是超脑挂了
  660. *
  661. * @param strTime
  662. * @return true
  663. * @return false
  664. */
  665. bool SPAServer::isEventTimeVaild(const std::string& strTime)
  666. {
  667. /* 获取当前时间 */
  668. std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
  669. /* 字符串转成时间 */
  670. std::istringstream iss(strTime);
  671. std::tm tmEvent = {};
  672. iss >> std::get_time(&tmEvent, "%Y-%m-%d %H:%M:%S");
  673. /* 时间差 */
  674. std::chrono::system_clock::time_point eventTime = std::chrono::system_clock::from_time_t(std::mktime(&tmEvent));
  675. std::chrono::duration<double> diff = now - eventTime;
  676. // SPDLOG_LOGGER_DEBUG(m_logger, "now:{} eventTime: {} 时间差:{}秒",now, eventTime, diff);
  677. if(diff.count() > 600)
  678. {
  679. // SPDLOG_LOGGER_ERROR(m_logger, "Redis数据长时间没有更新,EventTime:{}", strTime);
  680. return false;
  681. }
  682. return true;
  683. }
  684. /**
  685. * @brief 分派任务的线程
  686. 1、 线程定时刷新房间和摄像机的关联关系,以及和算法Action的关联关系
  687. 2、 将算法信息加入到不同的列表中
  688. 需要多个摄像机配合的加入到m_runListRoomActionInfo列表
  689. 不需要多个摄像机配合的加入到m_runListActionInfo列表
  690. 3、 每次刷新都会清空ActionInfo或者RoomActionInfo的摄像机列表,但是不会动其他基本信息,如果是列表中没有对应的信息,
  691. 就会创建新的ActionInfo,那么RunState的状态是INIT,需要开启新的线程
  692. 如果刷新完成后,算法对应的摄像机列表为空,那么对应的线程就会停止运行,将RunState设置为STOP,本线程的下一轮
  693. 循环就会删除这个ActionInfo
  694. */
  695. void SPAServer::threadRoomCamera()
  696. {
  697. /* 房间相机关联信息 */
  698. std::list<RoomCameraInfo> listRC;
  699. /* 存储获取到的应用和启用时间的信息 */
  700. std::list<AppAndTimeInfo> listAppAndTime;
  701. /* 创建连接数据库实例 */
  702. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  703. while (m_threadRunning)
  704. {
  705. m_mutexRunFAI.lock();
  706. /* 先清理已经退出的线程所用到的Action或者RoomAction */
  707. m_runListFuncActInfo.clearNoneFuncActionInfo();
  708. /* 创建应用信息,根据从EQM数据库读取到的配置的应用信息创建 */
  709. toEQMDataBase->getAlarmAppInfo(listAppAndTime);
  710. for(const auto& it : listAppAndTime)
  711. {
  712. /* 创建应用信息,如果已有该应用,就更新时间 */
  713. m_runListFuncActInfo.addFuncActionInfo(it);
  714. }
  715. /* 先获取EQM数据库信息,取出房间和摄像机关联信息 */
  716. m_mutexActionInfo.lock();
  717. toEQMDataBase->getActionInfo(m_listActionInfo);
  718. /* 将算法信息加入到不同的列表中 */
  719. m_runListFuncActInfo.clearActionList();
  720. for(const auto& it : m_listActionInfo.getData())
  721. {
  722. m_runListFuncActInfo.addActionInfo(*it);
  723. }
  724. /* 检查算法信息的状态,频率里的应用没有配置摄像机就设置为线程运行状态为停止,退出该线程 */
  725. for(const auto& it : m_runListFuncActInfo.getData())
  726. {
  727. if(it->listRoomCamActInfo.size() == 0)
  728. {
  729. it->RunState = RunTimeState::RUN_STATE_STOP;
  730. }
  731. }
  732. m_mutexActionInfo.unlock();
  733. /* 开启线程 */
  734. for(const auto& it0 : m_runListFuncActInfo.getData())
  735. {
  736. /* 人员在岗识别 */
  737. /* 区域人员检测 */
  738. /* 非法入侵检测 */
  739. /* 违禁品识别 */
  740. /* 普通任务线程,一个任务对应一个摄像机和一个算法ID,无需联动 */
  741. if(it0->appFunction == AppFunction::APP_ALLDOWN)
  742. {
  743. if(it0->RunState == RunTimeState::RUN_STATE_INIT)
  744. {
  745. CPPTP.add_task(&SPAServer::threadActNormal, this, it0);
  746. }
  747. }
  748. }
  749. m_mutexRunFAI.unlock();
  750. /* 休眠n秒,默认应该是300秒 */
  751. std::this_thread::sleep_for(std::chrono::seconds(g_config.CheckSet));
  752. }
  753. }
  754. /**
  755. * @brief 人员在岗识别线程,这个功能主要是给客户端提供实时的人脸识别信息
  756. * 1、这个写入tWorkOnInfo表格,十分钟写一次
  757. * 2、理论上这里需要实时更新数据库,但是实际上时每十分钟写入一次数据
  758. *
  759. * @param RFAInfo 传入房间ID和算法ID
  760. */
  761. void SPAServer::threadActPersonWork(FuncActionInfo* RFAInfo)
  762. {
  763. SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程,ChannelID:{} ,", RFAInfo->strFunctionName, RFAInfo->ChannelID);
  764. /* 创建读取Redis的实例 */
  765. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  766. /* 创建写入数据库实例 */
  767. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  768. /* 局部变量 */
  769. std::shared_ptr<FuncActionInfo> pRFAInfo = std::make_shared<FuncActionInfo>();
  770. *pRFAInfo = *RFAInfo;
  771. /* 保存每个摄像机的报警信息 */
  772. std::shared_ptr<std::list<AlarmInfo>> pListAlarmInfo = std::make_shared<std::list<AlarmInfo>>();
  773. /* 保存人脸信息的数据 */
  774. std::shared_ptr<ListRoomFaceInfo> pListRoomFaceInfo = std::make_shared<ListRoomFaceInfo>();
  775. while (m_threadRunning)
  776. {
  777. /* 更新线程信息 */
  778. updateFuncInfo(pRFAInfo);
  779. if(pRFAInfo->appFunction == AppFunction::APP_NONE)
  780. {
  781. break;
  782. }
  783. /* 读取Redis数据 */
  784. pListAlarmInfo->clear();
  785. for(const auto& RoomInfo : pRFAInfo->listRoomCamActInfo)
  786. {
  787. for(const auto& it : RoomInfo.mapCameraAction)
  788. {
  789. std::string strKey = std::to_string(it.first) + ":" + it.second;
  790. std::string strRetValue;
  791. if(!fromRedis->getRedisString(strKey, strRetValue))
  792. {
  793. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  794. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  795. continue;
  796. }
  797. /* 解析数据 */
  798. AlarmInfo alarmInfo;
  799. parseRedisData(strRetValue, alarmInfo);
  800. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  801. if(isEventTimeVaild(alarmInfo.EventTime))
  802. {
  803. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  804. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  805. continue;
  806. }
  807. pListAlarmInfo->push_back(alarmInfo);
  808. }
  809. }
  810. /* 处理数据,将报警信息的人脸信息取出来,放入到人脸信息列表中 */
  811. for(auto& alarmInfo : *pListAlarmInfo)
  812. {
  813. /* 添加到人脸列表中,会自动去重,自动创建对应的频道和房间信息 */
  814. pListRoomFaceInfo->addRoomFaceInfo(alarmInfo);
  815. }
  816. /* 计算人脸个数,并判断是否需要写入数据库 */
  817. QDateTime now = QDateTime::currentDateTime();
  818. for(auto it = pListRoomFaceInfo->listRoomFaceInfo.begin(); it != pListRoomFaceInfo->listRoomFaceInfo.end(); )
  819. {
  820. if(it->MaxNum < it->listPersonInfo.size())
  821. {
  822. it->MaxNum = it->listPersonInfo.size();
  823. }
  824. if(it->MinNum > it->listPersonInfo.size())
  825. {
  826. it->MinNum = it->listPersonInfo.size();
  827. }
  828. /* 判断是否需要写入数据库,超过设置的时间就写入,默认是600秒 */
  829. if(now.toSecsSinceEpoch() - it->StartTime.toSecsSinceEpoch() > g_config.AppUpdateOnWorkTimeInterval)
  830. {
  831. /* 写入数据库 */
  832. if(toEQMDataBase->insertOnWorkInfo(*it))
  833. {
  834. SPDLOG_LOGGER_INFO(m_logger, "ChannelID:{}, RoomID:{}, 在岗信息写入数据库成功, StartTime", it->ChannelID, it->RoomID, it->StartTime.toString("yyyy-MM-dd hh:mm:ss").toStdString());
  835. /* 删除这条信息 */
  836. it = pListRoomFaceInfo->listRoomFaceInfo.erase(it);
  837. continue;
  838. }
  839. }
  840. ++it;
  841. }
  842. }
  843. setThreadState(pRFAInfo, RunTimeState::RUN_STATE_STOP);
  844. SPDLOG_LOGGER_INFO(m_logger, "关闭 {} 线程,ChannelID:{}", pRFAInfo->strFunctionName, pRFAInfo->ChannelID);
  845. }
  846. /**
  847. * @brief 区域非法入侵检测
  848. 1、野猫、宠物识别:报警判断条件:机房内出现摄像头的宠物识别算法输出报警结果时,记为报警行为,直接
  849. 展示报警结果
  850. 2、无权限人员识别:报警判断条件:当判断出区域非法入侵行为时,依据人脸识别结果,判断是否为人脸库人
  851. 员,非人脸库的人员时判断为非法入侵行为(背影需要报警需要结合区域人员检测算法判断)
  852. 3、人员计数和人脸识别数不相等时,判断为非法入侵行为
  853. *
  854. * @param info
  855. */
  856. void SPAServer::threadActIllegalInvasion(FuncActionInfo* RFAInfo)
  857. {
  858. SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程,ChannelID:{} ,", RFAInfo->strFunctionName, RFAInfo->ChannelID);
  859. /* 创建读取Redis的实例 */
  860. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  861. /* 创建写入数据库实例 */
  862. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  863. /* 局部变量 */
  864. std::shared_ptr<FuncActionInfo> pRFAInfo = std::make_shared<FuncActionInfo>();
  865. *pRFAInfo = *RFAInfo;
  866. /* 保存每个摄像机的报警信息 */
  867. std::shared_ptr<std::list<AlarmInfo>> pListAlarmInfo = std::make_shared<std::list<AlarmInfo>>();
  868. /* 保存非法入侵的信息 */
  869. std::shared_ptr<ListIllegalInvasionInfo> pListIllInfo = std::make_shared<ListIllegalInvasionInfo>();
  870. std::string strFaceActionID; /* 人脸识别的Action */
  871. std::string strCountActionID; /* 人员计数的Action */
  872. /* 封装lambda表达式 */
  873. std::function<bool(const std::vector<PersonInfo>&)> funcIsAlarm = [](const std::vector<PersonInfo>& vecPersion) {
  874. for(const auto& it : vecPersion)
  875. {
  876. if(it.PersonID == "-1")
  877. {
  878. return true;
  879. }
  880. }
  881. return false;
  882. };
  883. while (m_threadRunning)
  884. {
  885. /* 更新线程信息 */
  886. updateFuncInfo(pRFAInfo);
  887. if(pRFAInfo->appFunction == AppFunction::APP_NONE)
  888. {
  889. break;
  890. }
  891. /* 读取Redis数据 */
  892. pListAlarmInfo->clear();
  893. for(const auto& RoomInfo : pRFAInfo->listRoomCamActInfo)
  894. {
  895. for(const auto& it : RoomInfo.mapCameraAction)
  896. {
  897. std::string strKey = std::to_string(it.first) + ":" + it.second;
  898. std::string strRetValue;
  899. if(!fromRedis->getRedisString(strKey, strRetValue))
  900. {
  901. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  902. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  903. continue;
  904. }
  905. /* 解析数据 */
  906. AlarmInfo alarmInfo;
  907. parseRedisData(strRetValue, alarmInfo);
  908. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  909. if(isEventTimeVaild(alarmInfo.EventTime))
  910. {
  911. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  912. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  913. continue;
  914. }
  915. pListAlarmInfo->push_back(alarmInfo);
  916. }
  917. }
  918. /* 更新算法ActionID */
  919. {
  920. g_actionList.mutexRW.lockForRead();
  921. strFaceActionID = g_actionList.ActFace;
  922. strCountActionID = g_actionList.ActPersonNumber;
  923. g_actionList.mutexRW.unlock();
  924. }
  925. /* 找出房间的个数,按照房间进行判断 */
  926. ListRoomIll listRoomIll;
  927. for(auto& it : pRFAInfo->listRoomCamActInfo)
  928. {
  929. listRoomIll.addRoom(it.RoomID, it.RoomType);
  930. }
  931. /* 根据房间解析人脸识别的非法入侵,这个有了就不用判断后面的人员计数不相等了 */
  932. for(auto& room : listRoomIll.getData())
  933. {
  934. for(const auto& it : *pListAlarmInfo)
  935. {
  936. /* 人俩识别算法 */
  937. if(it.ActionID == strFaceActionID)
  938. {
  939. if(it.vecPersonInfo.size() > room.numMaxFace)
  940. {
  941. room.numMaxFace = it.vecPersonInfo.size();
  942. room.strBoxList = it.BboxList;
  943. room.vecPersonInfo = it.vecPersonInfo;
  944. }
  945. }
  946. /* 判断有没有非法入侵人员 */
  947. if(funcIsAlarm(it.vecPersonInfo))
  948. {
  949. room.isAlarm = true;
  950. room.strMessage = "人员非法入侵(未知人员)";
  951. if( !it.ImageInfo.empty() )
  952. {
  953. room.strImage = it.ImageInfo;
  954. }
  955. room.CameraID = it.DeviceID;
  956. }
  957. }
  958. }
  959. /* 判断人脸识别到的数目和人员计数算法识别到的人数是否相等,如果上面报警了,这里就不用检测了 */
  960. for(auto& room : listRoomIll.getData())
  961. {
  962. if(room.isAlarm)
  963. {
  964. continue;
  965. }
  966. /* 这个房间的人脸算法没有识别到非法入侵 */
  967. for(const auto& it : *pListAlarmInfo)
  968. {
  969. if(it.ActionID == strCountActionID)
  970. {
  971. if(it.vecPersonInfo.size() > room.numMaxPerson)
  972. {
  973. room.numMaxPerson = it.vecPersonInfo.size();
  974. room.strBoxList = it.BboxList;
  975. if(!it.ImageInfo.empty())
  976. {
  977. room.strImage = it.ImageInfo;
  978. }
  979. room.CameraID = it.DeviceID;
  980. }
  981. }
  982. }
  983. /* 判断人数是否一致 */
  984. if(room.numMaxFace != room.numMaxPerson)
  985. {
  986. room.strMessage = fmt::format("非法入侵(人员计数 {} 和人脸数 {} 不一致)", room.numMaxPerson, room.numMaxFace);
  987. room.isAlarm = true;
  988. }
  989. }
  990. /* 将非法入侵的信息存储到数组缓存中,等待报警结束后判断是否需要写入EQM数据库 */
  991. for(auto& room : listRoomIll.getData())
  992. {
  993. if(room.isAlarm)
  994. {
  995. /* 正在报警,检查是否有 */
  996. if(pListIllInfo->findIllInfo(room.RoomID, room.RoomType) == nullptr)
  997. {
  998. IllegalInvasionInfo info;
  999. info.ChannelID = pRFAInfo->ChannelID;
  1000. info.CameraID = room.CameraID;
  1001. info.RoomID = room.RoomID;
  1002. info.RoomType = room.RoomType;
  1003. info.strActionDec = room.strMessage;
  1004. info.strImageInfo = room.strImage;
  1005. info.FirstTime = QDateTime::currentDateTime();;
  1006. pListIllInfo->addIllInfo(info);
  1007. }
  1008. }
  1009. else {
  1010. /* 没有报警,检查是否是报警结束了,是否符合写入数据库的条件 */
  1011. QDateTime currTime = QDateTime::currentDateTime();
  1012. }
  1013. }
  1014. }
  1015. setThreadState(pRFAInfo, RunTimeState::RUN_STATE_STOP);
  1016. SPDLOG_LOGGER_INFO(m_logger, "关闭 {} 线程,ChannelID:{}", pRFAInfo->strFunctionName, pRFAInfo->ChannelID);
  1017. }
  1018. /**
  1019. * @brief 区域人员检测,检测这个区域内的人数,不能少于多少人,不能多余多少人
  1020. * 1、报警判断条件:直播间报警:当前频率所有直播间摄像头的区域人员检测算法输出结果均大于或小于设定人
  1021. 数值时,记为报警行为,直接展示报警结果
  1022. 2、录播间报警:当前频率所有录播间摄像头的区域人员检测算法输出结果均大于或小于设定人数值时,记为报
  1023. 警行为,直接展示报警结果
  1024. 3、直播间+录播间报警:当前频率直播间人数+录播间人数大于或小于设定人数值时,记为报警行为,直接展示
  1025. 报警结果
  1026. *
  1027. * @param RAInfo 传入的房间ID和算法ID
  1028. */
  1029. void SPAServer::threadActRegionalPersonnelDetection(FuncActionInfo* RFAInfo)
  1030. {
  1031. SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程,ChannelID:{} ,", RFAInfo->strFunctionName, RFAInfo->ChannelID);
  1032. /* 创建读取Redis的实例 */
  1033. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  1034. /* 创建写入数据库实例 */
  1035. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  1036. /* 局部变量 */
  1037. std::shared_ptr<FuncActionInfo> pRFAInfo = std::make_shared<FuncActionInfo>();
  1038. *pRFAInfo = *RFAInfo;
  1039. /* 保存每个摄像机的报警信息 */
  1040. std::shared_ptr<std::list<AlarmInfo>> pListAlarmInfo = std::make_shared<std::list<AlarmInfo>>();
  1041. while (m_threadRunning)
  1042. {
  1043. /* 更新线程信息 */
  1044. updateFuncInfo(pRFAInfo);
  1045. if(pRFAInfo->appFunction == AppFunction::APP_NONE)
  1046. {
  1047. break;
  1048. }
  1049. /* 读取Redis数据 */
  1050. pListAlarmInfo->clear();
  1051. for(const auto& RoomInfo : pRFAInfo->listRoomCamActInfo)
  1052. {
  1053. for(const auto& it : RoomInfo.mapCameraAction)
  1054. {
  1055. std::string strKey = std::to_string(it.first) + ":" + it.second;
  1056. std::string strRetValue;
  1057. if(!fromRedis->getRedisString(strKey, strRetValue))
  1058. {
  1059. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  1060. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1061. continue;
  1062. }
  1063. /* 解析数据 */
  1064. AlarmInfo alarmInfo;
  1065. parseRedisData(strRetValue, alarmInfo);
  1066. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  1067. if(isEventTimeVaild(alarmInfo.EventTime))
  1068. {
  1069. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  1070. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1071. continue;
  1072. }
  1073. pListAlarmInfo->push_back(alarmInfo);
  1074. }
  1075. }
  1076. }
  1077. setThreadState(pRFAInfo, RunTimeState::RUN_STATE_STOP);
  1078. SPDLOG_LOGGER_INFO(m_logger, "关闭 {} 线程,ChannelID:{}", pRFAInfo->strFunctionName, pRFAInfo->ChannelID);
  1079. }
  1080. /**
  1081. * @brief 普通任务识别,一个算法只需要一个摄像机,包括 违禁品识别、玩手机检测、老鼠等
  1082. 1、报警判断条件:机房内出现摄像头的违禁物品识别算法输出结果包含指定违规内容时,记为报警行为,直接
  1083. 展示报警结果
  1084. 2、这里应该是不区分违禁物品是什么,只要有违禁物品就报警。如果一个违禁物品消失之前又出现了第二个违禁物品,
  1085. 两个违禁物品都消失后,这次报警才会结束。
  1086. 3、一有报警信息就写入到EQM数据库的tAlarmInfo表中,报警开始时间是EventTime
  1087. 4、如果报警结束时间小于设置的最小间隔,就删除这条数据,超过这个时间,就更新结束时间
  1088. 5、报警信息要有图片,如果当前没有图片,就使用上一张图片
  1089. *
  1090. * @param info 线程信息
  1091. */
  1092. void SPAServer::threadActNormal(FuncActionInfo* info)
  1093. {
  1094. SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程, ChannelID:{} ,App:{}",info->strFunctionName, info->ChannelID, info->strFunctionName);
  1095. /* 创建读取Redis的实例 */
  1096. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  1097. /* 创建写入EQM数据库实例 */
  1098. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  1099. /* 局部变量,保存这个线程的信息 */
  1100. std::shared_ptr<FuncActionInfo> pFuncAct = std::make_shared<FuncActionInfo>();
  1101. *pFuncAct = *info;
  1102. /* 保存报警数据 */
  1103. std::shared_ptr<ListAlarmInfo> listAlarmInfo = std::make_shared<ListAlarmInfo>();
  1104. while (m_threadRunning)
  1105. {
  1106. /* 更新算法关联的摄像机ID */
  1107. updateFuncInfo(pFuncAct);
  1108. /* 如果线程的运行状态为 */
  1109. if(pFuncAct->RunState == RunTimeState::RUN_STATE_STOP)
  1110. {
  1111. pFuncAct->appFunction = AppFunction::APP_NONE;
  1112. break;
  1113. }
  1114. /* 读取Redis信息,处理数据 */
  1115. for(const auto& roomInfo : pFuncAct->listRoomCamActInfo)
  1116. {
  1117. for(const auto& it : roomInfo.mapCameraAction)
  1118. {
  1119. /* 读取Redis数据 */
  1120. std::string strKey = std::to_string(it.first) + ":" + it.second;
  1121. std::string strRetValue;
  1122. if(!fromRedis->getRedisString(strKey, strRetValue))
  1123. {
  1124. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  1125. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1126. continue;
  1127. }
  1128. /* 解析数据 */
  1129. AlarmInfo alarmInfo;
  1130. parseRedisBaseData(strRetValue, alarmInfo);
  1131. parseRedisBBoxesData(strRetValue, alarmInfo);
  1132. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  1133. if(isEventTimeVaild(alarmInfo.EventTime))
  1134. {
  1135. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  1136. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1137. continue;
  1138. }
  1139. /* 找出数组中与当前报警ID相同的的报警信息 */
  1140. auto pAlarmInfo = listAlarmInfo->findAlarmInfo(alarmInfo);
  1141. if(pAlarmInfo == nullptr)
  1142. {
  1143. /* 没有找到报警记录,就新建一个 */
  1144. AlarmInfo newAlarmInfo = alarmInfo;
  1145. listAlarmInfo->addAlarmInfo(newAlarmInfo);
  1146. /* 重新查找该报警信息 */
  1147. pAlarmInfo = listAlarmInfo->findAlarmInfo(alarmInfo);
  1148. if(pAlarmInfo == nullptr)
  1149. {
  1150. SPDLOG_LOGGER_ERROR(m_logger, "查找报警信息失败, Key:{}", strKey);
  1151. continue;
  1152. }
  1153. }
  1154. /* 判断有无报警记录,新的报警就写入到EQM的tAlarmInfo表中,正在报警的记录就判断和更新,
  1155. * 结束报警就更新tAlarmInfo的报警数据的结束时间,并清空pAlarmInfo */
  1156. if(pAlarmInfo->Is_Alarm)
  1157. {
  1158. /* 正在报警中,检查是否报警结束 */
  1159. if(alarmInfo.Is_Alarm)
  1160. {
  1161. /* 更新图片信息 */
  1162. if(!alarmInfo.ImageInfo.empty())
  1163. {
  1164. pAlarmInfo->ImageInfo = alarmInfo.ImageInfo;
  1165. }
  1166. } else {
  1167. /* 报警结束,判断时长,更新数据库结束时间,结束时间是此时电脑时间 */
  1168. if(timeDiffWithNow(pAlarmInfo->EventTime) > g_config.Contraband)
  1169. {
  1170. pAlarmInfo->EndTime = chronoToStrTime(std::chrono::system_clock::now());
  1171. toEQMDataBase->updateAlarmEndTime(*pAlarmInfo);
  1172. }else {
  1173. /* 不够报警时间,目前不做任何处理,不删除EQM报警记录 */
  1174. }
  1175. /* 清空报警信息 */
  1176. pAlarmInfo->reInit();
  1177. }
  1178. }
  1179. else
  1180. {
  1181. /* 是新的报警 */
  1182. if(alarmInfo.Is_Alarm)
  1183. {
  1184. /* 图片不能是空,如果是空的,就不写入数据库 */
  1185. if(!alarmInfo.ImageInfo.empty())
  1186. {
  1187. /* 违禁品检测,开始时间是事件时间 */
  1188. alarmInfo.StartTime = alarmInfo.EventTime;
  1189. alarmInfo.EndTime = "";
  1190. if(toEQMDataBase->insertAlarmInfo(alarmInfo))
  1191. {
  1192. /* 保存新的报警记录 */
  1193. *pAlarmInfo = alarmInfo;
  1194. }else {
  1195. SPDLOG_LOGGER_ERROR(m_logger, "写入tAlarmInfo报警数据失败, Key: {}", strKey);
  1196. }
  1197. }else {
  1198. SPDLOG_LOGGER_ERROR(m_logger, "频道:{}, 房间:{}, 摄像机:{}, 算法:{}, 有报警区域, 但是没有图片信息", pFuncAct->ChannelID, roomInfo.RoomID, it.first, it.second);
  1199. }
  1200. }
  1201. }
  1202. }
  1203. }
  1204. /* 休眠设置的时间 */
  1205. std::this_thread::sleep_for(std::chrono::seconds(g_config.ThreadSleepMS));
  1206. }
  1207. /* 设置线程退出的状态 */
  1208. setThreadState(pFuncAct, RunTimeState::RUN_STATE_STOP);
  1209. SPDLOG_LOGGER_INFO(m_logger, "{} 线程退出,Channel:{}",pFuncAct->strFunctionName, pFuncAct->ChannelID);
  1210. }
  1211. /* 将该算法对应的摄像机放入摄像机列表 */
  1212. bool SPAServer::insertCameraToAction(RoomActionInfo* pRAInfo, std::list<RoomCameraInfo>& listRC, std::multimap<int, std::string>& mapCameraActionID)
  1213. {
  1214. for(const auto& rci : listRC)
  1215. {
  1216. if(rci.RoomID == pRAInfo->RoomID)
  1217. {
  1218. /* 这个摄像机在这个房间内,再判断这个摄像机有没有这个算法 */
  1219. for(const auto& camID : rci.listCameraID)
  1220. {
  1221. for(const auto& cai : mapCameraActionID)
  1222. {
  1223. if(camID == cai.first)
  1224. {
  1225. /* 再判断这个摄像机的算法是否是当前需要的 */
  1226. if(cai.second == pRAInfo->ActionID)
  1227. {
  1228. pRAInfo->listCameraID.push_back(camID);
  1229. }
  1230. }
  1231. }
  1232. }
  1233. }
  1234. }
  1235. return true;
  1236. }
  1237. /**
  1238. * @brief 功能线程更新功能信息,这里是从内存中的数组里获取
  1239. *
  1240. * @param pInfo
  1241. * @return true
  1242. * @return false
  1243. */
  1244. bool SPAServer::updateFuncInfo(std::shared_ptr<FuncActionInfo> pInfo)
  1245. {
  1246. pInfo->clearActionList();
  1247. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  1248. auto fa = m_runListFuncActInfo.findAppFunction(*pInfo);
  1249. if(fa == nullptr)
  1250. {
  1251. return false;
  1252. }
  1253. *pInfo = *fa;
  1254. return true;
  1255. }
  1256. /* 设置线程状态 */
  1257. void SPAServer::setThreadState(std::shared_ptr<FuncActionInfo> pInfo, RunTimeState state)
  1258. {
  1259. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  1260. auto p = m_runListFuncActInfo.findAppFunction(*pInfo);
  1261. if(p != nullptr)
  1262. {
  1263. p->RunState = state;
  1264. }
  1265. }
  1266. /* 计算与当前时间的时间差,返回秒 */
  1267. int SPAServer::timeDiffWithNow(const std::string& strTime)
  1268. {
  1269. auto now = std::chrono::system_clock::now();
  1270. auto eventTime = strTimeToChrono(strTime);
  1271. std::chrono::duration<double> diff = now - eventTime;
  1272. return diff.count();
  1273. }
  1274. /* 字符串时间转换成std::chrono时间点 */
  1275. std::chrono::system_clock::time_point SPAServer::strTimeToChrono(const std::string& strTime)
  1276. {
  1277. std::istringstream iss(strTime);
  1278. std::tm tmEvent = {};
  1279. iss >> std::get_time(&tmEvent, "%Y-%m-%d %H:%M:%S");
  1280. return std::chrono::system_clock::from_time_t(std::mktime(&tmEvent));
  1281. }
  1282. /* 时间点转换成字符串 */
  1283. std::string SPAServer::chronoToStrTime(const std::chrono::system_clock::time_point& timePoint)
  1284. {
  1285. std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
  1286. std::tm tmEvent = *std::localtime(&time);
  1287. char buf[64] = {0};
  1288. std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tmEvent);
  1289. return std::string(buf);
  1290. }