GlobalInfo.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. #include "GlobalInfo.h"
  2. #include <QSettings>
  3. #include "spdlog/spdlog.h"
  4. /* ====================================================================================
  5. * ******************************* 全局变量定义 **********************************
  6. * ====================================================================================*/
  7. int g_eventTimeVaild = 600; /* 事件时间有效性,单位秒,超过这个时间就认为是无效数据 */
  8. /* ====================================================================================
  9. * *************************** DeviceInfo成员函数 *******************************
  10. * ====================================================================================*/
  11. /* 对比是否相等 */
  12. bool DeviceInfo::operator==(const DeviceInfo& other)
  13. {
  14. if(DeviceID != other.DeviceID) {
  15. return false;
  16. }
  17. else if(DeviceName != other.DeviceName || DeviceIP != other.DeviceIP) {
  18. return false;
  19. }
  20. else if(DeviceSerial != other.DeviceSerial || DeviceType != other.DeviceType) {
  21. return false;
  22. }
  23. else if(DevicePort != other.DevicePort || UserAccount != other.UserAccount || UserPassword != other.UserPassword) {
  24. return false;
  25. }
  26. return true;
  27. }
  28. /* 对比设备关联的算法信息是否相等 */
  29. bool DeviceInfo::isEqualAlgorithmInfo(const std::vector<AlgorithmInfo>& other)
  30. {
  31. /* 算法数目不相等,直接返回false */
  32. if(vecAlgorithmInfo.size() != other.size()) {
  33. return false;
  34. }else {
  35. /* 算法数目相等,进一步对比算法信息 */
  36. bool isEquality = true;
  37. /* 逐步对比算法信息 */
  38. for(const auto& it0 : vecAlgorithmInfo) {
  39. bool isEq2 = true;
  40. for(const auto& it1 : other) {
  41. if(it0 == it1) {
  42. continue;
  43. }else {
  44. isEq2 = false;
  45. break;
  46. }
  47. }
  48. if(!isEq2) {
  49. isEquality = false;
  50. break;
  51. }
  52. }
  53. if(!isEquality) {
  54. return false;
  55. }
  56. }
  57. return true;
  58. }
  59. /* ====================================================================================
  60. * ************************** AlarmInfo成员函数 ****************************
  61. * ====================================================================================*/
  62. AlarmInfo::AlarmInfo()
  63. {
  64. Is_Alarm = false;
  65. AlarmID = 0;
  66. DeviceID = 0;
  67. RoomID = 0;
  68. ChannelID = 0;
  69. State = 0;
  70. OnWork = 0;
  71. StartTime = "";
  72. EndTime = "";
  73. EventTime = "";
  74. PicUrl = "";
  75. ImageInfo = "";
  76. AppID = "";
  77. ActionID = "";
  78. ActionDes = "";
  79. FaceIDList = "";
  80. FaceNameList = "";
  81. BboxList = "";
  82. }
  83. AlarmInfo& AlarmInfo::operator=(AlarmInfo& other)
  84. {
  85. if(this != &other)
  86. {
  87. Is_Alarm = other.Is_Alarm;
  88. AlarmID = other.AlarmID;
  89. DeviceID = other.DeviceID;
  90. RoomID = other.RoomID;
  91. ChannelID = other.ChannelID;
  92. State = other.State;
  93. OnWork = other.OnWork;
  94. StartTime = other.StartTime;
  95. EndTime = other.EndTime;
  96. EventTime = other.EventTime;
  97. PicUrl = other.PicUrl;
  98. ImageInfo = other.ImageInfo;
  99. AppID = other.AppID;
  100. ActionID = other.ActionID;
  101. ActionDes = other.ActionDes;
  102. FaceIDList = other.FaceIDList;
  103. FaceNameList = other.FaceNameList;
  104. BboxList = other.BboxList;
  105. vecPersonInfo = other.vecPersonInfo;
  106. }
  107. return *this;
  108. }
  109. /* 清空内容 */
  110. void AlarmInfo::reInit()
  111. {
  112. Is_Alarm = false;
  113. AlarmID = 0;
  114. DeviceID = 0;
  115. RoomID = 0;
  116. ChannelID = 0;
  117. State = 0;
  118. OnWork = 0;
  119. StartTime = "";
  120. EndTime = "";
  121. EventTime = "";
  122. PicUrl = "";
  123. ImageInfo = "";
  124. AppID = "";
  125. ActionID = "";
  126. ActionDes = "";
  127. FaceIDList = "";
  128. FaceNameList = "";
  129. BboxList = "";
  130. }
  131. AlarmRuleInfo::AlarmRuleInfo()
  132. {
  133. LiveMinEnable = false;
  134. LiveMaxEnable = false;
  135. DicMinEnable = false;
  136. DicMaxEnable = false;
  137. LiveDicMinEnable = false;
  138. LiveDicMaxEnable = false;
  139. LiveMin = 0;
  140. LiveMax = 0;
  141. DicMin = 0;
  142. DicMax = 0;
  143. LiveDicMin = 0;
  144. LiveDicMax = 0;
  145. RuleName = "";
  146. }
  147. AlarmRuleInfo& AlarmRuleInfo::operator=(AlarmRuleInfo& other)
  148. {
  149. if(this != &other)
  150. {
  151. LiveMinEnable = other.LiveMinEnable;
  152. LiveMaxEnable = other.LiveMaxEnable;
  153. DicMinEnable = other.DicMinEnable;
  154. DicMaxEnable = other.DicMaxEnable;
  155. LiveDicMinEnable = other.LiveDicMinEnable;
  156. LiveDicMaxEnable = other.LiveDicMaxEnable;
  157. LiveMin = other.LiveMin;
  158. LiveMax = other.LiveMax;
  159. DicMin = other.DicMin;
  160. DicMax = other.DicMax;
  161. LiveDicMin = other.LiveDicMin;
  162. LiveDicMax = other.LiveDicMax;
  163. RuleName = other.RuleName;
  164. }
  165. return *this;
  166. }
  167. /* ====================================================================================
  168. * ************************** ListActionInfo成员函数 ****************************
  169. * ====================================================================================*/
  170. /* 对比除摄像机外的基础信息是否相等 */
  171. bool ActionInfo::isEqualBaseInfo(const ActionInfo& other)
  172. {
  173. if(ChannelID != other.ChannelID) {
  174. return false;
  175. }
  176. if(RoomID != other.RoomID) {
  177. return false;
  178. }
  179. if(ActionID != other.ActionID) {
  180. return false;
  181. }
  182. if(RoomType != other.RoomType) {
  183. return false;
  184. }
  185. return true;
  186. }
  187. /* 添加关联信息,会自动进行重复判断,如果已有相同的信息,则跳过 */
  188. bool ListActionInfo::insertActionInfo(ActionInfo* info)
  189. {
  190. /* 先判断是否已经在列表中了 */
  191. if(findActionInList(info) == nullptr)
  192. {
  193. ActionInfo* pActionInfo = new ActionInfo;
  194. *pActionInfo = *info;
  195. listActionInfo.push_back(pActionInfo);
  196. }
  197. return true;
  198. }
  199. /* 删除信息 */
  200. bool ListActionInfo::deleteActionInfo(ActionInfo* info)
  201. {
  202. if(info == nullptr)
  203. {
  204. return false;
  205. }
  206. if(findActionInList(info) != nullptr)
  207. {
  208. listActionInfo.remove(info);
  209. delete info;
  210. info = nullptr;
  211. }
  212. return true;
  213. }
  214. /* 给算法添加摄像机,原有的会被替换掉 */
  215. bool ListActionInfo::addActionCamera(ActionInfo* pInfo)
  216. {
  217. auto pActionInfo = findActionIDInListNoCamera(pInfo);
  218. if(pActionInfo != nullptr)
  219. {
  220. pActionInfo->CameraID = pInfo->CameraID;
  221. }
  222. return true;
  223. }
  224. /* 清空算法中的摄像机信息 */
  225. void ListActionInfo::clearCameraList()
  226. {
  227. for(auto& it0 : listActionInfo)
  228. {
  229. it0->CameraID = -1;
  230. }
  231. }
  232. /* 清空设置成STOP或ERROR的Action */
  233. void ListActionInfo::clearStopAction()
  234. {
  235. for(auto it0 = listActionInfo.begin(); it0 != listActionInfo.end();)
  236. {
  237. if(( (*it0)->RunState == RunTimeState::RUN_STATE_STOP) || ((*it0)->RunState == RunTimeState::RUN_STATE_ERROR))
  238. {
  239. delete *it0;
  240. it0 = listActionInfo.erase(it0);
  241. }else {
  242. ++it0;
  243. }
  244. }
  245. }
  246. /* 查找算法ID是否已在列表中 */
  247. ActionInfo* ListActionInfo::findActionInList(ActionInfo* pInfo)
  248. {
  249. for(const auto& it0 : listActionInfo)
  250. {
  251. if(*it0 == *pInfo)
  252. {
  253. return it0;
  254. }
  255. }
  256. return nullptr;
  257. }
  258. /* 查找算法ID是否在列表中,这里查找不会对比摄像机ID */
  259. ActionInfo* ListActionInfo::findActionIDInListNoCamera(ActionInfo* pInfo)
  260. {
  261. for(const auto& it0 : listActionInfo)
  262. {
  263. if(it0->isEqualBaseInfo(*pInfo))
  264. {
  265. return it0;
  266. }
  267. }
  268. return nullptr;
  269. }
  270. /* 清空容器 */
  271. void ListActionInfo::clear()
  272. {
  273. for(auto& it0 : listActionInfo)
  274. {
  275. delete it0;
  276. it0 = nullptr;
  277. }
  278. listActionInfo.clear();
  279. }
  280. /* ====================================================================================
  281. * *********************** ListRoomActionInfo成员函数 ***************************
  282. * ====================================================================================*/
  283. /* 对比频道信息、房间信息、算法ID是否相等 */
  284. bool RoomActionInfo::isEqualBaseInfo(const RoomActionInfo& other)
  285. {
  286. if(ChannelID != other.ChannelID) {
  287. return false;
  288. }
  289. if(RoomID != other.RoomID) {
  290. return false;
  291. }
  292. if(ActionID != other.ActionID) {
  293. return false;
  294. }
  295. if(RoomType != other.RoomType) {
  296. return false;
  297. }
  298. return true;
  299. }
  300. /* 添加关联信息,会自动进行重复判断,如果已有相同的room和action关联信息,则跳过 */
  301. bool ListRoomActionInfo::insertRoomActionInfo(const RoomActionInfo& info)
  302. {
  303. /* 先判断是否已经在列表中了 */
  304. if(findActionIDInList(info.ChannelID, info.RoomID, info.ActionID) == nullptr)
  305. {
  306. RoomActionInfo* pRoomActionInfo = new RoomActionInfo;
  307. *pRoomActionInfo = info;
  308. listRoomActionInfo.push_back(pRoomActionInfo);
  309. }
  310. return true;
  311. }
  312. /* 添加关联信息,会自动进行重复判断,如果已有相同的room和action关联信息,则跳过 */
  313. bool ListRoomActionInfo::insertRoomActionInfo(const int ChannelID, const int RoomID, const std::string& strActionID)
  314. {
  315. /* 先判断是否已经在列表中了 */
  316. if(findActionIDInList(ChannelID, RoomID, strActionID) == nullptr)
  317. {
  318. RoomActionInfo* pRoomActionInfo = new RoomActionInfo;
  319. pRoomActionInfo->RoomID = RoomID;
  320. pRoomActionInfo->ActionID = strActionID;
  321. listRoomActionInfo.push_back(pRoomActionInfo);
  322. }
  323. return true;
  324. }
  325. /* 删除一个容器,注意,这个不能在别的for循环中删除,只能单独删除 */
  326. bool ListRoomActionInfo::deleteRoomActionInfo(RoomActionInfo* pInfo)
  327. {
  328. if(pInfo == nullptr)
  329. {
  330. return false;
  331. }
  332. for(auto it0 = listRoomActionInfo.begin(); it0 != listRoomActionInfo.end(); ++it0)
  333. {
  334. if(*it0 == pInfo)
  335. {
  336. listRoomActionInfo.erase(it0);
  337. delete pInfo;
  338. pInfo = nullptr;
  339. return true;
  340. }
  341. }
  342. return false;
  343. }
  344. /* 清空容器 */
  345. void ListRoomActionInfo::clear()
  346. {
  347. for(auto& it0 : listRoomActionInfo)
  348. {
  349. delete it0;
  350. it0 = nullptr;
  351. }
  352. listRoomActionInfo.clear();
  353. }
  354. /* 添加算法信息,根据传入的算法信息,自动将其加入到对应的容器中 */
  355. void ListRoomActionInfo::addActionInfo(const ActionInfo& info)
  356. {
  357. auto pRAInfo = findActionIDInList(info.ChannelID, info.RoomID, info.ActionID);
  358. if(pRAInfo != nullptr)
  359. {
  360. pRAInfo->listCameraID.push_back(info.CameraID);
  361. }else {
  362. RoomActionInfo* pRoomActionInfo = new RoomActionInfo;
  363. pRoomActionInfo->ChannelID = info.ChannelID;
  364. pRoomActionInfo->RoomID = info.RoomID;
  365. pRoomActionInfo->ActionID = info.ActionID;
  366. pRoomActionInfo->RoomType = info.RoomType;
  367. pRoomActionInfo->strRoomName = info.strRoomName;
  368. pRoomActionInfo->listCameraID.push_back(info.CameraID);
  369. listRoomActionInfo.push_back(pRoomActionInfo);
  370. }
  371. }
  372. /* 清空算法对应的摄像机列表 */
  373. void ListRoomActionInfo::clearCameraList()
  374. {
  375. for(auto& it0 : listRoomActionInfo)
  376. {
  377. it0->listCameraID.clear();
  378. }
  379. }
  380. /* 清理设置为STOP或者ERROR的RoomAction */
  381. void ListRoomActionInfo::clearStopRoomAction()
  382. {
  383. for(auto it0 = listRoomActionInfo.begin(); it0 != listRoomActionInfo.end();)
  384. {
  385. if(( (*it0)->RunState == RunTimeState::RUN_STATE_STOP) || ((*it0)->RunState == RunTimeState::RUN_STATE_ERROR))
  386. {
  387. delete *it0;
  388. it0 = listRoomActionInfo.erase(it0);
  389. }else {
  390. ++it0;
  391. }
  392. }
  393. }
  394. /* 查找算法ID是否已在列表中 */
  395. RoomActionInfo* ListRoomActionInfo::findActionIDInList(const int chnID, const int RoomID, const std::string& strActionID)
  396. {
  397. for(const auto& it0 : listRoomActionInfo)
  398. {
  399. if((it0->RoomID == RoomID) && (it0->ActionID == strActionID) && (it0->ChannelID == chnID))
  400. {
  401. return it0;
  402. }
  403. }
  404. return nullptr;
  405. }
  406. /* ====================================================================================
  407. * ************************** GlobalConfig成员函数 ******************************
  408. * ====================================================================================*/
  409. /* 创建实例 */
  410. GlobalConfig g_config;
  411. GlobalConfig::GlobalConfig()
  412. {
  413. ThreadSleepMS = 300;
  414. }
  415. /* 读取配置文件 */
  416. bool GlobalConfig::readConfig(const QString& strConfigFile)
  417. {
  418. if(strConfigFile.isEmpty())
  419. {
  420. SPDLOG_ERROR("读取配置文件失败! 配置文件名为空");
  421. return false;
  422. }
  423. SPDLOG_DEBUG("读取配置文件: {}", strConfigFile.toStdString());
  424. QSettings settings(strConfigFile, QSettings::IniFormat);
  425. settings.setIniCodec("UTF-8");
  426. settings.beginGroup("System");
  427. AppPeopleOnWork = settings.value("APPPEPOLEONWORK", 300).toInt(); /* 离岗时间 */
  428. Contraband = settings.value("APPBADTHING", 50).toInt(); /* 违禁物品出现的时间 */
  429. AppBadMan = settings.value("APPBADMAN", 50).toInt(); /* 非法入侵 */
  430. AppTired = settings.value("APPTIRED", 50).toInt(); /* 疲劳检测时间 */
  431. AppPeopleCont = settings.value("APPPEPOLECONT", 50).toInt(); /* 人员聚集时间 */
  432. AppPlayPhone = settings.value("APPPLAYPHONE", 50).toInt(); /* 玩手机识别 */
  433. AppMouse = settings.value("APPMOUSE", 50).toInt(); /* 手势识别 */
  434. AppMask = settings.value("APPMASK", 5).toInt(); /* 戴口罩识别 */
  435. CheckSet = settings.value("CHECKSET", 300).toInt(); /* 服务端多久检测一次配置 */
  436. EventTimeValid = settings.value("EventTimeValid", 300).toInt(); /* 事件时间有效期 */
  437. Key = settings.value("Key").toString().toStdString(); /* Key */
  438. Secret = settings.value("Secret").toString().toStdString(); /* Secret */
  439. settings.endGroup();
  440. if(Key.empty() || Secret.empty())
  441. {
  442. SPDLOG_ERROR("读取配置文件失败! Key或Secret为空");
  443. return false;
  444. }
  445. return true;
  446. }
  447. /* 打印读取到的值 */
  448. void GlobalConfig::printValue()
  449. {
  450. SPDLOG_INFO("APPPEPOLEONWORK: {}", AppPeopleOnWork);
  451. SPDLOG_INFO("APPBADTHING: {}", Contraband);
  452. SPDLOG_INFO("APPBADMAN: {}", AppBadMan);
  453. SPDLOG_INFO("APPTIRED: {}", AppTired);
  454. SPDLOG_INFO("APPPEOPLECONT: {}", AppPeopleCont);
  455. SPDLOG_INFO("APPPLAYPHONE: {}", AppPlayPhone);
  456. SPDLOG_INFO("APPMOUSE: {}", AppMouse);
  457. SPDLOG_INFO("APPMASK: {}", AppMask);
  458. SPDLOG_INFO("CHECKSET: {}", CheckSet);
  459. SPDLOG_INFO("EventTimeValid: {}", EventTimeValid);
  460. }