StrategyStation.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "StraStreamPipe.h"
  2. #include "StrategyStation.h"
  3. #include "DBMgr.h"
  4. #include "ProtoCvt.h"
  5. #include "FileHelper.h"
  6. NAMESPACE_MAS_BEGIN
  7. StrategyStation::StrategyStation()
  8. {
  9. }
  10. StrategyStation::~StrategyStation()
  11. {
  12. this->Dispose();
  13. }
  14. TZ_INT StrategyStation::Initialize()
  15. {
  16. this->loadStraLibs();
  17. TZLogInfo("stra station init~~~");
  18. return MEC_OK;
  19. }
  20. TZ_INT StrategyStation::Dispose()
  21. {
  22. TZLogInfo("stra station dispose~~~");
  23. return MEC_OK;
  24. }
  25. TZ_INT StrategyStation::QueryStrategy(
  26. const QueryCondition & reqInfo, PageResult & info,
  27. std::list<StrategyInfo> & infos)
  28. {
  29. std::list<TblStrategyInfoItem> tinfos;
  30. std::vector<sdb::ValuePair> cds;
  31. ORDER_PAIR order;
  32. ProtoCvt::Cvt(reqInfo, order, cds);
  33. DBMGR->QueryTblStrategyInfoByCd(cds, order, tinfos);
  34. auto irst = DBMGR->QueryTblStrategyInfoCount(cds, info.Total);
  35. if (irst)
  36. {
  37. TZLogWarn("query strategy count from db failed irst:%d!!!", irst);
  38. return irst;
  39. }
  40. info.Page = reqInfo.Page;
  41. info.Num = reqInfo.Size;
  42. info.TotalPage = (info.Total / reqInfo.Size) +
  43. ((info.Total % reqInfo.Size) ? 1 : 0);
  44. for (auto & mem : tinfos)
  45. {
  46. StrategyInfo info;
  47. ProtoCvt::Tbl2Json(mem, info);
  48. infos.emplace_back(info);
  49. }
  50. TZLogInfo("query strategy size %d~~~", infos.size());
  51. return MEC_OK;
  52. }
  53. TZ_INT StrategyStation::EditStrategy(
  54. const StrategyInfo & info)
  55. {
  56. TblStrategyInfoItem tInfo;
  57. tInfo.Tb_Id = info.Id;
  58. auto irst = DBMGR->QueryTblStrategyInfoByKey(tInfo);
  59. if (irst) return irst;
  60. tInfo.Tb_StraDets = tzc::json::ArrayToJson(info.StraDets);
  61. tInfo.Tb_StraCfgs = tzc::json::ArrayToJson(info.StraCfgs);
  62. return DBMGR->AddOrUpdateTblStrategyInfo(tInfo);
  63. }
  64. SPtr<IStreamPipe> StrategyStation::BuildPipe(
  65. TZ_INT sceneId, const std::string & sceneName,
  66. TZ_INT straId, const std::string & straParams)
  67. {
  68. TblStrategyInfoItem tbl;
  69. tbl.Tb_Id = straId;
  70. auto irst = DBMGR->QueryTblStrategyInfoByKey(tbl);
  71. if (irst)
  72. {
  73. TZLogWarn("can't find stra by id %d!!!", straId);
  74. return nullptr;
  75. }
  76. auto iter = m_straLib.find(straId);
  77. if (m_straLib.end() == iter)
  78. {
  79. TZLogWarn("can't find stra lib by %d!!!", straId);
  80. return nullptr;
  81. }
  82. ExcepInfo excepinfo(tbl.Tb_Name, 3000);
  83. auto pipe = std::make_shared<StraStreamPipe>(
  84. sceneId, sceneName, straId, straParams, excepinfo);
  85. irst = pipe->Initialize(iter->second);
  86. if (irst)
  87. {
  88. TZLogWarn("stra pipe %s init failed!!!",
  89. tbl.Tb_Name.c_str());
  90. return nullptr;
  91. }
  92. TZLogInfo("build strapipe %s success~~~", tbl.Tb_Name.c_str());
  93. return std::move(pipe);
  94. }
  95. TZ_INT StrategyStation::loadStraLibs()
  96. {
  97. std::set<std::string> paths;
  98. FileHelper::GetComponentFilePaths(STRA_LIB_PATH, paths);
  99. for (auto & libpath : paths)
  100. {
  101. StrategyInfo stra;
  102. auto pstra = this->genStraLibIns(libpath, stra);
  103. if (!pstra) continue;
  104. this->updateDbByStrategy(stra, libpath);
  105. m_straLib.emplace(stra.Id, pstra);
  106. }
  107. return MEC_OK;
  108. }
  109. TZ_INT StrategyStation::updateDbByStrategy(
  110. StrategyInfo & stra, const std::string & libpath)
  111. {
  112. TblStrategyInfoItem tinfo;
  113. tinfo.Tb_Key = stra.Key;
  114. tinfo.Tb_Id = INVALID_PRIMARY_KEY;
  115. std::list<TblStrategyInfoItem> infos;
  116. ORDER_PAIR order;
  117. std::vector<sdb::ValuePair> cds;
  118. PUSH_VALUE_PAIR(cds, TblStrategyInfo::COL_KEY, tinfo.Tb_Key);
  119. auto irst = DBMGR->QueryTblStrategyInfoByCd(cds, order, infos);
  120. if (!irst) tinfo.Tb_Id = infos.front().Tb_Id;
  121. tinfo.Tb_Name = stra.Name;
  122. tinfo.Tb_Version = stra.Version;
  123. tinfo.Tb_Author = stra.Author;
  124. tinfo.Tb_Path = libpath;
  125. for (auto & ldet : stra.StraDets)
  126. {
  127. ldet.DetId = this->getDetIdByKey(ldet.Key);
  128. }
  129. tinfo.Tb_StraDets = tzc::json::ArrayToJson(stra.StraDets);
  130. tinfo.Tb_StraCfgs = tzc::json::ArrayToJson(stra.StraCfgs);
  131. tinfo.Tb_StraRsts = tzc::json::ArrayToJson(stra.StraRsts);
  132. tinfo.Tb_Description = stra.Description;
  133. irst = DBMGR->AddOrUpdateTblStrategyInfo(tinfo);
  134. stra.Id = tinfo.Tb_Id;
  135. TZLogInfo("load stra %s -- %s succeed, update db irst %d~~~",
  136. stra.Key.c_str(), stra.Name.c_str(), irst);
  137. return irst;
  138. }
  139. SPtr<StrategyLib> StrategyStation::genStraLibIns(
  140. const std::string & libpath, StrategyInfo & stra)
  141. {
  142. auto pstra = std::make_shared<StrategyLib>(libpath);
  143. TZ_INT irst = pstra->Initialize(stra);
  144. if (irst)
  145. {
  146. TZLogWarn("stra lib %s init failed!!!", libpath.c_str());
  147. return nullptr;
  148. }
  149. TZLogInfo("stra %s -- %s init succeed~~~",
  150. stra.Key.c_str(), stra.Name.c_str());
  151. return std::move(pstra);
  152. }
  153. TZ_INT StrategyStation::getDetIdByKey(const std::string & key)
  154. {
  155. std::list<TblDetectorInfoItem> infos;
  156. ORDER_PAIR order;
  157. std::vector<sdb::ValuePair> cds;
  158. PUSH_VALUE_PAIR(cds, TblDetectorInfo::COL_KEY, key);
  159. auto irst = DBMGR->QueryTblDetectorInfoByCd(cds, order, infos);
  160. if (irst || !infos.size())
  161. {
  162. TZLogWarn("can't find det by key %s!!!", key.c_str());
  163. return -1;
  164. }
  165. return infos.front().Tb_Id;
  166. }
  167. NAMESPACE_MAS_END