FuncPersonOnWork.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "FuncPersonOnWork.h"
  2. #include "GlobalVariable.h"
  3. #include "GlobalConfig.h"
  4. #include "FromRedis.h"
  5. #include "ToEQMDataBase.h"
  6. #include "UniversalFunc.h"
  7. FuncPersonOnWork::FuncPersonOnWork()
  8. {
  9. m_logger = spdlog::get("SPAServer");
  10. if(m_logger == nullptr)
  11. {
  12. SPDLOG_LOGGER_ERROR(m_logger, "SPAServer logger is nullptr");
  13. return;
  14. }
  15. /* 给变量分配内存 */
  16. /* 保存每个摄像机的报警信息 */
  17. m_pListAlarm = std::make_shared<std::list<AlarmInfo>>();
  18. /* 保存人脸信息的数据 */
  19. m_pListRoomFace = new ListRoomFaceInfo();
  20. }
  21. FuncPersonOnWork::~FuncPersonOnWork()
  22. {
  23. if(m_pListRoomFace != nullptr)
  24. {
  25. delete m_pListRoomFace;
  26. m_pListRoomFace = nullptr;
  27. }
  28. }
  29. /**
  30. * @brief 工作线程
  31. *
  32. */
  33. void FuncPersonOnWork::task()
  34. {
  35. while (GThreadInfo.getRunning())
  36. {
  37. /* 更新线程信息 */
  38. GThreadInfo.updateFuncInfo(m_funcAct);
  39. if( (m_funcAct.appFunction == AppFunction::APP_NONE) ||
  40. (m_funcAct.RunState == RunTimeState::RUN_STATE_STOP) )
  41. {
  42. break;
  43. }
  44. /* 判断是否在检测时间段内 */
  45. if(!isInDetectTime(m_funcAct.StartTime, m_funcAct.EndTime))
  46. {
  47. std::this_thread::sleep_for(std::chrono::milliseconds(GConfig.ThreadSleepMS));
  48. continue;
  49. }
  50. /* -----------------------------------------------------------------------
  51. * 读取Redis数据
  52. * ----------------------------------------------------------------------- */
  53. m_pListAlarm->clear();
  54. for(const auto& RoomInfo : m_funcAct.listRoomCamActInfo)
  55. {
  56. for(const auto& it : RoomInfo.mapCameraAction)
  57. {
  58. std::string strKey = std::to_string(it.first) + ":" + it.second;
  59. std::string strRetValue;
  60. if(!m_fromRedis->getRedisString(strKey, strRetValue))
  61. {
  62. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  63. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  64. continue;
  65. }
  66. /* 解析数据 */
  67. AlarmInfo alarmInfo;
  68. alarmInfo.ActionID = it.second;
  69. parseRedisBaseData(strRetValue, alarmInfo);
  70. parseRedisBBoxesData(strRetValue, alarmInfo);
  71. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  72. if(isEventTimeVaild(alarmInfo.EventTime))
  73. {
  74. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  75. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  76. continue;
  77. }
  78. m_pListAlarm->push_back(alarmInfo);
  79. }
  80. }
  81. /* 处理数据,将报警信息的人脸信息取出来,放入到人脸信息列表中 */
  82. for(auto& alarmInfo : *m_pListAlarm)
  83. {
  84. /* 添加到人脸列表中,会自动去重,自动创建对应的频道和房间信息 */
  85. m_pListRoomFace->addRoomFaceInfo(alarmInfo);
  86. }
  87. /* 计算人脸个数,每个房间逐个对比 */
  88. QDateTime now = QDateTime::currentDateTime();
  89. for(auto it = m_pListRoomFace->listRoomFaceInfo.begin(); it != m_pListRoomFace->listRoomFaceInfo.end(); )
  90. {
  91. /* m_pListRoomFace的数据不会删除,会和历史对比,这里的MaxNum和MinNum就是历史数据 */
  92. if(it->MaxNum < it->listPersonInfo.size())
  93. {
  94. it->MaxNum = it->listPersonInfo.size();
  95. }
  96. if(it->MinNum > it->listPersonInfo.size())
  97. {
  98. it->MinNum = it->listPersonInfo.size();
  99. }
  100. /* 判断是否需要写入数据库,一定时间写入一次,默认是10分钟写一次 */
  101. if(now.toSecsSinceEpoch() - it->StartTime.toSecsSinceEpoch() > GConfig.AppUpdateOnWorkTimeInterval_Time)
  102. {
  103. /* 写入数据库 */
  104. if(m_toEQMDataBase->insertOnWorkInfo(*it))
  105. {
  106. SPDLOG_LOGGER_INFO(m_logger, "☆ 新增人脸信息,频道[{}][{}],摄像头[{}][{}],时间范围[{} - {}],人数范围[{} - {}]",
  107. it->ChannelID, GConfig.getChannelName(it->ChannelID), it->CameraID, GConfig.getCameraName(it->CameraID),
  108. it->StartTime.toString("yyyy-MM-dd hh:mm:ss").toStdString(), it->EndTime.toString("yyyy-MM-dd hh:mm:ss").toStdString(),
  109. it->MinNum, it->MaxNum);
  110. /* 删除这条信息 */
  111. it = m_pListRoomFace->listRoomFaceInfo.erase(it);
  112. continue;
  113. }else {
  114. SPDLOG_LOGGER_ERROR(m_logger, "写入数据库tWorkOnInfo失败");
  115. }
  116. }
  117. ++it;
  118. }
  119. std::this_thread::sleep_for(std::chrono::milliseconds(GConfig.ThreadSleepMS));
  120. }
  121. }