SPAServer.cpp 47 KB

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