GlobalInfo.h 17 KB

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