GlobalInfo.h 18 KB

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