SPAServer.cpp 48 KB

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