FuncBase.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "FuncBase.h"
  2. #include "GlobalVariable.h"
  3. #include "GlobalConfig.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, "开启 {} 功能线程, ChannelID:{}",m_funcThreadInfo.strFunctionName, m_funcThreadInfo.ChannelID);
  49. /* 设置线程运行状态 */
  50. m_bRunning = true;
  51. /* 执行线程功能,会一直阻塞,直到退出 */
  52. task();
  53. /* 设置线程退出的状态,设置成 RUN_STATE_EXITCOMPLET ,就会被管理线程回收 */
  54. GThreadInfo.setThreadState(m_funcThreadInfo, RunTimeState::RUN_STATE_EXITCOMPLET);
  55. SPDLOG_LOGGER_INFO(m_logger, "{} 功能线程退出,Channel:{}", m_funcThreadInfo.strFunctionName, m_funcThreadInfo.ChannelID);
  56. m_bRunning = false;
  57. }
  58. /* 设置功能信息 */
  59. void FuncBase::setFuncThreadInfo(FuncThreadInfo* pFuncAct)
  60. {
  61. m_funcThreadInfo = *pFuncAct;
  62. }
  63. void FuncBase::setFuncThreadInfo(FuncThreadInfo& FuncAct)
  64. {
  65. m_funcThreadInfo = FuncAct;
  66. }
  67. /* 获取该实例的功能 */
  68. AppFunction FuncBase::getApp()
  69. {
  70. return m_funcThreadInfo.appFunction;
  71. }
  72. /* 获取线程运行状态 */
  73. bool FuncBase::getThreadRunning() const
  74. {
  75. return m_bRunning;
  76. }
  77. /* 判断是否在检测时间内 */
  78. bool FuncBase::isInDetectTime(const QDateTime& start, const QDateTime& end)
  79. {
  80. QDateTime now = QDateTime::currentDateTime();
  81. if(start.isValid() && end.isValid())
  82. {
  83. if(now >= start && now <= end)
  84. {
  85. return true;
  86. }
  87. }
  88. return false;
  89. }