FuncPersonOnWork.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "FuncPersonOnWork.h"
  2. #include "GlobalVariable.h"
  3. #include "GlobalConfig.h"
  4. #include "FromRedis.h"
  5. #include "FromWebAPI.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_funcThreadInfo);
  39. if( (m_funcThreadInfo.appFunction == AppFunction::APP_NONE) ||
  40. (m_funcThreadInfo.RunState == RunTimeState::RUN_STATE_STOP) )
  41. {
  42. break;
  43. }
  44. /* 判断是否在检测时间段内 */
  45. if(!isInDetectTime(m_funcThreadInfo.StartTime, m_funcThreadInfo.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_funcThreadInfo.listRoomCamActInfo)
  55. {
  56. for(const auto& it : RoomInfo.mapCameraAction)
  57. {
  58. for(const auto& act : it.second)
  59. {
  60. /* 读取Redis数据 */
  61. std::string strKey = std::to_string(it.first) + ":" + act;
  62. std::string strRetValue;
  63. if(!m_fromRedis->getRedisString(strKey, strRetValue))
  64. {
  65. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  66. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  67. continue;
  68. }
  69. /* 解析数据 */
  70. AlarmInfo alarmInfo;
  71. alarmInfo.ActionID = act;
  72. parseRedisBaseData(strRetValue, alarmInfo);
  73. parseRedisBBoxesData(strRetValue, alarmInfo);
  74. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  75. if(isEventTimeVaild(alarmInfo.EventTime))
  76. {
  77. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  78. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  79. continue;
  80. }
  81. m_pListAlarm->push_back(alarmInfo);
  82. }
  83. }
  84. }
  85. /* 处理数据,将报警信息的人脸信息取出来,放入到人脸信息列表中 */
  86. for(auto& alarmInfo : *m_pListAlarm)
  87. {
  88. /* 添加到人脸列表中,会自动去重,自动创建对应的频道和房间信息 */
  89. m_pListRoomFace->addRoomFaceInfo(alarmInfo);
  90. }
  91. /* 计算人脸个数,每个房间逐个对比 */
  92. QDateTime now = QDateTime::currentDateTime();
  93. for(auto it = m_pListRoomFace->listRoomFaceInfo.begin(); it != m_pListRoomFace->listRoomFaceInfo.end(); )
  94. {
  95. /* m_pListRoomFace的数据不会删除,会和历史对比,这里的MaxNum和MinNum就是历史数据 */
  96. if(it->MaxNum < it->listPersonInfo.size())
  97. {
  98. it->MaxNum = it->listPersonInfo.size();
  99. }
  100. if(it->MinNum > it->listPersonInfo.size())
  101. {
  102. it->MinNum = it->listPersonInfo.size();
  103. }
  104. /* 判断是否需要写入数据库,一定时间写入一次,默认是10分钟写一次 */
  105. if(now.toSecsSinceEpoch() - it->StartTime.toSecsSinceEpoch() > GConfig.AppUpdateOnWorkTimeInterval_Time)
  106. {
  107. /* 写入数据库 */
  108. if(m_fromWebAPI->insertOnWorkInfo(*it))
  109. {
  110. SPDLOG_LOGGER_INFO(m_logger, "☆ 新增人脸信息,频道[{}][{}],摄像头[{}][{}],时间范围[{} - {}],人数范围[{} - {}]",
  111. it->ChannelID, GConfig.getChannelName(it->ChannelID), it->CameraID, GConfig.getCameraName(it->CameraID),
  112. it->StartTime.toString("yyyy-MM-dd hh:mm:ss").toStdString(), it->EndTime.toString("yyyy-MM-dd hh:mm:ss").toStdString(),
  113. it->MinNum, it->MaxNum);
  114. /* 删除这条信息 */
  115. it = m_pListRoomFace->listRoomFaceInfo.erase(it);
  116. continue;
  117. }else {
  118. SPDLOG_LOGGER_ERROR(m_logger, "写入数据库tWorkOnInfo失败");
  119. }
  120. }
  121. ++it;
  122. }
  123. std::this_thread::sleep_for(std::chrono::milliseconds(GConfig.ThreadSleepMS));
  124. }
  125. }