GlobalVariable.h 18 KB

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