123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- #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<StrategyInfo> & infos)
- {
- std::list<TblStrategyInfoItem> tinfos;
- std::vector<sdb::ValuePair> 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<IStreamPipe> 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<StraStreamPipe>(
- 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<std::string> 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<TblStrategyInfoItem> infos;
- ORDER_PAIR order;
- std::vector<sdb::ValuePair> 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<StrategyLib> StrategyStation::genStraLibIns(
- const std::string & libpath, StrategyInfo & stra)
- {
- auto pstra = std::make_shared<StrategyLib>(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<TblDetectorInfoItem> infos;
- ORDER_PAIR order;
- std::vector<sdb::ValuePair> 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
|