GlobalVariable.h 19 KB

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