FuncOrdinary.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "FuncOrdinary.h"
  2. #include "GlobalVariable.h"
  3. #include "FromRedis.h"
  4. #include "ToEQMDataBase.h"
  5. #include "UniversalFunc.h"
  6. #include "GlobalConfig.h"
  7. FuncOrdinary::FuncOrdinary()
  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. m_listAlarm = new ListAlarmInfo();
  17. }
  18. FuncOrdinary::~FuncOrdinary()
  19. {
  20. if(m_listAlarm != nullptr)
  21. {
  22. delete m_listAlarm;
  23. m_listAlarm = nullptr;
  24. }
  25. }
  26. /* 任务线程 */
  27. void FuncOrdinary::thread_task()
  28. {
  29. if(m_pFuncAct == nullptr)
  30. {
  31. SPDLOG_LOGGER_ERROR(m_logger, "未设置线程功能信息");
  32. return;
  33. }
  34. SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程, ChannelID:{}",m_pFuncAct->strFunctionName, m_pFuncAct->ChannelID);
  35. while (GThreadInfo.getRunning())
  36. {
  37. /* 更新算法关联的摄像机ID */
  38. // updateFuncInfo(pFuncAct);
  39. /* 如果线程的运行状态为 */
  40. if(m_pFuncAct->RunState == RunTimeState::RUN_STATE_STOP)
  41. {
  42. /* 设置为NONE,就会被管理线程回收释放内存 */
  43. m_pFuncAct->appFunction = AppFunction::APP_NONE;
  44. break;
  45. }
  46. /* 读取Redis信息,处理数据 */
  47. for(const auto& roomInfo : m_pFuncAct->listRoomCamActInfo)
  48. {
  49. for(const auto& it : roomInfo.mapCameraAction)
  50. {
  51. /* 读取Redis数据 */
  52. std::string strKey = std::to_string(it.first) + ":" + it.second;
  53. std::string strRetValue;
  54. if(!m_fromRedis->getRedisString(strKey, strRetValue))
  55. {
  56. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  57. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  58. continue;
  59. }
  60. /* 解析数据 */
  61. AlarmInfo alarmInfo;
  62. parseRedisBaseData(strRetValue, alarmInfo);
  63. parseRedisBBoxesData(strRetValue, alarmInfo);
  64. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  65. if(isEventTimeVaild(alarmInfo.EventTime))
  66. {
  67. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  68. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  69. continue;
  70. }
  71. /* 找出数组中与当前报警ID相同的的报警信息 */
  72. auto pAlarmInfo = m_listAlarm->findAlarmInfo(alarmInfo);
  73. if(pAlarmInfo == nullptr)
  74. {
  75. /* 没有找到报警记录,就新建一个 */
  76. AlarmInfo newAlarmInfo = alarmInfo;
  77. m_listAlarm->addAlarmInfo(newAlarmInfo);
  78. /* 重新查找该报警信息 */
  79. pAlarmInfo = m_listAlarm->findAlarmInfo(alarmInfo);
  80. if(pAlarmInfo == nullptr)
  81. {
  82. SPDLOG_LOGGER_ERROR(m_logger, "查找报警信息失败, Key:{}", strKey);
  83. continue;
  84. }
  85. }
  86. /* 判断有无报警记录,新的报警就写入到EQM的tAlarmInfo表中,正在报警的记录就判断和更新,
  87. * 结束报警就更新tAlarmInfo的报警数据的结束时间,并清空pAlarmInfo */
  88. if(pAlarmInfo->Is_Alarm)
  89. {
  90. /* 正在报警中,检查是否报警结束 */
  91. if(alarmInfo.Is_Alarm)
  92. {
  93. /* 更新图片信息 */
  94. if(!alarmInfo.ImageInfo.empty())
  95. {
  96. pAlarmInfo->ImageInfo = alarmInfo.ImageInfo;
  97. }
  98. } else {
  99. /* 报警结束,判断时长,更新数据库结束时间,结束时间是此时电脑时间 */
  100. if(timeDiffWithNow(pAlarmInfo->EventTime) > g_config.Contraband)
  101. {
  102. pAlarmInfo->EndTime = chronoToStrTime(std::chrono::system_clock::now());
  103. m_toEQMDataBase->updateAlarmEndTime(*pAlarmInfo);
  104. }else {
  105. /* 不够报警时间,目前不做任何处理,不删除EQM报警记录 */
  106. }
  107. /* 清空报警信息 */
  108. pAlarmInfo->reInit();
  109. }
  110. }
  111. else
  112. {
  113. /* 是新的报警 */
  114. if(alarmInfo.Is_Alarm)
  115. {
  116. /* 图片不能是空,如果是空的,就不写入数据库 */
  117. if(!alarmInfo.ImageInfo.empty())
  118. {
  119. /* 违禁品检测,开始时间是事件时间 */
  120. alarmInfo.StartTime = alarmInfo.EventTime;
  121. alarmInfo.EndTime = "";
  122. if(m_toEQMDataBase->insertAlarmInfo(alarmInfo))
  123. {
  124. /* 保存新的报警记录 */
  125. *pAlarmInfo = alarmInfo;
  126. }else {
  127. SPDLOG_LOGGER_ERROR(m_logger, "写入tAlarmInfo报警数据失败, Key: {}", strKey);
  128. }
  129. }else {
  130. SPDLOG_LOGGER_ERROR(m_logger, "频道:{}, 房间:{}, 摄像机:{}, 算法:{}, 有报警区域, 但是没有图片信息", pFuncAct->ChannelID, roomInfo.RoomID, it.first, it.second);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. /* 休眠设置的时间 */
  137. std::this_thread::sleep_for(std::chrono::seconds(g_config.ThreadSleepMS));
  138. }
  139. /* 设置线程退出的状态 */
  140. setThreadState(m_pFuncAct, RunTimeState::RUN_STATE_STOP);
  141. SPDLOG_LOGGER_INFO(m_logger, "{} 线程退出,Channel:{}", m_pFuncAct->strFunctionName, m_pFuncAct->ChannelID);
  142. }