SPAServer.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  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. m_keyContraband = "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. m_keyContraband = "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 == m_keyContraband)
  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 == m_keyContraband) && !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 == m_KeyFace)
  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 == m_keyContraband)
  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 == m_keyContraband) && !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 == m_KeyFace)
  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::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  701. while (m_threadRunning)
  702. {
  703. /* 先获取EQM数据库信息,取出房间和摄像机关联信息 */
  704. m_mutexActionInfo.lock();
  705. toEQMDataBase->getActionInfo(m_listActionInfo);
  706. /* 取出每个房间的所有算法,int是RoomID,string是Action */
  707. // std::multimap<int, std::string> mapCameraActionID;
  708. m_mutexRunRAI.lock();
  709. m_mutexRunAI.lock();
  710. /* 先清理已经退出的线程所用到的Action或者RoomAction */
  711. m_runListRoomActionInfo.clearStopRoomAction();
  712. m_runListActionInfo.clearStopAction();
  713. /* 将算法信息加入到不同的列表中
  714. * 需要多个摄像机配合的加入到m_runListRoomActionInfo列表
  715. * 不需要多个摄像机配合的加入到m_runListActionInfo列表
  716. * */
  717. m_runListRoomActionInfo.clearCameraList();
  718. m_runListActionInfo.clearCameraList();
  719. for(const auto& it : m_listActionInfo.getData())
  720. {
  721. if(it->ActionID == ActPersonWork || it->ActionID == ActPersonNumber)
  722. {
  723. m_runListRoomActionInfo.addActionInfo(*it);
  724. }else {
  725. m_runListActionInfo.addActionCamera(it);
  726. }
  727. }
  728. m_mutexActionInfo.unlock();
  729. /* 这个容器只用于多个摄像机融合的算法,一个功能是一个线程 */
  730. for(const auto& it0 : m_runListRoomActionInfo.getData())
  731. {
  732. /* 人员在岗识别 */
  733. if(it0->ActionID == ActPersonWork)
  734. {
  735. SPDLOG_LOGGER_INFO(m_logger, "RoomID:{} 人员在岗识别", it0->RoomID);
  736. /* 判断是否需要创建新的线程 */
  737. if(it0->RunState == RunTimeState::RUN_STATE_INIT)
  738. {
  739. /* 创建新的线程 */
  740. CPPTP.add_task(&SPAServer::threadActPersonWork, this, it0);
  741. }
  742. }
  743. /* 区域人员检测 */
  744. else if (it0->ActionID == ActPersonNumber)
  745. {
  746. SPDLOG_LOGGER_INFO(m_logger, "RoomID:{} 区域人员检测", it0->RoomID);
  747. if(it0->RunState == RunTimeState::RUN_STATE_INIT)
  748. {
  749. CPPTP.add_task(&SPAServer::threadActRegionalPersonnelDetection, this, it0);
  750. }
  751. }
  752. /* 非法入侵 */
  753. else if( it0->ActionID == ActIllegalInvasion)
  754. {
  755. }
  756. }
  757. /* 这个容器用于不关联的算法信息,每个设备上的每个算法是一个线程
  758. * ActionInfo.RunState = RUN_STATE_INIT ,就是新增的算法,需要创建新的线程 */
  759. for(const auto& it0 : m_runListActionInfo.getData())
  760. {
  761. /* 违禁物品识别,不需要多个摄像机融合判断,多少个报警都直接上报 */
  762. if (it0->ActionID == ActContraband)
  763. {
  764. SPDLOG_LOGGER_INFO(m_logger, "RoomID:{} 违禁品检测", it0->RoomID);
  765. it0->strActionName = "违禁品检测";
  766. if(it0->RunState == RunTimeState::RUN_STATE_INIT)
  767. {
  768. CPPTP.add_task(&SPAServer::threadActNormal, this, it0);
  769. }
  770. }
  771. /* 非法入侵检测,这个也不需要摄像机融合 */
  772. else if (it0->ActionID == ActIllegalInvasion)
  773. {
  774. SPDLOG_LOGGER_INFO(m_logger, "RoomID:{} 非法入侵检测", it0->RoomID);
  775. }
  776. /* 疲劳检测,不需要摄像机融合检测,直接上报 */
  777. else if (it0->ActionID == ActFatigueDetection)
  778. {
  779. SPDLOG_LOGGER_INFO(m_logger, "RoomID:{} 疲劳检测", it0->RoomID);
  780. }
  781. }
  782. m_mutexRunRAI.unlock();
  783. m_mutexRunAI.unlock();
  784. /* 休眠n秒,默认应该是300秒 */
  785. std::this_thread::sleep_for(std::chrono::seconds(g_config.CheckSet));
  786. }
  787. }
  788. /* 将该算法对应的摄像机放入摄像机列表 */
  789. bool SPAServer::insertCameraToAction(RoomActionInfo* pRAInfo, std::list<RoomCameraInfo>& listRC, std::multimap<int, std::string>& mapCameraActionID)
  790. {
  791. for(const auto& rci : listRC)
  792. {
  793. if(rci.RoomID == pRAInfo->RoomID)
  794. {
  795. /* 这个摄像机在这个房间内,再判断这个摄像机有没有这个算法 */
  796. for(const auto& camID : rci.listCameraID)
  797. {
  798. for(const auto& cai : mapCameraActionID)
  799. {
  800. if(camID == cai.first)
  801. {
  802. /* 再判断这个摄像机的算法是否是当前需要的 */
  803. if(cai.second == pRAInfo->ActionID)
  804. {
  805. pRAInfo->listCameraID.push_back(camID);
  806. }
  807. }
  808. }
  809. }
  810. }
  811. }
  812. return true;
  813. }
  814. /* 更新房间、算法需要的摄像机个数,从内存中更新 */
  815. bool SPAServer::updateRoomActionCameraCount(std::shared_ptr<RoomActionInfo> pRAInfo)
  816. {
  817. std::lock_guard<std::mutex> look(m_mutexRunRAI);
  818. for(auto& it : m_runListRoomActionInfo.getData())
  819. {
  820. if((it->RoomID == pRAInfo->RoomID) && (it->ActionID == pRAInfo->ActionID))
  821. {
  822. it->listCameraID = pRAInfo->listCameraID;
  823. break;
  824. }
  825. }
  826. return true;
  827. }
  828. /* 更新算法的摄像机ID,这里是从内存中的数组里获取 */
  829. bool SPAServer::updateActionCameraID(std::shared_ptr<FuncActionInfo> pInfo)
  830. {
  831. pInfo->listRoomCamActInfo = -1;
  832. std::lock_guard<std::mutex> look(m_mutexRunAI);
  833. for(auto& it : m_runListActionInfo.getData())
  834. {
  835. if(it->isEqualBaseInfo(*pInfo))
  836. {
  837. pInfo->CameraID = it->CameraID;
  838. break;
  839. }
  840. }
  841. return true;
  842. }
  843. /* 设置线程状态 */
  844. void SPAServer::setThreadState(std::shared_ptr<ActionInfo> pInfo, RunTimeState state)
  845. {
  846. std::lock_guard<std::mutex> look(m_mutexRunAI);
  847. for(auto& it : m_runListActionInfo.getData())
  848. {
  849. if(it->isEqualBaseInfo(*pInfo))
  850. {
  851. it->RunState = state;
  852. break;
  853. }
  854. }
  855. }
  856. /* 设置线程状态 */
  857. void SPAServer::setThreadState(std::shared_ptr<RoomActionInfo> pInfo, RunTimeState state)
  858. {
  859. std::lock_guard<std::mutex> look(m_mutexRunRAI);
  860. for(auto& it : m_runListRoomActionInfo.getData())
  861. {
  862. if(it->isEqualBaseInfo(*pInfo))
  863. {
  864. it->RunState = state;
  865. break;
  866. }
  867. }
  868. }
  869. /**
  870. * @brief 人员在岗识别线程,应该是人脸识别线程,这个需要房间内多个摄像机共同识别
  871. 1、离岗判断条件:直播间无人算离岗(直播间所有摄像头的区域人员检测算法输出结果都为0时记为离岗开始,
  872. 当结果存在非0时记为离岗结束)
  873. 2、离岗输出结果:当判断出离岗行为时,依据人脸识别结果,找到离岗时间点前有人的最近一帧画面中的人员,
  874. 判断为离岗人员,并计算离岗时长,形成离岗记录(频率、机房、离岗人员、离岗开始时间、离岗结束时间、离
  875. 岗持续时长)
  876. 3、在岗时长计算方式:每天00:00:00-23:59:59人脸识别结果中所有人员名称的出现次数✖抽帧频率(如5秒抽
  877. 取1帧,则出现200次人名表示在岗1000秒时长),形成每日的人员在岗时长记录
  878. 4、报警判断条件:依据离岗次数和单次离岗时长进行报警,展示报警结果
  879. *
  880. * @param pRAInfo 传入房间ID和算法ID
  881. */
  882. void SPAServer::threadActPersonWork(RoomActionInfo* RAInfo)
  883. {
  884. SPDLOG_LOGGER_INFO(m_logger, "开启人员在岗识别线程,RoomID:{} ,Action:{}", RAInfo->RoomID, RAInfo->ActionID);
  885. /* 创建读取Redis的实例 */
  886. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  887. /* 局部变量 */
  888. std::shared_ptr<RoomActionInfo> pRAInfo = std::make_shared<RoomActionInfo>();
  889. *pRAInfo = *RAInfo;
  890. int CameraCount = pRAInfo->listCameraID.size();
  891. /* 摄像机数目小于0就退出线程 */
  892. while (m_threadRunning)
  893. {
  894. /* 更新算法关联的摄像机个数 */
  895. pRAInfo->listCameraID.clear();
  896. updateRoomActionCameraCount(pRAInfo);
  897. CameraCount = pRAInfo->listCameraID.size();
  898. if(CameraCount == 0)
  899. {
  900. break;
  901. }
  902. /* 读取Redis数据 */
  903. for(const auto& camID : pRAInfo->listCameraID)
  904. {
  905. std::string strKey = std::to_string(camID) + ":" + pRAInfo->ActionID;
  906. std::string strRetValue;
  907. if(!fromRedis->getRedisString(strKey, strRetValue))
  908. {
  909. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  910. continue;
  911. }
  912. /* 解析数据 */
  913. AlarmInfo alarmInfo;
  914. parseRedisData(strRetValue, alarmInfo);
  915. }
  916. }
  917. }
  918. /**
  919. * @brief 区域人员检测,检测这个区域内的人数,不能少于多少人,不能多余多少人
  920. * 1、报警判断条件:直播间报警:当前频率所有直播间摄像头的区域人员检测算法输出结果均大于或小于设定人
  921. 数值时,记为报警行为,直接展示报警结果
  922. 2、录播间报警:当前频率所有录播间摄像头的区域人员检测算法输出结果均大于或小于设定人数值时,记为报
  923. 警行为,直接展示报警结果
  924. 3、直播间+录播间报警:当前频率直播间人数+录播间人数大于或小于设定人数值时,记为报警行为,直接展示
  925. 报警结果
  926. *
  927. * @param RAInfo 传入的房间ID和算法ID
  928. */
  929. void SPAServer::threadActRegionalPersonnelDetection(RoomActionInfo* RAInfo)
  930. {
  931. SPDLOG_LOGGER_INFO(m_logger, "开启区域人员检测线程,RoomID:{} ,Action:{}", RAInfo->RoomID, RAInfo->ActionID);
  932. /* 创建读取Redis的实例 */
  933. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  934. /* 局部变量 */
  935. std::shared_ptr<RoomActionInfo> pRAInfo = std::make_shared<RoomActionInfo>();
  936. /* 创建写入数据库实例 */
  937. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  938. *pRAInfo = *RAInfo;
  939. int CameraCount = 0;
  940. /* 保存每个摄像机的报警信息 */
  941. std::shared_ptr<std::list<AlarmInfo>> pListAlarmInfo = std::make_shared<std::list<AlarmInfo>>();
  942. while (m_threadRunning)
  943. {
  944. /* 更新算法关联的摄像机个数 */
  945. pRAInfo->listCameraID.clear();
  946. updateRoomActionCameraCount(pRAInfo);
  947. CameraCount = pRAInfo->listCameraID.size();
  948. /* 摄像机数目等于0就退出线程 */
  949. if(CameraCount == 0)
  950. {
  951. break;
  952. }
  953. /* 读取Redis数据 */
  954. for(const auto& camID : pRAInfo->listCameraID)
  955. {
  956. std::string strKey = std::to_string(camID) + ":" + pRAInfo->ActionID;
  957. std::string strRetValue;
  958. if(!fromRedis->getRedisString(strKey, strRetValue))
  959. {
  960. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  961. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  962. continue;
  963. }
  964. /* 解析数据 */
  965. AlarmInfo alarmInfo;
  966. parseRedisBaseData(strRetValue, alarmInfo);
  967. parseRedisBBoxesData(strRetValue, alarmInfo);
  968. }
  969. }
  970. setThreadState(pRAInfo, RunTimeState::RUN_STATE_STOP);
  971. SPDLOG_LOGGER_INFO(m_logger, "关闭区域人员检测线程,RoomID:{} ,Action:{}", RAInfo->RoomID, RAInfo->ActionID);
  972. }
  973. /**
  974. * @brief 普通任务识别,一个算法只需要一个摄像机,包括 违禁品识别、玩手机检测、老鼠等
  975. 1、报警判断条件:机房内出现摄像头的违禁物品识别算法输出结果包含指定违规内容时,记为报警行为,直接
  976. 展示报警结果
  977. 2、这里应该是不区分违禁物品是什么,只要有违禁物品就报警。如果一个违禁物品消失之前又出现了第二个违禁物品,
  978. 两个违禁物品都消失后,这次报警才会结束。
  979. 3、一有报警信息就写入到EQM数据库的tAlarmInfo表中,报警开始时间是EventTime
  980. 4、如果报警结束时间小于设置的最小间隔,就删除这条数据,超过这个时间,就更新结束时间
  981. 5、报警信息要有图片,如果当前没有图片,就使用上一张图片
  982. *
  983. * @param info 线程信息
  984. */
  985. void SPAServer::threadActNormal(FuncActionInfo* info)
  986. {
  987. SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程, ChannelID:{} ,Action:{}",info->strFunctionName, info->ChannelID, info->listRoomCamActInfo.front().mapCameraAction.begin()->second);
  988. /* 创建读取Redis的实例 */
  989. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  990. /* 创建写入EQM数据库实例 */
  991. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  992. /* 局部变量,保存这个线程的信息 */
  993. std::shared_ptr<FuncActionInfo> pFuncAct = std::make_shared<FuncActionInfo>();
  994. *pFuncAct = *info;
  995. /* 保存报警数据 */
  996. std::shared_ptr<AlarmInfo> pAlarmInfo = std::make_shared<AlarmInfo>();
  997. while (m_threadRunning)
  998. {
  999. /* 更新算法关联的摄像机ID */
  1000. updateActionCameraID(pFuncAct);
  1001. /* 如果摄像机ID是小于0,那么这个算法就没有对应的摄像机了,线程就退出了 */
  1002. if(pActInfo->CameraID < 0)
  1003. {
  1004. break;
  1005. }
  1006. /* 读取Redis数据 */
  1007. std::string strKey = std::to_string(pActInfo->CameraID) + ":" + pActInfo->ActionID;
  1008. std::string strRetValue;
  1009. if(!fromRedis->getRedisString(strKey, strRetValue))
  1010. {
  1011. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  1012. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1013. continue;
  1014. }
  1015. /* 解析数据 */
  1016. AlarmInfo alarmInfo;
  1017. parseRedisBaseData(strRetValue, alarmInfo);
  1018. parseRedisBBoxesData(strRetValue, alarmInfo);
  1019. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  1020. if(isEventTimeVaild(alarmInfo.EventTime))
  1021. {
  1022. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  1023. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1024. continue;
  1025. }
  1026. /* 判断有无报警记录,新的报警就写入到EQM的tAlarmInfo表中,正在报警的记录就判断和更新,
  1027. * 结束报警就更新tAlarmInfo的报警数据的结束时间,并清空pAlarmInfo */
  1028. if(pAlarmInfo->Is_Alarm)
  1029. {
  1030. /* 正在报警中,检查是否报警结束 */
  1031. if(alarmInfo.Is_Alarm)
  1032. {
  1033. /* 更新图片信息 */
  1034. if(!alarmInfo.ImageInfo.empty())
  1035. {
  1036. pAlarmInfo->ImageInfo = alarmInfo.ImageInfo;
  1037. }
  1038. } else {
  1039. /* 报警结束,判断时长,更新数据库结束时间,结束时间是此时电脑时间 */
  1040. if(timeDiffWithNow(pAlarmInfo->EventTime) > g_config.Contraband)
  1041. {
  1042. pAlarmInfo->EndTime = chronoToStrTime(std::chrono::system_clock::now());
  1043. toEQMDataBase->updateAlarmEndTime(*pAlarmInfo);
  1044. }else {
  1045. /* 不够报警时间,目前不做任何处理,不删除EQM报警记录 */
  1046. }
  1047. /* 清空报警信息 */
  1048. pAlarmInfo->reInit();
  1049. }
  1050. }
  1051. else
  1052. {
  1053. /* 是新的报警 */
  1054. if(alarmInfo.Is_Alarm)
  1055. {
  1056. /* 布片不能是空,如果是空的,就不写入数据库 */
  1057. if(!alarmInfo.ImageInfo.empty())
  1058. {
  1059. /* 违禁品检测,开始时间是事件时间 */
  1060. alarmInfo.StartTime = alarmInfo.EventTime;
  1061. alarmInfo.EndTime = "";
  1062. if(toEQMDataBase->insertAlarmInfo(alarmInfo))
  1063. {
  1064. /* 保存新的报警记录 */
  1065. *pAlarmInfo = alarmInfo;
  1066. }else {
  1067. SPDLOG_LOGGER_ERROR(m_logger, "写入tAlarmInfo报警数据失败, Key: {}", strKey);
  1068. }
  1069. }else {
  1070. SPDLOG_LOGGER_ERROR(m_logger, "频道:{}, 房间:{}, 摄像机:{}, 算法:{}, 有报警区域, 但是没有图片信息", pActInfo->ChannelID, pActInfo->RoomID, pActInfo->CameraID, pActInfo->ActionID);
  1071. }
  1072. }
  1073. }
  1074. /* 休眠设置的时间 */
  1075. std::this_thread::sleep_for(std::chrono::seconds(g_config.ThreadSleepMS));
  1076. }
  1077. /* 设置线程退出的状态 */
  1078. setThreadState(pActInfo, RunTimeState::RUN_STATE_STOP);
  1079. SPDLOG_LOGGER_INFO(m_logger, "{} 线程退出,RoomID:{} ,CameraID, Action:{}",pActInfo->strActionName, pActInfo->RoomID, pActInfo->CameraID, pActInfo->ActionID);
  1080. }
  1081. /**
  1082. * @brief 非法入侵检测
  1083. 1、野猫、宠物识别:报警判断条件:机房内出现摄像头的宠物识别算法输出报警结果时,记为报警行为,直接
  1084. 展示报警结果
  1085. 2、无权限人员识别:报警判断条件:当判断出区域非法入侵行为时,依据人脸识别结果,判断是否为人脸库人
  1086. 员,非人脸库的人员时判断为非法入侵行为(背影需要报警需要结合区域人员检测算法判断)
  1087. *
  1088. * @param info
  1089. */
  1090. void SPAServer::threadActIllegalInvasion(ActionInfo* info)
  1091. {
  1092. SPDLOG_LOGGER_INFO(m_logger, "开启非法入侵检测线程,RoomID:{} ,Action:{}", info->RoomID, info->ActionID);
  1093. /* 创建读取Redis的实例 */
  1094. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  1095. /* 局部变量,保存这个线程的信息 */
  1096. std::shared_ptr<ActionInfo> pActInfo = std::make_shared<ActionInfo>();
  1097. *pActInfo = *info;
  1098. /* 摄像机ID小于0就退出线程 */
  1099. while (m_threadRunning)
  1100. {
  1101. /* 更新算法关联的摄像机ID */
  1102. updateActionCameraID(pActInfo);
  1103. /* 如果摄像机ID是小于0,那么这个算法就没有对应的摄像机了,线程就退出了 */
  1104. if(pActInfo->CameraID < 0)
  1105. {
  1106. break;
  1107. }
  1108. /* 读取Redis数据 */
  1109. std::string strKey = std::to_string(pActInfo->CameraID) + ":" + pActInfo->ActionID;
  1110. std::string strRetValue;
  1111. if(!fromRedis->getRedisString(strKey, strRetValue))
  1112. {
  1113. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  1114. continue;
  1115. }
  1116. /* 解析数据 */
  1117. AlarmInfo alarmInfo;
  1118. parseRedisData(strRetValue, alarmInfo);
  1119. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  1120. if(isEventTimeVaild(alarmInfo.EventTime))
  1121. {
  1122. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  1123. continue;
  1124. }
  1125. /* 判断有无报警记录,写入到EQM数据库 */
  1126. }
  1127. /* 设置线程退出的状态 */
  1128. setThreadState(pActInfo, RunTimeState::RUN_STATE_STOP);
  1129. SPDLOG_LOGGER_INFO(m_logger, "非法入侵检测线程退出,RoomID:{} ,CameraID:{}, Action:{}", pActInfo->RoomID, pActInfo->CameraID, pActInfo->ActionID);
  1130. }
  1131. /* 计算与当前时间的时间差,返回秒 */
  1132. int SPAServer::timeDiffWithNow(const std::string& strTime)
  1133. {
  1134. auto now = std::chrono::system_clock::now();
  1135. auto eventTime = strTimeToChrono(strTime);
  1136. std::chrono::duration<double> diff = now - eventTime;
  1137. return diff.count();
  1138. }
  1139. /* 字符串时间转换成std::chrono时间点 */
  1140. std::chrono::system_clock::time_point SPAServer::strTimeToChrono(const std::string& strTime)
  1141. {
  1142. std::istringstream iss(strTime);
  1143. std::tm tmEvent = {};
  1144. iss >> std::get_time(&tmEvent, "%Y-%m-%d %H:%M:%S");
  1145. return std::chrono::system_clock::from_time_t(std::mktime(&tmEvent));
  1146. }
  1147. /* 时间点转换成字符串 */
  1148. std::string SPAServer::chronoToStrTime(const std::chrono::system_clock::time_point& timePoint)
  1149. {
  1150. std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
  1151. std::tm tmEvent = *std::localtime(&time);
  1152. char buf[64] = {0};
  1153. std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tmEvent);
  1154. return std::string(buf);
  1155. }