GlobalInfo.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. * ************************** ListActionInfo成员函数 ****************************
  61. * ====================================================================================*/
  62. /* 对比除摄像机外的基础信息是否相等 */
  63. bool ActionInfo::isEqualBaseInfo(const ActionInfo& other)
  64. {
  65. if(ChannelID != other.ChannelID) {
  66. return false;
  67. }
  68. if(RoomID != other.RoomID) {
  69. return false;
  70. }
  71. if(ActionID != other.ActionID) {
  72. return false;
  73. }
  74. if(RoomType != other.RoomType) {
  75. return false;
  76. }
  77. return true;
  78. }
  79. /* 添加关联信息,会自动进行重复判断,如果已有相同的信息,则跳过 */
  80. bool ListActionInfo::insertActionInfo(ActionInfo* info)
  81. {
  82. /* 先判断是否已经在列表中了 */
  83. if(findActionInList(info) == nullptr)
  84. {
  85. ActionInfo* pActionInfo = new ActionInfo;
  86. *pActionInfo = *info;
  87. listActionInfo.push_back(pActionInfo);
  88. }
  89. return true;
  90. }
  91. /* 删除信息 */
  92. bool ListActionInfo::deleteActionInfo(ActionInfo* info)
  93. {
  94. if(info == nullptr)
  95. {
  96. return false;
  97. }
  98. if(findActionInList(info) != nullptr)
  99. {
  100. listActionInfo.remove(info);
  101. delete info;
  102. info = nullptr;
  103. }
  104. return true;
  105. }
  106. /* 给算法添加摄像机,原有的会被替换掉 */
  107. bool ListActionInfo::addActionCamera(ActionInfo* pInfo)
  108. {
  109. auto pActionInfo = findActionIDInListNoCamera(pInfo);
  110. if(pActionInfo != nullptr)
  111. {
  112. pActionInfo->CameraID = pInfo->CameraID;
  113. }
  114. return true;
  115. }
  116. /* 清空算法中的摄像机信息 */
  117. void ListActionInfo::clearCameraList()
  118. {
  119. for(auto& it0 : listActionInfo)
  120. {
  121. it0->CameraID = -1;
  122. }
  123. }
  124. /* 清空设置成STOP或ERROR的Action */
  125. void ListActionInfo::clearStopAction()
  126. {
  127. for(auto it0 = listActionInfo.begin(); it0 != listActionInfo.end();)
  128. {
  129. if(( (*it0)->RunState == RunTimeState::RUN_STATE_STOP) || ((*it0)->RunState == RunTimeState::RUN_STATE_ERROR))
  130. {
  131. delete *it0;
  132. it0 = listActionInfo.erase(it0);
  133. }else {
  134. ++it0;
  135. }
  136. }
  137. }
  138. /* 查找算法ID是否已在列表中 */
  139. ActionInfo* ListActionInfo::findActionInList(ActionInfo* pInfo)
  140. {
  141. for(const auto& it0 : listActionInfo)
  142. {
  143. if(*it0 == *pInfo)
  144. {
  145. return it0;
  146. }
  147. }
  148. return nullptr;
  149. }
  150. /* 查找算法ID是否在列表中,这里查找不会对比摄像机ID */
  151. ActionInfo* ListActionInfo::findActionIDInListNoCamera(ActionInfo* pInfo)
  152. {
  153. for(const auto& it0 : listActionInfo)
  154. {
  155. if(it0->isEqualBaseInfo(*pInfo))
  156. {
  157. return it0;
  158. }
  159. }
  160. return nullptr;
  161. }
  162. /* 清空容器 */
  163. void ListActionInfo::clear()
  164. {
  165. for(auto& it0 : listActionInfo)
  166. {
  167. delete it0;
  168. it0 = nullptr;
  169. }
  170. listActionInfo.clear();
  171. }
  172. /* ====================================================================================
  173. * *********************** ListRoomActionInfo成员函数 ***************************
  174. * ====================================================================================*/
  175. /* 添加关联信息,会自动进行重复判断,如果已有相同的room和action关联信息,则跳过 */
  176. bool ListRoomActionInfo::insertRoomActionInfo(const RoomActionInfo& info)
  177. {
  178. /* 先判断是否已经在列表中了 */
  179. if(findActionIDInList(info.ChannelID, info.RoomID, info.ActionID) == nullptr)
  180. {
  181. RoomActionInfo* pRoomActionInfo = new RoomActionInfo;
  182. *pRoomActionInfo = info;
  183. listRoomActionInfo.push_back(pRoomActionInfo);
  184. }
  185. return true;
  186. }
  187. /* 添加关联信息,会自动进行重复判断,如果已有相同的room和action关联信息,则跳过 */
  188. bool ListRoomActionInfo::insertRoomActionInfo(const int ChannelID, const int RoomID, const std::string& strActionID)
  189. {
  190. /* 先判断是否已经在列表中了 */
  191. if(findActionIDInList(ChannelID, RoomID, strActionID) == nullptr)
  192. {
  193. RoomActionInfo* pRoomActionInfo = new RoomActionInfo;
  194. pRoomActionInfo->RoomID = RoomID;
  195. pRoomActionInfo->ActionID = strActionID;
  196. listRoomActionInfo.push_back(pRoomActionInfo);
  197. }
  198. return true;
  199. }
  200. /* 删除一个容器,注意,这个不能在别的for循环中删除,只能单独删除 */
  201. bool ListRoomActionInfo::deleteRoomActionInfo(RoomActionInfo* pInfo)
  202. {
  203. if(pInfo == nullptr)
  204. {
  205. return false;
  206. }
  207. for(auto it0 = listRoomActionInfo.begin(); it0 != listRoomActionInfo.end(); ++it0)
  208. {
  209. if(*it0 == pInfo)
  210. {
  211. listRoomActionInfo.erase(it0);
  212. delete pInfo;
  213. pInfo = nullptr;
  214. return true;
  215. }
  216. }
  217. return false;
  218. }
  219. /* 清空容器 */
  220. void ListRoomActionInfo::clear()
  221. {
  222. for(auto& it0 : listRoomActionInfo)
  223. {
  224. delete it0;
  225. it0 = nullptr;
  226. }
  227. listRoomActionInfo.clear();
  228. }
  229. /* 添加算法信息,根据传入的算法信息,自动将其加入到对应的容器中 */
  230. void ListRoomActionInfo::addActionInfo(const ActionInfo& info)
  231. {
  232. auto pRAInfo = findActionIDInList(info.ChannelID, info.RoomID, info.ActionID);
  233. if(pRAInfo != nullptr)
  234. {
  235. pRAInfo->listCameraID.push_back(info.CameraID);
  236. }else {
  237. RoomActionInfo* pRoomActionInfo = new RoomActionInfo;
  238. pRoomActionInfo->ChannelID = info.ChannelID;
  239. pRoomActionInfo->RoomID = info.RoomID;
  240. pRoomActionInfo->ActionID = info.ActionID;
  241. pRoomActionInfo->RoomType = info.RoomType;
  242. pRoomActionInfo->strRoomName = info.strRoomName;
  243. pRoomActionInfo->listCameraID.push_back(info.CameraID);
  244. listRoomActionInfo.push_back(pRoomActionInfo);
  245. }
  246. }
  247. /* 清空算法对应的摄像机列表 */
  248. void ListRoomActionInfo::clearCameraList()
  249. {
  250. for(auto& it0 : listRoomActionInfo)
  251. {
  252. it0->listCameraID.clear();
  253. }
  254. }
  255. /* 清理设置为STOP或者ERROR的RoomAction */
  256. void ListRoomActionInfo::clearStopRoomAction()
  257. {
  258. for(auto it0 = listRoomActionInfo.begin(); it0 != listRoomActionInfo.end();)
  259. {
  260. if(( (*it0)->RunState == RunTimeState::RUN_STATE_STOP) || ((*it0)->RunState == RunTimeState::RUN_STATE_ERROR))
  261. {
  262. delete *it0;
  263. it0 = listRoomActionInfo.erase(it0);
  264. }else {
  265. ++it0;
  266. }
  267. }
  268. }
  269. /* 查找算法ID是否已在列表中 */
  270. RoomActionInfo* ListRoomActionInfo::findActionIDInList(const int chnID, const int RoomID, const std::string& strActionID)
  271. {
  272. for(const auto& it0 : listRoomActionInfo)
  273. {
  274. if((it0->RoomID == RoomID) && (it0->ActionID == strActionID) && (it0->ChannelID == chnID))
  275. {
  276. return it0;
  277. }
  278. }
  279. return nullptr;
  280. }
  281. /* ====================================================================================
  282. * ************************** GlobalConfig成员函数 ******************************
  283. * ====================================================================================*/
  284. /* 创建实例 */
  285. GlobalConfig g_config;
  286. GlobalConfig::GlobalConfig()
  287. {
  288. }
  289. /* 读取配置文件 */
  290. bool GlobalConfig::readConfig(const QString& strConfigFile)
  291. {
  292. if(strConfigFile.isEmpty())
  293. {
  294. SPDLOG_ERROR("读取配置文件失败! 配置文件名为空");
  295. return false;
  296. }
  297. SPDLOG_DEBUG("读取配置文件: {}", strConfigFile.toStdString());
  298. QSettings settings(strConfigFile, QSettings::IniFormat);
  299. settings.setIniCodec("UTF-8");
  300. settings.beginGroup("System");
  301. AppPeopleOnWork = settings.value("APPPEPOLEONWORK", 300).toInt(); /* 离岗时间 */
  302. AppBadthing = settings.value("APPBADTHING", 50).toInt(); /* 违禁物品出现的时间 */
  303. AppBadMan = settings.value("APPBADMAN", 50).toInt(); /* 非法入侵 */
  304. AppTired = settings.value("APPTIRED", 50).toInt(); /* 疲劳检测时间 */
  305. AppPeopleCont = settings.value("APPPEPOLECONT", 50).toInt(); /* 人员聚集时间 */
  306. AppPlayPhone = settings.value("APPPLAYPHONE", 50).toInt(); /* 玩手机识别 */
  307. AppMouse = settings.value("APPMOUSE", 50).toInt(); /* 手势识别 */
  308. AppMask = settings.value("APPMASK", 5).toInt(); /* 戴口罩识别 */
  309. CheckSet = settings.value("CHECKSET", 300).toInt(); /* 服务端多久检测一次配置 */
  310. EventTimeValid = settings.value("EventTimeValid", 300).toInt(); /* 事件时间有效期 */
  311. Key = settings.value("Key").toString().toStdString(); /* Key */
  312. Secret = settings.value("Secret").toString().toStdString(); /* Secret */
  313. settings.endGroup();
  314. if(Key.empty() || Secret.empty())
  315. {
  316. SPDLOG_ERROR("读取配置文件失败! Key或Secret为空");
  317. return false;
  318. }
  319. return true;
  320. }
  321. /* 打印读取到的值 */
  322. void GlobalConfig::printValue()
  323. {
  324. SPDLOG_INFO("APPPEPOLEONWORK: {}", AppPeopleOnWork);
  325. SPDLOG_INFO("APPBADTHING: {}", AppBadthing);
  326. SPDLOG_INFO("APPBADMAN: {}", AppBadMan);
  327. SPDLOG_INFO("APPTIRED: {}", AppTired);
  328. SPDLOG_INFO("APPPEOPLECONT: {}", AppPeopleCont);
  329. SPDLOG_INFO("APPPLAYPHONE: {}", AppPlayPhone);
  330. SPDLOG_INFO("APPMOUSE: {}", AppMouse);
  331. SPDLOG_INFO("APPMASK: {}", AppMask);
  332. SPDLOG_INFO("CHECKSET: {}", CheckSet);
  333. SPDLOG_INFO("EventTimeValid: {}", EventTimeValid);
  334. }