UniversalFunc.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "UniversalFunc.h"
  2. #include "spdlog/spdlog.h"
  3. #include "GlobalVariable.h"
  4. /**
  5. * @brief 解析Redis的基础通用数据,不包含bBoxes数组数据
  6. *
  7. * @param strData Redis返回的源数据,JSON格式
  8. * @param alarmInfo 解析出来的数据
  9. */
  10. void parseRedisBaseData(const std::string& strData, AlarmInfo& alarmInfo)
  11. {
  12. try
  13. {
  14. nJson json0;
  15. json0 = nJson::parse(strData);
  16. alarmInfo.AlarmID = json0["alarmId"].get<int>();
  17. alarmInfo.ChannelID = json0["channel"].get<int>();
  18. alarmInfo.PicUrl = json0["picUrl"].get<std::string>();
  19. alarmInfo.ImageInfo = json0["imageInfo"].get<std::string>();
  20. /* 解析时间,需要将时间中的“T”换成空格 */
  21. alarmInfo.StartTime = json0["beginTime"].get<std::string>();
  22. std::replace(alarmInfo.StartTime.begin(), alarmInfo.StartTime.end(), 'T', ' ');
  23. alarmInfo.EndTime = json0["endTime"].get<std::string>();
  24. std::replace(alarmInfo.EndTime.begin(), alarmInfo.EndTime.end(), 'T', ' ');
  25. alarmInfo.EventTime = json0["eventTime"].get<std::string>();
  26. std::replace(alarmInfo.EventTime.begin(), alarmInfo.EventTime.end(), 'T', ' ');
  27. }
  28. catch (const nJson::parse_error& e)
  29. {
  30. SPDLOG_ERROR("解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  31. return;
  32. }
  33. catch (const nJson::type_error& e)
  34. {
  35. SPDLOG_ERROR("解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  36. return;
  37. }
  38. catch (...)
  39. {
  40. SPDLOG_ERROR("解析Redis数据失败,其他错误!");
  41. return;
  42. }
  43. }
  44. /**
  45. * @brief 解析Redis的bBoxes数据,这个内容可能根据算法ID不同,内容不同
  46. *
  47. * @param strData
  48. * @param alarmInfo
  49. */
  50. void parseRedisBBoxesData(const std::string& strData, AlarmInfo& alarmInfo)
  51. {
  52. try
  53. {
  54. nJson json0;
  55. json0 = nJson::parse(strData);
  56. /* 判断bBoxes有无数据,有数据就解析,没数据就直接返回了 */
  57. nJson json1 = json0["bBoxes"];
  58. std::string labelList; /* 记录违禁品 */
  59. std::list<std::string> listBbox; /* 记录bbox */
  60. if(!json1.empty())
  61. {
  62. for(auto& it0 : json1)
  63. {
  64. /* 如果status是true,就不是报警,直接跳过 */
  65. bool status = it0["status"].get<bool>();
  66. if(status)
  67. {
  68. continue;
  69. }
  70. /* 这一条Box数据报警了将其内容存储起来 */
  71. alarmInfo.Is_Alarm = true;
  72. /* 解析label,违禁品关键字,先判断这个是不是违禁品检测的算法ID */
  73. if(alarmInfo.ActionID == g_actionList.ActContraband)
  74. {
  75. /* 解析报警,取出报警类型 */
  76. nJson label = it0["label"];
  77. for(auto& it1 : label)
  78. {
  79. std::string strLabel = it1.get<std::string>();
  80. /* 检测是否已经加入到字符串中了 */
  81. strLabel += "|";
  82. if(labelList.find(strLabel) != std::string::npos)
  83. {
  84. continue;
  85. }
  86. labelList += strLabel;
  87. }
  88. }
  89. /* 解析bbox,貌似是在图像中的位置 */
  90. nJson bbox = it0["bbox"];
  91. std::string strBbox;
  92. for(auto& it1 : bbox)
  93. {
  94. strBbox += std::to_string(it1.get<int>()) + ",";
  95. }
  96. /* 去掉最后一个“,” */
  97. if(!strBbox.empty())
  98. {
  99. strBbox.pop_back();
  100. }
  101. listBbox.push_back(strBbox);
  102. }
  103. /* 去掉最后一个“|” */
  104. if(!labelList.empty())
  105. {
  106. labelList.pop_back();
  107. }
  108. SPDLOG_DEBUG("违禁品列表:{}", labelList);
  109. }
  110. /* 如果有报警的Box,解析出报警的说明 */
  111. if(alarmInfo.Is_Alarm)
  112. {
  113. /* 添加报警信息的提示信息 */
  114. alarmInfo.listBbox = listBbox;
  115. /* 违禁品报警信息,违禁品列表不是空的,就添加补充的文字 */
  116. if( (alarmInfo.ActionID == g_actionList.ActContraband) && !labelList.empty() )
  117. {
  118. alarmInfo.ActionDes = fmt::format("出现违禁品[{}]告警", labelList);
  119. SPDLOG_INFO("{}", alarmInfo.ActionDes);
  120. }else {
  121. /* 其他报警信息,直接获取 */
  122. alarmInfo.ActionDes = json0["actionDes"].get<std::string>();
  123. }
  124. /* 判断有没有报警数据 */
  125. if(alarmInfo.ImageInfo.empty())
  126. {
  127. SPDLOG_WARN("有报警区域,但是没有图片信息");
  128. return;
  129. }
  130. /* 如果是人员报警,就存储人员报警信息 */
  131. if(alarmInfo.ActionID == g_actionList.ActFace)
  132. {
  133. nJson jsonArray = json0["personList"];
  134. for(auto& it : jsonArray)
  135. {
  136. PersonInfo personInfo;
  137. personInfo.PersonID = it["personId"].get<std::string>();
  138. personInfo.PersonName = it["personName"].get<std::string>();
  139. alarmInfo.listPersonInfo.push_back(personInfo);
  140. }
  141. }
  142. }
  143. }
  144. catch (const nJson::parse_error& e)
  145. {
  146. SPDLOG_ERROR("解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  147. return;
  148. }
  149. catch (const nJson::type_error& e)
  150. {
  151. SPDLOG_ERROR("解析Redis数据失败:{}, 错误ID:{}", e.what(), e.id);
  152. return;
  153. }
  154. catch (...)
  155. {
  156. SPDLOG_ERROR("解析Redis数据失败,其他错误!");
  157. return;
  158. }
  159. }
  160. /**
  161. * @brief 判断时间是否长时间没有更新,默认的是600秒,超过这个时间Redis还未更新,可能是超脑挂了
  162. *
  163. * @param strTime
  164. * @return true
  165. * @return false
  166. */
  167. bool isEventTimeVaild(const std::string& strTime)
  168. {
  169. /* 获取当前时间 */
  170. std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
  171. /* 字符串转成时间 */
  172. std::istringstream iss(strTime);
  173. std::tm tmEvent = {};
  174. iss >> std::get_time(&tmEvent, "%Y-%m-%d %H:%M:%S");
  175. /* 时间差 */
  176. std::chrono::system_clock::time_point eventTime = std::chrono::system_clock::from_time_t(std::mktime(&tmEvent));
  177. std::chrono::duration<double> diff = now - eventTime;
  178. // SPDLOG_LOGGER_DEBUG(m_logger, "now:{} eventTime: {} 时间差:{}秒",now, eventTime, diff);
  179. if(diff.count() > 600)
  180. {
  181. // SPDLOG_LOGGER_ERROR(m_logger, "Redis数据长时间没有更新,EventTime:{}", strTime);
  182. return false;
  183. }
  184. return true;
  185. }
  186. /* 计算与当前时间的时间差,返回秒 */
  187. int timeDiffWithNow(const std::string& strTime)
  188. {
  189. auto now = std::chrono::system_clock::now();
  190. auto eventTime = strTimeToChrono(strTime);
  191. std::chrono::duration<double> diff = now - eventTime;
  192. return diff.count();
  193. }
  194. /* 字符串时间转换成std::chrono时间点 */
  195. std::chrono::system_clock::time_point strTimeToChrono(const std::string& strTime)
  196. {
  197. std::istringstream iss(strTime);
  198. std::tm tmEvent = {};
  199. iss >> std::get_time(&tmEvent, "%Y-%m-%d %H:%M:%S");
  200. return std::chrono::system_clock::from_time_t(std::mktime(&tmEvent));
  201. }
  202. /* 时间点转换成字符串 */
  203. std::string chronoToStrTime(const std::chrono::system_clock::time_point& timePoint)
  204. {
  205. std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
  206. std::tm tmEvent = *std::localtime(&time);
  207. char buf[64] = {0};
  208. std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tmEvent);
  209. return std::string(buf);
  210. }
  211. /* 通过应用ID获取应用名称 */
  212. std::string getAppFunctionName(const AppFunction appID)
  213. {
  214. std::string strRet;
  215. switch(appID)
  216. {
  217. case AppFunction::APP_OnWork:
  218. strRet = "人员在岗识别";
  219. break;
  220. case AppFunction::APP_Contraband:
  221. strRet = "违禁物品识别";
  222. break;
  223. case AppFunction::APP_Illegal:
  224. strRet = "区域非法入侵检测";
  225. break;
  226. case AppFunction::APP_Fatigue:
  227. strRet = "疲劳检测识别";
  228. break;
  229. case AppFunction::APP_Regional:
  230. strRet = "区域人员统计";
  231. break;
  232. case AppFunction::APP_Mouse:
  233. strRet = "老鼠识别";
  234. break;
  235. case AppFunction::APP_PlayPhone:
  236. strRet = "玩手机识别";
  237. break;
  238. case AppFunction::APP_NoMask:
  239. strRet = "未戴口罩识别";
  240. break;
  241. case AppFunction::APP_AllDown:
  242. strRet = "摔倒识别";
  243. break;
  244. default:
  245. strRet = "未知功能";
  246. break;
  247. }
  248. return strRet;
  249. }