SPAServer.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. std::this_thread::sleep_for(std::chrono::seconds(10));
  70. }
  71. }
  72. /* 处理算法信息,返回值为true,说明有改变,需要重新读取 */
  73. bool SPAServer::processAlgorithmInfo(std::vector<AlgorithmInfo> vecNewAlgInfo)
  74. {
  75. std::vector<AlgorithmInfo> vecAlgUpdate;
  76. std::vector<AlgorithmInfo> vecAlgDelete;
  77. /* 对比数据库表格信息,这里只有插入和删除,没有更新 */
  78. compareAlgorithmInfo(vecNewAlgInfo, vecAlgUpdate, vecAlgDelete);
  79. /* 更新数据库,先删除,再写入刷新 */
  80. bool isUpdate = false;
  81. if(vecAlgDelete.size() > 0)
  82. {
  83. SPDLOG_LOGGER_DEBUG(m_logger, "删除算法信息");
  84. m_toEQMDataBase.deleteAlgorithmInfo(vecAlgDelete);
  85. isUpdate = true;
  86. }
  87. if(vecAlgUpdate.size() > 0)
  88. {
  89. SPDLOG_LOGGER_DEBUG(m_logger, "写入算法信息");
  90. m_toEQMDataBase.writeAlgorithmInfo(vecAlgUpdate);
  91. isUpdate = true;
  92. }
  93. return isUpdate;
  94. }
  95. /**
  96. * @brief 处理设备信息
  97. *
  98. * @param vecNewDevInfo 传入新获取到的值
  99. * @return true 需要重新读取数据库,获取新的数据
  100. * @return false 无需读取数据库
  101. */
  102. bool SPAServer::processDeviceInfo(std::vector<DeviceInfo> vecNewDevInfo)
  103. {
  104. std::vector<DeviceInfo> vecDevInsert;
  105. std::vector<DeviceInfo> vecDevUpdate;
  106. std::vector<DeviceInfo> vecDevDelete;
  107. /*-------------------------------------------------------------------------
  108. ****** 这里只对比设备信息,不对比设备的算法信息,算法信息在下面单独对比 *******
  109. *------------------------------------------------------------------------*/
  110. /* 如果本地缓存没有数据,那么就全部插入 */
  111. if(m_vecEqmDevInfo.size() > 0)
  112. {
  113. for(const auto& it : vecNewDevInfo)
  114. {
  115. bool isExist = false;
  116. for(const auto& it0 : m_vecEqmDevInfo)
  117. {
  118. if(it.DeviceID == it0.DeviceID)
  119. {
  120. isExist = true;
  121. /* 对比其他项是否相等,不相等就更新 */
  122. if(it == it0)
  123. {
  124. continue;
  125. }else {
  126. vecDevUpdate.push_back(it);
  127. }
  128. break;
  129. }
  130. }
  131. if(!isExist)
  132. {
  133. vecDevInsert.push_back(it);
  134. }
  135. }
  136. }else {
  137. vecDevInsert = vecNewDevInfo;
  138. }
  139. /* 获取删除列表 */
  140. if(vecNewDevInfo.size() > 0)
  141. {
  142. bool isExist = false;
  143. for(const auto& it : m_vecEqmDevInfo)
  144. {
  145. isExist = false;
  146. for(const auto& it0 : vecNewDevInfo)
  147. {
  148. if(it.DeviceID == it0.DeviceID)
  149. {
  150. isExist = true;
  151. break;
  152. }
  153. }
  154. if(!isExist)
  155. {
  156. vecDevDelete.push_back(it);
  157. }
  158. }
  159. }else {
  160. vecDevDelete = m_vecEqmDevInfo;
  161. }
  162. bool isUpdate = false;
  163. /* 先删除多余的数据 */
  164. if(vecDevDelete.size() > 0)
  165. {
  166. SPDLOG_LOGGER_DEBUG(m_logger, "删除设备信息");
  167. m_toEQMDataBase.deleteDeviceInfo(vecDevDelete);
  168. isUpdate = true;
  169. }
  170. /* 更新数据 */
  171. if(vecDevUpdate.size() > 0)
  172. {
  173. SPDLOG_LOGGER_DEBUG(m_logger, "更新设备信息");
  174. m_toEQMDataBase.updateDeviceInfo(vecDevUpdate);
  175. isUpdate = true;
  176. }
  177. /* 插入数据 */
  178. if(vecDevInsert.size() > 0)
  179. {
  180. SPDLOG_LOGGER_DEBUG(m_logger, "插入设备信息");
  181. m_toEQMDataBase.insertDeviceInfo(vecDevInsert);
  182. isUpdate = true;
  183. }
  184. /*-------------------------------------------------------------------------
  185. ************* 处理设备和算子关联的表格,单独对比设备的算法信息 *************
  186. *------------------------------------------------------------------------*/
  187. /* 插入新的设备信息 */
  188. if(vecDevInsert.size() > 0)
  189. {
  190. SPDLOG_LOGGER_DEBUG(m_logger, "插入设备和算法关联表");
  191. m_toEQMDataBase.insertDeviceAlgorithmInfo(vecDevInsert);
  192. isUpdate = true;
  193. }
  194. vecDevUpdate.clear();
  195. /* 对比现有的设备是否需要更新算法 */
  196. compareDeviceAlgorithmInfo(vecNewDevInfo, vecDevUpdate);
  197. if(vecDevUpdate.size() > 0)
  198. {
  199. SPDLOG_LOGGER_DEBUG(m_logger, "更新设备和算法关联表, 更新设备数目:{}", vecDevUpdate.size());
  200. m_toEQMDataBase.updateDeviceAlgorithmInfo(vecDevUpdate);
  201. }
  202. /* 删除tActionCamer表中消失的设备信息 */
  203. if(m_listDevIDDelete.size() > 0)
  204. {
  205. SPDLOG_LOGGER_DEBUG(m_logger, "删除消失的设备关联的算法");
  206. m_toEQMDataBase.deleteDeviceAlgorithmInfo(m_listDevIDDelete);
  207. isUpdate = true;
  208. }
  209. return isUpdate;
  210. }
  211. /* 对比现有的数据和新获取到的数据,取出要删除和添加的数据 */
  212. void SPAServer::compareAlgorithmInfo(const std::vector<AlgorithmInfo>& vecNewInfo, std::vector<AlgorithmInfo>& vecAlgUpdate, std::vector<AlgorithmInfo>& vecAlgDelete)
  213. {
  214. /* 取出要添加的,如果本地缓存是0,那么全部都要添加 */
  215. if(m_vecEqmAlgInfo.size() > 0)
  216. {
  217. for(const auto& it : vecNewInfo)
  218. {
  219. bool isExist = false;
  220. for(const auto& it0 : m_vecEqmAlgInfo)
  221. {
  222. /* 如果存在就退出循环 */
  223. if(it.ActionID == it0.ActionID)
  224. {
  225. isExist = true;
  226. break;
  227. }
  228. }
  229. if(!isExist)
  230. {
  231. vecAlgUpdate.push_back(it);
  232. }
  233. }
  234. }else {
  235. vecAlgUpdate = vecNewInfo;
  236. }
  237. /* 取出要删除的,如果新的数据是0,那么全部都要删除 */
  238. if(vecNewInfo.size() > 0)
  239. {
  240. bool isExist = false;
  241. for(const auto& it : m_vecEqmAlgInfo)
  242. {
  243. isExist = false;
  244. for(const auto& it0 : vecNewInfo)
  245. {
  246. if(it.ActionID == it0.ActionID)
  247. {
  248. isExist = true;
  249. break;
  250. }
  251. }
  252. if(!isExist)
  253. {
  254. vecAlgDelete.push_back(it);
  255. }
  256. }
  257. }else {
  258. vecAlgDelete = m_vecEqmAlgInfo;
  259. }
  260. }
  261. /**
  262. * @brief 对比设备和算法关联表是否需要更新
  263. * 对比规则:
  264. * 1、这里只对比已有的设备ID,需要删除的ID在获取到tActionCamer表是就已经取出来了
  265. * 2、如果设备ID相等,那么进一步对比算法信息是否相等
  266. * 3、如果设备ID相等,但是算法信息数目不相等,那么直接加入更新列表
  267. * 4、如果设备ID相等,算法信息数目相等,进一步对比算法信息
  268. *
  269. * @param vecNewInfo
  270. * @param vecDevUpdate
  271. */
  272. void SPAServer::compareDeviceAlgorithmInfo(const std::vector<DeviceInfo>& vecNewInfo, std::vector<DeviceInfo>& vecDevUpdate)
  273. {
  274. vecDevUpdate.clear();
  275. for(const auto& it0 : vecNewInfo)
  276. {
  277. for(const auto& it1 : m_vecEqmDevInfo)
  278. {
  279. if(it0.DeviceID == it1.DeviceID)
  280. {
  281. /* 设备的算法信息数目不相等,直接加入更新列表 */
  282. if(it0.vecAlgorithmInfo.size() != it1.vecAlgorithmInfo.size())
  283. {
  284. vecDevUpdate.push_back(it0);
  285. break;
  286. }
  287. /* 设备的算法信息数目相等,进一步对比算法信息 */
  288. bool isEquality = true;
  289. for(const auto& it2 : it0.vecAlgorithmInfo)
  290. {
  291. bool isEq2 = false;
  292. for(const auto& it3 : it1.vecAlgorithmInfo)
  293. {
  294. /* 这里只对比算法ID */
  295. if(it2.ActionID != it3.ActionID)
  296. {
  297. continue;
  298. }else {
  299. isEq2 = true;
  300. break;
  301. }
  302. }
  303. if(!isEq2)
  304. {
  305. isEquality = false;
  306. break;
  307. }
  308. }
  309. if(!isEquality)
  310. {
  311. vecDevUpdate.push_back(it0);
  312. break;
  313. }
  314. }
  315. }
  316. }
  317. }
  318. /**
  319. * @brief 从Redis获取数据线程函数,这个是摄像机线程
  320. * 一个设备一个线程,这个线程的相关变量只在这个线程中使用
  321. *
  322. * @param info
  323. */
  324. void SPAServer::threadFromRedis(const CameraThreadInfo& info)
  325. {
  326. SPDLOG_LOGGER_INFO(m_logger, "开启 fromRedisThread 线程,设备ID:{}", info.DeviceID);
  327. FromRedis fromRedis;
  328. fromRedis.setRedisIPAndPort(info.RedisIP, info.RedisPort);
  329. fromRedis.setRedisPassword(info.RedisPWD);
  330. if(fromRedis.connectRedis())
  331. {
  332. SPDLOG_LOGGER_INFO(m_logger, "连接Redis成功");
  333. }else {
  334. SPDLOG_LOGGER_ERROR(m_logger, "连接Redis失败");
  335. return;
  336. }
  337. CameraThreadInfo threadInfo = info;
  338. // std::string strKey = "117:OD210_026_005246_001-IZRTKyEx";
  339. /* 取出该设备的Key */
  340. std::vector<std::string> vecKey;
  341. for(const auto& it : info.vecAction)
  342. {
  343. std::string strKey = std::to_string(info.DeviceID) + ":" + it;
  344. vecKey.push_back(strKey);
  345. }
  346. std::string strRetValue;
  347. /* 进入线程循环 */
  348. while (m_threadRunning)
  349. {
  350. /* 循环读取这个设备关联的算法信息 */
  351. for(const auto& it : vecKey)
  352. {
  353. SPDLOG_LOGGER_INFO(m_logger, "读取Redis信息, Key: {}", it);
  354. fromRedis.getRedisString(it, strRetValue);
  355. SPDLOG_LOGGER_TRACE(m_logger, "Redis Value:\n{}", strRetValue);
  356. /* 解析数据 */
  357. AlarmInfo alarmInfo;
  358. alarmInfo.ActionID = it.substr(it.find(":") + 1);
  359. /* 解析数据 */
  360. parseRedisData(strRetValue, alarmInfo);
  361. /* 信息时间有的效性判断,主要是记录Redis数据的有效性,是否长时间没有变化,排查错误时用的
  362. * 如果数据长时间数据不变,那么超脑那里就挂了,写入日志 */
  363. isEventTimeVaild(alarmInfo.EventTime);
  364. /* 是否需要写入EQM数据库判断
  365. * 人员计数和区域人员检测需要多次判断,其他的直接写入即可 */
  366. }
  367. std::this_thread::sleep_for(std::chrono::seconds(2));
  368. }
  369. return;
  370. }
  371. /* 解析Redis基础数据 */
  372. void SPAServer::parseRedisData(const std::string& strData, AlarmInfo& alarmInfo)
  373. {
  374. try
  375. {
  376. nJson json0;
  377. json0 = nJson::parse(strData);
  378. alarmInfo.AlarmID = json0["alarmId"].get<int>();
  379. alarmInfo.ChannelID = json0["channel"].get<int>();
  380. alarmInfo.PicUrl = json0["picUrl"].get<std::string>();
  381. alarmInfo.ImageInfo = json0["imageInfo"].get<std::string>();
  382. /* 解析时间,需要将时间中的“T”换成空格 */
  383. alarmInfo.StartTime = json0["beginTime"].get<std::string>();
  384. std::replace(alarmInfo.StartTime.begin(), alarmInfo.StartTime.end(), 'T', ' ');
  385. alarmInfo.EndTime = json0["endTime"].get<std::string>();
  386. std::replace(alarmInfo.EndTime.begin(), alarmInfo.EndTime.end(), 'T', ' ');
  387. alarmInfo.EventTime = json0["eventTime"].get<std::string>();
  388. std::replace(alarmInfo.EventTime.begin(), alarmInfo.EventTime.end(), 'T', ' ');
  389. /* 判断bBoxes有无数据,有数据就解析,没数据就直接返回了 */
  390. nJson json1 = json0["bBoxes"];
  391. std::string labelList; /* 记录违禁品 */
  392. std::string bboxList; /* 记录bbox */
  393. if(!json1.empty())
  394. {
  395. for(auto& it0 : json1)
  396. {
  397. /* 如果status是true,就不是报警,直接跳过 */
  398. bool status = it0["status"].get<bool>();
  399. if(status)
  400. {
  401. continue;
  402. }
  403. /* 这一条Box数据报警了将其内容存储起来 */
  404. alarmInfo.Is_Alarm = true;
  405. /* 解析label,违禁品关键字,先判断这个是不是违禁品检测的算法ID */
  406. if(alarmInfo.ActionID == m_keyContraband)
  407. {
  408. /* 解析报警,取出报警类型 */
  409. nJson label = it0["label"];
  410. for(auto& it1 : label)
  411. {
  412. std::string strLabel = it1.get<std::string>();
  413. /* 检测是否已经加入到字符串中了 */
  414. strLabel += "|";
  415. if(labelList.find(strLabel) != std::string::npos)
  416. {
  417. continue;
  418. }
  419. labelList += strLabel;
  420. }
  421. }
  422. /* 解析bbox,貌似是在图像中的位置 */
  423. nJson bbox = it0["bbox"];
  424. std::string strBbox;
  425. for(auto& it1 : bbox)
  426. {
  427. strBbox += std::to_string(it1.get<int>()) + ",";
  428. }
  429. /* 去掉最后一个“,” */
  430. if(!strBbox.empty())
  431. {
  432. strBbox.pop_back();
  433. }
  434. bboxList += strBbox + "|";
  435. }
  436. /* 去掉最后一个“|” */
  437. if(!labelList.empty())
  438. {
  439. labelList.pop_back();
  440. }
  441. if(!bboxList.empty())
  442. {
  443. bboxList.pop_back();
  444. }
  445. SPDLOG_LOGGER_DEBUG(m_logger, "违禁品列表:{}", labelList);
  446. SPDLOG_LOGGER_DEBUG(m_logger, "bbox列表:{}", bboxList);
  447. }
  448. /* 如果有报警的Box */
  449. if(alarmInfo.Is_Alarm)
  450. {
  451. /* 添加报警信息的提示信息 */
  452. alarmInfo.BboxList = bboxList;
  453. if( (alarmInfo.ActionID == m_keyContraband) && !labelList.empty() )
  454. {
  455. alarmInfo.ActionDes = fmt::format("出现违禁品[{}]告警", labelList);
  456. SPDLOG_LOGGER_INFO(m_logger, "{}", alarmInfo.ActionDes);
  457. }else {
  458. alarmInfo.ActionDes = json0["actionDes"].get<std::string>();
  459. }
  460. /* 判断有没有报警数据 */
  461. if(alarmInfo.ImageInfo.empty())
  462. {
  463. SPDLOG_LOGGER_ERROR(m_logger, "有报警区域,但是没有图片信息");
  464. return;
  465. }
  466. /* 如果是人员报警,就存储人员报警信息 */
  467. if(alarmInfo.ActionID == m_KeyFace)
  468. {
  469. nJson jsonArray = json0["personList"];
  470. for(auto& it : jsonArray)
  471. {
  472. PersonInfo personInfo;
  473. personInfo.PersonID = it["personId"].get<std::string>();
  474. personInfo.PersonName = it["personName"].get<std::string>();
  475. alarmInfo.vecPersonInfo.push_back(personInfo);
  476. }
  477. }
  478. }
  479. }
  480. catch (const nJson::parse_error& e)
  481. {
  482. SPDLOG_LOGGER_ERROR(m_logger, "解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  483. return;
  484. }
  485. catch (const nJson::type_error& e)
  486. {
  487. SPDLOG_LOGGER_ERROR(m_logger, "解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  488. return;
  489. }
  490. }
  491. /* 判断时间是否长时间没有更新 */
  492. bool SPAServer::isEventTimeVaild(const std::string& strTime)
  493. {
  494. /* 获取当前时间 */
  495. time_t now = time(0);
  496. /* 字符串转成时间 */
  497. tm tmTime;
  498. strptime(strTime.c_str(), "%Y-%m-%d %H:%M:%S", &tmTime);
  499. time_t eventTime = mktime(&tmTime);
  500. /* 时间差 */
  501. double diff = difftime(now, eventTime);
  502. SPDLOG_LOGGER_DEBUG(m_logger, "now:{} eventTime: {} 时间差:{}秒",now, eventTime, diff);
  503. return true;
  504. }