GlobalInfo.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. #ifndef GLOBALINFO_H
  2. #define GLOBALINFO_H
  3. #include <QString>
  4. #include <vector>
  5. #include <list>
  6. #include <map>
  7. #include "nlohmann/json.hpp"
  8. /* ====================================================================================
  9. * ******************************* 全局变量定义 **********************************
  10. * ====================================================================================*/
  11. extern int g_eventTimeVaild;
  12. /* 线程运行时的状态 */
  13. enum class RunTimeState
  14. {
  15. RUN_STATE_INIT = 0, /* 初始化 */
  16. RUN_STATE_RUN, /* 运行 */
  17. RUN_STATE_STOP, /* 停止 */
  18. RUN_STATE_ERROR, /* 错误 */
  19. RUN_STATE_NONE /* 无状态 */
  20. };
  21. /* 应用功能 */
  22. enum class AppFunction
  23. {
  24. APP_NONE = 0, /* 无功能 */
  25. APP_ONWORK = 1, /* 人员在岗识别 */
  26. APP_CONTRABAND, /* 违禁品识别 */
  27. APP_ILLEGAL, /* 非法入侵检测 */
  28. APP_FATIGUE, /* 疲劳检测 */
  29. APP_REGIONAL, /* 区域人员检测 */
  30. APP_MOUSE, /* 老鼠识别 */
  31. APP_PLAYPHONE, /* 玩手机识别 */
  32. APP_NOMASK, /* 无口罩识别 */
  33. APP_ALLDOWN, /* 摔倒识别 */
  34. };
  35. /**
  36. * @brief 全局信息
  37. *
  38. */
  39. using nJson = nlohmann::json;
  40. /* 算法相关信息 */
  41. struct AlgorithmInfo
  42. {
  43. int PKID; /* 主键ID */
  44. int ActionTaskID; /* 算法任务ID */
  45. std::string ActionID; /* 算法ID */
  46. std::string ActionName; /* 算法名称 */
  47. AlgorithmInfo() = default;
  48. AlgorithmInfo(const AlgorithmInfo& other)
  49. : PKID(other.PKID), ActionID(other.ActionID), ActionName(other.ActionName), ActionTaskID(other.ActionTaskID) {}
  50. AlgorithmInfo& operator=(const AlgorithmInfo& other) {
  51. if (this != &other) {
  52. ActionID = other.ActionID;
  53. ActionName = other.ActionName;
  54. ActionTaskID = other.ActionTaskID;
  55. PKID = other.PKID;
  56. }
  57. return *this;
  58. }
  59. /* 对比是否相等 */
  60. bool operator==(const AlgorithmInfo& other) const {
  61. if(ActionID != other.ActionID) {
  62. return false;
  63. }
  64. if(ActionName != other.ActionName || ActionTaskID != other.ActionTaskID) {
  65. return false;
  66. }
  67. return true;
  68. }
  69. };
  70. /* 设备列表 */
  71. struct DeviceInfo
  72. {
  73. int PKID; /* 主键ID */
  74. int DeviceID; /* 设备ID */
  75. int DevicePort; /* 设备端口 */
  76. std::string DeviceName; /* 设备名称 */
  77. std::string DeviceIP; /* 设备IP */
  78. std::string DeviceSerial; /* 设备序列号 */
  79. std::string DeviceType; /* 设备类型 */
  80. std::string UserAccount; /* 用户账号 */
  81. std::string UserPassword; /* 用户密码 */
  82. std::vector<AlgorithmInfo> vecAlgorithmInfo; /* 算法信息 */
  83. DeviceInfo() = default;
  84. DeviceInfo(const DeviceInfo& other)
  85. : PKID(other.PKID),
  86. DeviceID(other.DeviceID),
  87. DevicePort(other.DevicePort),
  88. DeviceName(other.DeviceName),
  89. DeviceSerial(other.DeviceSerial),
  90. DeviceType(other.DeviceType),
  91. DeviceIP(other.DeviceIP),
  92. UserAccount(other.UserAccount),
  93. UserPassword(other.UserPassword),
  94. vecAlgorithmInfo(other.vecAlgorithmInfo) {}
  95. DeviceInfo& operator=(const DeviceInfo& other) {
  96. if (this != &other) {
  97. DeviceID = other.DeviceID;
  98. DevicePort = other.DevicePort;
  99. DeviceName = other.DeviceName;
  100. DeviceSerial = other.DeviceSerial;
  101. DeviceType = other.DeviceType;
  102. DeviceIP = other.DeviceIP;
  103. UserAccount = other.UserAccount;
  104. UserPassword = other.UserPassword;
  105. vecAlgorithmInfo = other.vecAlgorithmInfo;
  106. }
  107. return *this;
  108. }
  109. /* 对比是否相等 */
  110. bool operator==(const DeviceInfo& other);
  111. /* 对比设备关联的算法信息是否相等 */
  112. bool isEqualAlgorithmInfo(const std::vector<AlgorithmInfo>& other);
  113. };
  114. /* 算法和设备关联的相关信息 */
  115. struct DeviceAlgorithmInfo
  116. {
  117. int DeviceID; /* 设备ID */
  118. int ActionTaskID; /* 算法任务ID */
  119. std::string ActionID; /* 算法ID */
  120. std::string ActionName; /* 算法名称 */
  121. };
  122. /**
  123. * @brief 摄像机线程需要的信息
  124. *
  125. */
  126. struct CameraThreadInfo
  127. {
  128. std::string RedisIP; /* Redis IP */
  129. int RedisPort; /* Redis Port */
  130. std::string RedisPWD; /* Redis Password */
  131. int DeviceID; /* 设备ID */
  132. std::vector<std::string> vecAction; /* Redis Key,一个设备可能会有多个算法ID */
  133. CameraThreadInfo() = default;
  134. CameraThreadInfo& operator=(const CameraThreadInfo& other) {
  135. if (this != &other) {
  136. RedisIP = other.RedisIP;
  137. RedisPort = other.RedisPort;
  138. RedisPWD = other.RedisPWD;
  139. DeviceID = other.DeviceID;
  140. vecAction = other.vecAction;
  141. }
  142. return *this;
  143. }
  144. };
  145. /**
  146. * @brief 人员信息
  147. *
  148. */
  149. struct PersonInfo
  150. {
  151. std::string PersonID; /* 人员ID */
  152. std::string PersonName; /* 人员名称 */
  153. };
  154. /**
  155. * @brief 报警信息
  156. *
  157. */
  158. struct AlarmInfo
  159. {
  160. bool Is_Alarm; /* 是否报警 */
  161. int AlarmID; /* 报警ID */
  162. int DeviceID; /* 设备ID,数据库表格中对应的是CamerID */
  163. int RoomID; /* 房间ID */
  164. int ChannelID; /* 通道ID */
  165. int State; /* 状态 */
  166. int OnWork; /* 是否在工作 */
  167. std::string ActionID; /* 算法ID */
  168. std::string StartTime; /* 报警开始时间 */
  169. std::string EndTime; /* 报警结束时间 */
  170. std::string EventTime; /* 事件时间(报警发生时间,在数据库里对应的是CreateTime) */
  171. std::string PicUrl; /* 报警图片URL */
  172. std::string ImageInfo; /* 图片信息 */
  173. std::string AppID; /* 客户端应用ID */
  174. std::string ActionDes; /* 算法描述信息 */
  175. std::string FaceIDList; /* 人脸ID列表 */
  176. std::string FaceNameList; /* 人脸名称列表 */
  177. std::string BboxList; /* Bbox列表,应该是图片中的报警位置 */
  178. std::vector<PersonInfo> vecPersonInfo; /* 人员信息 */
  179. AlarmInfo();
  180. AlarmInfo& operator=(AlarmInfo& other);
  181. void reInit();
  182. };
  183. /**
  184. * @brief 报警规则
  185. *
  186. */
  187. struct AlarmRuleInfo
  188. {
  189. bool LiveMinEnable; /* 启用直播间最小人数 */
  190. bool LiveMaxEnable; /* 启用直播间最大人数 */
  191. bool DicMinEnable; /* 启用导播间最小人数 */
  192. bool DicMaxEnable; /* 启用导播间最大人数 */
  193. bool LiveDicMinEnable; /* 启用直播间和导播间最小人数 */
  194. bool LiveDicMaxEnable; /* 启用直播间和导播间最大人数 */
  195. int LiveMin; /* 直播间最小人数 */
  196. int LiveMax; /* 直播间最大人数 */
  197. int DicMin; /* 导播间最小人数 */
  198. int DicMax; /* 导播间最大人数 */
  199. int LiveDicMin; /* 直播间和导播间最小人数 */
  200. int LiveDicMax; /* 直播间和导播间最大人数 */
  201. std::string RuleName; /* 规则名称 */
  202. AlarmRuleInfo();
  203. AlarmRuleInfo& operator=(AlarmRuleInfo& other);
  204. };
  205. /**
  206. * @brief 房间和摄像机关联信息,这个是从客户端配置的
  207. * 一个房间对应多个摄像机
  208. *
  209. */
  210. struct RoomCameraInfo
  211. {
  212. int RoomID; /* 房间ID */
  213. std::list<int> listCameraID; /* 摄像机列表 */
  214. RoomCameraInfo() = default;
  215. RoomCameraInfo& operator=(const RoomCameraInfo& other) {
  216. if (this != &other) {
  217. RoomID = other.RoomID;
  218. listCameraID = other.listCameraID;
  219. }
  220. return *this;
  221. }
  222. /* 这里只对比RoomID */
  223. bool operator==(const RoomCameraInfo& other) const {
  224. if(RoomID != other.RoomID) {
  225. return false;
  226. }
  227. return true;
  228. }
  229. };
  230. /* ====================================================================================
  231. * **************************** 线程功能需要的结构体 *****************************
  232. * ====================================================================================*/
  233. /**
  234. * @brief 单个算法信息,包括包含的摄像机,房间、频率信息
  235. *
  236. */
  237. struct ActionInfo
  238. {
  239. RunTimeState RunState; /* 线程运行状态 */
  240. int ChannelID; /* 通道ID */
  241. int RoomID; /* 房间ID */
  242. int CameraID; /* 摄像机ID */
  243. int RoomType; /* 房间类型 */
  244. std::string ActionID; /* 算法ID */
  245. std::string strRoomName; /* 房间名称 */
  246. std::string strActionName; /* 算法名称,这个是自定义的,不需要对比 */
  247. ActionInfo();
  248. ActionInfo& operator=(const ActionInfo& other);
  249. /* 对比是否相等,这个需要完全相等,包括算法信息和摄像机信息 */
  250. bool operator==(const ActionInfo& other);
  251. /* 对比除摄像机外的基础信息是否相等 */
  252. bool isEqualBaseInfo(const ActionInfo& other);
  253. };
  254. /**
  255. * @brief 算法信息列表,这个列表既可以存储房间和算法ID的关联信息,也可以用来存储运行线程的信息
  256. *
  257. */
  258. class ListActionInfo
  259. {
  260. public:
  261. ListActionInfo() = default;
  262. /* 添加关联信息,会自动进行重复判断,如果已有相同的信息,则跳过 */
  263. bool insertActionInfo(ActionInfo* info);
  264. /* 删除信息 */
  265. bool deleteActionInfo(ActionInfo* info);
  266. /* 给算法添加摄像机,原有的会被替换掉 */
  267. bool addActionCamera(ActionInfo* pInfo);
  268. /* 清空算法中的摄像机信息 */
  269. void clearCameraList();
  270. /* 清空设置成STOP或ERROR的Action */
  271. void clearStopAction();
  272. /* 查找算法ID是否已在列表中 */
  273. ActionInfo* findActionInList(ActionInfo* pInfo);
  274. /* 查找算法ID是否在列表中,这里查找不会对比摄像机ID */
  275. ActionInfo* findActionIDInListNoCamera(ActionInfo* pInfo);
  276. /* 获取容器 */
  277. std::list<ActionInfo*>& getData() {
  278. return listActionInfo;
  279. }
  280. /* 清空容器 */
  281. void clear();
  282. std::list<ActionInfo*> listActionInfo; /* 房间和算法ID关联列表 */
  283. };
  284. /**
  285. * @brief 算法信息,以房间分类,算法包含在这干房间内所需要的摄像机ID
  286. *
  287. */
  288. struct RoomActionInfo
  289. {
  290. RunTimeState RunState; /* 线程运行状态 */
  291. int ChannelID; /* 通道ID */
  292. int RoomID; /* 房间ID */
  293. int RoomType; /* 房间类型 */
  294. std::string ActionID; /* 算法ID */
  295. std::string strRoomName; /* 房间名称 */
  296. std::list<int> listCameraID; /* 摄像机ID */
  297. RoomActionInfo() {
  298. RunState = RunTimeState::RUN_STATE_INIT;
  299. ChannelID = -1;
  300. RoomID = -1;
  301. RoomType = -1;
  302. ActionID = "";
  303. strRoomName = "";
  304. listCameraID.clear();
  305. }
  306. RoomActionInfo(const RoomActionInfo& other)
  307. : ChannelID(other.ChannelID), RoomID(other.RoomID),
  308. RoomType(other.RoomType), ActionID(other.ActionID),
  309. strRoomName(other.strRoomName), listCameraID(other.listCameraID) {}
  310. RoomActionInfo& operator=(const RoomActionInfo& other) {
  311. if (this != &other) {
  312. ChannelID = other.ChannelID;
  313. RoomID = other.RoomID;
  314. RoomType = other.RoomType;
  315. ActionID = other.ActionID;
  316. strRoomName = other.strRoomName;
  317. listCameraID = other.listCameraID;
  318. }
  319. return *this;
  320. }
  321. /* 对比频道信息、房间信息、算法ID是否相等 */
  322. bool isEqualBaseInfo(const RoomActionInfo& other);
  323. };
  324. /* 房间和算法ID关联列表 */
  325. class ListRoomActionInfo
  326. {
  327. public:
  328. ListRoomActionInfo() = default;
  329. /* 添加关联信息,会自动进行重复判断,如果已有相同的room和action关联信息,则跳过 */
  330. bool insertRoomActionInfo(const RoomActionInfo& info);
  331. /* 添加关联信息,会自动进行重复判断,如果已有相同的room和action关联信息,则跳过 */
  332. bool insertRoomActionInfo(const int ChannelID, const int RoomID, const std::string& strActionID);
  333. /* 删除一个容器,注意,这个不能在别的for循环中删除,只能单独删除 */
  334. bool deleteRoomActionInfo(RoomActionInfo* pInfo);
  335. /* 清空容器 */
  336. void clear();
  337. /* 添加算法信息,根据传入的算法信息,自动将其加入到对应的容器中 */
  338. void addActionInfo(const ActionInfo& info);
  339. /* 清空算法对应的摄像机列表 */
  340. void clearCameraList();
  341. /* 清理设置为STOP或者ERROR的RoomAction */
  342. void clearStopRoomAction();
  343. /* 查找算法ID是否已在列表中 */
  344. RoomActionInfo* findActionIDInList(const int chnID, const int RoomID, const std::string& strActionID);
  345. /* 获取容器 */
  346. std::list<RoomActionInfo*>& getData() {
  347. return listRoomActionInfo;
  348. }
  349. std::list<RoomActionInfo*> listRoomActionInfo; /* 房间和算法ID关联列表 */
  350. };
  351. /**
  352. * @brief 房间内的摄像机和算法关联信息
  353. *
  354. */
  355. struct RoomCamActInfo
  356. {
  357. int RoomID; /* 房间ID */
  358. int RoomType; /* 房间类型 */
  359. std::multimap<int, std::string> mapCameraAction; /* 摄像机ID和算法ID关联信息 */
  360. };
  361. /**
  362. * @brief 按照功能分类的线程信息,一个实例就是一个功能,
  363. 比如区域入侵检测,需要多个算法多个摄像机共同配合
  364. *
  365. */
  366. struct FuncActionInfo
  367. {
  368. int ChannelID; /* 通道ID */
  369. AppFunction appFunction; /* 任务功能 */
  370. RunTimeState RunState; /* 线程运行状态 */
  371. std::string strFunctionName; /* 功能名称 */
  372. std::chrono::system_clock::time_point StartTime; /* 开始时间 */
  373. std::chrono::system_clock::time_point EndTime; /* 结束时间 */
  374. std::list<RoomCamActInfo> listRoomCamActInfo; /* 房间内的摄像机和算法关联信息 */
  375. FuncActionInfo();
  376. FuncActionInfo& operator=(FuncActionInfo& other);
  377. /* 添加算法信息 */
  378. bool addActionInfo(const ActionInfo& info);
  379. };
  380. /**
  381. * @brief
  382. *
  383. */
  384. struct ListFuncActInfo
  385. {
  386. ListFuncActInfo() = default;
  387. /* 添加功能信息 */
  388. bool addFuncActionInfo(FuncActionInfo* pInfo);
  389. /* 添加算法信息 */
  390. bool addActionInfo(const ActionInfo& info);
  391. /* 清空无用的功能信息 */
  392. void clearNoneFuncActionInfo();
  393. /* 查找功能信息 */
  394. FuncActionInfo* findFuncActionInfo(const std::string& strFunctionName);
  395. /* 获取容器 */
  396. std::list<FuncActionInfo*>& getData() {
  397. return listFuncActionInfo;
  398. }
  399. std::list<FuncActionInfo*> listFuncActionInfo; /* 功能信息列表 */
  400. };
  401. /**
  402. * @brief 读取配置文件
  403. *
  404. */
  405. class GlobalConfig
  406. {
  407. public:
  408. GlobalConfig();
  409. ~GlobalConfig() = default;
  410. /* 读取配置文件 */
  411. bool readConfig(const QString& strConfigFile);
  412. /* 打印读取到的值 */
  413. void printValue();
  414. int AppPeopleOnWork; /* 离岗时间 */
  415. int Contraband; /* 违禁物品出现的时间 */
  416. int AppBadMan; /* 非法入侵 */
  417. int AppTired; /* 疲劳检测时间 */
  418. int AppPeopleCont; /* 区域人员统计 */
  419. int AppPlayPhone; /* 玩手机识别 */
  420. int AppMouse; /* 老鼠识别 */
  421. int AppMask; /* 戴口罩识别 */
  422. int CheckSet; /* 服务端多久检测一次配置 */
  423. int EventTimeValid; /* 事件时间有效期 */
  424. int ThreadSleepMS; /* 任务线程休眠时间,单位是ms */
  425. std::string Key; /* Key */
  426. std::string Secret; /* Secret */
  427. };
  428. extern GlobalConfig g_config;
  429. #endif /* GLOBALINFO_H */