123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041 |
- #include "SPAServer.h"
- #include "spdlog/spdlog.h"
- #include <filesystem>
- #include "ThreadPool/ThreadPool.h"
- #include "GlobalInfo/GlobalVariable.h"
- #include "GlobalInfo/GlobalConfig.h"
- #include "UniversalFunc.h"
- #include <QApplication>
- #include <QVector>
- SPAServer::SPAServer()
- {
- m_logger = spdlog::get("SPAServer");
- if(m_logger == nullptr)
- {
- SPDLOG_ERROR("APAServer logger is nullptr");
- return;
- }
- /* 读取全局的配置文件 */
- QString strConfigFile = QApplication::applicationDirPath() + "/config.ini";
- if(!GConfig.readConfig(strConfigFile))
- {
- /* 读取配置文件失败,直接退出程序 */
- return;
- }
- GConfig.printValue();
-
- m_threadRunning = true;
- /* 初始化WebAPI */
- m_toEQMDataBase.initWebApi("http://192.1.3.133:31000/v6/", "", "4c2f9fc91c22dd98331e47af2e2964f4");
- /* 模拟违禁品算法ID,后续需要动态调整 */
- g_actionList.ActContraband = "OD210_026_005246_001-IZRTKyEx";
-
- }
- SPAServer::~SPAServer()
- {
- }
- /* 启动服务 */
- void SPAServer::startServer()
- {
- /* 添加获取基础信息的线程 */
- // CPPTP.add_task(&SPAServer::threadFromSuperBrain, this);
- /* 测试Redis读取并解析数据线程 */
- CameraThreadInfo info;
- info.RedisIP = "172.16.36.80";
- info.RedisPort = 32222;
- info.RedisPWD = "Ff1z@TOFr^iwd%Ra";
- info.DeviceID = 117;
- info.vecAction.push_back("OD210_026_005246_001-IZRTKyEx");
- g_actionList.ActContraband = "OD210_026_005246_001-IZRTKyEx";
- // CPPTP.add_task(&SPAServer::threadFromRedis, this, info);
- // threadFromRedis(info);
- }
- /**
- * @brief 从基础平台获取算法信息和设备信息的线程函数
- * 从基础平台获取算法信息和设备信息,然后更新到EQM数据库所对应的表中
- *
- */
- void SPAServer::threadFromSuperBrain()
- {
- SPDLOG_LOGGER_INFO(m_logger, "开启 fromSuperBrainThread 线程, 获取算法和设备信息");
- /* 创建变量 */
- std::vector<AlgorithmInfo> vecAlgNewInfo;
- std::vector<DeviceInfo> vecDevNewInfo;
- /* 获取一次token,后续失效了再获取 */
- m_fromSuperBrain.getToken();
- while (m_threadRunning)
- {
- SPDLOG_LOGGER_INFO(m_logger, "刷新算法和设备信息");
- /* 先更新数据库的信息,防止从其他地方更改了数据库,这里没有刷新本地缓存 */
- m_toEQMDataBase.getAlgorithmInfo(m_vecEqmAlgInfo);
- m_toEQMDataBase.getDeviceInfo(m_vecEqmDevInfo);
- m_toEQMDataBase.getDeviceAlgorithmInfo(m_vecEqmDevInfo, m_listDevIDDelete);
- /* 从超脑获取基础信息 */
- m_fromSuperBrain.getTaskTypeList(vecAlgNewInfo);
- m_fromSuperBrain.getDeviceList(vecDevNewInfo);
- /* 处理算法信息 */
- bool algIsUpdate = processAlgorithmInfo(vecAlgNewInfo);
- /* 处理设备信息 */
- bool devIsUpdate = processDeviceInfo(vecDevNewInfo);
- vecAlgNewInfo.clear();
- vecDevNewInfo.clear();
- /* 更新算法详细信息 */
- m_mutexActionInfo.lock();
- m_toEQMDataBase.getActionInfo(m_listActionInfo);
- m_mutexActionInfo.unlock();
- /* 10秒更新一次 */
- std::this_thread::sleep_for(std::chrono::seconds(10));
- }
- SPDLOG_LOGGER_INFO(m_logger, "退出 fromSuperBrainThread 线程");
- }
- /* 处理算法信息,返回值为true,说明有改变,需要重新读取 */
- bool SPAServer::processAlgorithmInfo(std::vector<AlgorithmInfo> vecNewAlgInfo)
- {
- std::vector<AlgorithmInfo> vecAlgUpdate;
- std::vector<AlgorithmInfo> vecAlgDelete;
- /* 对比数据库表格信息,这里只有插入和删除,没有更新 */
- compareAlgorithmInfo(vecNewAlgInfo, vecAlgUpdate, vecAlgDelete);
-
- /* 更新数据库,先删除,再写入刷新 */
- bool isUpdate = false;
- if(vecAlgDelete.size() > 0)
- {
- SPDLOG_LOGGER_DEBUG(m_logger, "删除算法信息");
- m_toEQMDataBase.deleteAlgorithmInfo(vecAlgDelete);
- isUpdate = true;
- }
- if(vecAlgUpdate.size() > 0)
- {
- SPDLOG_LOGGER_DEBUG(m_logger, "写入算法信息");
- m_toEQMDataBase.writeAlgorithmInfo(vecAlgUpdate);
- isUpdate = true;
- }
- return isUpdate;
- }
- /**
- * @brief 处理设备信息
- *
- * @param vecNewDevInfo 传入新获取到的值
- * @return true 需要重新读取数据库,获取新的数据
- * @return false 无需读取数据库
- */
- bool SPAServer::processDeviceInfo(std::vector<DeviceInfo> vecNewDevInfo)
- {
- std::vector<DeviceInfo> vecDevInsert;
- std::vector<DeviceInfo> vecDevUpdate;
- std::vector<DeviceInfo> vecDevDelete;
- /*-------------------------------------------------------------------------
- ****** 这里只对比设备信息,不对比设备的算法信息,算法信息在下面单独对比 *******
- *------------------------------------------------------------------------*/
- /* 如果本地缓存没有数据,那么就全部插入 */
- if(m_vecEqmDevInfo.size() > 0)
- {
- for(auto& DevInfo : vecNewDevInfo)
- {
- bool isExist = false;
- for(auto& it0 : m_vecEqmDevInfo)
- {
- if(DevInfo.DeviceID == it0.DeviceID)
- {
- isExist = true;
- /* 对比其他项是否相等,不相等就更新 */
- if(DevInfo == it0)
- {
- continue;
- }else {
- vecDevUpdate.push_back(DevInfo);
- }
- break;
- }
- }
- if(!isExist)
- {
- vecDevInsert.push_back(DevInfo);
- }
- }
- }else {
- vecDevInsert = vecNewDevInfo;
- }
- /* 获取删除列表 */
- if(vecNewDevInfo.size() > 0)
- {
- bool isExist = false;
- for(const auto& it : m_vecEqmDevInfo)
- {
- isExist = false;
- for(const auto& it0 : vecNewDevInfo)
- {
- if(it.DeviceID == it0.DeviceID)
- {
- isExist = true;
- break;
- }
- }
- if(!isExist)
- {
- vecDevDelete.push_back(it);
- }
- }
- }else {
- vecDevDelete = m_vecEqmDevInfo;
- }
- bool isUpdate = false;
- /* 先删除多余的数据 */
- if(vecDevDelete.size() > 0)
- {
- SPDLOG_LOGGER_DEBUG(m_logger, "删除设备信息");
- m_toEQMDataBase.deleteDeviceInfo(vecDevDelete);
- isUpdate = true;
- }
- /* 更新数据 */
- if(vecDevUpdate.size() > 0)
- {
- SPDLOG_LOGGER_DEBUG(m_logger, "更新设备信息");
- m_toEQMDataBase.updateDeviceInfo(vecDevUpdate);
- isUpdate = true;
- }
- /* 插入数据 */
- if(vecDevInsert.size() > 0)
- {
- SPDLOG_LOGGER_DEBUG(m_logger, "插入设备信息");
- m_toEQMDataBase.insertDeviceInfo(vecDevInsert);
- isUpdate = true;
- }
- /*-------------------------------------------------------------------------
- ************* 处理设备和算子关联的表格,单独对比设备的算法信息 *************
- *------------------------------------------------------------------------*/
- /* 插入新的设备信息 */
- if(vecDevInsert.size() > 0)
- {
- SPDLOG_LOGGER_DEBUG(m_logger, "插入设备和算法关联表");
- m_toEQMDataBase.insertDeviceAlgorithmInfo(vecDevInsert);
- isUpdate = true;
- }
- vecDevUpdate.clear();
- /* 对比现有的设备是否需要更新算法 */
- compareDeviceAlgorithmInfo(vecNewDevInfo, vecDevUpdate);
- if(vecDevUpdate.size() > 0)
- {
- SPDLOG_LOGGER_DEBUG(m_logger, "更新设备和算法关联表, 更新设备数目:{}", vecDevUpdate.size());
- m_toEQMDataBase.updateDeviceAlgorithmInfo(vecDevUpdate);
- }
-
-
- /* 删除tActionCamer表中消失的设备信息 */
- if(m_listDevIDDelete.size() > 0)
- {
- SPDLOG_LOGGER_DEBUG(m_logger, "删除消失的设备关联的算法");
- m_toEQMDataBase.deleteDeviceAlgorithmInfo(m_listDevIDDelete);
- isUpdate = true;
- }
- return isUpdate;
- }
- /* 对比现有的数据和新获取到的数据,取出要删除和添加的数据 */
- void SPAServer::compareAlgorithmInfo(const std::vector<AlgorithmInfo>& vecNewInfo, std::vector<AlgorithmInfo>& vecAlgUpdate, std::vector<AlgorithmInfo>& vecAlgDelete)
- {
- /* 取出要添加的,如果本地缓存是0,那么全部都要添加 */
- if(m_vecEqmAlgInfo.size() > 0)
- {
- for(const auto& it : vecNewInfo)
- {
- bool isExist = false;
- for(const auto& it0 : m_vecEqmAlgInfo)
- {
- /* 如果存在就退出循环 */
- if(it.ActionID == it0.ActionID)
- {
- isExist = true;
- break;
- }
- }
- if(!isExist)
- {
- vecAlgUpdate.push_back(it);
- }
- }
- }else {
- vecAlgUpdate = vecNewInfo;
- }
- /* 取出要删除的,如果新的数据是0,那么全部都要删除 */
- if(vecNewInfo.size() > 0)
- {
- bool isExist = false;
- for(const auto& it : m_vecEqmAlgInfo)
- {
- isExist = false;
- for(const auto& it0 : vecNewInfo)
- {
- if(it.ActionID == it0.ActionID)
- {
- isExist = true;
- break;
- }
- }
- if(!isExist)
- {
- vecAlgDelete.push_back(it);
- }
- }
- }else {
- vecAlgDelete = m_vecEqmAlgInfo;
- }
- }
- /**
- * @brief 对比设备和算法关联表是否需要更新
- * 对比规则:
- * 1、这里只对比已有的设备ID,需要删除的ID在获取到tActionCamer表是就已经取出来了
- * 2、如果设备ID相等,那么进一步对比算法信息是否相等
- * 3、如果设备ID相等,但是算法信息数目不相等,那么直接加入更新列表
- * 4、如果设备ID相等,算法信息数目相等,进一步对比算法信息
- *
- * @param vecNewInfo
- * @param vecDevUpdate
- */
- void SPAServer::compareDeviceAlgorithmInfo(const std::vector<DeviceInfo>& vecNewInfo, std::vector<DeviceInfo>& vecDevUpdate)
- {
- vecDevUpdate.clear();
- for(const auto& it0 : vecNewInfo)
- {
- for(const auto& it1 : m_vecEqmDevInfo)
- {
- if(it0.DeviceID == it1.DeviceID)
- {
- /* 设备的算法信息数目不相等,直接加入更新列表 */
- if(it0.vecAlgorithmInfo.size() != it1.vecAlgorithmInfo.size())
- {
- vecDevUpdate.push_back(it0);
- break;
- }
- /* 设备的算法信息数目相等,进一步对比算法信息 */
- bool isEquality = true;
- for(const auto& it2 : it0.vecAlgorithmInfo)
- {
- bool isEq2 = false;
- for(const auto& it3 : it1.vecAlgorithmInfo)
- {
- /* 这里只对比算法ID */
- if(it2.ActionID != it3.ActionID)
- {
- continue;
- }else {
- isEq2 = true;
- break;
- }
- }
- if(!isEq2)
- {
- isEquality = false;
- break;
- }
- }
- if(!isEquality)
- {
- vecDevUpdate.push_back(it0);
- break;
- }
- }
- }
- }
- }
- /**
- * @brief 从Redis获取数据线程函数,这个是摄像机线程
- * 一个设备一个线程,这个线程的相关变量只在这个线程中使用
- * (注意,这个函数未被使用,不从这里获取Redis数据)
- * @param info
- */
- void SPAServer::threadFromRedis(const CameraThreadInfo& info)
- {
- SPDLOG_LOGGER_INFO(m_logger, "开启 fromRedisThread 线程,设备ID:{}", info.DeviceID);
- FromRedis fromRedis;
- fromRedis.setRedisIPAndPort(info.RedisIP, info.RedisPort);
- fromRedis.setRedisPassword(info.RedisPWD);
- if(fromRedis.connectRedis())
- {
- SPDLOG_LOGGER_INFO(m_logger, "连接Redis成功");
- }else {
- SPDLOG_LOGGER_ERROR(m_logger, "连接Redis失败");
- return;
- }
- CameraThreadInfo threadInfo = info;
- // std::string strKey = "117:OD210_026_005246_001-IZRTKyEx";
- /* 取出该设备的Key */
- std::vector<std::string> vecKey;
- for(const auto& it : info.vecAction)
- {
- std::string strKey = std::to_string(info.DeviceID) + ":" + it;
- vecKey.push_back(strKey);
- }
- std::string strRetValue;
- /* 进入线程循环 */
- while (m_threadRunning)
- {
- /* 循环读取这个设备关联的算法信息 */
- for(const auto& it : vecKey)
- {
- SPDLOG_LOGGER_INFO(m_logger, "读取Redis信息, Key: {}", it);
- if( !fromRedis.getRedisString(it, strRetValue) )
- {
- continue;
- }
- SPDLOG_LOGGER_TRACE(m_logger, "Redis Value:\n{}", strRetValue);
- /* 解析数据 */
- AlarmInfo alarmInfo;
- alarmInfo.ActionID = it.substr(it.find(":") + 1);
- /* 解析数据 */
- parseRedisBaseData(strRetValue, alarmInfo);
- parseRedisBBoxesData(strRetValue, alarmInfo);
- /* 信息时间有的效性判断,主要是记录Redis数据的有效性,是否长时间没有变化,排查错误时用的
- * 如果数据长时间数据不变,那么超脑那里就挂了,写入日志 */
- isEventTimeVaild(alarmInfo.EventTime);
-
- /* 是否需要写入EQM数据库判断
- * 人员计数和区域人员检测需要多次判断,其他的直接写入即可 */
-
- }
- std::this_thread::sleep_for(std::chrono::seconds(2));
- }
-
- return;
- }
- /**
- * @brief 判断时间是否长时间没有更新,默认的是600秒,超过这个时间Redis还未更新,可能是超脑挂了
- *
- * @param strTime
- * @return true
- * @return false
- */
- bool SPAServer::isEventTimeVaild(const std::string& strTime)
- {
- /* 获取当前时间 */
- std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
- /* 字符串转成时间 */
- std::istringstream iss(strTime);
- std::tm tmEvent = {};
- iss >> std::get_time(&tmEvent, "%Y-%m-%d %H:%M:%S");
- /* 时间差 */
- std::chrono::system_clock::time_point eventTime = std::chrono::system_clock::from_time_t(std::mktime(&tmEvent));
- std::chrono::duration<double> diff = now - eventTime;
- // SPDLOG_LOGGER_DEBUG(m_logger, "now:{} eventTime: {} 时间差:{}秒",now, eventTime, diff);
- if(diff.count() > 600)
- {
- // SPDLOG_LOGGER_ERROR(m_logger, "Redis数据长时间没有更新,EventTime:{}", strTime);
- return false;
- }
- return true;
- }
- /**
- * @brief 分派任务的线程
- 1、 线程定时刷新房间和摄像机的关联关系,以及和算法Action的关联关系
- 2、 将算法信息加入到不同的列表中
- 需要多个摄像机配合的加入到m_runListRoomActionInfo列表
- 不需要多个摄像机配合的加入到m_runListActionInfo列表
- 3、 每次刷新都会清空ActionInfo或者RoomActionInfo的摄像机列表,但是不会动其他基本信息,如果是列表中没有对应的信息,
- 就会创建新的ActionInfo,那么RunState的状态是INIT,需要开启新的线程
- 如果刷新完成后,算法对应的摄像机列表为空,那么对应的线程就会停止运行,将RunState设置为STOP,本线程的下一轮
- 循环就会删除这个ActionInfo
- */
- void SPAServer::threadRoomCamera()
- {
- /* 房间相机关联信息 */
- std::list<RoomCameraInfo> listRC;
- /* 存储获取到的应用和启用时间的信息 */
- std::list<AppAndTimeInfo> listAppAndTime;
- /* 创建连接数据库实例 */
- std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
- while (m_threadRunning)
- {
- GThreadInfo.lockRunFAI();
- /* 先清理已经退出的线程所用到的Action或者RoomAction */
- GThreadInfo.clearNoneFuncActionInfo();
- /* 创建应用信息,根据从EQM数据库读取到的配置的应用信息创建 */
- toEQMDataBase->getAlarmAppInfo(listAppAndTime);
- for(const auto& it : listAppAndTime)
- {
- /* 创建应用信息,如果已有该应用,就更新时间 */
- GThreadInfo.addFuncActionInfo(it);
- }
- /* 先获取EQM数据库信息,取出房间和摄像机关联信息 */
- m_mutexActionInfo.lock();
- toEQMDataBase->getActionInfo(m_listActionInfo);
- /* 将算法信息加入到不同的列表中 */
- GThreadInfo.clearActionList();
- for(const auto& it : m_listActionInfo.getData())
- {
- GThreadInfo.addActionInfo(*it);
- }
- /* 检查算法信息的状态,频率里的应用没有配置摄像机就设置为线程运行状态为停止,退出该线程 */
- GThreadInfo.setNoneCameraFuncStop();
- m_mutexActionInfo.unlock();
- /* 开启线程 */
- for(const auto& it0 : GThreadInfo.getList())
- {
- /* 人员在岗识别 */
- /* 区域人员检测 */
- /* 非法入侵检测 */
- /* 违禁品识别 */
-
- /* 普通任务线程,一个任务对应一个摄像机和一个算法ID,无需联动 */
- if(it0->appFunction == AppFunction::APP_AllDown)
- {
- if(it0->RunState == RunTimeState::RUN_STATE_INIT)
- {
- // CPPTP.add_task(&SPAServer::threadActNormal, this, it0);
- }
- }
- }
-
- GThreadInfo.unlockRunFAI();
- /* 休眠n秒,默认应该是300秒 */
- std::this_thread::sleep_for(std::chrono::seconds(GConfig.CheckSet));
- }
- }
- /**
- * @brief 人员在岗识别线程,这个功能主要是给客户端提供实时的人脸识别信息
- * 1、这个写入tWorkOnInfo表格,十分钟写一次
- * 2、理论上这里需要实时更新数据库,但是实际上时每十分钟写入一次数据
- * 3、这个线程也会进行在岗离岗的识别报警
- *
- * @param RFAInfo 传入房间ID和算法ID
- */
- void SPAServer::threadActPersonWork(FuncActionInfo* RFAInfo)
- {
- SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程,ChannelID:{} ,", RFAInfo->strFunctionName, RFAInfo->ChannelID);
- /* 创建读取Redis的实例 */
- std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
- /* 创建写入数据库实例 */
- std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
- /* 局部变量 */
- std::shared_ptr<FuncActionInfo> pRFAInfo = std::make_shared<FuncActionInfo>();
- *pRFAInfo = *RFAInfo;
- /* 保存每个摄像机的报警信息 */
- std::shared_ptr<std::list<AlarmInfo>> pListAlarmInfo = std::make_shared<std::list<AlarmInfo>>();
- /* 保存人脸信息的数据 */
- std::shared_ptr<ListRoomFaceInfo> pListRoomFaceInfo = std::make_shared<ListRoomFaceInfo>();
-
- while (m_threadRunning)
- {
- /* 更新线程信息 */
- GThreadInfo.updateFuncInfo(pRFAInfo.get());
- if(pRFAInfo->appFunction == AppFunction::APP_NONE)
- {
- break;
- }
- /* 读取Redis数据 */
- pListAlarmInfo->clear();
- for(const auto& RoomInfo : pRFAInfo->listRoomCamActInfo)
- {
- for(const auto& it : RoomInfo.mapCameraAction)
- {
- std::string strKey = std::to_string(it.first) + ":" + it.second;
- std::string strRetValue;
- if(!fromRedis->getRedisString(strKey, strRetValue))
- {
- SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- continue;
- }
- /* 解析数据 */
- AlarmInfo alarmInfo;
- parseRedisBaseData(strRetValue, alarmInfo);
- parseRedisBBoxesData(strRetValue, alarmInfo);
- /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
- if(isEventTimeVaild(alarmInfo.EventTime))
- {
- SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- continue;
- }
- pListAlarmInfo->push_back(alarmInfo);
- }
- }
- /* 处理数据,将报警信息的人脸信息取出来,放入到人脸信息列表中 */
- for(auto& alarmInfo : *pListAlarmInfo)
- {
- /* 添加到人脸列表中,会自动去重,自动创建对应的频道和房间信息 */
- pListRoomFaceInfo->addRoomFaceInfo(alarmInfo);
- }
- /* 计算人脸个数,并判断是否需要写入数据库 */
- QDateTime now = QDateTime::currentDateTime();
- for(auto it = pListRoomFaceInfo->listRoomFaceInfo.begin(); it != pListRoomFaceInfo->listRoomFaceInfo.end(); )
- {
- if(it->MaxNum < it->listPersonInfo.size())
- {
- it->MaxNum = it->listPersonInfo.size();
- }
- if(it->MinNum > it->listPersonInfo.size())
- {
- it->MinNum = it->listPersonInfo.size();
- }
- /* 判断是否需要写入数据库,超过设置的时间就写入,默认是600秒 */
- if(now.toSecsSinceEpoch() - it->StartTime.toSecsSinceEpoch() > GConfig.AppUpdateOnWorkTimeInterval)
- {
- /* 写入数据库 */
- if(toEQMDataBase->insertOnWorkInfo(*it))
- {
- SPDLOG_LOGGER_INFO(m_logger, "ChannelID:{}, RoomID:{}, 在岗信息写入数据库成功, StartTime", it->ChannelID, it->RoomID, it->StartTime.toString("yyyy-MM-dd hh:mm:ss").toStdString());
- /* 删除这条信息 */
- it = pListRoomFaceInfo->listRoomFaceInfo.erase(it);
- continue;
- }
- }
- ++it;
- }
- std::this_thread::sleep_for(std::chrono::milliseconds(GConfig.ThreadSleepMS));
- }
- GThreadInfo.setThreadState(pRFAInfo.get(), RunTimeState::RUN_STATE_STOP);
- SPDLOG_LOGGER_INFO(m_logger, "关闭 {} 线程,ChannelID:{}", pRFAInfo->strFunctionName, pRFAInfo->ChannelID);
- }
- /**
- * @brief 区域非法入侵检测
- 1、野猫、宠物识别:报警判断条件:机房内出现摄像头的宠物识别算法输出报警结果时,记为报警行为,直接
- 展示报警结果
- 2、无权限人员识别:报警判断条件:当判断出区域非法入侵行为时,依据人脸识别结果,判断是否为人脸库人
- 员,非人脸库的人员时判断为非法入侵行为(背影需要报警需要结合区域人员检测算法判断)
- 3、人员计数和人脸识别数不相等时,判断为非法入侵行为
-
- 检测逻辑:
- 1、先检测非法的人脸信息
- 2、再检测人员计数和人脸识别数不相等
- *
- * @param info
- */
- void SPAServer::threadActIllegalInvasion(FuncActionInfo* RFAInfo)
- {
- SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程,ChannelID:{} ,", RFAInfo->strFunctionName, RFAInfo->ChannelID);
- /* 创建读取Redis的实例 */
- std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
- /* 创建写入数据库实例 */
- std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
- /* 局部变量 */
- std::shared_ptr<FuncActionInfo> pRFAInfo = std::make_shared<FuncActionInfo>();
- *pRFAInfo = *RFAInfo;
- /* 保存每个摄像机的报警信息 */
- std::shared_ptr<std::list<AlarmInfo>> pListAlarmInfo = std::make_shared<std::list<AlarmInfo>>();
- /* 保存非法入侵的信息 */
- std::shared_ptr<ListIllegalInvasionInfo> pListIllInfo = std::make_shared<ListIllegalInvasionInfo>();
-
- std::string strFaceActionID; /* 人脸识别的Action */
- std::string strCountActionID; /* 人员计数的Action */
- /* 封装lambda表达式 */
- std::function<bool(const std::vector<PersonInfo>&)> funcIsAlarm = [](const std::vector<PersonInfo>& vecPersion) {
- for(const auto& it : vecPersion)
- {
- if(it.PersonID == "-1")
- {
- return true;
- }
- }
- return false;
- };
- while (m_threadRunning)
- {
- /* 更新线程信息 */
- GThreadInfo.updateFuncInfo(pRFAInfo.get());
- if(pRFAInfo->appFunction == AppFunction::APP_NONE)
- {
- break;
- }
- /* 更新算法ActionID */
- {
- g_actionList.mutexRW.lockForRead();
- strFaceActionID = g_actionList.ActFace;
- strCountActionID = g_actionList.ActPersonNumber;
- g_actionList.mutexRW.unlock();
- }
- /* 读取Redis数据 */
- pListAlarmInfo->clear();
- for(const auto& RoomInfo : pRFAInfo->listRoomCamActInfo)
- {
- for(const auto& it : RoomInfo.mapCameraAction)
- {
- std::string strKey = std::to_string(it.first) + ":" + it.second;
- std::string strRetValue;
- if(!fromRedis->getRedisString(strKey, strRetValue))
- {
- SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- continue;
- }
- /* 解析数据 */
- AlarmInfo alarmInfo;
- // parseRedisData(strRetValue, alarmInfo);
- parseRedisBaseData(strRetValue, alarmInfo);
- parseRedisBBoxesData(strRetValue, alarmInfo);
- /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
- if(isEventTimeVaild(alarmInfo.EventTime))
- {
- SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- continue;
- }
- pListAlarmInfo->push_back(alarmInfo);
- }
- }
- /* 找出房间的个数,按照房间进行判断 */
- ListRoomIll listRoomIll;
- for(auto& it : pRFAInfo->listRoomCamActInfo)
- {
- listRoomIll.addRoom(it.RoomID, it.RoomType);
- }
- /* 根据房间解析人脸识别的非法入侵,这个有了就不用判断后面的人员计数不相等了 */
- for(auto& room : listRoomIll.getData())
- {
- for(const auto& it : *pListAlarmInfo)
- {
- /* 人俩识别算法 */
- if(it.ActionID == strFaceActionID)
- {
- if(it.vecPersonInfo.size() > room.numMaxFace)
- {
- room.numMaxFace = it.vecPersonInfo.size();
- room.strBoxList = it.listBbox;
- room.vecPersonInfo = it.vecPersonInfo;
- }
- }
- /* 判断有没有非法入侵人员 */
- if(funcIsAlarm(it.vecPersonInfo))
- {
- room.isAlarm = true;
- room.strMessage = "人员非法入侵(未知人员)";
- if( !it.ImageInfo.empty() )
- {
- room.strImage = it.ImageInfo;
- }
- room.CameraID = it.DeviceID;
- }
- }
-
- }
- /* 判断人脸识别到的数目和人员计数算法识别到的人数是否相等,如果上面报警了,这里就不用检测了 */
- for(auto& room : listRoomIll.getData())
- {
- if(room.isAlarm)
- {
- continue;
- }
- /* 这个房间的人脸算法没有识别到非法入侵 */
- for(const auto& it : *pListAlarmInfo)
- {
- if(it.ActionID == strCountActionID)
- {
- if(it.vecPersonInfo.size() > room.numMaxPerson)
- {
- room.numMaxPerson = it.vecPersonInfo.size();
- room.strBoxList = it.listBbox;
- if(!it.ImageInfo.empty())
- {
- room.strImage = it.ImageInfo;
- }
- room.CameraID = it.DeviceID;
- }
- }
- }
- /* 判断人数是否一致 */
- if(room.numMaxFace != room.numMaxPerson)
- {
- room.strMessage = fmt::format("非法入侵(人员计数 {} 和人脸数 {} 不一致)", room.numMaxPerson, room.numMaxFace);
- room.isAlarm = true;
- }
- }
- /* 将非法入侵的信息存储到数组缓存中,等待报警结束后判断是否需要写入EQM数据库 */
- for(auto& room : listRoomIll.getData())
- {
- auto pIll = pListIllInfo->findIllInfo(room.RoomID, room.RoomType);
- if(room.isAlarm)
- {
- /* 正在报警,检查是否有 */
- if(pIll == nullptr)
- {
- IllegalInvasionInfo info;
- info.ChannelID = pRFAInfo->ChannelID;
- info.CameraID = room.CameraID;
- info.RoomID = room.RoomID;
- info.RoomType = room.RoomType;
- info.strActionDec = room.strMessage;
- info.strImageInfo = room.strImage;
- info.FirstTime = QDateTime::currentDateTime();;
- pListIllInfo->addIllInfo(info);
- }
- }
- else
- {
- /* 没有报警,检查是否是报警结束了,是否符合写入数据库的条件 */
- QDateTime currTime = QDateTime::currentDateTime();
- int secs = currTime.toSecsSinceEpoch() - pIll->FirstTime.toSecsSinceEpoch();
- if(secs >= GConfig.AppBadMan)
- {
- if(!pIll->strImageInfo.empty())
- {
- /* 超过非法入侵的时间,写入数据库 */
- AlarmInfo ai;
- ai.ChannelID = pRFAInfo->ChannelID;
- ai.RoomID = pIll->RoomID;
- ai.ActionID = g_actionList.ActPersonNumber;
- ai.ActionDes = pIll->strActionDec;
- ai.ImageInfo = pIll->strImageInfo;
- ai.StartTime = pIll->FirstTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
- ai.EndTime = currTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
- ai.EventTime = pIll->FirstTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
- ai.vecPersonInfo = room.vecPersonInfo;
- ai.listBbox = room.strBoxList;
- bool insertRet = toEQMDataBase->insertAlarmInfo(ai);
- if(insertRet)
- {
- std::string actionName = g_actionList.getActionName(ai.ActionID);
- SPDLOG_LOGGER_INFO(m_logger, "☆ 新增报警信息,频道[{}],房间[{}],摄像头[{}],{},{},有{}个区域,有{}个人脸,{}",
- pIll->ChannelID, pIll->RoomID, pIll->CameraID, actionName, ai.ActionDes, ai.listBbox.size(), ai.FaceIDList.size(), ai.ImageInfo);
- }
- }
- /* 删除这个非法入侵信息 */
- pListIllInfo->deleteIllInfo(*pIll);
- }
- }
- }
- std::this_thread::sleep_for(std::chrono::milliseconds(GConfig.ThreadSleepMS));
- }
- GThreadInfo.setThreadState(pRFAInfo.get(), RunTimeState::RUN_STATE_STOP);
- SPDLOG_LOGGER_INFO(m_logger, "关闭 {} 线程,ChannelID:{}", pRFAInfo->strFunctionName, pRFAInfo->ChannelID);
- }
- /**
- * @brief 区域人员检测,检测这个区域内的人数,不能少于多少人,不能多余多少人
- * 1、报警判断条件:直播间报警:当前频率所有直播间摄像头的区域人员检测算法输出结果均大于或小于设定人
- 数值时,记为报警行为,直接展示报警结果
- 2、录播间报警:当前频率所有录播间摄像头的区域人员检测算法输出结果均大于或小于设定人数值时,记为报
- 警行为,直接展示报警结果
- 3、直播间+录播间报警:当前频率直播间人数+录播间人数大于或小于设定人数值时,记为报警行为,直接展示
- 报警结果
- *
- * @param RAInfo 传入的房间ID和算法ID
- */
- void SPAServer::threadActRegionalPersonnelDetection(FuncActionInfo* RFAInfo)
- {
- SPDLOG_LOGGER_INFO(m_logger, "开启 {} 线程,ChannelID:{} ,", RFAInfo->strFunctionName, RFAInfo->ChannelID);
- /* 创建读取Redis的实例 */
- std::shared_ptr<FromRedis> fromRedis = std::make_shared<FromRedis>();
- /* 创建写入数据库实例 */
- std::shared_ptr<ToEQMDataBase> toEQMDataBase = std::make_shared<ToEQMDataBase>();
- /* 局部变量,线程功能需要的信息 */
- std::shared_ptr<FuncActionInfo> pRFAInfo = std::make_shared<FuncActionInfo>();
- *pRFAInfo = *RFAInfo;
- /* 保存每个摄像机的报警信息 */
- std::shared_ptr<std::list<AlarmInfo>> pListAlarmInfo = std::make_shared<std::list<AlarmInfo>>();
- /* 报警时间段 */
- std::shared_ptr<std::vector<PersonCountRuleInfo>> pPersonCountRuleInfo = std::make_shared<std::vector<PersonCountRuleInfo>>();
- /* 获取报警规则信息 */
- toEQMDataBase->getPersonCountRuleInfo(*pPersonCountRuleInfo);
-
- while (m_threadRunning)
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(GConfig.ThreadSleepMS));
- /************ 更新线程信息 ************/
- GThreadInfo.updateFuncInfo(pRFAInfo.get());
- if(pRFAInfo->appFunction == AppFunction::APP_NONE)
- {
- break;
- }
- /************ 读取Redis数据 ************/
- pListAlarmInfo->clear();
- for(const auto& RoomInfo : pRFAInfo->listRoomCamActInfo)
- {
- for(const auto& it : RoomInfo.mapCameraAction)
- {
- std::string strKey = std::to_string(it.first) + ":" + it.second;
- std::string strRetValue;
- if(!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;
- }
- pListAlarmInfo->push_back(alarmInfo);
- }
- }
- /************ 获取报警规则(从数据库更新?) ************/
- if(!toEQMDataBase->getPersonCountRuleInfo(*pPersonCountRuleInfo))
- {
- SPDLOG_LOGGER_ERROR(m_logger, "《人员计数》获取报警规则失败");
- continue;
- }
- if(pPersonCountRuleInfo->size() == 0)
- {
- SPDLOG_LOGGER_ERROR(m_logger, "《人员计数》无报警规则");
- break;
- }
- /* 获取这个频率的报警信息 */
- PersonCountRuleInfo personCountRuleInfo;
- for(auto& it : *pPersonCountRuleInfo)
- {
- if(it.ChannelID == pRFAInfo->ChannelID)
- {
- personCountRuleInfo = it;
- break;
- }
- }
- if(personCountRuleInfo.ChannelID < 0)
- {
- SPDLOG_LOGGER_ERROR(m_logger, "《人员计数》获取报警规则失败, ChannelID:{}", pRFAInfo->ChannelID);
- break;
- }
- /************ 判断是否在检测时间内 ************/
- QDateTime now = QDateTime::currentDateTime();
- /* 判断检测时间类型 */
- if(personCountRuleInfo.RuleType == 0)
- {
- /* 所有时段 */
- }
- else if(personCountRuleInfo.RuleType == 1)
- {
- /* 按天检测 */
- if(personCountRuleInfo.StartTime <= now && personCountRuleInfo.EndTime >= now)
- {
- /* 在检测时间内 */
- } else
- {
- /* 不在检测时间内 */
- continue;
- }
- }
- else if (personCountRuleInfo.RuleType == 2)
- {
- /* 按周检测 */
- if(personCountRuleInfo.week != now.date().dayOfWeek())
- {
- /* 不在检测时间内 */
- continue;
- }
- /* 在检测时间内 */
- if(personCountRuleInfo.StartTime.time() <= now.time() && personCountRuleInfo.EndTime.time() >= now.time())
- {
- /* 在检测时间内 */
- } else
- {
- /* 不在检测时间内 */
- continue;
- }
- }
- /************ 挨个房间检测人数 ************/
-
- /************ 检测频率直播间 + 导播间房间的人数 ************/
- }
- GThreadInfo.setThreadState(pRFAInfo.get(), RunTimeState::RUN_STATE_STOP);
- SPDLOG_LOGGER_INFO(m_logger, "关闭 {} 线程,ChannelID:{}", pRFAInfo->strFunctionName, pRFAInfo->ChannelID);
- }
- /* 将该算法对应的摄像机放入摄像机列表 */
- bool SPAServer::insertCameraToAction(RoomActionInfo* pRAInfo, std::list<RoomCameraInfo>& listRC, std::multimap<int, std::string>& mapCameraActionID)
- {
- for(const auto& rci : listRC)
- {
- if(rci.RoomID == pRAInfo->RoomID)
- {
- /* 这个摄像机在这个房间内,再判断这个摄像机有没有这个算法 */
- for(const auto& camID : rci.listCameraID)
- {
- for(const auto& cai : mapCameraActionID)
- {
- if(camID == cai.first)
- {
- /* 再判断这个摄像机的算法是否是当前需要的 */
- if(cai.second == pRAInfo->ActionID)
- {
- pRAInfo->listCameraID.push_back(camID);
- }
- }
- }
- }
- }
- }
- return true;
- }
|