GlobalInfo.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 RoomFaceInfo
  220. {
  221. int ChannelID = 0;
  222. int RoomID = 0;
  223. int MaxNum = 0;
  224. int MinNum = 0;
  225. QDateTime StartTime;
  226. QDateTime EndTime;
  227. std::list<PersonInfo> listPersonInfo;
  228. RoomFaceInfo() = default;
  229. RoomFaceInfo& operator=(const RoomFaceInfo& other) {
  230. if (this != &other) {
  231. ChannelID = other.ChannelID;
  232. RoomID = other.RoomID;
  233. MaxNum = other.MaxNum;
  234. MinNum = other.MinNum;
  235. StartTime = other.StartTime;
  236. EndTime = other.EndTime;
  237. listPersonInfo = other.listPersonInfo;
  238. }
  239. return *this;
  240. }
  241. /* 查找是否有相同的人脸信息 */
  242. bool findPersonInfo(const PersonInfo& info);
  243. };
  244. /**
  245. * @brief 房间内的人脸信息
  246. *
  247. */
  248. struct ListRoomFaceInfo
  249. {
  250. std::list<RoomFaceInfo> listRoomFaceInfo;
  251. void addRoomFaceInfo(RoomFaceInfo& info);
  252. void addRoomFaceInfo(AlarmInfo& info);
  253. RoomFaceInfo* findRoomFaceInfo(RoomFaceInfo& info);
  254. RoomFaceInfo* findRoomFaceInfo(int ChannelID, int RoomID);
  255. };
  256. /**
  257. * @brief 报警规则
  258. *
  259. */
  260. struct AlarmRuleInfo
  261. {
  262. bool LiveMinEnable; /* 启用直播间最小人数 */
  263. bool LiveMaxEnable; /* 启用直播间最大人数 */
  264. bool DicMinEnable; /* 启用导播间最小人数 */
  265. bool DicMaxEnable; /* 启用导播间最大人数 */
  266. bool LiveDicMinEnable; /* 启用直播间和导播间最小人数 */
  267. bool LiveDicMaxEnable; /* 启用直播间和导播间最大人数 */
  268. int LiveMin; /* 直播间最小人数 */
  269. int LiveMax; /* 直播间最大人数 */
  270. int DicMin; /* 导播间最小人数 */
  271. int DicMax; /* 导播间最大人数 */
  272. int LiveDicMin; /* 直播间和导播间最小人数 */
  273. int LiveDicMax; /* 直播间和导播间最大人数 */
  274. std::string RuleName; /* 规则名称 */
  275. AlarmRuleInfo();
  276. AlarmRuleInfo& operator=(AlarmRuleInfo& other);
  277. };
  278. /**
  279. * @brief 房间和摄像机关联信息,这个是从客户端配置的
  280. * 一个房间对应多个摄像机
  281. *
  282. */
  283. struct RoomCameraInfo
  284. {
  285. int RoomID; /* 房间ID */
  286. std::list<int> listCameraID; /* 摄像机列表 */
  287. RoomCameraInfo() = default;
  288. RoomCameraInfo& operator=(const RoomCameraInfo& other) {
  289. if (this != &other) {
  290. RoomID = other.RoomID;
  291. listCameraID = other.listCameraID;
  292. }
  293. return *this;
  294. }
  295. /* 这里只对比RoomID */
  296. bool operator==(const RoomCameraInfo& other) const {
  297. if(RoomID != other.RoomID) {
  298. return false;
  299. }
  300. return true;
  301. }
  302. };
  303. /* ====================================================================================
  304. * **************************** 线程功能需要的结构体 *****************************
  305. * ====================================================================================*/
  306. /**
  307. * @brief 单个算法信息,包括包含的摄像机,房间、频率信息
  308. *
  309. */
  310. struct ActionInfo
  311. {
  312. RunTimeState RunState; /* 线程运行状态 */
  313. int ChannelID; /* 通道ID */
  314. int RoomID; /* 房间ID */
  315. int CameraID; /* 摄像机ID */
  316. int RoomType; /* 房间类型 */
  317. std::string ActionID; /* 算法ID */
  318. std::string strRoomName; /* 房间名称 */
  319. std::string strActionName; /* 算法名称,这个是自定义的,不需要对比 */
  320. ActionInfo();
  321. ActionInfo& operator=(const ActionInfo& other);
  322. /* 对比是否相等,这个需要完全相等,包括算法信息和摄像机信息 */
  323. bool operator==(const ActionInfo& other);
  324. /* 对比除摄像机外的基础信息是否相等 */
  325. bool isEqualBaseInfo(const ActionInfo& other);
  326. };
  327. /**
  328. * @brief 算法信息列表,这个列表既可以存储房间和算法ID的关联信息,也可以用来存储运行线程的信息
  329. *
  330. */
  331. class ListActionInfo
  332. {
  333. public:
  334. ListActionInfo() = default;
  335. /* 添加关联信息,会自动进行重复判断,如果已有相同的信息,则跳过 */
  336. bool insertActionInfo(ActionInfo* info);
  337. /* 删除信息 */
  338. bool deleteActionInfo(ActionInfo* info);
  339. /* 给算法添加摄像机,原有的会被替换掉 */
  340. bool addActionCamera(ActionInfo* pInfo);
  341. /* 清空算法中的摄像机信息 */
  342. void clearCameraList();
  343. /* 清空设置成STOP或ERROR的Action */
  344. void clearStopAction();
  345. /* 查找算法ID是否已在列表中 */
  346. ActionInfo* findActionInList(ActionInfo* pInfo);
  347. /* 查找算法ID是否在列表中,这里查找不会对比摄像机ID */
  348. ActionInfo* findActionIDInListNoCamera(ActionInfo* pInfo);
  349. /* 获取容器 */
  350. std::list<ActionInfo*>& getData() {
  351. return listActionInfo;
  352. }
  353. /* 清空容器 */
  354. void clear();
  355. std::list<ActionInfo*> listActionInfo; /* 房间和算法ID关联列表 */
  356. };
  357. /**
  358. * @brief 算法信息,以房间分类,算法包含在这干房间内所需要的摄像机ID
  359. *
  360. */
  361. struct RoomActionInfo
  362. {
  363. RunTimeState RunState; /* 线程运行状态 */
  364. int ChannelID; /* 通道ID */
  365. int RoomID; /* 房间ID */
  366. int RoomType; /* 房间类型 */
  367. std::string ActionID; /* 算法ID */
  368. std::string strRoomName; /* 房间名称 */
  369. std::list<int> listCameraID; /* 摄像机ID */
  370. RoomActionInfo() {
  371. RunState = RunTimeState::RUN_STATE_INIT;
  372. ChannelID = -1;
  373. RoomID = -1;
  374. RoomType = -1;
  375. ActionID = "";
  376. strRoomName = "";
  377. listCameraID.clear();
  378. }
  379. RoomActionInfo(const RoomActionInfo& other)
  380. : ChannelID(other.ChannelID), RoomID(other.RoomID),
  381. RoomType(other.RoomType), ActionID(other.ActionID),
  382. strRoomName(other.strRoomName), listCameraID(other.listCameraID) {}
  383. RoomActionInfo& operator=(const RoomActionInfo& other) {
  384. if (this != &other) {
  385. ChannelID = other.ChannelID;
  386. RoomID = other.RoomID;
  387. RoomType = other.RoomType;
  388. ActionID = other.ActionID;
  389. strRoomName = other.strRoomName;
  390. listCameraID = other.listCameraID;
  391. }
  392. return *this;
  393. }
  394. /* 对比频道信息、房间信息、算法ID是否相等 */
  395. bool isEqualBaseInfo(const RoomActionInfo& other);
  396. };
  397. /* 房间和算法ID关联列表 */
  398. class ListRoomActionInfo
  399. {
  400. public:
  401. ListRoomActionInfo() = default;
  402. /* 添加关联信息,会自动进行重复判断,如果已有相同的room和action关联信息,则跳过 */
  403. bool insertRoomActionInfo(const RoomActionInfo& info);
  404. /* 添加关联信息,会自动进行重复判断,如果已有相同的room和action关联信息,则跳过 */
  405. bool insertRoomActionInfo(const int ChannelID, const int RoomID, const std::string& strActionID);
  406. /* 删除一个容器,注意,这个不能在别的for循环中删除,只能单独删除 */
  407. bool deleteRoomActionInfo(RoomActionInfo* pInfo);
  408. /* 清空容器 */
  409. void clear();
  410. /* 添加算法信息,根据传入的算法信息,自动将其加入到对应的容器中 */
  411. void addActionInfo(const ActionInfo& info);
  412. /* 清空算法对应的摄像机列表 */
  413. void clearCameraList();
  414. /* 清理设置为STOP或者ERROR的RoomAction */
  415. void clearStopRoomAction();
  416. /* 查找算法ID是否已在列表中 */
  417. RoomActionInfo* findActionIDInList(const int chnID, const int RoomID, const std::string& strActionID);
  418. /* 获取容器 */
  419. std::list<RoomActionInfo*>& getData() {
  420. return listRoomActionInfo;
  421. }
  422. std::list<RoomActionInfo*> listRoomActionInfo; /* 房间和算法ID关联列表 */
  423. };
  424. /**
  425. * @brief 房间内的摄像机和算法关联信息
  426. *
  427. */
  428. struct RoomCamActInfo
  429. {
  430. int RoomID; /* 房间ID */
  431. int RoomType; /* 房间类型 */
  432. std::multimap<int, std::string> mapCameraAction; /* 摄像机ID和算法ID关联信息 */
  433. };
  434. /**
  435. * @brief 读取到的应用信息和启用时段
  436. *
  437. */
  438. struct AppAndTimeInfo
  439. {
  440. uint8_t AppType; /* 应用信息,按位计算 */
  441. int ChannelID; /* 通道ID */
  442. QDateTime StartTime; /* 开始时间 */
  443. QDateTime EndTime; /* 结束时间 */
  444. AppAndTimeInfo() = default;
  445. AppAndTimeInfo& operator=(AppAndTimeInfo& other) {
  446. if (this != &other) {
  447. AppType = other.AppType;
  448. ChannelID = other.ChannelID;
  449. StartTime = other.StartTime;
  450. EndTime = other.EndTime;
  451. }
  452. return *this;
  453. }
  454. };
  455. /**
  456. * @brief 按照功能分类的线程信息,一个实例就是一个功能,
  457. 比如区域入侵检测,需要多个算法多个摄像机共同配合
  458. *
  459. */
  460. struct FuncActionInfo
  461. {
  462. int ChannelID; /* 通道ID */
  463. AppFunction appFunction; /* 任务功能 */
  464. RunTimeState RunState; /* 线程运行状态 */
  465. std::string strFunctionName; /* 功能名称 */
  466. QDateTime StartTime; /* 开始时间 */
  467. QDateTime EndTime; /* 结束时间 */
  468. std::list<RoomCamActInfo> listRoomCamActInfo; /* 房间内的摄像机和算法关联信息 */
  469. FuncActionInfo();
  470. FuncActionInfo& operator=(FuncActionInfo& other);
  471. /* 添加算法信息 */
  472. bool addActionInfo(const ActionInfo& info);
  473. /* 清空算法信息 */
  474. void clearActionList();
  475. };
  476. /**
  477. * @brief
  478. *
  479. */
  480. struct ListFuncActInfo
  481. {
  482. ListFuncActInfo() = default;
  483. /* 添加功能信息 */
  484. bool addFuncActionInfo(const AppAndTimeInfo& func);
  485. // bool addFuncActionInfo(AppFunction& func);
  486. /* 添加算法信息 */
  487. bool addActionInfo(const ActionInfo& info);
  488. /* 清空无用的功能信息 */
  489. void clearNoneFuncActionInfo();
  490. /* 清空房间和算法列表 */
  491. void clearActionList();
  492. /* 获取容器 */
  493. std::list<FuncActionInfo*>& getData() {
  494. return listFuncActionInfo;
  495. }
  496. /* 查找应用信息 */
  497. bool findAppFunction(const AppFunction func);
  498. FuncActionInfo* findAppFunction(const int ChannelID, const AppFunction func);
  499. FuncActionInfo* findAppFunction(const FuncActionInfo& func);
  500. std::list<FuncActionInfo*> listFuncActionInfo; /* 功能信息列表 */
  501. };
  502. /**
  503. * @brief 读取配置文件
  504. *
  505. */
  506. class GlobalConfig
  507. {
  508. public:
  509. GlobalConfig();
  510. ~GlobalConfig() = default;
  511. /* 读取配置文件 */
  512. bool readConfig(const QString& strConfigFile);
  513. /* 打印读取到的值 */
  514. void printValue();
  515. int AppUpdateOnWorkTimeInterval;/* 更新在岗信息的时间间隔 */
  516. int AppPeopleOnWork; /* 离岗时间 */
  517. int Contraband; /* 违禁物品出现的时间 */
  518. int AppBadMan; /* 非法入侵 */
  519. int AppTired; /* 疲劳检测时间 */
  520. int AppPeopleCont; /* 区域人员统计 */
  521. int AppPlayPhone; /* 玩手机识别 */
  522. int AppMouse; /* 老鼠识别 */
  523. int AppMask; /* 戴口罩识别 */
  524. int CheckSet; /* 服务端多久检测一次配置 */
  525. int EventTimeValid; /* 事件时间有效期 */
  526. int ThreadSleepMS; /* 任务线程休眠时间,单位是ms */
  527. std::string Key; /* Key */
  528. std::string Secret; /* Secret */
  529. };
  530. extern GlobalConfig g_config;
  531. #endif /* GLOBALINFO_H */