SPAServer.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. #include "SPAServer.h"
  2. #include "spdlog/spdlog.h"
  3. #include <filesystem>
  4. #include "ThreadPool/ThreadPool.h"
  5. #include "GlobalInfo/GlobalVariable.h"
  6. #include "GlobalInfo/GlobalConfig.h"
  7. #include "UniversalFunc.h"
  8. #include <QApplication>
  9. #include <QVector>
  10. SPAServer::SPAServer()
  11. {
  12. m_logger = spdlog::get("SPAServer");
  13. if(m_logger == nullptr)
  14. {
  15. SPDLOG_ERROR("APAServer logger is nullptr");
  16. return;
  17. }
  18. /* 读取全局的配置文件 */
  19. QString strConfigFile = QApplication::applicationDirPath() + "/config.ini";
  20. if(!GConfig.readConfig(strConfigFile))
  21. {
  22. /* 读取配置文件失败,直接退出程序 */
  23. return;
  24. }
  25. GConfig.printValue();
  26. m_threadRunning = true;
  27. /* 初始化WebAPI */
  28. m_toEQMDataBase.initWebApi("http://192.1.3.133:31000/v6/", "", "4c2f9fc91c22dd98331e47af2e2964f4");
  29. /* 模拟违禁品算法ID,后续需要动态调整 */
  30. g_actionList.ActContraband = "OD210_026_005246_001-IZRTKyEx";
  31. }
  32. SPAServer::~SPAServer()
  33. {
  34. }
  35. /* 启动服务 */
  36. void SPAServer::startServer()
  37. {
  38. /* 添加获取基础信息的线程 */
  39. // CPPTP.add_task(&SPAServer::threadFromSuperBrain, this);
  40. /* 测试Redis读取并解析数据线程 */
  41. CameraThreadInfo info;
  42. info.RedisIP = "172.16.36.80";
  43. info.RedisPort = 32222;
  44. info.RedisPWD = "Ff1z@TOFr^iwd%Ra";
  45. info.DeviceID = 117;
  46. info.vecAction.push_back("OD210_026_005246_001-IZRTKyEx");
  47. g_actionList.ActContraband = "OD210_026_005246_001-IZRTKyEx";
  48. // CPPTP.add_task(&SPAServer::threadFromRedis, this, info);
  49. // threadFromRedis(info);
  50. }
  51. /**
  52. * @brief 从基础平台获取算法信息和设备信息的线程函数
  53. * 从基础平台获取算法信息和设备信息,然后更新到EQM数据库所对应的表中
  54. *
  55. */
  56. void SPAServer::threadFromSuperBrain()
  57. {
  58. SPDLOG_LOGGER_INFO(m_logger, "开启 fromSuperBrainThread 线程, 获取算法和设备信息");
  59. /* 创建变量 */
  60. std::vector<AlgorithmInfo> vecAlgNewInfo;
  61. std::vector<DeviceInfo> vecDevNewInfo;
  62. /* 获取一次token,后续失效了再获取 */
  63. m_fromSuperBrain.getToken();
  64. while (m_threadRunning)
  65. {
  66. SPDLOG_LOGGER_INFO(m_logger, "刷新算法和设备信息");
  67. /* 先更新数据库的信息,防止从其他地方更改了数据库,这里没有刷新本地缓存 */
  68. m_toEQMDataBase.getAlgorithmInfo(m_vecEqmAlgInfo);
  69. m_toEQMDataBase.getDeviceInfo(m_vecEqmDevInfo);
  70. m_toEQMDataBase.getDeviceAlgorithmInfo(m_vecEqmDevInfo, m_listDevIDDelete);
  71. /* 从超脑获取基础信息 */
  72. m_fromSuperBrain.getTaskTypeList(vecAlgNewInfo);
  73. m_fromSuperBrain.getDeviceList(vecDevNewInfo);
  74. /* 处理算法信息 */
  75. bool algIsUpdate = processAlgorithmInfo(vecAlgNewInfo);
  76. /* 处理设备信息 */
  77. bool devIsUpdate = processDeviceInfo(vecDevNewInfo);
  78. vecAlgNewInfo.clear();
  79. vecDevNewInfo.clear();
  80. /* 更新算法详细信息 */
  81. m_mutexActionInfo.lock();
  82. m_toEQMDataBase.getActionInfo(m_listActionInfo);
  83. m_mutexActionInfo.unlock();
  84. /* 10秒更新一次 */
  85. std::this_thread::sleep_for(std::chrono::seconds(10));
  86. }
  87. SPDLOG_LOGGER_INFO(m_logger, "退出 fromSuperBrainThread 线程");
  88. }
  89. /* 处理算法信息,返回值为true,说明有改变,需要重新读取 */
  90. bool SPAServer::processAlgorithmInfo(std::vector<AlgorithmInfo> vecNewAlgInfo)
  91. {
  92. std::vector<AlgorithmInfo> vecAlgUpdate;
  93. std::vector<AlgorithmInfo> vecAlgDelete;
  94. /* 对比数据库表格信息,这里只有插入和删除,没有更新 */
  95. compareAlgorithmInfo(vecNewAlgInfo, vecAlgUpdate, vecAlgDelete);
  96. /* 更新数据库,先删除,再写入刷新 */
  97. bool isUpdate = false;
  98. if(vecAlgDelete.size() > 0)
  99. {
  100. SPDLOG_LOGGER_DEBUG(m_logger, "删除算法信息");
  101. m_toEQMDataBase.deleteAlgorithmInfo(vecAlgDelete);
  102. isUpdate = true;
  103. }
  104. if(vecAlgUpdate.size() > 0)
  105. {
  106. SPDLOG_LOGGER_DEBUG(m_logger, "写入算法信息");
  107. m_toEQMDataBase.writeAlgorithmInfo(vecAlgUpdate);
  108. isUpdate = true;
  109. }
  110. return isUpdate;
  111. }
  112. /**
  113. * @brief 处理设备信息
  114. *
  115. * @param vecNewDevInfo 传入新获取到的值
  116. * @return true 需要重新读取数据库,获取新的数据
  117. * @return false 无需读取数据库
  118. */
  119. bool SPAServer::processDeviceInfo(std::vector<DeviceInfo> vecNewDevInfo)
  120. {
  121. std::vector<DeviceInfo> vecDevInsert;
  122. std::vector<DeviceInfo> vecDevUpdate;
  123. std::vector<DeviceInfo> vecDevDelete;
  124. /*-------------------------------------------------------------------------
  125. ****** 这里只对比设备信息,不对比设备的算法信息,算法信息在下面单独对比 *******
  126. *------------------------------------------------------------------------*/
  127. /* 如果本地缓存没有数据,那么就全部插入 */
  128. if(m_vecEqmDevInfo.size() > 0)
  129. {
  130. for(auto& DevInfo : vecNewDevInfo)
  131. {
  132. bool isExist = false;
  133. for(auto& it0 : m_vecEqmDevInfo)
  134. {
  135. if(DevInfo.DeviceID == it0.DeviceID)
  136. {
  137. isExist = true;
  138. /* 对比其他项是否相等,不相等就更新 */
  139. if(DevInfo == it0)
  140. {
  141. continue;
  142. }else {
  143. vecDevUpdate.push_back(DevInfo);
  144. }
  145. break;
  146. }
  147. }
  148. if(!isExist)
  149. {
  150. vecDevInsert.push_back(DevInfo);
  151. }
  152. }
  153. }else {
  154. vecDevInsert = vecNewDevInfo;
  155. }
  156. /* 获取删除列表 */
  157. if(vecNewDevInfo.size() > 0)
  158. {
  159. bool isExist = false;
  160. for(const auto& it : m_vecEqmDevInfo)
  161. {
  162. isExist = false;
  163. for(const auto& it0 : vecNewDevInfo)
  164. {
  165. if(it.DeviceID == it0.DeviceID)
  166. {
  167. isExist = true;
  168. break;
  169. }
  170. }
  171. if(!isExist)
  172. {
  173. vecDevDelete.push_back(it);
  174. }
  175. }
  176. }else {
  177. vecDevDelete = m_vecEqmDevInfo;
  178. }
  179. bool isUpdate = false;
  180. /* 先删除多余的数据 */
  181. if(vecDevDelete.size() > 0)
  182. {
  183. SPDLOG_LOGGER_DEBUG(m_logger, "删除设备信息");
  184. m_toEQMDataBase.deleteDeviceInfo(vecDevDelete);
  185. isUpdate = true;
  186. }
  187. /* 更新数据 */
  188. if(vecDevUpdate.size() > 0)
  189. {
  190. SPDLOG_LOGGER_DEBUG(m_logger, "更新设备信息");
  191. m_toEQMDataBase.updateDeviceInfo(vecDevUpdate);
  192. isUpdate = true;
  193. }
  194. /* 插入数据 */
  195. if(vecDevInsert.size() > 0)
  196. {
  197. SPDLOG_LOGGER_DEBUG(m_logger, "插入设备信息");
  198. m_toEQMDataBase.insertDeviceInfo(vecDevInsert);
  199. isUpdate = true;
  200. }
  201. /*-------------------------------------------------------------------------
  202. ************* 处理设备和算子关联的表格,单独对比设备的算法信息 *************
  203. *------------------------------------------------------------------------*/
  204. /* 插入新的设备信息 */
  205. if(vecDevInsert.size() > 0)
  206. {
  207. SPDLOG_LOGGER_DEBUG(m_logger, "插入设备和算法关联表");
  208. m_toEQMDataBase.insertDeviceAlgorithmInfo(vecDevInsert);
  209. isUpdate = true;
  210. }
  211. vecDevUpdate.clear();
  212. /* 对比现有的设备是否需要更新算法 */
  213. compareDeviceAlgorithmInfo(vecNewDevInfo, vecDevUpdate);
  214. if(vecDevUpdate.size() > 0)
  215. {
  216. SPDLOG_LOGGER_DEBUG(m_logger, "更新设备和算法关联表, 更新设备数目:{}", vecDevUpdate.size());
  217. m_toEQMDataBase.updateDeviceAlgorithmInfo(vecDevUpdate);
  218. }
  219. /* 删除tActionCamer表中消失的设备信息 */
  220. if(m_listDevIDDelete.size() > 0)
  221. {
  222. SPDLOG_LOGGER_DEBUG(m_logger, "删除消失的设备关联的算法");
  223. m_toEQMDataBase.deleteDeviceAlgorithmInfo(m_listDevIDDelete);
  224. isUpdate = true;
  225. }
  226. return isUpdate;
  227. }
  228. /* 对比现有的数据和新获取到的数据,取出要删除和添加的数据 */
  229. void SPAServer::compareAlgorithmInfo(const std::vector<AlgorithmInfo>& vecNewInfo, std::vector<AlgorithmInfo>& vecAlgUpdate, std::vector<AlgorithmInfo>& vecAlgDelete)
  230. {
  231. /* 取出要添加的,如果本地缓存是0,那么全部都要添加 */
  232. if(m_vecEqmAlgInfo.size() > 0)
  233. {
  234. for(const auto& it : vecNewInfo)
  235. {
  236. bool isExist = false;
  237. for(const auto& it0 : m_vecEqmAlgInfo)
  238. {
  239. /* 如果存在就退出循环 */
  240. if(it.ActionID == it0.ActionID)
  241. {
  242. isExist = true;
  243. break;
  244. }
  245. }
  246. if(!isExist)
  247. {
  248. vecAlgUpdate.push_back(it);
  249. }
  250. }
  251. }else {
  252. vecAlgUpdate = vecNewInfo;
  253. }
  254. /* 取出要删除的,如果新的数据是0,那么全部都要删除 */
  255. if(vecNewInfo.size() > 0)
  256. {
  257. bool isExist = false;
  258. for(const auto& it : m_vecEqmAlgInfo)
  259. {
  260. isExist = false;
  261. for(const auto& it0 : vecNewInfo)
  262. {
  263. if(it.ActionID == it0.ActionID)
  264. {
  265. isExist = true;
  266. break;
  267. }
  268. }
  269. if(!isExist)
  270. {
  271. vecAlgDelete.push_back(it);
  272. }
  273. }
  274. }else {
  275. vecAlgDelete = m_vecEqmAlgInfo;
  276. }
  277. }
  278. /**
  279. * @brief 对比设备和算法关联表是否需要更新
  280. * 对比规则:
  281. * 1、这里只对比已有的设备ID,需要删除的ID在获取到tActionCamer表是就已经取出来了
  282. * 2、如果设备ID相等,那么进一步对比算法信息是否相等
  283. * 3、如果设备ID相等,但是算法信息数目不相等,那么直接加入更新列表
  284. * 4、如果设备ID相等,算法信息数目相等,进一步对比算法信息
  285. *
  286. * @param vecNewInfo
  287. * @param vecDevUpdate
  288. */
  289. void SPAServer::compareDeviceAlgorithmInfo(const std::vector<DeviceInfo>& vecNewInfo, std::vector<DeviceInfo>& vecDevUpdate)
  290. {
  291. vecDevUpdate.clear();
  292. for(const auto& it0 : vecNewInfo)
  293. {
  294. for(const auto& it1 : m_vecEqmDevInfo)
  295. {
  296. if(it0.DeviceID == it1.DeviceID)
  297. {
  298. /* 设备的算法信息数目不相等,直接加入更新列表 */
  299. if(it0.vecAlgorithmInfo.size() != it1.vecAlgorithmInfo.size())
  300. {
  301. vecDevUpdate.push_back(it0);
  302. break;
  303. }
  304. /* 设备的算法信息数目相等,进一步对比算法信息 */
  305. bool isEquality = true;
  306. for(const auto& it2 : it0.vecAlgorithmInfo)
  307. {
  308. bool isEq2 = false;
  309. for(const auto& it3 : it1.vecAlgorithmInfo)
  310. {
  311. /* 这里只对比算法ID */
  312. if(it2.ActionID != it3.ActionID)
  313. {
  314. continue;
  315. }else {
  316. isEq2 = true;
  317. break;
  318. }
  319. }
  320. if(!isEq2)
  321. {
  322. isEquality = false;
  323. break;
  324. }
  325. }
  326. if(!isEquality)
  327. {
  328. vecDevUpdate.push_back(it0);
  329. break;
  330. }
  331. }
  332. }
  333. }
  334. }
  335. /**
  336. * @brief 从Redis获取数据线程函数,这个是摄像机线程
  337. * 一个设备一个线程,这个线程的相关变量只在这个线程中使用
  338. * (注意,这个函数未被使用,不从这里获取Redis数据)
  339. * @param info
  340. */
  341. void SPAServer::threadFromRedis(const CameraThreadInfo& info)
  342. {
  343. SPDLOG_LOGGER_INFO(m_logger, "开启 fromRedisThread 线程,设备ID:{}", info.DeviceID);
  344. FromRedis fromRedis;
  345. fromRedis.setRedisIPAndPort(info.RedisIP, info.RedisPort);
  346. fromRedis.setRedisPassword(info.RedisPWD);
  347. if(fromRedis.connectRedis())
  348. {
  349. SPDLOG_LOGGER_INFO(m_logger, "连接Redis成功");
  350. }else {
  351. SPDLOG_LOGGER_ERROR(m_logger, "连接Redis失败");
  352. return;
  353. }
  354. CameraThreadInfo threadInfo = info;
  355. // std::string strKey = "117:OD210_026_005246_001-IZRTKyEx";
  356. /* 取出该设备的Key */
  357. std::vector<std::string> vecKey;
  358. for(const auto& it : info.vecAction)
  359. {
  360. std::string strKey = std::to_string(info.DeviceID) + ":" + it;
  361. vecKey.push_back(strKey);
  362. }
  363. std::string strRetValue;
  364. /* 进入线程循环 */
  365. while (m_threadRunning)
  366. {
  367. /* 循环读取这个设备关联的算法信息 */
  368. for(const auto& it : vecKey)
  369. {
  370. SPDLOG_LOGGER_INFO(m_logger, "读取Redis信息, Key: {}", it);
  371. if( !fromRedis.getRedisString(it, strRetValue) )
  372. {
  373. continue;
  374. }
  375. SPDLOG_LOGGER_TRACE(m_logger, "Redis Value:\n{}", strRetValue);
  376. /* 解析数据 */
  377. AlarmInfo alarmInfo;
  378. alarmInfo.ActionID = it.substr(it.find(":") + 1);
  379. /* 解析数据 */
  380. parseRedisBaseData(strRetValue, alarmInfo);
  381. parseRedisBBoxesData(strRetValue, alarmInfo);
  382. /* 信息时间有的效性判断,主要是记录Redis数据的有效性,是否长时间没有变化,排查错误时用的
  383. * 如果数据长时间数据不变,那么超脑那里就挂了,写入日志 */
  384. isEventTimeVaild(alarmInfo.EventTime);
  385. /* 是否需要写入EQM数据库判断
  386. * 人员计数和区域人员检测需要多次判断,其他的直接写入即可 */
  387. }
  388. std::this_thread::sleep_for(std::chrono::seconds(2));
  389. }
  390. return;
  391. }
  392. /**
  393. * @brief 判断时间是否长时间没有更新,默认的是600秒,超过这个时间Redis还未更新,可能是超脑挂了
  394. *
  395. * @param strTime
  396. * @return true
  397. * @return false
  398. */
  399. bool SPAServer::isEventTimeVaild(const std::string& strTime)
  400. {
  401. /* 获取当前时间 */
  402. std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
  403. /* 字符串转成时间 */
  404. std::istringstream iss(strTime);
  405. std::tm tmEvent = {};
  406. iss >> std::get_time(&tmEvent, "%Y-%m-%d %H:%M:%S");
  407. /* 时间差 */
  408. std::chrono::system_clock::time_point eventTime = std::chrono::system_clock::from_time_t(std::mktime(&tmEvent));
  409. std::chrono::duration<double> diff = now - eventTime;
  410. // SPDLOG_LOGGER_DEBUG(m_logger, "now:{} eventTime: {} 时间差:{}秒",now, eventTime, diff);
  411. if(diff.count() > 600)
  412. {
  413. // SPDLOG_LOGGER_ERROR(m_logger, "Redis数据长时间没有更新,EventTime:{}", strTime);
  414. return false;
  415. }
  416. return true;
  417. }
  418. /**
  419. * @brief 分派任务的线程
  420. 1、 线程定时刷新房间和摄像机的关联关系,以及和算法Action的关联关系
  421. 2、 将算法信息加入到不同的列表中
  422. 需要多个摄像机配合的加入到m_runListRoomActionInfo列表
  423. 不需要多个摄像机配合的加入到m_runListActionInfo列表
  424. 3、 每次刷新都会清空ActionInfo或者RoomActionInfo的摄像机列表,但是不会动其他基本信息,如果是列表中没有对应的信息,
  425. 就会创建新的ActionInfo,那么RunState的状态是INIT,需要开启新的线程
  426. 如果刷新完成后,算法对应的摄像机列表为空,那么对应的线程就会停止运行,将RunState设置为STOP,本线程的下一轮
  427. 循环就会删除这个ActionInfo
  428. */
  429. void SPAServer::threadRoomCamera()
  430. {
  431. /* 房间相机关联信息 */
  432. std::list<RoomCameraInfo> listRC;
  433. /* 存储获取到的应用和启用时间的信息 */
  434. std::list<AppAndTimeInfo> listAppAndTime;
  435. /* 创建连接数据库实例 */
  436. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  437. while (m_threadRunning)
  438. {
  439. GThreadInfo.lockRunFAI();
  440. /* 先清理已经退出的线程所用到的Action或者RoomAction */
  441. GThreadInfo.clearNoneFuncActionInfo();
  442. /* 创建应用信息,根据从EQM数据库读取到的配置的应用信息创建 */
  443. toEQMDataBase->getAlarmAppInfo(listAppAndTime);
  444. for(const auto& it : listAppAndTime)
  445. {
  446. /* 创建应用信息,如果已有该应用,就更新时间 */
  447. GThreadInfo.addFuncActionInfo(it);
  448. }
  449. /* 先获取EQM数据库信息,取出房间和摄像机关联信息 */
  450. m_mutexActionInfo.lock();
  451. toEQMDataBase->getActionInfo(m_listActionInfo);
  452. /* 将算法信息加入到不同的列表中 */
  453. GThreadInfo.clearActionList();
  454. for(const auto& it : m_listActionInfo.getData())
  455. {
  456. GThreadInfo.addActionInfo(*it);
  457. }
  458. /* 检查算法信息的状态,频率里的应用没有配置摄像机就设置为线程运行状态为停止,退出该线程 */
  459. GThreadInfo.setNoneCameraFuncStop();
  460. m_mutexActionInfo.unlock();
  461. /* 开启线程 */
  462. for(const auto& it0 : GThreadInfo.getList())
  463. {
  464. /* 人员在岗识别 */
  465. /* 区域人员检测 */
  466. /* 非法入侵检测 */
  467. /* 违禁品识别 */
  468. /* 普通任务线程,一个任务对应一个摄像机和一个算法ID,无需联动 */
  469. if(it0->appFunction == AppFunction::APP_AllDown)
  470. {
  471. if(it0->RunState == RunTimeState::RUN_STATE_INIT)
  472. {
  473. // CPPTP.add_task(&SPAServer::threadActNormal, this, it0);
  474. }
  475. }
  476. }
  477. GThreadInfo.unlockRunFAI();
  478. /* 休眠n秒,默认应该是300秒 */
  479. std::this_thread::sleep_for(std::chrono::seconds(GConfig.CheckSet));
  480. }
  481. }
  482. /**
  483. * @brief 人员在岗识别线程,这个功能主要是给客户端提供实时的人脸识别信息
  484. * 1、这个写入tWorkOnInfo表格,十分钟写一次
  485. * 2、理论上这里需要实时更新数据库,但是实际上时每十分钟写入一次数据
  486. * 3、这个线程也会进行在岗离岗的识别报警
  487. *
  488. * @param RFAInfo 传入房间ID和算法ID
  489. */
  490. void SPAServer::threadActPersonWork(FuncActionInfo* RFAInfo)
  491. {
  492. SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程,ChannelID:{} ,", RFAInfo->strFunctionName, RFAInfo->ChannelID);
  493. /* 创建读取Redis的实例 */
  494. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  495. /* 创建写入数据库实例 */
  496. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  497. /* 局部变量 */
  498. std::shared_ptr<FuncActionInfo> pRFAInfo = std::make_shared<FuncActionInfo>();
  499. *pRFAInfo = *RFAInfo;
  500. /* 保存每个摄像机的报警信息 */
  501. std::shared_ptr<std::list<AlarmInfo>> pListAlarmInfo = std::make_shared<std::list<AlarmInfo>>();
  502. /* 保存人脸信息的数据 */
  503. std::shared_ptr<ListRoomFaceInfo> pListRoomFaceInfo = std::make_shared<ListRoomFaceInfo>();
  504. while (m_threadRunning)
  505. {
  506. /* 更新线程信息 */
  507. GThreadInfo.updateFuncInfo(pRFAInfo.get());
  508. if(pRFAInfo->appFunction == AppFunction::APP_NONE)
  509. {
  510. break;
  511. }
  512. /* 读取Redis数据 */
  513. pListAlarmInfo->clear();
  514. for(const auto& RoomInfo : pRFAInfo->listRoomCamActInfo)
  515. {
  516. for(const auto& it : RoomInfo.mapCameraAction)
  517. {
  518. std::string strKey = std::to_string(it.first) + ":" + it.second;
  519. std::string strRetValue;
  520. if(!fromRedis->getRedisString(strKey, strRetValue))
  521. {
  522. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  523. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  524. continue;
  525. }
  526. /* 解析数据 */
  527. AlarmInfo alarmInfo;
  528. parseRedisBaseData(strRetValue, alarmInfo);
  529. parseRedisBBoxesData(strRetValue, alarmInfo);
  530. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  531. if(isEventTimeVaild(alarmInfo.EventTime))
  532. {
  533. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  534. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  535. continue;
  536. }
  537. pListAlarmInfo->push_back(alarmInfo);
  538. }
  539. }
  540. /* 处理数据,将报警信息的人脸信息取出来,放入到人脸信息列表中 */
  541. for(auto& alarmInfo : *pListAlarmInfo)
  542. {
  543. /* 添加到人脸列表中,会自动去重,自动创建对应的频道和房间信息 */
  544. pListRoomFaceInfo->addRoomFaceInfo(alarmInfo);
  545. }
  546. /* 计算人脸个数,并判断是否需要写入数据库 */
  547. QDateTime now = QDateTime::currentDateTime();
  548. for(auto it = pListRoomFaceInfo->listRoomFaceInfo.begin(); it != pListRoomFaceInfo->listRoomFaceInfo.end(); )
  549. {
  550. if(it->MaxNum < it->listPersonInfo.size())
  551. {
  552. it->MaxNum = it->listPersonInfo.size();
  553. }
  554. if(it->MinNum > it->listPersonInfo.size())
  555. {
  556. it->MinNum = it->listPersonInfo.size();
  557. }
  558. /* 判断是否需要写入数据库,超过设置的时间就写入,默认是600秒 */
  559. if(now.toSecsSinceEpoch() - it->StartTime.toSecsSinceEpoch() > GConfig.AppUpdateOnWorkTimeInterval)
  560. {
  561. /* 写入数据库 */
  562. if(toEQMDataBase->insertOnWorkInfo(*it))
  563. {
  564. SPDLOG_LOGGER_INFO(m_logger, "ChannelID:{}, RoomID:{}, 在岗信息写入数据库成功, StartTime", it->ChannelID, it->RoomID, it->StartTime.toString("yyyy-MM-dd hh:mm:ss").toStdString());
  565. /* 删除这条信息 */
  566. it = pListRoomFaceInfo->listRoomFaceInfo.erase(it);
  567. continue;
  568. }
  569. }
  570. ++it;
  571. }
  572. std::this_thread::sleep_for(std::chrono::milliseconds(GConfig.ThreadSleepMS));
  573. }
  574. GThreadInfo.setThreadState(pRFAInfo.get(), RunTimeState::RUN_STATE_STOP);
  575. SPDLOG_LOGGER_INFO(m_logger, "关闭 {} 线程,ChannelID:{}", pRFAInfo->strFunctionName, pRFAInfo->ChannelID);
  576. }
  577. /**
  578. * @brief 区域非法入侵检测
  579. 1、野猫、宠物识别:报警判断条件:机房内出现摄像头的宠物识别算法输出报警结果时,记为报警行为,直接
  580. 展示报警结果
  581. 2、无权限人员识别:报警判断条件:当判断出区域非法入侵行为时,依据人脸识别结果,判断是否为人脸库人
  582. 员,非人脸库的人员时判断为非法入侵行为(背影需要报警需要结合区域人员检测算法判断)
  583. 3、人员计数和人脸识别数不相等时,判断为非法入侵行为
  584. 检测逻辑:
  585. 1、先检测非法的人脸信息
  586. 2、再检测人员计数和人脸识别数不相等
  587. *
  588. * @param info
  589. */
  590. void SPAServer::threadActIllegalInvasion(FuncActionInfo* RFAInfo)
  591. {
  592. SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程,ChannelID:{} ,", RFAInfo->strFunctionName, RFAInfo->ChannelID);
  593. /* 创建读取Redis的实例 */
  594. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  595. /* 创建写入数据库实例 */
  596. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  597. /* 局部变量 */
  598. std::shared_ptr<FuncActionInfo> pRFAInfo = std::make_shared<FuncActionInfo>();
  599. *pRFAInfo = *RFAInfo;
  600. /* 保存每个摄像机的报警信息 */
  601. std::shared_ptr<std::list<AlarmInfo>> pListAlarmInfo = std::make_shared<std::list<AlarmInfo>>();
  602. /* 保存非法入侵的信息 */
  603. std::shared_ptr<ListIllegalInvasionInfo> pListIllInfo = std::make_shared<ListIllegalInvasionInfo>();
  604. std::string strFaceActionID; /* 人脸识别的Action */
  605. std::string strCountActionID; /* 人员计数的Action */
  606. /* 封装lambda表达式 */
  607. std::function<bool(const std::vector<PersonInfo>&)> funcIsAlarm = [](const std::vector<PersonInfo>& vecPersion) {
  608. for(const auto& it : vecPersion)
  609. {
  610. if(it.PersonID == "-1")
  611. {
  612. return true;
  613. }
  614. }
  615. return false;
  616. };
  617. while (m_threadRunning)
  618. {
  619. /* 更新线程信息 */
  620. GThreadInfo.updateFuncInfo(pRFAInfo.get());
  621. if(pRFAInfo->appFunction == AppFunction::APP_NONE)
  622. {
  623. break;
  624. }
  625. /* 更新算法ActionID */
  626. {
  627. g_actionList.mutexRW.lockForRead();
  628. strFaceActionID = g_actionList.ActFace;
  629. strCountActionID = g_actionList.ActPersonNumber;
  630. g_actionList.mutexRW.unlock();
  631. }
  632. /* 读取Redis数据 */
  633. pListAlarmInfo->clear();
  634. for(const auto& RoomInfo : pRFAInfo->listRoomCamActInfo)
  635. {
  636. for(const auto& it : RoomInfo.mapCameraAction)
  637. {
  638. std::string strKey = std::to_string(it.first) + ":" + it.second;
  639. std::string strRetValue;
  640. if(!fromRedis->getRedisString(strKey, strRetValue))
  641. {
  642. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  643. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  644. continue;
  645. }
  646. /* 解析数据 */
  647. AlarmInfo alarmInfo;
  648. // parseRedisData(strRetValue, alarmInfo);
  649. parseRedisBaseData(strRetValue, alarmInfo);
  650. parseRedisBBoxesData(strRetValue, alarmInfo);
  651. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  652. if(isEventTimeVaild(alarmInfo.EventTime))
  653. {
  654. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  655. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  656. continue;
  657. }
  658. pListAlarmInfo->push_back(alarmInfo);
  659. }
  660. }
  661. /* 找出房间的个数,按照房间进行判断 */
  662. ListRoomIll listRoomIll;
  663. for(auto& it : pRFAInfo->listRoomCamActInfo)
  664. {
  665. listRoomIll.addRoom(it.RoomID, it.RoomType);
  666. }
  667. /* 根据房间解析人脸识别的非法入侵,这个有了就不用判断后面的人员计数不相等了 */
  668. for(auto& room : listRoomIll.getData())
  669. {
  670. for(const auto& it : *pListAlarmInfo)
  671. {
  672. /* 人俩识别算法 */
  673. if(it.ActionID == strFaceActionID)
  674. {
  675. if(it.vecPersonInfo.size() > room.numMaxFace)
  676. {
  677. room.numMaxFace = it.vecPersonInfo.size();
  678. room.strBoxList = it.listBbox;
  679. room.vecPersonInfo = it.vecPersonInfo;
  680. }
  681. }
  682. /* 判断有没有非法入侵人员 */
  683. if(funcIsAlarm(it.vecPersonInfo))
  684. {
  685. room.isAlarm = true;
  686. room.strMessage = "人员非法入侵(未知人员)";
  687. if( !it.ImageInfo.empty() )
  688. {
  689. room.strImage = it.ImageInfo;
  690. }
  691. room.CameraID = it.DeviceID;
  692. }
  693. }
  694. }
  695. /* 判断人脸识别到的数目和人员计数算法识别到的人数是否相等,如果上面报警了,这里就不用检测了 */
  696. for(auto& room : listRoomIll.getData())
  697. {
  698. if(room.isAlarm)
  699. {
  700. continue;
  701. }
  702. /* 这个房间的人脸算法没有识别到非法入侵 */
  703. for(const auto& it : *pListAlarmInfo)
  704. {
  705. if(it.ActionID == strCountActionID)
  706. {
  707. if(it.vecPersonInfo.size() > room.numMaxPerson)
  708. {
  709. room.numMaxPerson = it.vecPersonInfo.size();
  710. room.strBoxList = it.listBbox;
  711. if(!it.ImageInfo.empty())
  712. {
  713. room.strImage = it.ImageInfo;
  714. }
  715. room.CameraID = it.DeviceID;
  716. }
  717. }
  718. }
  719. /* 判断人数是否一致 */
  720. if(room.numMaxFace != room.numMaxPerson)
  721. {
  722. room.strMessage = fmt::format("非法入侵(人员计数 {} 和人脸数 {} 不一致)", room.numMaxPerson, room.numMaxFace);
  723. room.isAlarm = true;
  724. }
  725. }
  726. /* 将非法入侵的信息存储到数组缓存中,等待报警结束后判断是否需要写入EQM数据库 */
  727. for(auto& room : listRoomIll.getData())
  728. {
  729. auto pIll = pListIllInfo->findIllInfo(room.RoomID, room.RoomType);
  730. if(room.isAlarm)
  731. {
  732. /* 正在报警,检查是否有 */
  733. if(pIll == nullptr)
  734. {
  735. IllegalInvasionInfo info;
  736. info.ChannelID = pRFAInfo->ChannelID;
  737. info.CameraID = room.CameraID;
  738. info.RoomID = room.RoomID;
  739. info.RoomType = room.RoomType;
  740. info.strActionDec = room.strMessage;
  741. info.strImageInfo = room.strImage;
  742. info.FirstTime = QDateTime::currentDateTime();;
  743. pListIllInfo->addIllInfo(info);
  744. }
  745. }
  746. else
  747. {
  748. /* 没有报警,检查是否是报警结束了,是否符合写入数据库的条件 */
  749. QDateTime currTime = QDateTime::currentDateTime();
  750. int secs = currTime.toSecsSinceEpoch() - pIll->FirstTime.toSecsSinceEpoch();
  751. if(secs >= GConfig.AppBadMan)
  752. {
  753. if(!pIll->strImageInfo.empty())
  754. {
  755. /* 超过非法入侵的时间,写入数据库 */
  756. AlarmInfo ai;
  757. ai.ChannelID = pRFAInfo->ChannelID;
  758. ai.RoomID = pIll->RoomID;
  759. ai.ActionID = g_actionList.ActPersonNumber;
  760. ai.ActionDes = pIll->strActionDec;
  761. ai.ImageInfo = pIll->strImageInfo;
  762. ai.StartTime = pIll->FirstTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  763. ai.EndTime = currTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  764. ai.EventTime = pIll->FirstTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  765. ai.vecPersonInfo = room.vecPersonInfo;
  766. ai.listBbox = room.strBoxList;
  767. bool insertRet = toEQMDataBase->insertAlarmInfo(ai);
  768. if(insertRet)
  769. {
  770. std::string actionName = g_actionList.getActionName(ai.ActionID);
  771. SPDLOG_LOGGER_INFO(m_logger, "☆ 新增报警信息,频道[{}],房间[{}],摄像头[{}],{},{},有{}个区域,有{}个人脸,{}",
  772. pIll->ChannelID, pIll->RoomID, pIll->CameraID, actionName, ai.ActionDes, ai.listBbox.size(), ai.FaceIDList.size(), ai.ImageInfo);
  773. }
  774. }
  775. /* 删除这个非法入侵信息 */
  776. pListIllInfo->deleteIllInfo(*pIll);
  777. }
  778. }
  779. }
  780. std::this_thread::sleep_for(std::chrono::milliseconds(GConfig.ThreadSleepMS));
  781. }
  782. GThreadInfo.setThreadState(pRFAInfo.get(), RunTimeState::RUN_STATE_STOP);
  783. SPDLOG_LOGGER_INFO(m_logger, "关闭 {} 线程,ChannelID:{}", pRFAInfo->strFunctionName, pRFAInfo->ChannelID);
  784. }
  785. /**
  786. * @brief 区域人员检测,检测这个区域内的人数,不能少于多少人,不能多余多少人
  787. * 1、报警判断条件:直播间报警:当前频率所有直播间摄像头的区域人员检测算法输出结果均大于或小于设定人
  788. 数值时,记为报警行为,直接展示报警结果
  789. 2、录播间报警:当前频率所有录播间摄像头的区域人员检测算法输出结果均大于或小于设定人数值时,记为报
  790. 警行为,直接展示报警结果
  791. 3、直播间+录播间报警:当前频率直播间人数+录播间人数大于或小于设定人数值时,记为报警行为,直接展示
  792. 报警结果
  793. *
  794. * @param RAInfo 传入的房间ID和算法ID
  795. */
  796. void SPAServer::threadActRegionalPersonnelDetection(FuncActionInfo* RFAInfo)
  797. {
  798. SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程,ChannelID:{} ,", RFAInfo->strFunctionName, RFAInfo->ChannelID);
  799. /* 创建读取Redis的实例 */
  800. std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
  801. /* 创建写入数据库实例 */
  802. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  803. /* 局部变量,线程功能需要的信息 */
  804. std::shared_ptr<FuncActionInfo> pRFAInfo = std::make_shared<FuncActionInfo>();
  805. *pRFAInfo = *RFAInfo;
  806. /* 保存每个摄像机的报警信息 */
  807. std::shared_ptr<std::list<AlarmInfo>> pListAlarmInfo = std::make_shared<std::list<AlarmInfo>>();
  808. /* 报警时间段 */
  809. std::shared_ptr<std::vector<PersonCountRuleInfo>> pPersonCountRuleInfo = std::make_shared<std::vector<PersonCountRuleInfo>>();
  810. /* 获取报警规则信息 */
  811. toEQMDataBase->getPersonCountRuleInfo(*pPersonCountRuleInfo);
  812. while (m_threadRunning)
  813. {
  814. std::this_thread::sleep_for(std::chrono::milliseconds(GConfig.ThreadSleepMS));
  815. /************ 更新线程信息 ************/
  816. GThreadInfo.updateFuncInfo(pRFAInfo.get());
  817. if(pRFAInfo->appFunction == AppFunction::APP_NONE)
  818. {
  819. break;
  820. }
  821. /************ 读取Redis数据 ************/
  822. pListAlarmInfo->clear();
  823. for(const auto& RoomInfo : pRFAInfo->listRoomCamActInfo)
  824. {
  825. for(const auto& it : RoomInfo.mapCameraAction)
  826. {
  827. std::string strKey = std::to_string(it.first) + ":" + it.second;
  828. std::string strRetValue;
  829. if(!fromRedis->getRedisString(strKey, strRetValue))
  830. {
  831. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  832. continue;
  833. }
  834. /* 解析数据 */
  835. AlarmInfo alarmInfo;
  836. parseRedisBaseData(strRetValue, alarmInfo);
  837. parseRedisBBoxesData(strRetValue, alarmInfo);
  838. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  839. if(isEventTimeVaild(alarmInfo.EventTime))
  840. {
  841. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  842. continue;
  843. }
  844. pListAlarmInfo->push_back(alarmInfo);
  845. }
  846. }
  847. /************ 获取报警规则(从数据库更新?) ************/
  848. if(!toEQMDataBase->getPersonCountRuleInfo(*pPersonCountRuleInfo))
  849. {
  850. SPDLOG_LOGGER_ERROR(m_logger, "《人员计数》获取报警规则失败");
  851. continue;
  852. }
  853. if(pPersonCountRuleInfo->size() == 0)
  854. {
  855. SPDLOG_LOGGER_ERROR(m_logger, "《人员计数》无报警规则");
  856. break;
  857. }
  858. /* 获取这个频率的报警信息 */
  859. PersonCountRuleInfo personCountRuleInfo;
  860. for(auto& it : *pPersonCountRuleInfo)
  861. {
  862. if(it.ChannelID == pRFAInfo->ChannelID)
  863. {
  864. personCountRuleInfo = it;
  865. break;
  866. }
  867. }
  868. if(personCountRuleInfo.ChannelID < 0)
  869. {
  870. SPDLOG_LOGGER_ERROR(m_logger, "《人员计数》获取报警规则失败, ChannelID:{}", pRFAInfo->ChannelID);
  871. break;
  872. }
  873. /************ 判断是否在检测时间内 ************/
  874. QDateTime now = QDateTime::currentDateTime();
  875. /* 判断检测时间类型 */
  876. if(personCountRuleInfo.RuleType == 0)
  877. {
  878. /* 所有时段 */
  879. }
  880. else if(personCountRuleInfo.RuleType == 1)
  881. {
  882. /* 按天检测 */
  883. if(personCountRuleInfo.StartTime <= now && personCountRuleInfo.EndTime >= now)
  884. {
  885. /* 在检测时间内 */
  886. } else
  887. {
  888. /* 不在检测时间内 */
  889. continue;
  890. }
  891. }
  892. else if (personCountRuleInfo.RuleType == 2)
  893. {
  894. /* 按周检测 */
  895. if(personCountRuleInfo.week != now.date().dayOfWeek())
  896. {
  897. /* 不在检测时间内 */
  898. continue;
  899. }
  900. /* 在检测时间内 */
  901. if(personCountRuleInfo.StartTime.time() <= now.time() && personCountRuleInfo.EndTime.time() >= now.time())
  902. {
  903. /* 在检测时间内 */
  904. } else
  905. {
  906. /* 不在检测时间内 */
  907. continue;
  908. }
  909. }
  910. /************ 挨个房间检测人数 ************/
  911. /************ 检测频率直播间 + 导播间房间的人数 ************/
  912. }
  913. GThreadInfo.setThreadState(pRFAInfo.get(), RunTimeState::RUN_STATE_STOP);
  914. SPDLOG_LOGGER_INFO(m_logger, "关闭 {} 线程,ChannelID:{}", pRFAInfo->strFunctionName, pRFAInfo->ChannelID);
  915. }
  916. /* 将该算法对应的摄像机放入摄像机列表 */
  917. bool SPAServer::insertCameraToAction(RoomActionInfo* pRAInfo, std::list<RoomCameraInfo>& listRC, std::multimap<int, std::string>& mapCameraActionID)
  918. {
  919. for(const auto& rci : listRC)
  920. {
  921. if(rci.RoomID == pRAInfo->RoomID)
  922. {
  923. /* 这个摄像机在这个房间内,再判断这个摄像机有没有这个算法 */
  924. for(const auto& camID : rci.listCameraID)
  925. {
  926. for(const auto& cai : mapCameraActionID)
  927. {
  928. if(camID == cai.first)
  929. {
  930. /* 再判断这个摄像机的算法是否是当前需要的 */
  931. if(cai.second == pRAInfo->ActionID)
  932. {
  933. pRAInfo->listCameraID.push_back(camID);
  934. }
  935. }
  936. }
  937. }
  938. }
  939. }
  940. return true;
  941. }