FuncOnAndOffJob.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include "FuncOnAndOffJob.h"
  2. #include "GlobalVariable.h"
  3. #include "GlobalConfig.h"
  4. #include "UniversalFunc.h"
  5. #include "FromRedis.h"
  6. #include "ToEQMDataBase.h"
  7. DecCondition::DecCondition()
  8. {
  9. ChannelID = -1;
  10. leaveNum = 0;
  11. leaveNumOnTime = 0;
  12. leaveOneTime = 0;
  13. }
  14. DecCondition::DecCondition(const DecCondition& other)
  15. {
  16. ChannelID = other.ChannelID;
  17. leaveNum = other.leaveNum;
  18. leaveNumOnTime = other.leaveNumOnTime;
  19. leaveOneTime = other.leaveOneTime;
  20. }
  21. DecCondition& DecCondition::operator=(const DecCondition& other)
  22. {
  23. if (this != &other)
  24. {
  25. ChannelID = other.ChannelID;
  26. leaveNum = other.leaveNum;
  27. leaveNumOnTime = other.leaveNumOnTime;
  28. leaveOneTime = other.leaveOneTime;
  29. }
  30. return *this;
  31. }
  32. RoomOnWorkInfo& RoomOnWorkInfo::operator=(const RoomOnWorkInfo& other)
  33. {
  34. if (this != &other)
  35. {
  36. bOnWork = other.bOnWork;
  37. RoomID = other.RoomID;
  38. CameraID = other.CameraID;
  39. RoomType = other.RoomType;
  40. StartTime = other.StartTime;
  41. EndTime = other.EndTime;
  42. strImagePath = other.strImagePath;
  43. listPersonInfo = other.listPersonInfo;
  44. }
  45. return *this;
  46. }
  47. RoomOnWorkInfo::RoomOnWorkInfo(const RoomOnWorkInfo& other)
  48. {
  49. bOnWork = other.bOnWork;
  50. RoomID = other.RoomID;
  51. CameraID = other.CameraID;
  52. RoomType = other.RoomType;
  53. StartTime = other.StartTime;
  54. EndTime = other.EndTime;
  55. strImagePath = other.strImagePath;
  56. listPersonInfo = other.listPersonInfo;
  57. }
  58. FuncOnAndOffJob::FuncOnAndOffJob()
  59. {
  60. m_pListAlarm = new ListAlarmInfo();
  61. m_logger = spdlog::get("SPAServer");
  62. if(m_logger == nullptr)
  63. {
  64. SPDLOG_ERROR("FuncOnAndOffJob logger is nullptr");
  65. return;
  66. }
  67. }
  68. FuncOnAndOffJob::~FuncOnAndOffJob()
  69. {
  70. }
  71. /* 获取当前频率信息 */
  72. bool FuncOnAndOffJob::getCurrentFrequencyInfo(const int ChannelID, DecCondition& info)
  73. {
  74. auto str = m_toEQMDataBase->getChannelName(ChannelID);
  75. if(str == "")
  76. {
  77. SPDLOG_LOGGER_ERROR(m_logger, "获取通道信息失败, ChannelID:{}", ChannelID);
  78. return false;
  79. }
  80. /* 解析数据 */
  81. try
  82. {
  83. nJson json1 = nJson::parse(str);
  84. info.ChannelID = ChannelID;
  85. info.leaveNum = json1["leaveNum"].is_null() ? 0 : json1["leaveNum"].get<int>();
  86. info.leaveNumOnTime = json1["leaveNumTime"].is_null() ? 0 : json1["leaveNumTime"].get<int>();
  87. info.leaveOneTime = json1["leaveLongTime"].is_null() ? 0 : json1["leaveLongTime"].get<int>();
  88. }
  89. catch (const nJson::parse_error& e) {
  90. SPDLOG_LOGGER_ERROR(m_logger,"解析 tChannel数据失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  91. return false;
  92. }
  93. catch (const nJson::type_error& e) {
  94. SPDLOG_LOGGER_ERROR(m_logger,"解析 tChannel数据失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  95. return false;
  96. }
  97. catch(...) {
  98. SPDLOG_LOGGER_ERROR(m_logger,"解析 tChannel数据失败 数据失败");
  99. return false;
  100. }
  101. return true;
  102. }
  103. void FuncOnAndOffJob::task()
  104. {
  105. while(GThreadInfo.getRunning())
  106. {
  107. /* 判断是否需要退出 */
  108. if(m_funcAct.RunState == RunTimeState::RUN_STATE_STOP)
  109. {
  110. break;
  111. }
  112. /* 判断是否在检测时间段内 */
  113. if(!isInDetectTime(m_funcAct.StartTime, m_funcAct.EndTime))
  114. {
  115. /* 休眠一段时间 */
  116. std::this_thread::sleep_for(std::chrono::milliseconds(GConfig.ThreadSleepMS));
  117. continue;
  118. }
  119. /* 先获取所有的直播间,这里不需要读取所有的房间数据 */
  120. std::map<int, RoomCamActInfo> mapRoomCamActInfo;
  121. for(auto& it : m_funcAct.listRoomCamActInfo)
  122. {
  123. /* 判断是不是直播间 */
  124. if(it.RoomType == GConfig.liveRoomType)
  125. {
  126. mapRoomCamActInfo.insert(std::make_pair(it.RoomID, it));
  127. }
  128. }
  129. /* 获取当前频率的在岗离岗信息,在表格“tChannel”中 */
  130. DecCondition decCondition;
  131. if(!getCurrentFrequencyInfo(m_funcAct.ChannelID, decCondition))
  132. {
  133. SPDLOG_LOGGER_ERROR(m_logger, "获取当前频率信息失败, ChannelID:{}", m_funcAct.ChannelID);
  134. std::this_thread::sleep_for(std::chrono::milliseconds(GConfig.ThreadSleepMS));
  135. continue;
  136. }
  137. /* 读取Redis数据 */
  138. for(const auto& RoomInfo : mapRoomCamActInfo)
  139. {
  140. for(const auto& it : RoomInfo.second.mapCameraAction)
  141. {
  142. std::string strKey = std::to_string(it.first) + ":" + it.second;
  143. std::string strRetValue;
  144. if(!m_fromRedis->getRedisString(strKey, strRetValue))
  145. {
  146. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  147. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  148. continue;
  149. }
  150. /* 解析数据 */
  151. AlarmInfo alarmInfo;
  152. parseRedisBaseData(strRetValue, alarmInfo);
  153. parseRedisBBoxesData(strRetValue, alarmInfo);
  154. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  155. if(isEventTimeVaild(alarmInfo.EventTime))
  156. {
  157. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  158. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  159. continue;
  160. }
  161. m_pListAlarm->addAlarmInfo(alarmInfo);
  162. }
  163. }
  164. /* 对所有房间挨个判断 */
  165. for(auto& roomInfo : mapRoomCamActInfo)
  166. {
  167. bool bHasPeople = false;
  168. bool bHasFace = false;
  169. /* 先使用人员计数判断直播间有没有人,如果直播间有人,再使用人脸识别算法判断是否是认识的人(是认识的人才会被识别到) */
  170. for(auto& alarm : m_pListAlarm->listAlarmInfo)
  171. {
  172. if(alarm->RoomID == roomInfo.first)
  173. {
  174. if(alarm->ActionID == g_actionList.ActPersonNumber)
  175. {
  176. if(alarm->listBbox.size() > 0)
  177. {
  178. bHasPeople = true;
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. /* 如果有人,再判断是不是认识的人 */
  185. if(bHasPeople)
  186. {
  187. for(auto& alarm : m_pListAlarm->listAlarmInfo)
  188. {
  189. if(alarm->RoomID == roomInfo.first)
  190. {
  191. if(alarm->ActionID == g_actionList.ActFace)
  192. {
  193. if(alarm->vecPersonInfo.size() > 0)
  194. {
  195. bHasFace = true;
  196. break;
  197. }
  198. }
  199. }
  200. }
  201. }
  202. /* 查找这个房间对应的房间信息 */
  203. RoomOnWorkInfo* pRoomOnWorkInfo = nullptr;
  204. auto it0 = m_mapRoomOnWorkInfo.find(roomInfo.first);
  205. if(it0 != m_mapRoomOnWorkInfo.end())
  206. {
  207. pRoomOnWorkInfo = it0->second;
  208. }
  209. else
  210. {
  211. /* 没找到,就创建一个 */
  212. pRoomOnWorkInfo = new RoomOnWorkInfo();
  213. pRoomOnWorkInfo->RoomID = roomInfo.first;
  214. }
  215. }
  216. }
  217. }