#include "StraStreamPipe.h" #include "StrategyStation.h" #include "DBMgr.h" #include "ProtoCvt.h" #include "FileHelper.h" NAMESPACE_MAS_BEGIN StrategyStation::StrategyStation() { } StrategyStation::~StrategyStation() { this->Dispose(); } TZ_INT StrategyStation::Initialize() { this->loadStraLibs(); TZLogInfo("stra station init~~~"); return MEC_OK; } TZ_INT StrategyStation::Dispose() { TZLogInfo("stra station dispose~~~"); return MEC_OK; } TZ_INT StrategyStation::QueryStrategy( const QueryCondition & reqInfo, PageResult & info, std::list & infos) { std::list tinfos; std::vector cds; ORDER_PAIR order; ProtoCvt::Cvt(reqInfo, order, cds); DBMGR->QueryTblStrategyInfoByCd(cds, order, tinfos); auto irst = DBMGR->QueryTblStrategyInfoCount(cds, info.Total); if (irst) { TZLogWarn("query strategy count from db failed irst:%d!!!", irst); return irst; } info.Page = reqInfo.Page; info.Num = reqInfo.Size; info.TotalPage = (info.Total / reqInfo.Size) + ((info.Total % reqInfo.Size) ? 1 : 0); for (auto & mem : tinfos) { StrategyInfo info; ProtoCvt::Tbl2Json(mem, info); infos.emplace_back(info); } TZLogInfo("query strategy size %d~~~", infos.size()); return MEC_OK; } TZ_INT StrategyStation::EditStrategy( const StrategyInfo & info) { TblStrategyInfoItem tInfo; tInfo.Tb_Id = info.Id; auto irst = DBMGR->QueryTblStrategyInfoByKey(tInfo); if (irst) return irst; tInfo.Tb_StraDets = tzc::json::ArrayToJson(info.StraDets); tInfo.Tb_StraCfgs = tzc::json::ArrayToJson(info.StraCfgs); return DBMGR->AddOrUpdateTblStrategyInfo(tInfo); } SPtr StrategyStation::BuildPipe( TZ_INT sceneId, const std::string & sceneName, TZ_INT straId, const std::string & straParams) { TblStrategyInfoItem tbl; tbl.Tb_Id = straId; auto irst = DBMGR->QueryTblStrategyInfoByKey(tbl); if (irst) { TZLogWarn("can't find stra by id %d!!!", straId); return nullptr; } auto iter = m_straLib.find(straId); if (m_straLib.end() == iter) { TZLogWarn("can't find stra lib by %d!!!", straId); return nullptr; } ExcepInfo excepinfo(tbl.Tb_Name, 3000); auto pipe = std::make_shared( sceneId, sceneName, straId, straParams, excepinfo); irst = pipe->Initialize(iter->second); if (irst) { TZLogWarn("stra pipe %s init failed!!!", tbl.Tb_Name.c_str()); return nullptr; } TZLogInfo("build strapipe %s success~~~", tbl.Tb_Name.c_str()); return std::move(pipe); } TZ_INT StrategyStation::loadStraLibs() { std::set paths; FileHelper::GetComponentFilePaths(STRA_LIB_PATH, paths); for (auto & libpath : paths) { StrategyInfo stra; auto pstra = this->genStraLibIns(libpath, stra); if (!pstra) continue; this->updateDbByStrategy(stra, libpath); m_straLib.emplace(stra.Id, pstra); } return MEC_OK; } TZ_INT StrategyStation::updateDbByStrategy( StrategyInfo & stra, const std::string & libpath) { TblStrategyInfoItem tinfo; tinfo.Tb_Key = stra.Key; tinfo.Tb_Id = INVALID_PRIMARY_KEY; std::list infos; ORDER_PAIR order; std::vector cds; PUSH_VALUE_PAIR(cds, TblStrategyInfo::COL_KEY, tinfo.Tb_Key); auto irst = DBMGR->QueryTblStrategyInfoByCd(cds, order, infos); if (!irst) tinfo.Tb_Id = infos.front().Tb_Id; tinfo.Tb_Name = stra.Name; tinfo.Tb_Version = stra.Version; tinfo.Tb_Author = stra.Author; tinfo.Tb_Path = libpath; for (auto & ldet : stra.StraDets) { ldet.DetId = this->getDetIdByKey(ldet.Key); } tinfo.Tb_StraDets = tzc::json::ArrayToJson(stra.StraDets); tinfo.Tb_StraCfgs = tzc::json::ArrayToJson(stra.StraCfgs); tinfo.Tb_StraRsts = tzc::json::ArrayToJson(stra.StraRsts); tinfo.Tb_Description = stra.Description; irst = DBMGR->AddOrUpdateTblStrategyInfo(tinfo); stra.Id = tinfo.Tb_Id; TZLogInfo("load stra %s -- %s succeed, update db irst %d~~~", stra.Key.c_str(), stra.Name.c_str(), irst); return irst; } SPtr StrategyStation::genStraLibIns( const std::string & libpath, StrategyInfo & stra) { auto pstra = std::make_shared(libpath); TZ_INT irst = pstra->Initialize(stra); if (irst) { TZLogWarn("stra lib %s init failed!!!", libpath.c_str()); return nullptr; } TZLogInfo("stra %s -- %s init succeed~~~", stra.Key.c_str(), stra.Name.c_str()); return std::move(pstra); } TZ_INT StrategyStation::getDetIdByKey(const std::string & key) { std::list infos; ORDER_PAIR order; std::vector cds; PUSH_VALUE_PAIR(cds, TblDetectorInfo::COL_KEY, key); auto irst = DBMGR->QueryTblDetectorInfoByCd(cds, order, infos); if (irst || !infos.size()) { TZLogWarn("can't find det by key %s!!!", key.c_str()); return -1; } return infos.front().Tb_Id; } NAMESPACE_MAS_END