FuncBase.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. GThreadInfo.setThreadState(m_funcThreadInfo, RunTimeState::RUN_STATE_RUN);
  61. /* 设置线程运行状态 */
  62. m_bRunning = true;
  63. /* 设置初始化字符串 */
  64. m_baseLog = fmt::format("★ 频道[{}] 功能线程[{}] ", m_funcThreadInfo.strChannelName, m_funcThreadInfo.strFunctionName);
  65. /* 执行线程功能,会一直阻塞,直到退出 */
  66. task();
  67. /* 设置线程退出的状态,设置成 RUN_STATE_EXITCOMPLET ,就会被管理线程回收 */
  68. GThreadInfo.setThreadState(m_funcThreadInfo, RunTimeState::RUN_STATE_EXITCOMPLET);
  69. SPDLOG_LOGGER_INFO(m_logger, "{} 退出", m_baseLog);
  70. m_bRunning = false;
  71. }
  72. /* 设置功能信息 */
  73. void FuncBase::setFuncThreadInfo(FuncThreadInfo* pFuncAct)
  74. {
  75. m_funcThreadInfo = *pFuncAct;
  76. }
  77. void FuncBase::setFuncThreadInfo(FuncThreadInfo& FuncAct)
  78. {
  79. m_funcThreadInfo = FuncAct;
  80. }
  81. /* 获取该实例的功能 */
  82. AppFunction FuncBase::getApp()
  83. {
  84. return m_funcThreadInfo.appFunction;
  85. }
  86. /* 获取线程运行状态 */
  87. bool FuncBase::getThreadRunning() const
  88. {
  89. return m_bRunning;
  90. }
  91. /* 初始化数据接口 */
  92. bool FuncBase::initDataInterface()
  93. {
  94. /* 初始化WebAPI */
  95. if(!m_fromWebAPI->initWebApi(GConfig.webapiUrl(), GConfig.webapiKey(), GConfig.webapiAppType()))
  96. {
  97. SPDLOG_LOGGER_ERROR(m_logger, "{} 初始化WebAPI失败, 退出线程", m_baseLog);
  98. return false;
  99. }else {
  100. SPDLOG_LOGGER_INFO(m_logger, "{} 初始化WebAPI成功", m_baseLog);
  101. }
  102. /* 初始化Redis */
  103. m_fromRedis->setRedisIPAndPort(GConfig.redisIP(), GConfig.redisPort());
  104. m_fromRedis->setRedisPassword(GConfig.redisPWD());
  105. if(!m_fromRedis->connectRedis())
  106. {
  107. SPDLOG_LOGGER_ERROR(m_logger, "{} 连接Redis失败, 退出线程", m_baseLog);
  108. return false;
  109. }else {
  110. SPDLOG_LOGGER_INFO(m_logger, "{} 连接Redis成功", m_baseLog);
  111. }
  112. return true;
  113. }
  114. /* 判断是否在检测时间内 */
  115. bool FuncBase::isInDetectTime(const QDateTime& start, const QDateTime& end)
  116. {
  117. QDateTime now = QDateTime::currentDateTime();
  118. if(start.isValid() && end.isValid())
  119. {
  120. if(now >= start && now <= end)
  121. {
  122. return true;
  123. }
  124. }
  125. return false;
  126. }
  127. /* 更新检测时间范围,一定时间更新一次,不是每次都更新 */
  128. bool FuncBase::updateDetectTime(DetectPeriodInfo& periodInfo)
  129. {
  130. /* 从数据库获取监测时间,10秒钟获取一次 */
  131. if(periodInfo.lastTime.secsTo(m_nowTime) > 10)
  132. {
  133. periodInfo.lastTime = m_nowTime;
  134. /* 获取监测时间 */
  135. if(!m_fromWebAPI->getFuncDetectTime(m_funcThreadInfo.ChannelID, m_funcThreadInfo.appFunction, m_periodInfo))
  136. {
  137. SPDLOG_LOGGER_ERROR(m_logger, "{} 获取检测时间失败", m_baseLog);
  138. return false;
  139. }
  140. }
  141. return true;
  142. }
  143. /* 判断是否在检测时间内,重载版,会自动打印日志 */
  144. bool FuncBase::isInDetectTime(const DetectPeriodInfo& periodInfo)
  145. {
  146. /* 刷新当前时间 */
  147. m_nowTime = QDateTime::currentDateTime();
  148. /* 先更新检测时间段 */
  149. if(!updateDetectTime(m_periodInfo))
  150. {
  151. return false;
  152. }
  153. /* 判断是按照天还是按周执行 */
  154. if(periodInfo.PeriodType == Enum_PeriodType::PERIOD_ALL)
  155. {
  156. /* 所有时段都可以检测 */
  157. return true;
  158. }
  159. else if(periodInfo.PeriodType == Enum_PeriodType::PERIOD_DAY)
  160. {
  161. /* 按天检测 */
  162. QTime st = periodInfo.listWeekTime.front().StartTime.time();
  163. QTime et = periodInfo.listWeekTime.front().EndTime.time();
  164. if(st.isValid() && et.isValid())
  165. {
  166. if(m_nowTime.time() >= st && m_nowTime.time() <= et)
  167. {
  168. return true;
  169. }
  170. }
  171. }
  172. else if(periodInfo.PeriodType == Enum_PeriodType::PERIOD_WEEK)
  173. {
  174. /* 按周检测 */
  175. for(const auto& it : periodInfo.listWeekTime)
  176. {
  177. /* 其他星期几 */
  178. if(it.WeekDay == m_nowTime.date().dayOfWeek())
  179. {
  180. if(it.StartTime.time() <= m_nowTime.time() && it.EndTime.time() >= m_nowTime.time())
  181. {
  182. return true;
  183. }
  184. }
  185. }
  186. }
  187. /* 拼接时间段,如果时按周计算,会有好多个时间段 */
  188. std::string dectectTime = "检测时间: ";
  189. if(periodInfo.PeriodType == Enum_PeriodType::PERIOD_WEEK)
  190. {
  191. dectectTime += fmt::format("按周检测\n");
  192. for(const auto& it : periodInfo.listWeekTime)
  193. {
  194. dectectTime += fmt::format("{} [{} - {}] \n", getWeekDayString(it.WeekDay), it.StartTime.toString("hh:mm:ss").toStdString(),
  195. it.EndTime.toString("hh:mm:ss").toStdString());
  196. }
  197. }
  198. else if(Enum_PeriodType::PERIOD_DAY == periodInfo.PeriodType)
  199. {
  200. dectectTime += fmt::format("按天检测 [{} - {}]", periodInfo.listWeekTime.front().StartTime.toString("hh:mm:ss").toStdString(),
  201. periodInfo.listWeekTime.front().EndTime.toString("hh:mm:ss").toStdString());
  202. }
  203. SPDLOG_LOGGER_DEBUG(m_logger, "{} 不在检测时间段内, {}", m_baseLog, dectectTime);
  204. return false;
  205. }