SPAServer.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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 <QVector>
  8. SPAServer::SPAServer()
  9. {
  10. m_logger = spdlog::get("SPAServer");
  11. if(m_logger == nullptr)
  12. {
  13. SPDLOG_ERROR("APAServer logger is nullptr");
  14. return;
  15. }
  16. m_threadRunning = true;
  17. /* 初始化WebAPI */
  18. m_toEQMDataBase.initWebApi("http://192.1.3.133:31000/v6/", "", "4c2f9fc91c22dd98331e47af2e2964f4");
  19. /* 模拟违禁品算法ID,后续需要动态调整 */
  20. m_keyContraband = "OD210_026_005246_001-IZRTKyEx";
  21. }
  22. SPAServer::~SPAServer()
  23. {
  24. }
  25. /* 启动服务 */
  26. void SPAServer::startServer()
  27. {
  28. /* 添加获取基础信息的线程 */
  29. // CPPTP.add_task(&SPAServer::threadFromSuperBrain, this);
  30. /* 测试Redis读取并解析数据线程 */
  31. CameraThreadInfo info;
  32. info.RedisIP = "172.16.36.80";
  33. info.RedisPort = 32222;
  34. info.RedisPWD = "Ff1z@TOFr^iwd%Ra";
  35. info.DeviceID = 117;
  36. info.vecAction.push_back("OD210_026_005246_001-IZRTKyEx");
  37. m_keyContraband = "OD210_026_005246_001-IZRTKyEx";
  38. // CPPTP.add_task(&SPAServer::threadFromRedis, this, info);
  39. threadFromRedis(info);
  40. }
  41. /**
  42. * @brief 从基础平台获取算法信息和设备信息的线程函数
  43. *
  44. */
  45. void SPAServer::threadFromSuperBrain()
  46. {
  47. SPDLOG_LOGGER_INFO(m_logger, "开启 fromSuperBrainThread 线程");
  48. /* 创建变量 */
  49. std::vector<AlgorithmInfo> vecAlgNewInfo;
  50. std::vector<DeviceInfo> vecDevNewInfo;
  51. /* 获取一次token,后续失效了再获取 */
  52. m_fromSuperBrain.getToken();
  53. while (m_threadRunning)
  54. {
  55. SPDLOG_LOGGER_INFO(m_logger, "刷新算法和设备信息");
  56. /* 先更新数据库的信息,防止从其他地方更改了数据库,这里没有刷新本地缓存 */
  57. m_toEQMDataBase.getAlgorithmInfo(m_vecEqmAlgInfo);
  58. m_toEQMDataBase.getDeviceInfo(m_vecEqmDevInfo);
  59. m_toEQMDataBase.getDeviceAlgorithmInfo(m_vecEqmDevInfo, m_listDevIDDelete);
  60. /* 从超脑获取基础信息 */
  61. m_fromSuperBrain.getTaskTypeList(vecAlgNewInfo);
  62. m_fromSuperBrain.getDeviceList(vecDevNewInfo);
  63. /* 处理算法信息 */
  64. bool algIsUpdate = processAlgorithmInfo(vecAlgNewInfo);
  65. /* 处理设备信息 */
  66. bool devIsUpdate = processDeviceInfo(vecDevNewInfo);
  67. vecAlgNewInfo.clear();
  68. vecDevNewInfo.clear();
  69. /* 更新关联关系 */
  70. m_mutexCameraActionID.lock();
  71. m_toEQMDataBase.getDeviceAlgorithmInfo(m_mapCameraActionID);
  72. m_mutexCameraActionID.unlock();
  73. std::this_thread::sleep_for(std::chrono::seconds(10));
  74. }
  75. }
  76. /* 处理算法信息,返回值为true,说明有改变,需要重新读取 */
  77. bool SPAServer::processAlgorithmInfo(std::vector<AlgorithmInfo> vecNewAlgInfo)
  78. {
  79. std::vector<AlgorithmInfo> vecAlgUpdate;
  80. std::vector<AlgorithmInfo> vecAlgDelete;
  81. /* 对比数据库表格信息,这里只有插入和删除,没有更新 */
  82. compareAlgorithmInfo(vecNewAlgInfo, vecAlgUpdate, vecAlgDelete);
  83. /* 更新数据库,先删除,再写入刷新 */
  84. bool isUpdate = false;
  85. if(vecAlgDelete.size() > 0)
  86. {
  87. SPDLOG_LOGGER_DEBUG(m_logger, "删除算法信息");
  88. m_toEQMDataBase.deleteAlgorithmInfo(vecAlgDelete);
  89. isUpdate = true;
  90. }
  91. if(vecAlgUpdate.size() > 0)
  92. {
  93. SPDLOG_LOGGER_DEBUG(m_logger, "写入算法信息");
  94. m_toEQMDataBase.writeAlgorithmInfo(vecAlgUpdate);
  95. isUpdate = true;
  96. }
  97. return isUpdate;
  98. }
  99. /**
  100. * @brief 处理设备信息
  101. *
  102. * @param vecNewDevInfo 传入新获取到的值
  103. * @return true 需要重新读取数据库,获取新的数据
  104. * @return false 无需读取数据库
  105. */
  106. bool SPAServer::processDeviceInfo(std::vector<DeviceInfo> vecNewDevInfo)
  107. {
  108. std::vector<DeviceInfo> vecDevInsert;
  109. std::vector<DeviceInfo> vecDevUpdate;
  110. std::vector<DeviceInfo> vecDevDelete;
  111. /*-------------------------------------------------------------------------
  112. ****** 这里只对比设备信息,不对比设备的算法信息,算法信息在下面单独对比 *******
  113. *------------------------------------------------------------------------*/
  114. /* 如果本地缓存没有数据,那么就全部插入 */
  115. if(m_vecEqmDevInfo.size() > 0)
  116. {
  117. for(const auto& it : vecNewDevInfo)
  118. {
  119. bool isExist = false;
  120. for(const auto& it0 : m_vecEqmDevInfo)
  121. {
  122. if(it.DeviceID == it0.DeviceID)
  123. {
  124. isExist = true;
  125. /* 对比其他项是否相等,不相等就更新 */
  126. if(it == it0)
  127. {
  128. continue;
  129. }else {
  130. vecDevUpdate.push_back(it);
  131. }
  132. break;
  133. }
  134. }
  135. if(!isExist)
  136. {
  137. vecDevInsert.push_back(it);
  138. }
  139. }
  140. }else {
  141. vecDevInsert = vecNewDevInfo;
  142. }
  143. /* 获取删除列表 */
  144. if(vecNewDevInfo.size() > 0)
  145. {
  146. bool isExist = false;
  147. for(const auto& it : m_vecEqmDevInfo)
  148. {
  149. isExist = false;
  150. for(const auto& it0 : vecNewDevInfo)
  151. {
  152. if(it.DeviceID == it0.DeviceID)
  153. {
  154. isExist = true;
  155. break;
  156. }
  157. }
  158. if(!isExist)
  159. {
  160. vecDevDelete.push_back(it);
  161. }
  162. }
  163. }else {
  164. vecDevDelete = m_vecEqmDevInfo;
  165. }
  166. bool isUpdate = false;
  167. /* 先删除多余的数据 */
  168. if(vecDevDelete.size() > 0)
  169. {
  170. SPDLOG_LOGGER_DEBUG(m_logger, "删除设备信息");
  171. m_toEQMDataBase.deleteDeviceInfo(vecDevDelete);
  172. isUpdate = true;
  173. }
  174. /* 更新数据 */
  175. if(vecDevUpdate.size() > 0)
  176. {
  177. SPDLOG_LOGGER_DEBUG(m_logger, "更新设备信息");
  178. m_toEQMDataBase.updateDeviceInfo(vecDevUpdate);
  179. isUpdate = true;
  180. }
  181. /* 插入数据 */
  182. if(vecDevInsert.size() > 0)
  183. {
  184. SPDLOG_LOGGER_DEBUG(m_logger, "插入设备信息");
  185. m_toEQMDataBase.insertDeviceInfo(vecDevInsert);
  186. isUpdate = true;
  187. }
  188. /*-------------------------------------------------------------------------
  189. ************* 处理设备和算子关联的表格,单独对比设备的算法信息 *************
  190. *------------------------------------------------------------------------*/
  191. /* 插入新的设备信息 */
  192. if(vecDevInsert.size() > 0)
  193. {
  194. SPDLOG_LOGGER_DEBUG(m_logger, "插入设备和算法关联表");
  195. m_toEQMDataBase.insertDeviceAlgorithmInfo(vecDevInsert);
  196. isUpdate = true;
  197. }
  198. vecDevUpdate.clear();
  199. /* 对比现有的设备是否需要更新算法 */
  200. compareDeviceAlgorithmInfo(vecNewDevInfo, vecDevUpdate);
  201. if(vecDevUpdate.size() > 0)
  202. {
  203. SPDLOG_LOGGER_DEBUG(m_logger, "更新设备和算法关联表, 更新设备数目:{}", vecDevUpdate.size());
  204. m_toEQMDataBase.updateDeviceAlgorithmInfo(vecDevUpdate);
  205. }
  206. /* 删除tActionCamer表中消失的设备信息 */
  207. if(m_listDevIDDelete.size() > 0)
  208. {
  209. SPDLOG_LOGGER_DEBUG(m_logger, "删除消失的设备关联的算法");
  210. m_toEQMDataBase.deleteDeviceAlgorithmInfo(m_listDevIDDelete);
  211. isUpdate = true;
  212. }
  213. return isUpdate;
  214. }
  215. /* 对比现有的数据和新获取到的数据,取出要删除和添加的数据 */
  216. void SPAServer::compareAlgorithmInfo(const std::vector<AlgorithmInfo>& vecNewInfo, std::vector<AlgorithmInfo>& vecAlgUpdate, std::vector<AlgorithmInfo>& vecAlgDelete)
  217. {
  218. /* 取出要添加的,如果本地缓存是0,那么全部都要添加 */
  219. if(m_vecEqmAlgInfo.size() > 0)
  220. {
  221. for(const auto& it : vecNewInfo)
  222. {
  223. bool isExist = false;
  224. for(const auto& it0 : m_vecEqmAlgInfo)
  225. {
  226. /* 如果存在就退出循环 */
  227. if(it.ActionID == it0.ActionID)
  228. {
  229. isExist = true;
  230. break;
  231. }
  232. }
  233. if(!isExist)
  234. {
  235. vecAlgUpdate.push_back(it);
  236. }
  237. }
  238. }else {
  239. vecAlgUpdate = vecNewInfo;
  240. }
  241. /* 取出要删除的,如果新的数据是0,那么全部都要删除 */
  242. if(vecNewInfo.size() > 0)
  243. {
  244. bool isExist = false;
  245. for(const auto& it : m_vecEqmAlgInfo)
  246. {
  247. isExist = false;
  248. for(const auto& it0 : vecNewInfo)
  249. {
  250. if(it.ActionID == it0.ActionID)
  251. {
  252. isExist = true;
  253. break;
  254. }
  255. }
  256. if(!isExist)
  257. {
  258. vecAlgDelete.push_back(it);
  259. }
  260. }
  261. }else {
  262. vecAlgDelete = m_vecEqmAlgInfo;
  263. }
  264. }
  265. /**
  266. * @brief 对比设备和算法关联表是否需要更新
  267. * 对比规则:
  268. * 1、这里只对比已有的设备ID,需要删除的ID在获取到tActionCamer表是就已经取出来了
  269. * 2、如果设备ID相等,那么进一步对比算法信息是否相等
  270. * 3、如果设备ID相等,但是算法信息数目不相等,那么直接加入更新列表
  271. * 4、如果设备ID相等,算法信息数目相等,进一步对比算法信息
  272. *
  273. * @param vecNewInfo
  274. * @param vecDevUpdate
  275. */
  276. void SPAServer::compareDeviceAlgorithmInfo(const std::vector<DeviceInfo>& vecNewInfo, std::vector<DeviceInfo>& vecDevUpdate)
  277. {
  278. vecDevUpdate.clear();
  279. for(const auto& it0 : vecNewInfo)
  280. {
  281. for(const auto& it1 : m_vecEqmDevInfo)
  282. {
  283. if(it0.DeviceID == it1.DeviceID)
  284. {
  285. /* 设备的算法信息数目不相等,直接加入更新列表 */
  286. if(it0.vecAlgorithmInfo.size() != it1.vecAlgorithmInfo.size())
  287. {
  288. vecDevUpdate.push_back(it0);
  289. break;
  290. }
  291. /* 设备的算法信息数目相等,进一步对比算法信息 */
  292. bool isEquality = true;
  293. for(const auto& it2 : it0.vecAlgorithmInfo)
  294. {
  295. bool isEq2 = false;
  296. for(const auto& it3 : it1.vecAlgorithmInfo)
  297. {
  298. /* 这里只对比算法ID */
  299. if(it2.ActionID != it3.ActionID)
  300. {
  301. continue;
  302. }else {
  303. isEq2 = true;
  304. break;
  305. }
  306. }
  307. if(!isEq2)
  308. {
  309. isEquality = false;
  310. break;
  311. }
  312. }
  313. if(!isEquality)
  314. {
  315. vecDevUpdate.push_back(it0);
  316. break;
  317. }
  318. }
  319. }
  320. }
  321. }
  322. /**
  323. * @brief 从Redis获取数据线程函数,这个是摄像机线程
  324. * 一个设备一个线程,这个线程的相关变量只在这个线程中使用
  325. *
  326. * @param info
  327. */
  328. void SPAServer::threadFromRedis(const CameraThreadInfo& info)
  329. {
  330. SPDLOG_LOGGER_INFO(m_logger, "开启 fromRedisThread 线程,设备ID:{}", info.DeviceID);
  331. FromRedis fromRedis;
  332. fromRedis.setRedisIPAndPort(info.RedisIP, info.RedisPort);
  333. fromRedis.setRedisPassword(info.RedisPWD);
  334. if(fromRedis.connectRedis())
  335. {
  336. SPDLOG_LOGGER_INFO(m_logger, "连接Redis成功");
  337. }else {
  338. SPDLOG_LOGGER_ERROR(m_logger, "连接Redis失败");
  339. return;
  340. }
  341. CameraThreadInfo threadInfo = info;
  342. // std::string strKey = "117:OD210_026_005246_001-IZRTKyEx";
  343. /* 取出该设备的Key */
  344. std::vector<std::string> vecKey;
  345. for(const auto& it : info.vecAction)
  346. {
  347. std::string strKey = std::to_string(info.DeviceID) + ":" + it;
  348. vecKey.push_back(strKey);
  349. }
  350. std::string strRetValue;
  351. /* 进入线程循环 */
  352. while (m_threadRunning)
  353. {
  354. /* 循环读取这个设备关联的算法信息 */
  355. for(const auto& it : vecKey)
  356. {
  357. SPDLOG_LOGGER_INFO(m_logger, "读取Redis信息, Key: {}", it);
  358. fromRedis.getRedisString(it, strRetValue);
  359. SPDLOG_LOGGER_TRACE(m_logger, "Redis Value:\n{}", strRetValue);
  360. /* 解析数据 */
  361. AlarmInfo alarmInfo;
  362. alarmInfo.ActionID = it.substr(it.find(":") + 1);
  363. /* 解析数据 */
  364. parseRedisData(strRetValue, alarmInfo);
  365. /* 信息时间有的效性判断,主要是记录Redis数据的有效性,是否长时间没有变化,排查错误时用的
  366. * 如果数据长时间数据不变,那么超脑那里就挂了,写入日志 */
  367. isEventTimeVaild(alarmInfo.EventTime);
  368. /* 是否需要写入EQM数据库判断
  369. * 人员计数和区域人员检测需要多次判断,其他的直接写入即可 */
  370. }
  371. std::this_thread::sleep_for(std::chrono::seconds(2));
  372. }
  373. return;
  374. }
  375. /* 解析Redis基础数据 */
  376. void SPAServer::parseRedisData(const std::string& strData, AlarmInfo& alarmInfo)
  377. {
  378. try
  379. {
  380. nJson json0;
  381. json0 = nJson::parse(strData);
  382. alarmInfo.AlarmID = json0["alarmId"].get<int>();
  383. alarmInfo.ChannelID = json0["channel"].get<int>();
  384. alarmInfo.PicUrl = json0["picUrl"].get<std::string>();
  385. alarmInfo.ImageInfo = json0["imageInfo"].get<std::string>();
  386. /* 解析时间,需要将时间中的“T”换成空格 */
  387. alarmInfo.StartTime = json0["beginTime"].get<std::string>();
  388. std::replace(alarmInfo.StartTime.begin(), alarmInfo.StartTime.end(), 'T', ' ');
  389. alarmInfo.EndTime = json0["endTime"].get<std::string>();
  390. std::replace(alarmInfo.EndTime.begin(), alarmInfo.EndTime.end(), 'T', ' ');
  391. alarmInfo.EventTime = json0["eventTime"].get<std::string>();
  392. std::replace(alarmInfo.EventTime.begin(), alarmInfo.EventTime.end(), 'T', ' ');
  393. /* 判断bBoxes有无数据,有数据就解析,没数据就直接返回了 */
  394. nJson json1 = json0["bBoxes"];
  395. std::string labelList; /* 记录违禁品 */
  396. std::string bboxList; /* 记录bbox */
  397. if(!json1.empty())
  398. {
  399. for(auto& it0 : json1)
  400. {
  401. /* 如果status是true,就不是报警,直接跳过 */
  402. bool status = it0["status"].get<bool>();
  403. if(status)
  404. {
  405. continue;
  406. }
  407. /* 这一条Box数据报警了将其内容存储起来 */
  408. alarmInfo.Is_Alarm = true;
  409. /* 解析label,违禁品关键字,先判断这个是不是违禁品检测的算法ID */
  410. if(alarmInfo.ActionID == m_keyContraband)
  411. {
  412. /* 解析报警,取出报警类型 */
  413. nJson label = it0["label"];
  414. for(auto& it1 : label)
  415. {
  416. std::string strLabel = it1.get<std::string>();
  417. /* 检测是否已经加入到字符串中了 */
  418. strLabel += "|";
  419. if(labelList.find(strLabel) != std::string::npos)
  420. {
  421. continue;
  422. }
  423. labelList += strLabel;
  424. }
  425. }
  426. /* 解析bbox,貌似是在图像中的位置 */
  427. nJson bbox = it0["bbox"];
  428. std::string strBbox;
  429. for(auto& it1 : bbox)
  430. {
  431. strBbox += std::to_string(it1.get<int>()) + ",";
  432. }
  433. /* 去掉最后一个“,” */
  434. if(!strBbox.empty())
  435. {
  436. strBbox.pop_back();
  437. }
  438. bboxList += strBbox + "|";
  439. }
  440. /* 去掉最后一个“|” */
  441. if(!labelList.empty())
  442. {
  443. labelList.pop_back();
  444. }
  445. if(!bboxList.empty())
  446. {
  447. bboxList.pop_back();
  448. }
  449. SPDLOG_LOGGER_DEBUG(m_logger, "违禁品列表:{}", labelList);
  450. SPDLOG_LOGGER_DEBUG(m_logger, "bbox列表:{}", bboxList);
  451. }
  452. /* 如果有报警的Box */
  453. if(alarmInfo.Is_Alarm)
  454. {
  455. /* 添加报警信息的提示信息 */
  456. alarmInfo.BboxList = bboxList;
  457. if( (alarmInfo.ActionID == m_keyContraband) && !labelList.empty() )
  458. {
  459. alarmInfo.ActionDes = fmt::format("出现违禁品[{}]告警", labelList);
  460. SPDLOG_LOGGER_INFO(m_logger, "{}", alarmInfo.ActionDes);
  461. }else {
  462. alarmInfo.ActionDes = json0["actionDes"].get<std::string>();
  463. }
  464. /* 判断有没有报警数据 */
  465. if(alarmInfo.ImageInfo.empty())
  466. {
  467. SPDLOG_LOGGER_ERROR(m_logger, "有报警区域,但是没有图片信息");
  468. return;
  469. }
  470. /* 如果是人员报警,就存储人员报警信息 */
  471. if(alarmInfo.ActionID == m_KeyFace)
  472. {
  473. nJson jsonArray = json0["personList"];
  474. for(auto& it : jsonArray)
  475. {
  476. PersonInfo personInfo;
  477. personInfo.PersonID = it["personId"].get<std::string>();
  478. personInfo.PersonName = it["personName"].get<std::string>();
  479. alarmInfo.vecPersonInfo.push_back(personInfo);
  480. }
  481. }
  482. }
  483. }
  484. catch (const nJson::parse_error& e)
  485. {
  486. SPDLOG_LOGGER_ERROR(m_logger, "解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  487. return;
  488. }
  489. catch (const nJson::type_error& e)
  490. {
  491. SPDLOG_LOGGER_ERROR(m_logger, "解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  492. return;
  493. }
  494. }
  495. /* 判断时间是否长时间没有更新 */
  496. bool SPAServer::isEventTimeVaild(const std::string& strTime)
  497. {
  498. /* 获取当前时间 */
  499. time_t now = time(0);
  500. /* 字符串转成时间 */
  501. tm tmTime;
  502. strptime(strTime.c_str(), "%Y-%m-%d %H:%M:%S", &tmTime);
  503. time_t eventTime = mktime(&tmTime);
  504. /* 时间差 */
  505. double diff = difftime(now, eventTime);
  506. SPDLOG_LOGGER_DEBUG(m_logger, "now:{} eventTime: {} 时间差:{}秒",now, eventTime, diff);
  507. return true;
  508. }
  509. /**
  510. * @brief 刷新房间和摄像机关联的线程
  511. * 线程定时刷新房间和摄像机的管理关系,以及和算法Action的关联关系
  512. */
  513. void SPAServer::threadRoomCamera()
  514. {
  515. /* 房间相机关联信息 */
  516. std::list<RoomCameraInfo> listRC;
  517. /* 创建连接数据库实例 */
  518. std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
  519. while (m_threadRunning)
  520. {
  521. /* 先获取EQM数据库信息,取出房间和摄像机关联信息 */
  522. toEQMDataBase->getRoomCameraInfo(listRC);
  523. /* 取出每个房间的所有算法,int是RoomID,string是Action */
  524. std::multimap<int, std::string> mapCameraActionID;
  525. m_mutexCameraActionID.lock();
  526. for(const auto& it0 : listRC)
  527. {
  528. /* 根据房间内的摄像机的ID寻找对应的算法 */
  529. for(const auto& it1 : it0.listCameraID)
  530. {
  531. for(const auto& it2 : m_mapCameraActionID)
  532. {
  533. if(it1 == it2.first)
  534. {
  535. }
  536. }
  537. }
  538. }
  539. m_mutexCameraActionID.unlock();
  540. /* 根据每个房间的算法功能,匹配其对应的摄像机 */
  541. m_mutexRoomCameraInfo.lock();
  542. /* 循环查找每个房间的算法ID与之关联的设备 */
  543. for(const auto& it0 : mapCameraActionID)
  544. {
  545. /* 人员在岗识别 */
  546. if(it0.second == ActPersonWork)
  547. {
  548. SPDLOG_LOGGER_INFO(m_logger, "RoomID:{} 人员在岗识别", it0.first);
  549. /* 先查找这个算法ID是否已在列表中 */
  550. if(findActionIDInList(it0.first, it0.second) == nullptr)
  551. {
  552. /* 不在列表中就创建一个 */
  553. RoomActionInfo* roomActionInfo = new RoomActionInfo;
  554. roomActionInfo->RoomID = it0.first;
  555. roomActionInfo->ActionID = it0.second;
  556. /* 将这个算法相关联的在这个房间内的相机加入其中 */
  557. for(const auto& it1 : listRC)
  558. {
  559. if(it1.RoomID == it0.first)
  560. {
  561. /* 判断这些相机哪些有这个算法 */
  562. for(const auto& it2 : it1.listCameraID)
  563. {
  564. if(isCameraHasAction(it2, it0.second))
  565. {
  566. roomActionInfo->listCameraID.push_back(it2);
  567. }
  568. }
  569. }
  570. }
  571. m_listRoomCameraInfo.push_back(roomActionInfo);
  572. } else
  573. {
  574. /* 算法在列表中,清空算法的摄像机列表,重新加入列表中 */
  575. RoomActionInfo* roomActionInfo = findActionIDInList(it0.first, it0.second);
  576. roomActionInfo->listCameraID.clear();
  577. /* 将这个算法相关联的在这个房间内的相机加入其中 */
  578. for(const auto& it1 : listRC)
  579. {
  580. if(it1.RoomID == it0.first)
  581. {
  582. /* 判断这些相机哪些有这个算法 */
  583. for(const auto& it2 : it1.listCameraID)
  584. {
  585. if(isCameraHasAction(it2, it0.second))
  586. {
  587. roomActionInfo->listCameraID.push_back(it2);
  588. }
  589. }
  590. }
  591. }
  592. }
  593. }
  594. /* 违禁物品识别 */
  595. }
  596. }
  597. }
  598. /* 查找算法ID是否已在列表中 */
  599. RoomActionInfo* SPAServer::findActionIDInList(const int RoomID, const std::string& strActionID)
  600. {
  601. for(const auto& it0 : m_listRoomCameraInfo)
  602. {
  603. if(it0->RoomID == RoomID)
  604. {
  605. if(it0->ActionID == strActionID)
  606. {
  607. return it0;
  608. }
  609. }
  610. }
  611. return nullptr;
  612. }
  613. /* 判断相机是否有这个算法 */
  614. bool SPAServer::isCameraHasAction(const int CameraID, const std::string& strActionID)
  615. {
  616. for(const auto& it : m_mapCameraActionID)
  617. {
  618. if(it.first == CameraID)
  619. {
  620. if(it.second == strActionID)
  621. {
  622. return true;
  623. }
  624. }
  625. }
  626. return false;
  627. }