UniversalFunc.cpp 7.7 KB

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