GlobalVariable.h 20 KB

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