SPAServer.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  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. 1、先检测非法的人脸信息
  854. 2、再检测人员计数和人脸识别数不相等
  855. *
  856. * @param info
  857. */
  858. void SPAServer::threadActIllegalInvasion(FuncActionInfo* RFAInfo)
  859. {
  860. SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程,ChannelID:{} ,", RFAInfo->strFunctionName, RFAInfo->ChannelID);
  861. /* 创建读取Redis的实例 */
  862. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  863. /* 创建写入数据库实例 */
  864. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  865. /* 局部变量 */
  866. std::shared_ptr<FuncActionInfo> pRFAInfo = std::make_shared<FuncActionInfo>();
  867. *pRFAInfo = *RFAInfo;
  868. /* 保存每个摄像机的报警信息 */
  869. std::shared_ptr<std::list<AlarmInfo>> pListAlarmInfo = std::make_shared<std::list<AlarmInfo>>();
  870. /* 保存非法入侵的信息 */
  871. std::shared_ptr<ListIllegalInvasionInfo> pListIllInfo = std::make_shared<ListIllegalInvasionInfo>();
  872. std::string strFaceActionID; /* 人脸识别的Action */
  873. std::string strCountActionID; /* 人员计数的Action */
  874. /* 封装lambda表达式 */
  875. std::function<bool(const std::vector<PersonInfo>&)> funcIsAlarm = [](const std::vector<PersonInfo>& vecPersion) {
  876. for(const auto& it : vecPersion)
  877. {
  878. if(it.PersonID == "-1")
  879. {
  880. return true;
  881. }
  882. }
  883. return false;
  884. };
  885. while (m_threadRunning)
  886. {
  887. /* 更新线程信息 */
  888. updateFuncInfo(pRFAInfo);
  889. if(pRFAInfo->appFunction == AppFunction::APP_NONE)
  890. {
  891. break;
  892. }
  893. /* 更新算法ActionID */
  894. {
  895. g_actionList.mutexRW.lockForRead();
  896. strFaceActionID = g_actionList.ActFace;
  897. strCountActionID = g_actionList.ActPersonNumber;
  898. g_actionList.mutexRW.unlock();
  899. }
  900. /* 读取Redis数据 */
  901. pListAlarmInfo->clear();
  902. for(const auto& RoomInfo : pRFAInfo->listRoomCamActInfo)
  903. {
  904. for(const auto& it : RoomInfo.mapCameraAction)
  905. {
  906. std::string strKey = std::to_string(it.first) + ":" + it.second;
  907. std::string strRetValue;
  908. if(!fromRedis->getRedisString(strKey, strRetValue))
  909. {
  910. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  911. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  912. continue;
  913. }
  914. /* 解析数据 */
  915. AlarmInfo alarmInfo;
  916. // parseRedisData(strRetValue, alarmInfo);
  917. parseRedisBaseData(strRetValue, alarmInfo);
  918. parseRedisBBoxesData(strRetValue, alarmInfo);
  919. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  920. if(isEventTimeVaild(alarmInfo.EventTime))
  921. {
  922. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  923. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  924. continue;
  925. }
  926. pListAlarmInfo->push_back(alarmInfo);
  927. }
  928. }
  929. /* 找出房间的个数,按照房间进行判断 */
  930. ListRoomIll listRoomIll;
  931. for(auto& it : pRFAInfo->listRoomCamActInfo)
  932. {
  933. listRoomIll.addRoom(it.RoomID, it.RoomType);
  934. }
  935. /* 根据房间解析人脸识别的非法入侵,这个有了就不用判断后面的人员计数不相等了 */
  936. for(auto& room : listRoomIll.getData())
  937. {
  938. for(const auto& it : *pListAlarmInfo)
  939. {
  940. /* 人俩识别算法 */
  941. if(it.ActionID == strFaceActionID)
  942. {
  943. if(it.vecPersonInfo.size() > room.numMaxFace)
  944. {
  945. room.numMaxFace = it.vecPersonInfo.size();
  946. room.strBoxList = it.listBbox;
  947. room.vecPersonInfo = it.vecPersonInfo;
  948. }
  949. }
  950. /* 判断有没有非法入侵人员 */
  951. if(funcIsAlarm(it.vecPersonInfo))
  952. {
  953. room.isAlarm = true;
  954. room.strMessage = "人员非法入侵(未知人员)";
  955. if( !it.ImageInfo.empty() )
  956. {
  957. room.strImage = it.ImageInfo;
  958. }
  959. room.CameraID = it.DeviceID;
  960. }
  961. }
  962. }
  963. /* 判断人脸识别到的数目和人员计数算法识别到的人数是否相等,如果上面报警了,这里就不用检测了 */
  964. for(auto& room : listRoomIll.getData())
  965. {
  966. if(room.isAlarm)
  967. {
  968. continue;
  969. }
  970. /* 这个房间的人脸算法没有识别到非法入侵 */
  971. for(const auto& it : *pListAlarmInfo)
  972. {
  973. if(it.ActionID == strCountActionID)
  974. {
  975. if(it.vecPersonInfo.size() > room.numMaxPerson)
  976. {
  977. room.numMaxPerson = it.vecPersonInfo.size();
  978. room.strBoxList = it.listBbox;
  979. if(!it.ImageInfo.empty())
  980. {
  981. room.strImage = it.ImageInfo;
  982. }
  983. room.CameraID = it.DeviceID;
  984. }
  985. }
  986. }
  987. /* 判断人数是否一致 */
  988. if(room.numMaxFace != room.numMaxPerson)
  989. {
  990. room.strMessage = fmt::format("非法入侵(人员计数 {} 和人脸数 {} 不一致)", room.numMaxPerson, room.numMaxFace);
  991. room.isAlarm = true;
  992. }
  993. }
  994. /* 将非法入侵的信息存储到数组缓存中,等待报警结束后判断是否需要写入EQM数据库 */
  995. for(auto& room : listRoomIll.getData())
  996. {
  997. auto pIll = pListIllInfo->findIllInfo(room.RoomID, room.RoomType);
  998. if(room.isAlarm)
  999. {
  1000. /* 正在报警,检查是否有 */
  1001. if(pIll == nullptr)
  1002. {
  1003. IllegalInvasionInfo info;
  1004. info.ChannelID = pRFAInfo->ChannelID;
  1005. info.CameraID = room.CameraID;
  1006. info.RoomID = room.RoomID;
  1007. info.RoomType = room.RoomType;
  1008. info.strActionDec = room.strMessage;
  1009. info.strImageInfo = room.strImage;
  1010. info.FirstTime = QDateTime::currentDateTime();;
  1011. pListIllInfo->addIllInfo(info);
  1012. }
  1013. }
  1014. else
  1015. {
  1016. /* 没有报警,检查是否是报警结束了,是否符合写入数据库的条件 */
  1017. QDateTime currTime = QDateTime::currentDateTime();
  1018. int secs = currTime.toSecsSinceEpoch() - pIll->FirstTime.toSecsSinceEpoch();
  1019. if(secs >= g_config.AppBadMan)
  1020. {
  1021. if(!pIll->strImageInfo.empty())
  1022. {
  1023. /* 超过非法入侵的时间,写入数据库 */
  1024. AlarmInfo ai;
  1025. ai.ChannelID = pRFAInfo->ChannelID;
  1026. ai.RoomID = pIll->RoomID;
  1027. ai.ActionID = g_actionList.ActPersonNumber;
  1028. ai.ActionDes = pIll->strActionDec;
  1029. ai.ImageInfo = pIll->strImageInfo;
  1030. ai.StartTime = pIll->FirstTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  1031. ai.EndTime = currTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  1032. ai.EventTime = pIll->FirstTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  1033. ai.vecPersonInfo = room.vecPersonInfo;
  1034. ai.listBbox = room.strBoxList;
  1035. bool insertRet = toEQMDataBase->insertAlarmInfo(ai);
  1036. if(insertRet)
  1037. {
  1038. std::string actionName = g_actionList.getActionName(ai.ActionID);
  1039. SPDLOG_LOGGER_INFO(m_logger, "☆ 新增报警信息,频道[{}],房间[{}],摄像头[{}],{},{},有{}个区域,有{}个人脸,{}",
  1040. pIll->ChannelID, pIll->RoomID, pIll->CameraID, actionName, ai.ActionDes, ai.listBbox.size(), ai.FaceIDList.size(), ai.ImageInfo);
  1041. }
  1042. }
  1043. /* 删除这个非法入侵信息 */
  1044. pListIllInfo->deleteIllInfo(*pIll);
  1045. }
  1046. }
  1047. }
  1048. std::this_thread::sleep_for(std::chrono::milliseconds(g_config.ThreadSleepMS));
  1049. }
  1050. setThreadState(pRFAInfo, RunTimeState::RUN_STATE_STOP);
  1051. SPDLOG_LOGGER_INFO(m_logger, "关闭 {} 线程,ChannelID:{}", pRFAInfo->strFunctionName, pRFAInfo->ChannelID);
  1052. }
  1053. /**
  1054. * @brief 区域人员检测,检测这个区域内的人数,不能少于多少人,不能多余多少人
  1055. * 1、报警判断条件:直播间报警:当前频率所有直播间摄像头的区域人员检测算法输出结果均大于或小于设定人
  1056. 数值时,记为报警行为,直接展示报警结果
  1057. 2、录播间报警:当前频率所有录播间摄像头的区域人员检测算法输出结果均大于或小于设定人数值时,记为报
  1058. 警行为,直接展示报警结果
  1059. 3、直播间+录播间报警:当前频率直播间人数+录播间人数大于或小于设定人数值时,记为报警行为,直接展示
  1060. 报警结果
  1061. *
  1062. * @param RAInfo 传入的房间ID和算法ID
  1063. */
  1064. void SPAServer::threadActRegionalPersonnelDetection(FuncActionInfo* RFAInfo)
  1065. {
  1066. SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程,ChannelID:{} ,", RFAInfo->strFunctionName, RFAInfo->ChannelID);
  1067. /* 创建读取Redis的实例 */
  1068. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  1069. /* 创建写入数据库实例 */
  1070. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  1071. /* 局部变量,线程功能需要的信息 */
  1072. std::shared_ptr<FuncActionInfo> pRFAInfo = std::make_shared<FuncActionInfo>();
  1073. *pRFAInfo = *RFAInfo;
  1074. /* 保存每个摄像机的报警信息 */
  1075. std::shared_ptr<std::list<AlarmInfo>> pListAlarmInfo = std::make_shared<std::list<AlarmInfo>>();
  1076. /* 报警时间段 */
  1077. std::shared_ptr<std::vector<PersonCountRuleInfo>> pPersonCountRuleInfo = std::make_shared<std::vector<PersonCountRuleInfo>>();
  1078. /* 获取报警规则信息 */
  1079. toEQMDataBase->getPersonCountRuleInfo(*pPersonCountRuleInfo);
  1080. while (m_threadRunning)
  1081. {
  1082. std::this_thread::sleep_for(std::chrono::milliseconds(g_config.ThreadSleepMS));
  1083. /************ 更新线程信息 ************/
  1084. updateFuncInfo(pRFAInfo);
  1085. if(pRFAInfo->appFunction == AppFunction::APP_NONE)
  1086. {
  1087. break;
  1088. }
  1089. /************ 读取Redis数据 ************/
  1090. pListAlarmInfo->clear();
  1091. for(const auto& RoomInfo : pRFAInfo->listRoomCamActInfo)
  1092. {
  1093. for(const auto& it : RoomInfo.mapCameraAction)
  1094. {
  1095. std::string strKey = std::to_string(it.first) + ":" + it.second;
  1096. std::string strRetValue;
  1097. if(!fromRedis->getRedisString(strKey, strRetValue))
  1098. {
  1099. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  1100. continue;
  1101. }
  1102. /* 解析数据 */
  1103. AlarmInfo alarmInfo;
  1104. parseRedisBaseData(strRetValue, alarmInfo);
  1105. parseRedisBBoxesData(strRetValue, alarmInfo);
  1106. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  1107. if(isEventTimeVaild(alarmInfo.EventTime))
  1108. {
  1109. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  1110. continue;
  1111. }
  1112. pListAlarmInfo->push_back(alarmInfo);
  1113. }
  1114. }
  1115. /************ 获取报警规则(从数据库更新?) ************/
  1116. if(!toEQMDataBase->getPersonCountRuleInfo(*pPersonCountRuleInfo))
  1117. {
  1118. SPDLOG_LOGGER_ERROR(m_logger, "《人员计数》获取报警规则失败");
  1119. continue;
  1120. }
  1121. if(pPersonCountRuleInfo->size() == 0)
  1122. {
  1123. SPDLOG_LOGGER_ERROR(m_logger, "《人员计数》无报警规则");
  1124. break;
  1125. }
  1126. /* 获取这个频率的报警信息 */
  1127. PersonCountRuleInfo personCountRuleInfo;
  1128. for(auto& it : *pPersonCountRuleInfo)
  1129. {
  1130. if(it.ChannelID == pRFAInfo->ChannelID)
  1131. {
  1132. personCountRuleInfo = it;
  1133. break;
  1134. }
  1135. }
  1136. if(personCountRuleInfo.ChannelID < 0)
  1137. {
  1138. SPDLOG_LOGGER_ERROR(m_logger, "《人员计数》获取报警规则失败, ChannelID:{}", pRFAInfo->ChannelID);
  1139. break;
  1140. }
  1141. /************ 判断是否在检测时间内 ************/
  1142. QDateTime now = QDateTime::currentDateTime();
  1143. /* 判断检测时间类型 */
  1144. if(personCountRuleInfo.RuleType == 0)
  1145. {
  1146. /* 所有时段 */
  1147. }
  1148. else if(personCountRuleInfo.RuleType == 1)
  1149. {
  1150. /* 按天检测 */
  1151. if(personCountRuleInfo.StartTime <= now && personCountRuleInfo.EndTime >= now)
  1152. {
  1153. /* 在检测时间内 */
  1154. } else
  1155. {
  1156. /* 不在检测时间内 */
  1157. continue;
  1158. }
  1159. }
  1160. else if (personCountRuleInfo.RuleType == 2)
  1161. {
  1162. /* 按周检测 */
  1163. if(personCountRuleInfo.week != now.date().dayOfWeek())
  1164. {
  1165. /* 不在检测时间内 */
  1166. continue;
  1167. }
  1168. /* 在检测时间内 */
  1169. if(personCountRuleInfo.StartTime.time() <= now.time() && personCountRuleInfo.EndTime.time() >= now.time())
  1170. {
  1171. /* 在检测时间内 */
  1172. } else
  1173. {
  1174. /* 不在检测时间内 */
  1175. continue;
  1176. }
  1177. }
  1178. /************ 挨个房间检测人数 ************/
  1179. /************ 检测频率直播间 + 导播间房间的人数 ************/
  1180. }
  1181. setThreadState(pRFAInfo, RunTimeState::RUN_STATE_STOP);
  1182. SPDLOG_LOGGER_INFO(m_logger, "关闭 {} 线程,ChannelID:{}", pRFAInfo->strFunctionName, pRFAInfo->ChannelID);
  1183. }
  1184. /**
  1185. * @brief 普通任务识别,一个算法只需要一个摄像机,包括 违禁品识别、玩手机检测、老鼠等
  1186. 1、报警判断条件:机房内出现摄像头的违禁物品识别算法输出结果包含指定违规内容时,记为报警行为,直接
  1187. 展示报警结果
  1188. 2、这里应该是不区分违禁物品是什么,只要有违禁物品就报警。如果一个违禁物品消失之前又出现了第二个违禁物品,
  1189. 两个违禁物品都消失后,这次报警才会结束。
  1190. 3、一有报警信息就写入到EQM数据库的tAlarmInfo表中,报警开始时间是EventTime
  1191. 4、如果报警结束时间小于设置的最小间隔,就删除这条数据,超过这个时间,就更新结束时间
  1192. 5、报警信息要有图片,如果当前没有图片,就使用上一张图片
  1193. *
  1194. * @param info 线程信息
  1195. */
  1196. void SPAServer::threadActNormal(FuncActionInfo* info)
  1197. {
  1198. SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程, ChannelID:{} ,App:{}",info->strFunctionName, info->ChannelID, info->strFunctionName);
  1199. /* 创建读取Redis的实例 */
  1200. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  1201. /* 创建写入EQM数据库实例 */
  1202. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  1203. /* 局部变量,保存这个线程的信息 */
  1204. std::shared_ptr<FuncActionInfo> pFuncAct = std::make_shared<FuncActionInfo>();
  1205. *pFuncAct = *info;
  1206. /* 保存报警数据 */
  1207. std::shared_ptr<ListAlarmInfo> listAlarmInfo = std::make_shared<ListAlarmInfo>();
  1208. while (m_threadRunning)
  1209. {
  1210. /* 更新算法关联的摄像机ID */
  1211. updateFuncInfo(pFuncAct);
  1212. /* 如果线程的运行状态为 */
  1213. if(pFuncAct->RunState == RunTimeState::RUN_STATE_STOP)
  1214. {
  1215. pFuncAct->appFunction = AppFunction::APP_NONE;
  1216. break;
  1217. }
  1218. /* 读取Redis信息,处理数据 */
  1219. for(const auto& roomInfo : pFuncAct->listRoomCamActInfo)
  1220. {
  1221. for(const auto& it : roomInfo.mapCameraAction)
  1222. {
  1223. /* 读取Redis数据 */
  1224. std::string strKey = std::to_string(it.first) + ":" + it.second;
  1225. std::string strRetValue;
  1226. if(!fromRedis->getRedisString(strKey, strRetValue))
  1227. {
  1228. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  1229. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1230. continue;
  1231. }
  1232. /* 解析数据 */
  1233. AlarmInfo alarmInfo;
  1234. parseRedisBaseData(strRetValue, alarmInfo);
  1235. parseRedisBBoxesData(strRetValue, alarmInfo);
  1236. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  1237. if(isEventTimeVaild(alarmInfo.EventTime))
  1238. {
  1239. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  1240. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1241. continue;
  1242. }
  1243. /* 找出数组中与当前报警ID相同的的报警信息 */
  1244. auto pAlarmInfo = listAlarmInfo->findAlarmInfo(alarmInfo);
  1245. if(pAlarmInfo == nullptr)
  1246. {
  1247. /* 没有找到报警记录,就新建一个 */
  1248. AlarmInfo newAlarmInfo = alarmInfo;
  1249. listAlarmInfo->addAlarmInfo(newAlarmInfo);
  1250. /* 重新查找该报警信息 */
  1251. pAlarmInfo = listAlarmInfo->findAlarmInfo(alarmInfo);
  1252. if(pAlarmInfo == nullptr)
  1253. {
  1254. SPDLOG_LOGGER_ERROR(m_logger, "查找报警信息失败, Key:{}", strKey);
  1255. continue;
  1256. }
  1257. }
  1258. /* 判断有无报警记录,新的报警就写入到EQM的tAlarmInfo表中,正在报警的记录就判断和更新,
  1259. * 结束报警就更新tAlarmInfo的报警数据的结束时间,并清空pAlarmInfo */
  1260. if(pAlarmInfo->Is_Alarm)
  1261. {
  1262. /* 正在报警中,检查是否报警结束 */
  1263. if(alarmInfo.Is_Alarm)
  1264. {
  1265. /* 更新图片信息 */
  1266. if(!alarmInfo.ImageInfo.empty())
  1267. {
  1268. pAlarmInfo->ImageInfo = alarmInfo.ImageInfo;
  1269. }
  1270. } else {
  1271. /* 报警结束,判断时长,更新数据库结束时间,结束时间是此时电脑时间 */
  1272. if(timeDiffWithNow(pAlarmInfo->EventTime) > g_config.Contraband)
  1273. {
  1274. pAlarmInfo->EndTime = chronoToStrTime(std::chrono::system_clock::now());
  1275. toEQMDataBase->updateAlarmEndTime(*pAlarmInfo);
  1276. }else {
  1277. /* 不够报警时间,目前不做任何处理,不删除EQM报警记录 */
  1278. }
  1279. /* 清空报警信息 */
  1280. pAlarmInfo->reInit();
  1281. }
  1282. }
  1283. else
  1284. {
  1285. /* 是新的报警 */
  1286. if(alarmInfo.Is_Alarm)
  1287. {
  1288. /* 图片不能是空,如果是空的,就不写入数据库 */
  1289. if(!alarmInfo.ImageInfo.empty())
  1290. {
  1291. /* 违禁品检测,开始时间是事件时间 */
  1292. alarmInfo.StartTime = alarmInfo.EventTime;
  1293. alarmInfo.EndTime = "";
  1294. if(toEQMDataBase->insertAlarmInfo(alarmInfo))
  1295. {
  1296. /* 保存新的报警记录 */
  1297. *pAlarmInfo = alarmInfo;
  1298. }else {
  1299. SPDLOG_LOGGER_ERROR(m_logger, "写入tAlarmInfo报警数据失败, Key: {}", strKey);
  1300. }
  1301. }else {
  1302. SPDLOG_LOGGER_ERROR(m_logger, "频道:{}, 房间:{}, 摄像机:{}, 算法:{}, 有报警区域, 但是没有图片信息", pFuncAct->ChannelID, roomInfo.RoomID, it.first, it.second);
  1303. }
  1304. }
  1305. }
  1306. }
  1307. }
  1308. /* 休眠设置的时间 */
  1309. std::this_thread::sleep_for(std::chrono::seconds(g_config.ThreadSleepMS));
  1310. }
  1311. /* 设置线程退出的状态 */
  1312. setThreadState(pFuncAct, RunTimeState::RUN_STATE_STOP);
  1313. SPDLOG_LOGGER_INFO(m_logger, "{} 线程退出,Channel:{}",pFuncAct->strFunctionName, pFuncAct->ChannelID);
  1314. }
  1315. /* 将该算法对应的摄像机放入摄像机列表 */
  1316. bool SPAServer::insertCameraToAction(RoomActionInfo* pRAInfo, std::list<RoomCameraInfo>& listRC, std::multimap<int, std::string>& mapCameraActionID)
  1317. {
  1318. for(const auto& rci : listRC)
  1319. {
  1320. if(rci.RoomID == pRAInfo->RoomID)
  1321. {
  1322. /* 这个摄像机在这个房间内,再判断这个摄像机有没有这个算法 */
  1323. for(const auto& camID : rci.listCameraID)
  1324. {
  1325. for(const auto& cai : mapCameraActionID)
  1326. {
  1327. if(camID == cai.first)
  1328. {
  1329. /* 再判断这个摄像机的算法是否是当前需要的 */
  1330. if(cai.second == pRAInfo->ActionID)
  1331. {
  1332. pRAInfo->listCameraID.push_back(camID);
  1333. }
  1334. }
  1335. }
  1336. }
  1337. }
  1338. }
  1339. return true;
  1340. }
  1341. /**
  1342. * @brief 功能线程更新功能信息,这里是从内存中的数组里获取
  1343. *
  1344. * @param pInfo
  1345. * @return true
  1346. * @return false
  1347. */
  1348. bool SPAServer::updateFuncInfo(std::shared_ptr<FuncActionInfo> pInfo)
  1349. {
  1350. pInfo->clearActionList();
  1351. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  1352. auto fa = m_runListFuncActInfo.findAppFunction(*pInfo);
  1353. if(fa == nullptr)
  1354. {
  1355. return false;
  1356. }
  1357. *pInfo = *fa;
  1358. return true;
  1359. }
  1360. /* 设置线程状态 */
  1361. void SPAServer::setThreadState(std::shared_ptr<FuncActionInfo> pInfo, RunTimeState state)
  1362. {
  1363. std::lock_guard<std::mutex> look(m_mutexRunFAI);
  1364. auto p = m_runListFuncActInfo.findAppFunction(*pInfo);
  1365. if(p != nullptr)
  1366. {
  1367. p->RunState = state;
  1368. }
  1369. }
  1370. /* 计算与当前时间的时间差,返回秒 */
  1371. int SPAServer::timeDiffWithNow(const std::string& strTime)
  1372. {
  1373. auto now = std::chrono::system_clock::now();
  1374. auto eventTime = strTimeToChrono(strTime);
  1375. std::chrono::duration<double> diff = now - eventTime;
  1376. return diff.count();
  1377. }
  1378. /* 字符串时间转换成std::chrono时间点 */
  1379. std::chrono::system_clock::time_point SPAServer::strTimeToChrono(const std::string& strTime)
  1380. {
  1381. std::istringstream iss(strTime);
  1382. std::tm tmEvent = {};
  1383. iss >> std::get_time(&tmEvent, "%Y-%m-%d %H:%M:%S");
  1384. return std::chrono::system_clock::from_time_t(std::mktime(&tmEvent));
  1385. }
  1386. /* 时间点转换成字符串 */
  1387. std::string SPAServer::chronoToStrTime(const std::chrono::system_clock::time_point& timePoint)
  1388. {
  1389. std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
  1390. std::tm tmEvent = *std::localtime(&time);
  1391. char buf[64] = {0};
  1392. std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tmEvent);
  1393. return std::string(buf);
  1394. }