123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- #include "FuncRegionalPerson.h"
- #include "FromWebAPI.h"
- #include "FromRedis.h"
- #include "GlobalConfig.h"
- #include "GlobalVariable.h"
- #include "UniversalFunc.h"
- PersonCountRuleInfo::PersonCountRuleInfo()
- {
- ChannelID = -1;
- week = 0;
- RuleType = Enum_PeriodType::PERIOD_ALL;
- LiveMinEnable = false;
- LiveMaxEnable = false;
- DicMinEnable = false;
- DicMaxEnable = false;
- LiveDicMinEnable = false;
- LiveDicMaxEnable = false;
- LiveMin = 0;
- LiveMax = 0;
- DicMin = 0;
- DicMax = 0;
- LiveDicMin = 0;
- LiveDicMax = 0;
- RuleName = "";
- }
- PersonCountRuleInfo& PersonCountRuleInfo::operator=(PersonCountRuleInfo& other)
- {
- if(this != &other)
- {
- ChannelID = other.ChannelID;
- week = other.week;
- RuleType = other.RuleType;
- PeriodName = other.PeriodName;
- StartTime = other.StartTime;
- EndTime = other.EndTime;
- LiveMinEnable = other.LiveMinEnable;
- LiveMaxEnable = other.LiveMaxEnable;
- DicMinEnable = other.DicMinEnable;
- DicMaxEnable = other.DicMaxEnable;
- LiveDicMinEnable = other.LiveDicMinEnable;
- LiveDicMaxEnable = other.LiveDicMaxEnable;
- LiveMin = other.LiveMin;
- LiveMax = other.LiveMax;
- DicMin = other.DicMin;
- DicMax = other.DicMax;
- LiveDicMin = other.LiveDicMin;
- LiveDicMax = other.LiveDicMax;
- RuleName = other.RuleName;
- }
- return *this;
- }
- EndAlarmParam::EndAlarmParam()
- {
- ChannelID = -1;
- AppID = AppFunction::APP_NONE;
- CameraID = -1;
- insertAlarmNum = 0;
- }
- EndAlarmParam::EndAlarmParam(const EndAlarmParam& other)
- {
- ChannelID = other.ChannelID;
- AppID = other.AppID;
- CameraID = other.CameraID;
- insertAlarmNum = other.insertAlarmNum;
- }
- EndAlarmParam& EndAlarmParam::operator=(const EndAlarmParam& other)
- {
- if(this != &other)
- {
- ChannelID = other.ChannelID;
- AppID = other.AppID;
- CameraID = other.CameraID;
- insertAlarmNum = other.insertAlarmNum;
- }
- return *this;
- }
- bool EndAlarmParam::operator==(const EndAlarmParam& other)
- {
- if(this == &other)
- {
- return true;
- }
- if(ChannelID != other.ChannelID)
- {
- return false;
- }
- if(AppID != other.AppID)
- {
- return false;
- }
- if(CameraID != other.CameraID)
- {
- return false;
- }
- if(insertAlarmNum != other.insertAlarmNum)
- {
- return false;
- }
- return true;
- }
- FuncRegionalPersonCount::FuncRegionalPersonCount()
- {
- m_logger = spdlog::get("SPAServer");
- if(m_logger == nullptr)
- {
- SPDLOG_ERROR("SPAServer logger is nullptr");
- return;
- }
- m_pListAlarm = new ListAlarmInfo();
- if(m_pListAlarm == nullptr)
- {
- SPDLOG_ERROR("FuncRegionalPersonDetection m_pListAlarm is nullptr");
- return;
- }
- }
- FuncRegionalPersonCount::~FuncRegionalPersonCount()
- {
- if(m_pListAlarm != nullptr)
- {
- delete m_pListAlarm;
- m_pListAlarm = nullptr;
- }
- }
- void FuncRegionalPersonCount::task()
- {
-
-
- while (m_bRunning)
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(GConfig.ThreadSleepMS));
- /* 更新线程信息,并判断是否需要退出 */
- GThreadInfo.updateFuncInfo(m_funcThreadInfo);
- if( (m_funcThreadInfo.appFunction == AppFunction::APP_NONE) ||
- (m_funcThreadInfo.RunState == RunTimeState::RUN_STATE_STOP) )
- {
- break;
- }
-
- /* 判断是否在检测时间段内 */
- // if(!isInDetectTime(m_funcThreadInfo.StartTime, m_funcThreadInfo.EndTime))
- // {
- // continue;
- // }
- /* 更新报警规则 */
- if(!getPersonCountRuleInfo(m_personCountRule))
- {
- /* 判断报警是否已经结束,没有结束就结束报警 */
- autoEndAlarm();
- continue;
- }
- m_nowTime = QDateTime::currentDateTime();
- /************ 判断是否在检测时间内 ************/
- if(!isInPeriodTime())
- {
- SPDLOG_LOGGER_INFO(m_logger, "★ 频道[{}][{}],{},不在检测时段",
- m_funcThreadInfo.ChannelID, m_funcThreadInfo.strChannelName,
- getAppFunctionName(m_funcThreadInfo.appFunction));
- /* 判断报警是否已经结束,没有结束就结束报警 */
- autoEndAlarm();
- continue;
- }
- /* 更新房间列表 */
- updateRoomList();
- if(m_listRoomCamAct.empty())
- {
- SPDLOG_LOGGER_ERROR(m_logger, "★ 频道[{}][{}],{},房间列表为空",
- m_funcThreadInfo.ChannelID, m_funcThreadInfo.strChannelName,
- getAppFunctionName(m_funcThreadInfo.appFunction));
- /* 判断报警是否已经结束,没有结束就结束报警 */
- autoEndAlarm();
- continue;
- }
-
- /* -----------------------------------------------------------------------
- * 读取Redis数据
- * ----------------------------------------------------------------------- */
- m_pListAlarm->clearAlarmInfo();
- for(const auto& RoomInfo : m_listRoomCamAct)
- {
- for(const auto& it : RoomInfo.mapCameraAction)
- {
- for(const auto& act : it.second)
- {
- std::string strKey = std::to_string(it.first) + ":" + act;
- std::string strRetValue;
- if(!m_fromRedis->getRedisString(strKey, strRetValue))
- {
- SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
- continue;
- }
- /* 解析数据 */
- AlarmInfo alarmInfo;
- parseRedisBaseData(strRetValue, alarmInfo);
- parseRedisBBoxesData(strRetValue, alarmInfo);
- /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
- if(isEventTimeVaild(alarmInfo.EventTime))
- {
- SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
- continue;
- }
- m_pListAlarm->addAlarmInfo(alarmInfo);
- }
- }
- }
- /************ 挨个房间检测人数 ************/
-
- /************ 检测频率直播间 + 导播间房间的人数 ************/
- }
- }
- /* 获取该频率的人员计数规则 */
- bool FuncRegionalPersonCount::getPersonCountRuleInfo(PersonCountRuleInfo& personCountRuleInfo)
- {
- std::list<PersonCountRuleInfo> listRule;
- if(!m_fromWebAPI->getPersonCountRuleInfo(listRule))
- {
- SPDLOG_LOGGER_ERROR(m_logger, "获取《人员计数》报警规则失败");
- return false;
- }
- if(listRule.size() == 0)
- {
- SPDLOG_LOGGER_ERROR(m_logger, "★ 频道[{}][{}],{},未配置人员计数规则",
- m_funcThreadInfo.ChannelID, m_funcThreadInfo.strChannelName,
- getAppFunctionName(m_funcThreadInfo.appFunction));
- return false;
- }
- /* 获取这个频率的报警信息 */
- m_personCountRule.ChannelID = -1;
- for(auto& it : listRule)
- {
- if(it.ChannelID == m_funcThreadInfo.ChannelID)
- {
- m_personCountRule = it;
- break;
- }
- }
- if(m_personCountRule.ChannelID == -1)
- {
- SPDLOG_LOGGER_ERROR(m_logger, "频率:{}《人员计数》无报警规则", m_funcThreadInfo.ChannelID);
- return false;
- }
- return true;
- }
- /* 判断是否在检测时间段内 */
- bool FuncRegionalPersonCount::isInPeriodTime()
- {
- /* 所有时段 */
- if(m_personCountRule.RuleType == Enum_PeriodType::PERIOD_ALL)
- {
- return true;
- }
- /* 按天检测 */
- else if(m_personCountRule.RuleType == Enum_PeriodType::PERIOD_DAY)
- {
- if(m_personCountRule.StartTime <= m_nowTime && m_personCountRule.EndTime >= m_nowTime)
- {
- /* 在检测时间内 */
- return true;
- } else {
- /* 不在检测时间内 */
- return false;
- }
- }
- /* 按周检测 */
- else if (m_personCountRule.RuleType == Enum_PeriodType::PERIOD_WEEK)
- {
- if(m_personCountRule.week != m_nowTime.date().dayOfWeek())
- {
- /* 一周日期不相等 */
- return false;
- }
- /* 判断周日期是否相等 */
- if(m_personCountRule.StartTime.time() <= m_nowTime.time() && m_personCountRule.EndTime.time() >= m_nowTime.time())
- {
- return true;
- } else {
- /* 不在检测时间内 */
- return false;
- }
- }
- return true;
- }
- /* 更新房间列表 */
- void FuncRegionalPersonCount::updateRoomList()
- {
- /* 清空房间列表 */
- m_listRoomCamAct.clear();
- for(const auto& it : m_funcThreadInfo.listRoomCamActInfo)
- {
- /* 只添加直播间和导播间 */
- if(it.RoomType == Enum_RoomType::ROOM_LIVE || it.RoomType == Enum_RoomType::ROOM_DIC)
- {
- m_listRoomCamAct.push_back(it);
- }
- }
-
- }
- /* 自动结束报警 */
- void FuncRegionalPersonCount::autoEndAlarm()
- {
- EndAlarmParam alarmParam;
- alarmParam.ChannelID = m_funcThreadInfo.ChannelID;
- alarmParam.AppID = m_funcThreadInfo.appFunction;
- /* 判断报警是否已经结束,没有结束就结束报警 */
- if(findAlarmEnd(alarmParam))
- {
- SPDLOG_LOGGER_DEBUG(m_logger, "频率:{}《人员计数》报警已经结束", m_funcThreadInfo.strChannelName);
- return;
- }
- if(!m_fromWebAPI->endPersonCountAlarm(alarmParam))
- {
- SPDLOG_LOGGER_ERROR(m_logger, "频率:{}《人员计数》结束报警失败", m_funcThreadInfo.strChannelName);
- return;
- }
- /* 记录已经结束报警的信息,限制一下记录列表的大小 */
- if(m_listEndAlarmPara.size() > 1024)
- {
- m_listEndAlarmPara.clear();
- }
- m_listEndAlarmPara.push_back(alarmParam);
-
- }
- /* 查找是否已经结束了报警 */
- bool FuncRegionalPersonCount::findAlarmEnd(EndAlarmParam& alarmParam)
- {
- if(m_listEndAlarmPara.empty())
- {
- return false;
- }
- for(auto& it : m_listEndAlarmPara)
- {
- if(it == alarmParam)
- {
- return true;
- }
- }
- return false;
- }
|