FuncBase.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "FuncBase.h"
  2. #include "GlobalConfig.h"
  3. #include "UniversalFunc.h"
  4. #include "FromRedis.h"
  5. #include "FromWebAPI.h"
  6. FuncBase::FuncBase()
  7. {
  8. m_fromRedis = std::make_shared<FromRedis>();
  9. m_fromWebAPI = std::make_shared<FromWebAPI>();
  10. }
  11. FuncBase::~FuncBase()
  12. {
  13. if(m_fromRedis != nullptr)
  14. {
  15. m_fromRedis.reset();
  16. m_fromRedis = nullptr;
  17. }
  18. if(m_fromWebAPI != nullptr)
  19. {
  20. m_fromWebAPI.reset();
  21. m_fromWebAPI = nullptr;
  22. }
  23. if(m_logger != nullptr)
  24. {
  25. m_logger.reset();
  26. m_logger = nullptr;
  27. }
  28. }
  29. /* 任务线程 */
  30. void FuncBase::thread_task()
  31. {
  32. /* 先做一些通用的初始化 */
  33. if(m_logger == nullptr)
  34. {
  35. m_logger = spdlog::get("SPAServer");
  36. if(m_logger == nullptr)
  37. {
  38. SPDLOG_ERROR("SPAServer logger is nullptr");
  39. return;
  40. }
  41. }
  42. /* 线程功能信息 */
  43. if(m_funcThreadInfo.appFunction == AppFunction::APP_NONE)
  44. {
  45. SPDLOG_LOGGER_ERROR(m_logger, "未设置线程功能信息");
  46. return;
  47. }
  48. SPDLOG_LOGGER_INFO(m_logger, "开启频道: [{}:{}] “{}”功能线程", m_funcThreadInfo.strChannelName, m_funcThreadInfo.ChannelID, m_funcThreadInfo.strFunctionName);
  49. for(auto& room : m_funcThreadInfo.listRoomCamActInfo)
  50. {
  51. for(auto& cam : room.mapCameraAction)
  52. {
  53. for(auto& act : cam.second)
  54. {
  55. SPDLOG_LOGGER_DEBUG(m_logger, "房间ID: {}, 摄像机ID: {}, 算法ID: {}", room.RoomID, cam.first, act);
  56. }
  57. }
  58. }
  59. /* 设置线程运行状态 */
  60. m_bRunning = true;
  61. /* 设置初始化字符串 */
  62. m_baseLog = fmt::format("★ 频道[{}] 功能线程[{}] ", m_funcThreadInfo.strChannelName, m_funcThreadInfo.strFunctionName);
  63. /* 执行线程功能,会一直阻塞,直到退出 */
  64. task();
  65. /* 设置线程退出的状态,设置成 RUN_STATE_EXITCOMPLET ,就会被管理线程回收 */
  66. GThreadInfo.setThreadState(m_funcThreadInfo, RunTimeState::RUN_STATE_EXITCOMPLET);
  67. SPDLOG_LOGGER_INFO(m_logger, "{}}退出", m_baseLog);
  68. m_bRunning = false;
  69. }
  70. /* 设置功能信息 */
  71. void FuncBase::setFuncThreadInfo(FuncThreadInfo* pFuncAct)
  72. {
  73. m_funcThreadInfo = *pFuncAct;
  74. }
  75. void FuncBase::setFuncThreadInfo(FuncThreadInfo& FuncAct)
  76. {
  77. m_funcThreadInfo = FuncAct;
  78. }
  79. /* 获取该实例的功能 */
  80. AppFunction FuncBase::getApp()
  81. {
  82. return m_funcThreadInfo.appFunction;
  83. }
  84. /* 获取线程运行状态 */
  85. bool FuncBase::getThreadRunning() const
  86. {
  87. return m_bRunning;
  88. }
  89. /* 初始化数据接口 */
  90. bool FuncBase::initDataInterface()
  91. {
  92. /* 初始化WebAPI */
  93. if(!m_fromWebAPI->initWebApi(GConfig.webapiUrl(), GConfig.webapiKey(), GConfig.webapiAppType()))
  94. {
  95. SPDLOG_LOGGER_ERROR(m_logger, "{} 初始化WebAPI失败, 退出线程", m_baseLog);
  96. return false;
  97. }else {
  98. SPDLOG_LOGGER_INFO(m_logger, "{} 初始化WebAPI成功", m_baseLog);
  99. }
  100. /* 初始化Redis */
  101. m_fromRedis->setRedisIPAndPort(GConfig.redisIP(), GConfig.redisPort());
  102. m_fromRedis->setRedisPassword(GConfig.redisPWD());
  103. if(!m_fromRedis->connectRedis())
  104. {
  105. SPDLOG_LOGGER_ERROR(m_logger, "{} 连接Redis失败, 退出线程", m_baseLog);
  106. return false;
  107. }else {
  108. SPDLOG_LOGGER_INFO(m_logger, "{} 连接Redis成功", m_baseLog);
  109. }
  110. return true;
  111. }
  112. /* 判断是否在检测时间内 */
  113. bool FuncBase::isInDetectTime(const QDateTime& start, const QDateTime& end)
  114. {
  115. QDateTime now = QDateTime::currentDateTime();
  116. if(start.isValid() && end.isValid())
  117. {
  118. if(now >= start && now <= end)
  119. {
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125. /* 更新检测时间范围,一定时间更新一次,不是每次都更新 */
  126. bool FuncBase::updateDetectTime(DetectPeriodInfo& periodInfo)
  127. {
  128. /* 从数据库获取监测时间,10秒钟获取一次 */
  129. if(periodInfo.lastTime.secsTo(m_nowTime) > 10)
  130. {
  131. periodInfo.lastTime = m_nowTime;
  132. /* 获取监测时间 */
  133. if(!m_fromWebAPI->getFuncDetectTime(m_funcThreadInfo.ChannelID, m_funcThreadInfo.appFunction, m_periodInfo))
  134. {
  135. SPDLOG_LOGGER_ERROR(m_logger, "{} 获取检测时间失败", m_baseLog);
  136. return false;
  137. }
  138. }
  139. return true;
  140. }
  141. /* 判断是否在检测时间内,重载版,会自动打印日志 */
  142. bool FuncBase::isInDetectTime(const DetectPeriodInfo& periodInfo)
  143. {
  144. /* 刷新当前时间 */
  145. m_nowTime = QDateTime::currentDateTime();
  146. /* 先更新检测时间段 */
  147. if(!updateDetectTime(m_periodInfo))
  148. {
  149. return false;
  150. }
  151. /* 判断是按照天还是按周执行 */
  152. if(periodInfo.PeriodType == Enum_PeriodType::PERIOD_ALL)
  153. {
  154. /* 所有时段都可以检测 */
  155. return true;
  156. }
  157. else if(periodInfo.PeriodType == Enum_PeriodType::PERIOD_DAY)
  158. {
  159. /* 按天检测 */
  160. QTime st = periodInfo.listWeekTime.front().StartTime.time();
  161. QTime et = periodInfo.listWeekTime.front().EndTime.time();
  162. if(st.isValid() && et.isValid())
  163. {
  164. if(m_nowTime.time() >= st && m_nowTime.time() <= et)
  165. {
  166. return true;
  167. }
  168. }
  169. }
  170. else if(periodInfo.PeriodType == Enum_PeriodType::PERIOD_WEEK)
  171. {
  172. /* 按周检测 */
  173. for(const auto& it : periodInfo.listWeekTime)
  174. {
  175. /* 其他星期几 */
  176. if(it.WeekDay == m_nowTime.date().dayOfWeek())
  177. {
  178. if(it.StartTime.time() <= m_nowTime.time() && it.EndTime.time() >= m_nowTime.time())
  179. {
  180. return true;
  181. }
  182. }
  183. }
  184. }
  185. /* 拼接时间段,如果时按周计算,会有好多个时间段 */
  186. std::string dectectTime = "检测时间: ";
  187. if(periodInfo.PeriodType == Enum_PeriodType::PERIOD_WEEK)
  188. {
  189. dectectTime += fmt::format("按周检测\n");
  190. for(const auto& it : periodInfo.listWeekTime)
  191. {
  192. dectectTime += fmt::format("{} [{} - {}] \n", getWeekDayString(it.WeekDay), it.StartTime.toString("hh:mm:ss").toStdString(),
  193. it.EndTime.toString("hh:mm:ss").toStdString());
  194. }
  195. }
  196. else if(Enum_PeriodType::PERIOD_DAY == periodInfo.PeriodType)
  197. {
  198. dectectTime += fmt::format("按天检测 [{} - {}]", periodInfo.listWeekTime.front().StartTime.toString("hh:mm:ss").toStdString(),
  199. periodInfo.listWeekTime.front().EndTime.toString("hh:mm:ss").toStdString());
  200. }
  201. SPDLOG_LOGGER_DEBUG(m_logger, "{} 不在检测时间段内, {}", m_baseLog, dectectTime);
  202. return false;
  203. }