123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- #include "FuncBase.h"
- #include "GlobalConfig.h"
- #include "UniversalFunc.h"
- #include "FromRedis.h"
- #include "FromWebAPI.h"
- FuncBase::FuncBase()
- {
- m_fromRedis = std::make_shared<FromRedis>();
- m_fromWebAPI = std::make_shared<FromWebAPI>();
- }
- FuncBase::~FuncBase()
- {
- if(m_fromRedis != nullptr)
- {
- m_fromRedis.reset();
- m_fromRedis = nullptr;
- }
- if(m_fromWebAPI != nullptr)
- {
- m_fromWebAPI.reset();
- m_fromWebAPI = nullptr;
- }
- if(m_logger != nullptr)
- {
- m_logger.reset();
- m_logger = nullptr;
- }
- }
- /* 任务线程 */
- void FuncBase::thread_task()
- {
- /* 先做一些通用的初始化 */
- if(m_logger == nullptr)
- {
- m_logger = spdlog::get("SPAServer");
- if(m_logger == nullptr)
- {
- SPDLOG_ERROR("SPAServer logger is nullptr");
- return;
- }
- }
- /* 线程功能信息 */
- if(m_funcThreadInfo.appFunction == AppFunction::APP_NONE)
- {
- SPDLOG_LOGGER_ERROR(m_logger, "未设置线程功能信息");
- return;
- }
- SPDLOG_LOGGER_INFO(m_logger, "---------------------- 开启频道: [{}:{}] “{}”功能 ----------------------", m_funcThreadInfo.strChannelName, m_funcThreadInfo.ChannelID, m_funcThreadInfo.strFunctionName);
- for(auto& room : m_funcThreadInfo.listRoomCamActInfo)
- {
- for(auto& cam : room.mapCameraAction)
- {
- for(auto& act : cam.second)
- {
- SPDLOG_LOGGER_DEBUG(m_logger, "房间ID: {}, 摄像机ID: {}, 算法ID: {}", room.RoomID, cam.first, act);
- }
- }
- }
- /* 设置线程的全局运行状态 */
- GThreadInfo.setThreadState(m_funcThreadInfo, RunTimeState::RUN_STATE_RUN);
- /* 设置线程运行状态 */
- m_bRunning = true;
- /* 设置初始化字符串 */
- m_baseLog = fmt::format("★ 频道[{}] 功能线程[{}] ", m_funcThreadInfo.strChannelName, m_funcThreadInfo.strFunctionName);
- /* 执行线程功能,会一直阻塞,直到退出 */
- task();
- /* 设置线程退出的状态,设置成 RUN_STATE_EXITCOMPLET ,就会被管理线程回收 */
- GThreadInfo.setThreadState(m_funcThreadInfo, RunTimeState::RUN_STATE_EXITCOMPLET);
- SPDLOG_LOGGER_INFO(m_logger, "{} 退出", m_baseLog);
- m_bRunning = false;
- }
- /* 设置功能信息 */
- void FuncBase::setFuncThreadInfo(FuncThreadInfo* pFuncAct)
- {
- m_funcThreadInfo = *pFuncAct;
- }
- void FuncBase::setFuncThreadInfo(FuncThreadInfo& FuncAct)
- {
- m_funcThreadInfo = FuncAct;
- }
- /* 获取该实例的功能 */
- AppFunction FuncBase::getApp()
- {
- return m_funcThreadInfo.appFunction;
- }
- /* 获取线程运行状态 */
- bool FuncBase::getThreadRunning() const
- {
- return m_bRunning;
- }
- /* 初始化数据接口 */
- bool FuncBase::initDataInterface()
- {
- /* 初始化WebAPI */
- if(!m_fromWebAPI->initWebApi(GConfig.webapiUrl(), GConfig.webapiKey(), GConfig.webapiAppType()))
- {
- SPDLOG_LOGGER_ERROR(m_logger, "{} 初始化WebAPI失败, 退出线程", m_baseLog);
- return false;
- }else {
- SPDLOG_LOGGER_INFO(m_logger, "{} 初始化WebAPI成功", m_baseLog);
- }
- /* 初始化Redis */
- m_fromRedis->setRedisIPAndPort(GConfig.redisIP(), GConfig.redisPort());
- m_fromRedis->setRedisPassword(GConfig.redisPWD());
- if(!m_fromRedis->connectRedis())
- {
- SPDLOG_LOGGER_ERROR(m_logger, "{} 连接Redis失败, 退出线程", m_baseLog);
- return false;
- }else {
- SPDLOG_LOGGER_INFO(m_logger, "{} 连接Redis成功", m_baseLog);
- }
- return true;
- }
- /* 判断是否在检测时间内 */
- bool FuncBase::isInDetectTime(const QDateTime& start, const QDateTime& end)
- {
- QDateTime now = QDateTime::currentDateTime();
- if(start.isValid() && end.isValid())
- {
- if(now >= start && now <= end)
- {
- return true;
- }
- }
- return false;
- }
- /* 更新检测时间范围,一定时间更新一次,不是每次都更新 */
- bool FuncBase::updateDetectTime(DetectPeriodInfo& periodInfo)
- {
- /* 从数据库获取监测时间,10秒钟获取一次 */
- if(periodInfo.lastTime.secsTo(m_nowTime) > 10)
- {
- periodInfo.lastTime = m_nowTime;
- /* 获取监测时间 */
- if(!m_fromWebAPI->getFuncDetectTime(m_funcThreadInfo.ChannelID, m_funcThreadInfo.appFunction, m_periodInfo))
- {
- SPDLOG_LOGGER_ERROR(m_logger, "{} 获取检测时间失败", m_baseLog);
- return false;
- }
- }
- return true;
- }
- /* 判断是否在检测时间内,重载版,会自动打印日志 */
- bool FuncBase::isInDetectTime(const DetectPeriodInfo& periodInfo)
- {
- /* 刷新当前时间 */
- m_nowTime = QDateTime::currentDateTime();
- /* 先更新检测时间段 */
- if(!updateDetectTime(m_periodInfo))
- {
- return false;
- }
- /* 判断是按照天还是按周执行 */
- if(periodInfo.PeriodType == Enum_PeriodType::PERIOD_ALL)
- {
- /* 所有时段都可以检测 */
- return true;
- }
- else if(periodInfo.PeriodType == Enum_PeriodType::PERIOD_DAY)
- {
- /* 按天检测 */
- QTime st = periodInfo.listWeekTime.front().StartTime.time();
- QTime et = periodInfo.listWeekTime.front().EndTime.time();
- if(st.isValid() && et.isValid())
- {
- if(m_nowTime.time() >= st && m_nowTime.time() <= et)
- {
- return true;
- }
- }
-
- }
- else if(periodInfo.PeriodType == Enum_PeriodType::PERIOD_WEEK)
- {
- /* 按周检测 */
- for(const auto& it : periodInfo.listWeekTime)
- {
- /* 其他星期几 */
- if(it.WeekDay == m_nowTime.date().dayOfWeek())
- {
- if(it.StartTime.time() <= m_nowTime.time() && it.EndTime.time() >= m_nowTime.time())
- {
- return true;
- }
- }
- }
- }
- /* 拼接时间段,如果时按周计算,会有好多个时间段 */
- std::string dectectTime = "检测时间: ";
- if(periodInfo.PeriodType == Enum_PeriodType::PERIOD_WEEK)
- {
- dectectTime += fmt::format("按周检测\n");
- for(const auto& it : periodInfo.listWeekTime)
- {
- dectectTime += fmt::format("{} [{} - {}] \n", getWeekDayString(it.WeekDay), it.StartTime.toString("hh:mm:ss").toStdString(),
- it.EndTime.toString("hh:mm:ss").toStdString());
- }
- }
- else if(Enum_PeriodType::PERIOD_DAY == periodInfo.PeriodType)
- {
- dectectTime += fmt::format("按天检测 [{} - {}]", periodInfo.listWeekTime.front().StartTime.toString("hh:mm:ss").toStdString(),
- periodInfo.listWeekTime.front().EndTime.toString("hh:mm:ss").toStdString());
- }
- SPDLOG_LOGGER_DEBUG(m_logger, "{} 不在检测时间段内, {}", m_baseLog, dectectTime);
- return false;
- }
|