SPAServer.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  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. parseRedisBaseData(strRetValue, alarmInfo);
  377. parseRedisBBoxesData(strRetValue, alarmInfo);
  378. /* 信息时间有的效性判断,主要是记录Redis数据的有效性,是否长时间没有变化,排查错误时用的
  379. * 如果数据长时间数据不变,那么超脑那里就挂了,写入日志 */
  380. isEventTimeVaild(alarmInfo.EventTime);
  381. /* 是否需要写入EQM数据库判断
  382. * 人员计数和区域人员检测需要多次判断,其他的直接写入即可 */
  383. }
  384. std::this_thread::sleep_for(std::chrono::seconds(2));
  385. }
  386. return;
  387. }
  388. // /* 解析Redis基础数据 */
  389. // void SPAServer::parseRedisData(const std::string& strData, AlarmInfo& alarmInfo)
  390. // {
  391. // try
  392. // {
  393. // nJson json0;
  394. // json0 = nJson::parse(strData);
  395. // alarmInfo.AlarmID = json0["alarmId"].get<int>();
  396. // alarmInfo.ChannelID = json0["channel"].get<int>();
  397. // alarmInfo.PicUrl = json0["picUrl"].get<std::string>();
  398. // alarmInfo.ImageInfo = json0["imageInfo"].get<std::string>();
  399. // /* 解析时间,需要将时间中的“T”换成空格 */
  400. // alarmInfo.StartTime = json0["beginTime"].get<std::string>();
  401. // std::replace(alarmInfo.StartTime.begin(), alarmInfo.StartTime.end(), 'T', ' ');
  402. // alarmInfo.EndTime = json0["endTime"].get<std::string>();
  403. // std::replace(alarmInfo.EndTime.begin(), alarmInfo.EndTime.end(), 'T', ' ');
  404. // alarmInfo.EventTime = json0["eventTime"].get<std::string>();
  405. // std::replace(alarmInfo.EventTime.begin(), alarmInfo.EventTime.end(), 'T', ' ');
  406. // /* 判断bBoxes有无数据,有数据就解析,没数据就直接返回了 */
  407. // nJson json1 = json0["bBoxes"];
  408. // std::string labelList; /* 记录违禁品 */
  409. // std::string bboxList; /* 记录bbox */
  410. // if(!json1.empty())
  411. // {
  412. // for(auto& it0 : json1)
  413. // {
  414. // /* 如果status是true,就不是报警,直接跳过 */
  415. // bool status = it0["status"].get<bool>();
  416. // if(status)
  417. // {
  418. // continue;
  419. // }
  420. // /* 这一条Box数据报警了将其内容存储起来 */
  421. // alarmInfo.Is_Alarm = true;
  422. // /* 解析label,违禁品关键字,先判断这个是不是违禁品检测的算法ID */
  423. // if(alarmInfo.ActionID == g_actionList.ActContraband)
  424. // {
  425. // /* 解析报警,取出报警类型 */
  426. // nJson label = it0["label"];
  427. // for(auto& it1 : label)
  428. // {
  429. // std::string strLabel = it1.get<std::string>();
  430. // /* 检测是否已经加入到字符串中了 */
  431. // strLabel += "|";
  432. // if(labelList.find(strLabel) != std::string::npos)
  433. // {
  434. // continue;
  435. // }
  436. // labelList += strLabel;
  437. // }
  438. // }
  439. // /* 解析bbox,貌似是在图像中的位置 */
  440. // nJson bbox = it0["bbox"];
  441. // std::string strBbox;
  442. // for(auto& it1 : bbox)
  443. // {
  444. // strBbox += std::to_string(it1.get<int>()) + ",";
  445. // }
  446. // /* 去掉最后一个“,” */
  447. // if(!strBbox.empty())
  448. // {
  449. // strBbox.pop_back();
  450. // }
  451. // bboxList += strBbox + "|";
  452. // }
  453. // /* 去掉最后一个“|” */
  454. // if(!labelList.empty())
  455. // {
  456. // labelList.pop_back();
  457. // }
  458. // if(!bboxList.empty())
  459. // {
  460. // bboxList.pop_back();
  461. // }
  462. // SPDLOG_LOGGER_DEBUG(m_logger, "违禁品列表:{}", labelList);
  463. // SPDLOG_LOGGER_DEBUG(m_logger, "bbox列表:{}", bboxList);
  464. // }
  465. // /* 如果有报警的Box */
  466. // if(alarmInfo.Is_Alarm)
  467. // {
  468. // /* 添加报警信息的提示信息 */
  469. // alarmInfo.BboxList = bboxList;
  470. // if( (alarmInfo.ActionID == g_actionList.ActContraband) && !labelList.empty() )
  471. // {
  472. // alarmInfo.ActionDes = fmt::format("出现违禁品[{}]告警", labelList);
  473. // SPDLOG_LOGGER_INFO(m_logger, "{}", alarmInfo.ActionDes);
  474. // }else {
  475. // alarmInfo.ActionDes = json0["actionDes"].get<std::string>();
  476. // }
  477. // /* 判断有没有报警数据 */
  478. // if(alarmInfo.ImageInfo.empty())
  479. // {
  480. // SPDLOG_LOGGER_ERROR(m_logger, "有报警区域,但是没有图片信息");
  481. // return;
  482. // }
  483. // /* 如果是人员报警,就存储人员报警信息 */
  484. // if(alarmInfo.ActionID == g_actionList.ActFace)
  485. // {
  486. // nJson jsonArray = json0["personList"];
  487. // for(auto& it : jsonArray)
  488. // {
  489. // PersonInfo personInfo;
  490. // personInfo.PersonID = it["personId"].get<std::string>();
  491. // personInfo.PersonName = it["personName"].get<std::string>();
  492. // alarmInfo.vecPersonInfo.push_back(personInfo);
  493. // }
  494. // }
  495. // }
  496. // }
  497. // catch (const nJson::parse_error& e)
  498. // {
  499. // SPDLOG_LOGGER_ERROR(m_logger, "解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  500. // return;
  501. // }
  502. // catch (const nJson::type_error& e)
  503. // {
  504. // SPDLOG_LOGGER_ERROR(m_logger, "解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  505. // return;
  506. // }
  507. // }
  508. /**
  509. * @brief 解析Redis的基础通用数据,不包含bBoxes数组数据
  510. *
  511. * @param strData Redis返回的源数据,JSON格式
  512. * @param alarmInfo 解析出来的数据
  513. */
  514. void SPAServer::parseRedisBaseData(const std::string& strData, AlarmInfo& alarmInfo)
  515. {
  516. try
  517. {
  518. nJson json0;
  519. json0 = nJson::parse(strData);
  520. alarmInfo.AlarmID = json0["alarmId"].get<int>();
  521. alarmInfo.ChannelID = json0["channel"].get<int>();
  522. alarmInfo.PicUrl = json0["picUrl"].get<std::string>();
  523. alarmInfo.ImageInfo = json0["imageInfo"].get<std::string>();
  524. /* 解析时间,需要将时间中的“T”换成空格 */
  525. alarmInfo.StartTime = json0["beginTime"].get<std::string>();
  526. std::replace(alarmInfo.StartTime.begin(), alarmInfo.StartTime.end(), 'T', ' ');
  527. alarmInfo.EndTime = json0["endTime"].get<std::string>();
  528. std::replace(alarmInfo.EndTime.begin(), alarmInfo.EndTime.end(), 'T', ' ');
  529. alarmInfo.EventTime = json0["eventTime"].get<std::string>();
  530. std::replace(alarmInfo.EventTime.begin(), alarmInfo.EventTime.end(), 'T', ' ');
  531. }
  532. catch (const nJson::parse_error& e)
  533. {
  534. SPDLOG_LOGGER_ERROR(m_logger, "解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  535. return;
  536. }
  537. catch (const nJson::type_error& e)
  538. {
  539. SPDLOG_LOGGER_ERROR(m_logger, "解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  540. return;
  541. }
  542. }
  543. /**
  544. * @brief 解析Redis的bBoxes数据,这个内容可能根据算法ID不同,内容不同
  545. *
  546. * @param strData
  547. * @param alarmInfo
  548. */
  549. void SPAServer::parseRedisBBoxesData(const std::string& strData, AlarmInfo& alarmInfo)
  550. {
  551. try
  552. {
  553. nJson json0;
  554. json0 = nJson::parse(strData);
  555. /* 判断bBoxes有无数据,有数据就解析,没数据就直接返回了 */
  556. nJson json1 = json0["bBoxes"];
  557. std::string labelList; /* 记录违禁品 */
  558. std::list<std::string> listBbox; /* 记录bbox */
  559. if(!json1.empty())
  560. {
  561. for(auto& it0 : json1)
  562. {
  563. /* 如果status是true,就不是报警,直接跳过 */
  564. bool status = it0["status"].get<bool>();
  565. if(status)
  566. {
  567. continue;
  568. }
  569. /* 这一条Box数据报警了将其内容存储起来 */
  570. alarmInfo.Is_Alarm = true;
  571. /* 解析label,违禁品关键字,先判断这个是不是违禁品检测的算法ID */
  572. if(alarmInfo.ActionID == g_actionList.ActContraband)
  573. {
  574. /* 解析报警,取出报警类型 */
  575. nJson label = it0["label"];
  576. for(auto& it1 : label)
  577. {
  578. std::string strLabel = it1.get<std::string>();
  579. /* 检测是否已经加入到字符串中了 */
  580. strLabel += "|";
  581. if(labelList.find(strLabel) != std::string::npos)
  582. {
  583. continue;
  584. }
  585. labelList += strLabel;
  586. }
  587. }
  588. /* 解析bbox,貌似是在图像中的位置 */
  589. nJson bbox = it0["bbox"];
  590. std::string strBbox;
  591. for(auto& it1 : bbox)
  592. {
  593. strBbox += std::to_string(it1.get<int>()) + ",";
  594. }
  595. /* 去掉最后一个“,” */
  596. if(!strBbox.empty())
  597. {
  598. strBbox.pop_back();
  599. }
  600. listBbox.push_back(strBbox);
  601. }
  602. /* 去掉最后一个“|” */
  603. if(!labelList.empty())
  604. {
  605. labelList.pop_back();
  606. }
  607. SPDLOG_LOGGER_DEBUG(m_logger, "违禁品列表:{}", labelList);
  608. }
  609. /* 如果有报警的Box,解析出报警的说明 */
  610. if(alarmInfo.Is_Alarm)
  611. {
  612. /* 添加报警信息的提示信息 */
  613. alarmInfo.listBbox = listBbox;
  614. /* 违禁品报警信息,违禁品列表不是空的,就添加补充的文字 */
  615. if( (alarmInfo.ActionID == g_actionList.ActContraband) && !labelList.empty() )
  616. {
  617. alarmInfo.ActionDes = fmt::format("出现违禁品[{}]告警", labelList);
  618. SPDLOG_LOGGER_INFO(m_logger, "{}", alarmInfo.ActionDes);
  619. }else {
  620. /* 其他报警信息,直接获取 */
  621. alarmInfo.ActionDes = json0["actionDes"].get<std::string>();
  622. }
  623. /* 判断有没有报警数据 */
  624. if(alarmInfo.ImageInfo.empty())
  625. {
  626. SPDLOG_LOGGER_WARN(m_logger, "有报警区域,但是没有图片信息");
  627. return;
  628. }
  629. /* 如果是人员报警,就存储人员报警信息 */
  630. if(alarmInfo.ActionID == g_actionList.ActFace)
  631. {
  632. nJson jsonArray = json0["personList"];
  633. for(auto& it : jsonArray)
  634. {
  635. PersonInfo personInfo;
  636. personInfo.PersonID = it["personId"].get<std::string>();
  637. personInfo.PersonName = it["personName"].get<std::string>();
  638. alarmInfo.vecPersonInfo.push_back(personInfo);
  639. }
  640. }
  641. }
  642. }
  643. catch (const nJson::parse_error& e)
  644. {
  645. SPDLOG_LOGGER_ERROR(m_logger, "解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  646. return;
  647. }
  648. catch (const nJson::type_error& e)
  649. {
  650. SPDLOG_LOGGER_ERROR(m_logger, "解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  651. return;
  652. }
  653. }
  654. /**
  655. * @brief 判断时间是否长时间没有更新,默认的是600秒,超过这个时间Redis还未更新,可能是超脑挂了
  656. *
  657. * @param strTime
  658. * @return true
  659. * @return false
  660. */
  661. bool SPAServer::isEventTimeVaild(const std::string& strTime)
  662. {
  663. /* 获取当前时间 */
  664. std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
  665. /* 字符串转成时间 */
  666. std::istringstream iss(strTime);
  667. std::tm tmEvent = {};
  668. iss >> std::get_time(&tmEvent, "%Y-%m-%d %H:%M:%S");
  669. /* 时间差 */
  670. std::chrono::system_clock::time_point eventTime = std::chrono::system_clock::from_time_t(std::mktime(&tmEvent));
  671. std::chrono::duration<double> diff = now - eventTime;
  672. // SPDLOG_LOGGER_DEBUG(m_logger, "now:{} eventTime: {} 时间差:{}秒",now, eventTime, diff);
  673. if(diff.count() > 600)
  674. {
  675. // SPDLOG_LOGGER_ERROR(m_logger, "Redis数据长时间没有更新,EventTime:{}", strTime);
  676. return false;
  677. }
  678. return true;
  679. }
  680. /**
  681. * @brief 分派任务的线程
  682. 1、 线程定时刷新房间和摄像机的关联关系,以及和算法Action的关联关系
  683. 2、 将算法信息加入到不同的列表中
  684. 需要多个摄像机配合的加入到m_runListRoomActionInfo列表
  685. 不需要多个摄像机配合的加入到m_runListActionInfo列表
  686. 3、 每次刷新都会清空ActionInfo或者RoomActionInfo的摄像机列表,但是不会动其他基本信息,如果是列表中没有对应的信息,
  687. 就会创建新的ActionInfo,那么RunState的状态是INIT,需要开启新的线程
  688. 如果刷新完成后,算法对应的摄像机列表为空,那么对应的线程就会停止运行,将RunState设置为STOP,本线程的下一轮
  689. 循环就会删除这个ActionInfo
  690. */
  691. void SPAServer::threadRoomCamera()
  692. {
  693. /* 房间相机关联信息 */
  694. std::list<RoomCameraInfo> listRC;
  695. /* 存储获取到的应用和启用时间的信息 */
  696. std::list<AppAndTimeInfo> listAppAndTime;
  697. /* 创建连接数据库实例 */
  698. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  699. while (m_threadRunning)
  700. {
  701. m_mutexRunFAI.lock();
  702. /* 先清理已经退出的线程所用到的Action或者RoomAction */
  703. m_runListFuncActInfo.clearNoneFuncActionInfo();
  704. /* 创建应用信息,根据从EQM数据库读取到的配置的应用信息创建 */
  705. toEQMDataBase->getAlarmAppInfo(listAppAndTime);
  706. for(const auto& it : listAppAndTime)
  707. {
  708. /* 创建应用信息,如果已有该应用,就更新时间 */
  709. m_runListFuncActInfo.addFuncActionInfo(it);
  710. }
  711. /* 先获取EQM数据库信息,取出房间和摄像机关联信息 */
  712. m_mutexActionInfo.lock();
  713. toEQMDataBase->getActionInfo(m_listActionInfo);
  714. /* 将算法信息加入到不同的列表中 */
  715. m_runListFuncActInfo.clearActionList();
  716. for(const auto& it : m_listActionInfo.getData())
  717. {
  718. m_runListFuncActInfo.addActionInfo(*it);
  719. }
  720. /* 检查算法信息的状态,频率里的应用没有配置摄像机就设置为线程运行状态为停止,退出该线程 */
  721. for(const auto& it : m_runListFuncActInfo.getData())
  722. {
  723. if(it->listRoomCamActInfo.size() == 0)
  724. {
  725. it->RunState = RunTimeState::RUN_STATE_STOP;
  726. }
  727. }
  728. m_mutexActionInfo.unlock();
  729. /* 开启线程 */
  730. for(const auto& it0 : m_runListFuncActInfo.getData())
  731. {
  732. /* 人员在岗识别 */
  733. /* 区域人员检测 */
  734. /* 非法入侵检测 */
  735. /* 违禁品识别 */
  736. /* 普通任务线程,一个任务对应一个摄像机和一个算法ID,无需联动 */
  737. if(it0->appFunction == AppFunction::APP_ALLDOWN)
  738. {
  739. if(it0->RunState == RunTimeState::RUN_STATE_INIT)
  740. {
  741. CPPTP.add_task(&SPAServer::threadActNormal, this, it0);
  742. }
  743. }
  744. }
  745. m_mutexRunFAI.unlock();
  746. /* 休眠n秒,默认应该是300秒 */
  747. std::this_thread::sleep_for(std::chrono::seconds(g_config.CheckSet));
  748. }
  749. }
  750. /**
  751. * @brief 人员在岗识别线程,这个功能主要是给客户端提供实时的人脸识别信息
  752. * 1、这个写入tWorkOnInfo表格,十分钟写一次
  753. * 2、理论上这里需要实时更新数据库,但是实际上时每十分钟写入一次数据
  754. * 3、这个线程也会进行在岗离岗的识别报警
  755. *
  756. * @param RFAInfo 传入房间ID和算法ID
  757. */
  758. void SPAServer::threadActPersonWork(FuncActionInfo* RFAInfo)
  759. {
  760. SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程,ChannelID:{} ,", RFAInfo->strFunctionName, RFAInfo->ChannelID);
  761. /* 创建读取Redis的实例 */
  762. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  763. /* 创建写入数据库实例 */
  764. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  765. /* 局部变量 */
  766. std::shared_ptr<FuncActionInfo> pRFAInfo = std::make_shared<FuncActionInfo>();
  767. *pRFAInfo = *RFAInfo;
  768. /* 保存每个摄像机的报警信息 */
  769. std::shared_ptr<std::list<AlarmInfo>> pListAlarmInfo = std::make_shared<std::list<AlarmInfo>>();
  770. /* 保存人脸信息的数据 */
  771. std::shared_ptr<ListRoomFaceInfo> pListRoomFaceInfo = std::make_shared<ListRoomFaceInfo>();
  772. while (m_threadRunning)
  773. {
  774. /* 更新线程信息 */
  775. updateFuncInfo(pRFAInfo);
  776. if(pRFAInfo->appFunction == AppFunction::APP_NONE)
  777. {
  778. break;
  779. }
  780. /* 读取Redis数据 */
  781. pListAlarmInfo->clear();
  782. for(const auto& RoomInfo : pRFAInfo->listRoomCamActInfo)
  783. {
  784. for(const auto& it : RoomInfo.mapCameraAction)
  785. {
  786. std::string strKey = std::to_string(it.first) + ":" + it.second;
  787. std::string strRetValue;
  788. if(!fromRedis->getRedisString(strKey, strRetValue))
  789. {
  790. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  791. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  792. continue;
  793. }
  794. /* 解析数据 */
  795. AlarmInfo alarmInfo;
  796. parseRedisBaseData(strRetValue, alarmInfo);
  797. parseRedisBBoxesData(strRetValue, alarmInfo);
  798. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  799. if(isEventTimeVaild(alarmInfo.EventTime))
  800. {
  801. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  802. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  803. continue;
  804. }
  805. pListAlarmInfo->push_back(alarmInfo);
  806. }
  807. }
  808. /* 处理数据,将报警信息的人脸信息取出来,放入到人脸信息列表中 */
  809. for(auto& alarmInfo : *pListAlarmInfo)
  810. {
  811. /* 添加到人脸列表中,会自动去重,自动创建对应的频道和房间信息 */
  812. pListRoomFaceInfo->addRoomFaceInfo(alarmInfo);
  813. }
  814. /* 计算人脸个数,并判断是否需要写入数据库 */
  815. QDateTime now = QDateTime::currentDateTime();
  816. for(auto it = pListRoomFaceInfo->listRoomFaceInfo.begin(); it != pListRoomFaceInfo->listRoomFaceInfo.end(); )
  817. {
  818. if(it->MaxNum < it->listPersonInfo.size())
  819. {
  820. it->MaxNum = it->listPersonInfo.size();
  821. }
  822. if(it->MinNum > it->listPersonInfo.size())
  823. {
  824. it->MinNum = it->listPersonInfo.size();
  825. }
  826. /* 判断是否需要写入数据库,超过设置的时间就写入,默认是600秒 */
  827. if(now.toSecsSinceEpoch() - it->StartTime.toSecsSinceEpoch() > g_config.AppUpdateOnWorkTimeInterval)
  828. {
  829. /* 写入数据库 */
  830. if(toEQMDataBase->insertOnWorkInfo(*it))
  831. {
  832. SPDLOG_LOGGER_INFO(m_logger, "ChannelID:{}, RoomID:{}, 在岗信息写入数据库成功, StartTime", it->ChannelID, it->RoomID, it->StartTime.toString("yyyy-MM-dd hh:mm:ss").toStdString());
  833. /* 删除这条信息 */
  834. it = pListRoomFaceInfo->listRoomFaceInfo.erase(it);
  835. continue;
  836. }
  837. }
  838. ++it;
  839. }
  840. std::this_thread::sleep_for(std::chrono::milliseconds(g_config.ThreadSleepMS));
  841. }
  842. setThreadState(pRFAInfo, RunTimeState::RUN_STATE_STOP);
  843. SPDLOG_LOGGER_INFO(m_logger, "关闭 {} 线程,ChannelID:{}", pRFAInfo->strFunctionName, pRFAInfo->ChannelID);
  844. }
  845. /**
  846. * @brief 区域非法入侵检测
  847. 1、野猫、宠物识别:报警判断条件:机房内出现摄像头的宠物识别算法输出报警结果时,记为报警行为,直接
  848. 展示报警结果
  849. 2、无权限人员识别:报警判断条件:当判断出区域非法入侵行为时,依据人脸识别结果,判断是否为人脸库人
  850. 员,非人脸库的人员时判断为非法入侵行为(背影需要报警需要结合区域人员检测算法判断)
  851. 3、人员计数和人脸识别数不相等时,判断为非法入侵行为
  852. *
  853. * @param info
  854. */
  855. void SPAServer::threadActIllegalInvasion(FuncActionInfo* RFAInfo)
  856. {
  857. SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程,ChannelID:{} ,", RFAInfo->strFunctionName, RFAInfo->ChannelID);
  858. /* 创建读取Redis的实例 */
  859. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  860. /* 创建写入数据库实例 */
  861. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  862. /* 局部变量 */
  863. std::shared_ptr<FuncActionInfo> pRFAInfo = std::make_shared<FuncActionInfo>();
  864. *pRFAInfo = *RFAInfo;
  865. /* 保存每个摄像机的报警信息 */
  866. std::shared_ptr<std::list<AlarmInfo>> pListAlarmInfo = std::make_shared<std::list<AlarmInfo>>();
  867. /* 保存非法入侵的信息 */
  868. std::shared_ptr<ListIllegalInvasionInfo> pListIllInfo = std::make_shared<ListIllegalInvasionInfo>();
  869. std::string strFaceActionID; /* 人脸识别的Action */
  870. std::string strCountActionID; /* 人员计数的Action */
  871. /* 封装lambda表达式 */
  872. std::function<bool(const std::vector<PersonInfo>&)> funcIsAlarm = [](const std::vector<PersonInfo>& vecPersion) {
  873. for(const auto& it : vecPersion)
  874. {
  875. if(it.PersonID == "-1")
  876. {
  877. return true;
  878. }
  879. }
  880. return false;
  881. };
  882. while (m_threadRunning)
  883. {
  884. /* 更新线程信息 */
  885. updateFuncInfo(pRFAInfo);
  886. if(pRFAInfo->appFunction == AppFunction::APP_NONE)
  887. {
  888. break;
  889. }
  890. /* 更新算法ActionID */
  891. {
  892. g_actionList.mutexRW.lockForRead();
  893. strFaceActionID = g_actionList.ActFace;
  894. strCountActionID = g_actionList.ActPersonNumber;
  895. g_actionList.mutexRW.unlock();
  896. }
  897. /* 读取Redis数据 */
  898. pListAlarmInfo->clear();
  899. for(const auto& RoomInfo : pRFAInfo->listRoomCamActInfo)
  900. {
  901. for(const auto& it : RoomInfo.mapCameraAction)
  902. {
  903. std::string strKey = std::to_string(it.first) + ":" + it.second;
  904. std::string strRetValue;
  905. if(!fromRedis->getRedisString(strKey, strRetValue))
  906. {
  907. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  908. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  909. continue;
  910. }
  911. /* 解析数据 */
  912. AlarmInfo alarmInfo;
  913. // parseRedisData(strRetValue, alarmInfo);
  914. parseRedisBaseData(strRetValue, alarmInfo);
  915. parseRedisBBoxesData(strRetValue, alarmInfo);
  916. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  917. if(isEventTimeVaild(alarmInfo.EventTime))
  918. {
  919. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  920. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  921. continue;
  922. }
  923. pListAlarmInfo->push_back(alarmInfo);
  924. }
  925. }
  926. /* 找出房间的个数,按照房间进行判断 */
  927. ListRoomIll listRoomIll;
  928. for(auto& it : pRFAInfo->listRoomCamActInfo)
  929. {
  930. listRoomIll.addRoom(it.RoomID, it.RoomType);
  931. }
  932. /* 根据房间解析人脸识别的非法入侵,这个有了就不用判断后面的人员计数不相等了 */
  933. for(auto& room : listRoomIll.getData())
  934. {
  935. for(const auto& it : *pListAlarmInfo)
  936. {
  937. /* 人俩识别算法 */
  938. if(it.ActionID == strFaceActionID)
  939. {
  940. if(it.vecPersonInfo.size() > room.numMaxFace)
  941. {
  942. room.numMaxFace = it.vecPersonInfo.size();
  943. room.strBoxList = it.listBbox;
  944. room.vecPersonInfo = it.vecPersonInfo;
  945. }
  946. }
  947. /* 判断有没有非法入侵人员 */
  948. if(funcIsAlarm(it.vecPersonInfo))
  949. {
  950. room.isAlarm = true;
  951. room.strMessage = "人员非法入侵(未知人员)";
  952. if( !it.ImageInfo.empty() )
  953. {
  954. room.strImage = it.ImageInfo;
  955. }
  956. room.CameraID = it.DeviceID;
  957. }
  958. }
  959. }
  960. /* 判断人脸识别到的数目和人员计数算法识别到的人数是否相等,如果上面报警了,这里就不用检测了 */
  961. for(auto& room : listRoomIll.getData())
  962. {
  963. if(room.isAlarm)
  964. {
  965. continue;
  966. }
  967. /* 这个房间的人脸算法没有识别到非法入侵 */
  968. for(const auto& it : *pListAlarmInfo)
  969. {
  970. if(it.ActionID == strCountActionID)
  971. {
  972. if(it.vecPersonInfo.size() > room.numMaxPerson)
  973. {
  974. room.numMaxPerson = it.vecPersonInfo.size();
  975. room.strBoxList = it.listBbox;
  976. if(!it.ImageInfo.empty())
  977. {
  978. room.strImage = it.ImageInfo;
  979. }
  980. room.CameraID = it.DeviceID;
  981. }
  982. }
  983. }
  984. /* 判断人数是否一致 */
  985. if(room.numMaxFace != room.numMaxPerson)
  986. {
  987. room.strMessage = fmt::format("非法入侵(人员计数 {} 和人脸数 {} 不一致)", room.numMaxPerson, room.numMaxFace);
  988. room.isAlarm = true;
  989. }
  990. }
  991. /* 将非法入侵的信息存储到数组缓存中,等待报警结束后判断是否需要写入EQM数据库 */
  992. for(auto& room : listRoomIll.getData())
  993. {
  994. auto pIll = pListIllInfo->findIllInfo(room.RoomID, room.RoomType);
  995. if(room.isAlarm)
  996. {
  997. /* 正在报警,检查是否有 */
  998. if(pIll == nullptr)
  999. {
  1000. IllegalInvasionInfo info;
  1001. info.ChannelID = pRFAInfo->ChannelID;
  1002. info.CameraID = room.CameraID;
  1003. info.RoomID = room.RoomID;
  1004. info.RoomType = room.RoomType;
  1005. info.strActionDec = room.strMessage;
  1006. info.strImageInfo = room.strImage;
  1007. info.FirstTime = QDateTime::currentDateTime();;
  1008. pListIllInfo->addIllInfo(info);
  1009. }
  1010. }
  1011. else
  1012. {
  1013. /* 没有报警,检查是否是报警结束了,是否符合写入数据库的条件 */
  1014. QDateTime currTime = QDateTime::currentDateTime();
  1015. int secs = currTime.toSecsSinceEpoch() - pIll->FirstTime.toSecsSinceEpoch();
  1016. if(secs >= g_config.AppBadMan)
  1017. {
  1018. if(!pIll->strImageInfo.empty())
  1019. {
  1020. /* 超过非法入侵的时间,写入数据库 */
  1021. AlarmInfo ai;
  1022. ai.ChannelID = pRFAInfo->ChannelID;
  1023. ai.RoomID = pIll->RoomID;
  1024. ai.ActionID = g_actionList.ActPersonNumber;
  1025. ai.ActionDes = pIll->strActionDec;
  1026. ai.ImageInfo = pIll->strImageInfo;
  1027. ai.StartTime = pIll->FirstTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  1028. ai.EndTime = currTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  1029. ai.EventTime = pIll->FirstTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  1030. ai.vecPersonInfo = room.vecPersonInfo;
  1031. ai.listBbox = room.strBoxList;
  1032. bool insertRet = toEQMDataBase->insertAlarmInfo(ai);
  1033. if(insertRet)
  1034. {
  1035. std::string actionName = g_actionList.getActionName(ai.ActionID);
  1036. SPDLOG_LOGGER_INFO(m_logger, "☆ 新增报警信息,频道[{}],房间[{}],摄像头[{}],{},{},有{}个区域,有{}个人脸,{}",
  1037. pIll->ChannelID, pIll->RoomID, pIll->CameraID, actionName, ai.ActionDes, ai.listBbox, ai.FaceIDList.size(), ai.ImageInfo);
  1038. }
  1039. }
  1040. /* 删除这个非法入侵信息 */
  1041. pListIllInfo->deleteIllInfo(*pIll);
  1042. }
  1043. }
  1044. }
  1045. std::this_thread::sleep_for(std::chrono::milliseconds(g_config.ThreadSleepMS));
  1046. }
  1047. setThreadState(pRFAInfo, RunTimeState::RUN_STATE_STOP);
  1048. SPDLOG_LOGGER_INFO(m_logger, "关闭 {} 线程,ChannelID:{}", pRFAInfo->strFunctionName, pRFAInfo->ChannelID);
  1049. }
  1050. /**
  1051. * @brief 区域人员检测,检测这个区域内的人数,不能少于多少人,不能多余多少人
  1052. * 1、报警判断条件:直播间报警:当前频率所有直播间摄像头的区域人员检测算法输出结果均大于或小于设定人
  1053. 数值时,记为报警行为,直接展示报警结果
  1054. 2、录播间报警:当前频率所有录播间摄像头的区域人员检测算法输出结果均大于或小于设定人数值时,记为报
  1055. 警行为,直接展示报警结果
  1056. 3、直播间+录播间报警:当前频率直播间人数+录播间人数大于或小于设定人数值时,记为报警行为,直接展示
  1057. 报警结果
  1058. *
  1059. * @param RAInfo 传入的房间ID和算法ID
  1060. */
  1061. void SPAServer::threadActRegionalPersonnelDetection(FuncActionInfo* RFAInfo)
  1062. {
  1063. SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程,ChannelID:{} ,", RFAInfo->strFunctionName, RFAInfo->ChannelID);
  1064. /* 创建读取Redis的实例 */
  1065. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  1066. /* 创建写入数据库实例 */
  1067. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  1068. /* 局部变量 */
  1069. std::shared_ptr<FuncActionInfo> pRFAInfo = std::make_shared<FuncActionInfo>();
  1070. *pRFAInfo = *RFAInfo;
  1071. /* 保存每个摄像机的报警信息 */
  1072. std::shared_ptr<std::list<AlarmInfo>> pListAlarmInfo = std::make_shared<std::list<AlarmInfo>>();
  1073. while (m_threadRunning)
  1074. {
  1075. /* 更新线程信息 */
  1076. updateFuncInfo(pRFAInfo);
  1077. if(pRFAInfo->appFunction == AppFunction::APP_NONE)
  1078. {
  1079. break;
  1080. }
  1081. /* 读取Redis数据 */
  1082. pListAlarmInfo->clear();
  1083. for(const auto& RoomInfo : pRFAInfo->listRoomCamActInfo)
  1084. {
  1085. for(const auto& it : RoomInfo.mapCameraAction)
  1086. {
  1087. std::string strKey = std::to_string(it.first) + ":" + it.second;
  1088. std::string strRetValue;
  1089. if(!fromRedis->getRedisString(strKey, strRetValue))
  1090. {
  1091. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  1092. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1093. continue;
  1094. }
  1095. /* 解析数据 */
  1096. AlarmInfo alarmInfo;
  1097. parseRedisBaseData(strRetValue, alarmInfo);
  1098. parseRedisBBoxesData(strRetValue, alarmInfo);
  1099. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  1100. if(isEventTimeVaild(alarmInfo.EventTime))
  1101. {
  1102. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  1103. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1104. continue;
  1105. }
  1106. pListAlarmInfo->push_back(alarmInfo);
  1107. }
  1108. }
  1109. std::this_thread::sleep_for(std::chrono::milliseconds(g_config.ThreadSleepMS));
  1110. }
  1111. setThreadState(pRFAInfo, RunTimeState::RUN_STATE_STOP);
  1112. SPDLOG_LOGGER_INFO(m_logger, "关闭 {} 线程,ChannelID:{}", pRFAInfo->strFunctionName, pRFAInfo->ChannelID);
  1113. }
  1114. /**
  1115. * @brief 普通任务识别,一个算法只需要一个摄像机,包括 违禁品识别、玩手机检测、老鼠等
  1116. 1、报警判断条件:机房内出现摄像头的违禁物品识别算法输出结果包含指定违规内容时,记为报警行为,直接
  1117. 展示报警结果
  1118. 2、这里应该是不区分违禁物品是什么,只要有违禁物品就报警。如果一个违禁物品消失之前又出现了第二个违禁物品,
  1119. 两个违禁物品都消失后,这次报警才会结束。
  1120. 3、一有报警信息就写入到EQM数据库的tAlarmInfo表中,报警开始时间是EventTime
  1121. 4、如果报警结束时间小于设置的最小间隔,就删除这条数据,超过这个时间,就更新结束时间
  1122. 5、报警信息要有图片,如果当前没有图片,就使用上一张图片
  1123. *
  1124. * @param info 线程信息
  1125. */
  1126. void SPAServer::threadActNormal(FuncActionInfo* info)
  1127. {
  1128. SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程, ChannelID:{} ,App:{}",info->strFunctionName, info->ChannelID, info->strFunctionName);
  1129. /* 创建读取Redis的实例 */
  1130. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  1131. /* 创建写入EQM数据库实例 */
  1132. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  1133. /* 局部变量,保存这个线程的信息 */
  1134. std::shared_ptr<FuncActionInfo> pFuncAct = std::make_shared<FuncActionInfo>();
  1135. *pFuncAct = *info;
  1136. /* 保存报警数据 */
  1137. std::shared_ptr<ListAlarmInfo> listAlarmInfo = std::make_shared<ListAlarmInfo>();
  1138. while (m_threadRunning)
  1139. {
  1140. /* 更新算法关联的摄像机ID */
  1141. updateFuncInfo(pFuncAct);
  1142. /* 如果线程的运行状态为 */
  1143. if(pFuncAct->RunState == RunTimeState::RUN_STATE_STOP)
  1144. {
  1145. pFuncAct->appFunction = AppFunction::APP_NONE;
  1146. break;
  1147. }
  1148. /* 读取Redis信息,处理数据 */
  1149. for(const auto& roomInfo : pFuncAct->listRoomCamActInfo)
  1150. {
  1151. for(const auto& it : roomInfo.mapCameraAction)
  1152. {
  1153. /* 读取Redis数据 */
  1154. std::string strKey = std::to_string(it.first) + ":" + it.second;
  1155. std::string strRetValue;
  1156. if(!fromRedis->getRedisString(strKey, strRetValue))
  1157. {
  1158. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  1159. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1160. continue;
  1161. }
  1162. /* 解析数据 */
  1163. AlarmInfo alarmInfo;
  1164. parseRedisBaseData(strRetValue, alarmInfo);
  1165. parseRedisBBoxesData(strRetValue, alarmInfo);
  1166. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  1167. if(isEventTimeVaild(alarmInfo.EventTime))
  1168. {
  1169. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  1170. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1171. continue;
  1172. }
  1173. /* 找出数组中与当前报警ID相同的的报警信息 */
  1174. auto pAlarmInfo = listAlarmInfo->findAlarmInfo(alarmInfo);
  1175. if(pAlarmInfo == nullptr)
  1176. {
  1177. /* 没有找到报警记录,就新建一个 */
  1178. AlarmInfo newAlarmInfo = alarmInfo;
  1179. listAlarmInfo->addAlarmInfo(newAlarmInfo);
  1180. /* 重新查找该报警信息 */
  1181. pAlarmInfo = listAlarmInfo->findAlarmInfo(alarmInfo);
  1182. if(pAlarmInfo == nullptr)
  1183. {
  1184. SPDLOG_LOGGER_ERROR(m_logger, "查找报警信息失败, Key:{}", strKey);
  1185. continue;
  1186. }
  1187. }
  1188. /* 判断有无报警记录,新的报警就写入到EQM的tAlarmInfo表中,正在报警的记录就判断和更新,
  1189. * 结束报警就更新tAlarmInfo的报警数据的结束时间,并清空pAlarmInfo */
  1190. if(pAlarmInfo->Is_Alarm)
  1191. {
  1192. /* 正在报警中,检查是否报警结束 */
  1193. if(alarmInfo.Is_Alarm)
  1194. {
  1195. /* 更新图片信息 */
  1196. if(!alarmInfo.ImageInfo.empty())
  1197. {
  1198. pAlarmInfo->ImageInfo = alarmInfo.ImageInfo;
  1199. }
  1200. } else {
  1201. /* 报警结束,判断时长,更新数据库结束时间,结束时间是此时电脑时间 */
  1202. if(timeDiffWithNow(pAlarmInfo->EventTime) > g_config.Contraband)
  1203. {
  1204. pAlarmInfo->EndTime = chronoToStrTime(std::chrono::system_clock::now());
  1205. toEQMDataBase->updateAlarmEndTime(*pAlarmInfo);
  1206. }else {
  1207. /* 不够报警时间,目前不做任何处理,不删除EQM报警记录 */
  1208. }
  1209. /* 清空报警信息 */
  1210. pAlarmInfo->reInit();
  1211. }
  1212. }
  1213. else
  1214. {
  1215. /* 是新的报警 */
  1216. if(alarmInfo.Is_Alarm)
  1217. {
  1218. /* 图片不能是空,如果是空的,就不写入数据库 */
  1219. if(!alarmInfo.ImageInfo.empty())
  1220. {
  1221. /* 违禁品检测,开始时间是事件时间 */
  1222. alarmInfo.StartTime = alarmInfo.EventTime;
  1223. alarmInfo.EndTime = "";
  1224. if(toEQMDataBase->insertAlarmInfo(alarmInfo))
  1225. {
  1226. /* 保存新的报警记录 */
  1227. *pAlarmInfo = alarmInfo;
  1228. }else {
  1229. SPDLOG_LOGGER_ERROR(m_logger, "写入tAlarmInfo报警数据失败, Key: {}", strKey);
  1230. }
  1231. }else {
  1232. SPDLOG_LOGGER_ERROR(m_logger, "频道:{}, 房间:{}, 摄像机:{}, 算法:{}, 有报警区域, 但是没有图片信息", pFuncAct->ChannelID, roomInfo.RoomID, it.first, it.second);
  1233. }
  1234. }
  1235. }
  1236. }
  1237. }
  1238. /* 休眠设置的时间 */
  1239. std::this_thread::sleep_for(std::chrono::seconds(g_config.ThreadSleepMS));
  1240. }
  1241. /* 设置线程退出的状态 */
  1242. setThreadState(pFuncAct, RunTimeState::RUN_STATE_STOP);
  1243. SPDLOG_LOGGER_INFO(m_logger, "{} 线程退出,Channel:{}",pFuncAct->strFunctionName, pFuncAct->ChannelID);
  1244. }
  1245. /* 将该算法对应的摄像机放入摄像机列表 */
  1246. bool SPAServer::insertCameraToAction(RoomActionInfo* pRAInfo, std::list<RoomCameraInfo>& listRC, std::multimap<int, std::string>& mapCameraActionID)
  1247. {
  1248. for(const auto& rci : listRC)
  1249. {
  1250. if(rci.RoomID == pRAInfo->RoomID)
  1251. {
  1252. /* 这个摄像机在这个房间内,再判断这个摄像机有没有这个算法 */
  1253. for(const auto& camID : rci.listCameraID)
  1254. {
  1255. for(const auto& cai : mapCameraActionID)
  1256. {
  1257. if(camID == cai.first)
  1258. {
  1259. /* 再判断这个摄像机的算法是否是当前需要的 */
  1260. if(cai.second == pRAInfo->ActionID)
  1261. {
  1262. pRAInfo->listCameraID.push_back(camID);
  1263. }
  1264. }
  1265. }
  1266. }
  1267. }
  1268. }
  1269. return true;
  1270. }
  1271. /**
  1272. * @brief 功能线程更新功能信息,这里是从内存中的数组里获取
  1273. *
  1274. * @param pInfo
  1275. * @return true
  1276. * @return false
  1277. */
  1278. bool SPAServer::updateFuncInfo(std::shared_ptr<FuncActionInfo> pInfo)
  1279. {
  1280. pInfo->clearActionList();
  1281. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  1282. auto fa = m_runListFuncActInfo.findAppFunction(*pInfo);
  1283. if(fa == nullptr)
  1284. {
  1285. return false;
  1286. }
  1287. *pInfo = *fa;
  1288. return true;
  1289. }
  1290. /* 设置线程状态 */
  1291. void SPAServer::setThreadState(std::shared_ptr<FuncActionInfo> pInfo, RunTimeState state)
  1292. {
  1293. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  1294. auto p = m_runListFuncActInfo.findAppFunction(*pInfo);
  1295. if(p != nullptr)
  1296. {
  1297. p->RunState = state;
  1298. }
  1299. }
  1300. /* 计算与当前时间的时间差,返回秒 */
  1301. int SPAServer::timeDiffWithNow(const std::string& strTime)
  1302. {
  1303. auto now = std::chrono::system_clock::now();
  1304. auto eventTime = strTimeToChrono(strTime);
  1305. std::chrono::duration<double> diff = now - eventTime;
  1306. return diff.count();
  1307. }
  1308. /* 字符串时间转换成std::chrono时间点 */
  1309. std::chrono::system_clock::time_point SPAServer::strTimeToChrono(const std::string& strTime)
  1310. {
  1311. std::istringstream iss(strTime);
  1312. std::tm tmEvent = {};
  1313. iss >> std::get_time(&tmEvent, "%Y-%m-%d %H:%M:%S");
  1314. return std::chrono::system_clock::from_time_t(std::mktime(&tmEvent));
  1315. }
  1316. /* 时间点转换成字符串 */
  1317. std::string SPAServer::chronoToStrTime(const std::chrono::system_clock::time_point& timePoint)
  1318. {
  1319. std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
  1320. std::tm tmEvent = *std::localtime(&time);
  1321. char buf[64] = {0};
  1322. std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tmEvent);
  1323. return std::string(buf);
  1324. }