FuncIllegalInvasion.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #include "FuncIllegalInvasion.h"
  2. #include "GlobalVariable.h"
  3. #include "GlobalConfig.h"
  4. #include "FromRedis.h"
  5. #include "FromWebAPI.h"
  6. #include "UniversalFunc.h"
  7. RoomIllegalInvasionInfo::RoomIllegalInvasionInfo(const RoomIllegalInvasionInfo& o)
  8. {
  9. isAlarm = o.isAlarm;
  10. RoomID = o.RoomID;
  11. RoomType = o.RoomType;
  12. numMaxFace = o.numMaxFace;
  13. numMaxPerson = o.numMaxPerson;
  14. strBoxList = o.strBoxList;
  15. strMessage = o.strMessage;
  16. strImage = o.strImage;
  17. CameraID = o.CameraID;
  18. }
  19. RoomIllegalInvasionInfo& RoomIllegalInvasionInfo::operator=(const RoomIllegalInvasionInfo& o)
  20. {
  21. if(this != &o) {
  22. isAlarm = o.isAlarm;
  23. RoomID = o.RoomID;
  24. RoomType = o.RoomType;
  25. numMaxFace = o.numMaxFace;
  26. numMaxPerson = o.numMaxPerson;
  27. strBoxList = o.strBoxList;
  28. strMessage = o.strMessage;
  29. strImage = o.strImage;
  30. CameraID = o.CameraID;
  31. }
  32. return *this;
  33. }
  34. /* 添加房间 */
  35. void ListRoomIll::addRoom(int RoomID, Enum_RoomType RoomType)
  36. {
  37. auto p = findRoom(RoomID, RoomType);
  38. if(p == nullptr)
  39. {
  40. RoomIllegalInvasionInfo ill;
  41. ill.RoomID = RoomID;
  42. ill.RoomType = RoomType;
  43. listRoomIll.push_back(ill);
  44. }
  45. }
  46. /* 查找是否有相同的房间 */
  47. RoomIllegalInvasionInfo* ListRoomIll::findRoom(int RoomID, Enum_RoomType RoomType)
  48. {
  49. for(auto& it : listRoomIll)
  50. {
  51. if(it.RoomID == RoomID && it.RoomType == RoomType)
  52. {
  53. return ⁢
  54. }
  55. }
  56. return nullptr;
  57. }
  58. IllegalInvasionInfo::IllegalInvasionInfo(const IllegalInvasionInfo& other)
  59. {
  60. isInsertEQM = other.isInsertEQM;
  61. PKID = other.PKID;
  62. CameraID = other.CameraID;
  63. RoomID = other.RoomID;
  64. ChannelID = other.ChannelID;
  65. RoomType = other.RoomType;
  66. FirstTime = other.FirstTime;
  67. strActionDec = other.strActionDec;
  68. strImageInfo = other.strImageInfo;
  69. }
  70. IllegalInvasionInfo& IllegalInvasionInfo::operator=(const IllegalInvasionInfo& other)
  71. {
  72. if(this != &other)
  73. {
  74. isInsertEQM = other.isInsertEQM;
  75. PKID = other.PKID;
  76. CameraID = other.CameraID;
  77. RoomID = other.RoomID;
  78. ChannelID = other.ChannelID;
  79. RoomType = other.RoomType;
  80. FirstTime = other.FirstTime;
  81. strActionDec = other.strActionDec;
  82. strImageInfo = other.strImageInfo;
  83. }
  84. return *this;
  85. }
  86. /* 添加信息 */
  87. void ListIllegalInvasionInfo::addIllInfo(IllegalInvasionInfo& info)
  88. {
  89. if(findIllInfo(info) == nullptr)
  90. {
  91. listIll.push_back(info);
  92. }
  93. }
  94. /* 查找相同的信息 */
  95. IllegalInvasionInfo* ListIllegalInvasionInfo::findIllInfo(IllegalInvasionInfo& info)
  96. {
  97. for(auto& it : listIll)
  98. {
  99. if(it.RoomID == info.RoomID && it.RoomType == info.RoomType)
  100. {
  101. return ⁢
  102. }
  103. }
  104. return nullptr;
  105. }
  106. IllegalInvasionInfo* ListIllegalInvasionInfo::findIllInfo(int roomID, Enum_RoomType roomType)
  107. {
  108. for(auto& it : listIll)
  109. {
  110. if(it.RoomID == roomID && it.RoomType == roomType)
  111. {
  112. return ⁢
  113. }
  114. }
  115. return nullptr;
  116. }
  117. /* 删除报警信息 */
  118. void ListIllegalInvasionInfo::deleteIllInfo(IllegalInvasionInfo& info)
  119. {
  120. for(auto it = listIll.begin(); it != listIll.end();)
  121. {
  122. if(it->RoomID == info.RoomID && it->RoomType == info.RoomType)
  123. {
  124. it = listIll.erase(it);
  125. }else {
  126. ++it;
  127. }
  128. }
  129. }
  130. void ListIllegalInvasionInfo::deleteIllInfo(int roomID, Enum_RoomType roomType)
  131. {
  132. for(auto it = listIll.begin(); it != listIll.end();)
  133. {
  134. if(it->RoomID == roomID && it->RoomType == roomType)
  135. {
  136. it = listIll.erase(it);
  137. }else {
  138. ++it;
  139. }
  140. }
  141. }
  142. /* ===================================================================================
  143. * 功能类:FuncIllegalInvasion
  144. * =================================================================================== */
  145. FuncIllegalInvasion::FuncIllegalInvasion()
  146. {
  147. m_logger = spdlog::get("SPAServer");
  148. if(m_logger == nullptr)
  149. {
  150. SPDLOG_ERROR("SPAServer logger is nullptr");
  151. return;
  152. }
  153. m_pListAlarm = std::make_shared<std::list<AlarmInfo>>();
  154. m_pListIllInfo = std::make_shared<ListIllegalInvasionInfo>();
  155. }
  156. FuncIllegalInvasion::~FuncIllegalInvasion()
  157. {
  158. if(m_pListAlarm != nullptr)
  159. {
  160. m_pListAlarm.reset();
  161. m_pListAlarm = nullptr;
  162. }
  163. if(m_pListIllInfo != nullptr)
  164. {
  165. m_pListIllInfo.reset();
  166. m_pListIllInfo = nullptr;
  167. }
  168. }
  169. /* 是否报警 */
  170. bool FuncIllegalInvasion::isAlarm(const std::list<PersonInfo>& listPersion)
  171. {
  172. for(const auto& it : listPersion)
  173. {
  174. if(it.PersonID == "-1")
  175. {
  176. return true;
  177. }
  178. }
  179. return false;
  180. }
  181. /* 线程功能 */
  182. void FuncIllegalInvasion::task()
  183. {
  184. std::string strFaceActionID; /* 人脸识别的Action */
  185. std::string strCountActionID; /* 人员计数的Action */
  186. while (GThreadInfo.getRunning())
  187. {
  188. /* 更新线程信息 */
  189. GThreadInfo.updateFuncInfo(m_funcThreadInfo);
  190. if(m_funcThreadInfo.RunState == RunTimeState::RUN_STATE_STOP)
  191. {
  192. break;
  193. }
  194. /* 判断是否在检测时间段内 */
  195. if(!isInDetectTime(m_funcThreadInfo.StartTime, m_funcThreadInfo.EndTime))
  196. {
  197. std::this_thread::sleep_for(std::chrono::milliseconds(GVariable.ThreadSleepMS));
  198. continue;
  199. }
  200. /* 更新算法ActionID */
  201. {
  202. GVariable.mutexRW.lockForRead();
  203. strFaceActionID = GVariable.ActFaceIdentify;
  204. strCountActionID = GVariable.ActPersonCount;
  205. GVariable.mutexRW.unlock();
  206. }
  207. /* 读取Redis数据 */
  208. m_pListAlarm->clear();
  209. for(const auto& RoomInfo : m_funcThreadInfo.listRoomCamActInfo)
  210. {
  211. for(const auto& it : RoomInfo.mapCameraAction)
  212. {
  213. for(const auto& act : it.second)
  214. {
  215. std::string strKey = std::to_string(it.first) + ":" + act;
  216. std::string strRetValue;
  217. if(!m_fromRedis->getRedisString(strKey, strRetValue))
  218. {
  219. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  220. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  221. continue;
  222. }
  223. /* 解析数据 */
  224. AlarmInfo alarmInfo;
  225. // parseRedisData(strRetValue, alarmInfo);
  226. parseRedisBaseData(strRetValue, alarmInfo);
  227. parseRedisBBoxesData(strRetValue, alarmInfo);
  228. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  229. if(isEventTimeVaild(alarmInfo.EventTime))
  230. {
  231. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  232. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  233. continue;
  234. }
  235. m_pListAlarm->push_back(alarmInfo);
  236. }
  237. }
  238. }
  239. /* 找出房间的个数,按照房间进行判断 */
  240. ListRoomIll listRoomIll;
  241. for(auto& it : m_funcThreadInfo.listRoomCamActInfo)
  242. {
  243. listRoomIll.addRoom(it.RoomID, it.RoomType);
  244. }
  245. /* 根据房间解析人脸识别的非法入侵,检测到了非法人脸,就可以直接报警了 */
  246. for(auto& room : listRoomIll.getData())
  247. {
  248. for(const auto& alarm : *m_pListAlarm)
  249. {
  250. if(room.RoomID != alarm.RoomID)
  251. {
  252. continue;
  253. }
  254. /* 统计这个房间内《人脸算法》检测到的最大人员数 */
  255. if(alarm.ActionID == strFaceActionID)
  256. {
  257. if(alarm.listPersonInfo.size() > room.numMaxFace)
  258. {
  259. room.numMaxFace = alarm.listPersonInfo.size();
  260. room.strBoxList = alarm.listBbox;
  261. room.listPersonInfo = alarm.listPersonInfo;
  262. }
  263. }
  264. /* 判断有没有非法入侵人员 */
  265. if(isAlarm(alarm.listPersonInfo))
  266. {
  267. room.isAlarm = true;
  268. room.strMessage = "人员非法入侵(未知人员)";
  269. if( !alarm.ImageInfo.empty() )
  270. {
  271. room.strImage = alarm.ImageInfo;
  272. }
  273. room.CameraID = alarm.DeviceID;
  274. }
  275. }
  276. }
  277. /* 判断《人脸算法》识别到的数目和《人员计数算法》识别到的人数是否相等,如果上面报警了,这里就不用检测了 */
  278. for(auto& room : listRoomIll.getData())
  279. {
  280. if(room.isAlarm)
  281. {
  282. continue;
  283. }
  284. /* 这个房间的人脸算法没有识别到非法入侵 */
  285. for(const auto& alarm : *m_pListAlarm)
  286. {
  287. if(room.RoomID != alarm.RoomID)
  288. {
  289. continue;
  290. }
  291. /* 人员计数 */
  292. if(alarm.ActionID == strCountActionID)
  293. {
  294. /* 统计这个房间内《人员计数算法》检测到的最大人员数 */
  295. if(alarm.listPersonInfo.size() > room.numMaxPerson)
  296. {
  297. room.numMaxPerson = alarm.listPersonInfo.size();
  298. room.strBoxList = alarm.listBbox;
  299. if(!alarm.ImageInfo.empty())
  300. {
  301. room.strImage = alarm.ImageInfo;
  302. }
  303. room.CameraID = alarm.DeviceID;
  304. }
  305. }
  306. }
  307. /* 判断人数是否一致 */
  308. if(room.numMaxFace != room.numMaxPerson)
  309. {
  310. room.strMessage = fmt::format("非法入侵(人员计数 {} 和人脸数 {} 不一致)", room.numMaxPerson, room.numMaxFace);
  311. room.isAlarm = true;
  312. }
  313. }
  314. /* 将非法入侵的信息存储到数组缓存中,等待报警结束后判断是否需要写入EQM数据库 */
  315. for(auto& room : listRoomIll.getData())
  316. {
  317. auto pIll = m_pListIllInfo->findIllInfo(room.RoomID, room.RoomType);
  318. if(room.isAlarm)
  319. {
  320. /* 正在报警,检查缓存中是否有这个报警信息 */
  321. if(pIll == nullptr)
  322. {
  323. IllegalInvasionInfo info;
  324. info.ChannelID = m_funcThreadInfo.ChannelID;
  325. info.CameraID = room.CameraID;
  326. info.RoomID = room.RoomID;
  327. info.RoomType = room.RoomType;
  328. info.strActionDec = room.strMessage;
  329. info.strImageInfo = room.strImage;
  330. info.FirstTime = QDateTime::currentDateTime();;
  331. m_pListIllInfo->addIllInfo(info);
  332. }
  333. }
  334. else
  335. {
  336. /* 没有报警,检查是否是报警结束了,是否符合写入数据库的条件 */
  337. QDateTime currTime = QDateTime::currentDateTime();
  338. int secs = currTime.toSecsSinceEpoch() - pIll->FirstTime.toSecsSinceEpoch();
  339. if(secs >= GVariable.AppBadMan_Time)
  340. {
  341. if(!pIll->strImageInfo.empty())
  342. {
  343. /* 超过非法入侵的时间,写入数据库 */
  344. AlarmInfo ai;
  345. ai.ChannelID = m_funcThreadInfo.ChannelID;
  346. ai.RoomID = pIll->RoomID;
  347. ai.ActionID = GVariable.ActPersonCount;
  348. ai.ActionDes = pIll->strActionDec;
  349. ai.ImageInfo = pIll->strImageInfo;
  350. ai.StartTime = pIll->FirstTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  351. ai.EndTime = currTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  352. ai.EventTime = pIll->FirstTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  353. ai.listPersonInfo = room.listPersonInfo;
  354. ai.listBbox = room.strBoxList;
  355. bool insertRet = m_fromWebAPI->insertAlarmInfo(ai);
  356. if(insertRet)
  357. {
  358. std::string actionName = GVariable.getActionName(ai.ActionID);
  359. SPDLOG_LOGGER_INFO(m_logger, "☆ 新增报警信息,频道[{}],房间[{}],摄像头[{}],{},{},有{}个区域,有{}个人脸,{}",
  360. pIll->ChannelID, pIll->RoomID, pIll->CameraID, actionName, ai.ActionDes, ai.listBbox.size(), ai.FaceIDList.size(), ai.ImageInfo);
  361. }
  362. }
  363. else {
  364. SPDLOG_LOGGER_WARN(m_logger, "× 取消新增报警信息,因为没有图片,频道[{}],房间[{}],摄像头[{}],{},{},有{}个区域,有{}个人脸",
  365. pIll->ChannelID, pIll->RoomID, pIll->CameraID, pIll->strActionDec, pIll->strImageInfo, room.strBoxList.size(), room.listPersonInfo.size());
  366. }
  367. /* 删除这个非法入侵信息 */
  368. m_pListIllInfo->deleteIllInfo(*pIll);
  369. }
  370. }
  371. }
  372. std::this_thread::sleep_for(std::chrono::milliseconds(GVariable.ThreadSleepMS));
  373. }
  374. }