DetectorStation.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "DetStreamPipe.h"
  2. #include "DetectorStation.h"
  3. #include "DBMgr.h"
  4. #include "ProtoCvt.h"
  5. #include "FileHelper.h"
  6. NAMESPACE_MAS_BEGIN
  7. DetectorStation::DetectorStation() {}
  8. DetectorStation::~DetectorStation()
  9. {
  10. this->Dispose();
  11. }
  12. TZ_INT DetectorStation::Initialize()
  13. {
  14. this->loadDetLibs();
  15. TZLogInfo("detector station init~~");
  16. return MEC_OK;
  17. }
  18. TZ_INT DetectorStation::Dispose()
  19. {
  20. TZLogInfo("detector station dispose~~");
  21. return MEC_OK;
  22. }
  23. TZ_INT DetectorStation::QueryDetectorUnit(
  24. const QueryCondition & reqInfo,
  25. PageResult & info,
  26. std::list<DetectorInfo> & infos)
  27. {
  28. std::list<TblDetectorInfoItem> tinfos;
  29. std::vector<sdb::ValuePair> cds;
  30. ORDER_PAIR order;
  31. ProtoCvt::Cvt(reqInfo, order, cds);
  32. DBMGR->QueryTblDetectorInfoByCd(cds, order, tinfos);
  33. // 查询数据库中的检测器信息
  34. auto irst = DBMGR->QueryTblDetectorInfoCount(cds, info.Total);
  35. if (irst)
  36. {
  37. TZLogWarn("query detector count from db failed, rst: %d!!!", count_rst);
  38. return irst;
  39. }
  40. info.Page = reqInfo.Page;
  41. info.Num = reqInfo.Size;
  42. info.TotalPage = (info.Total / reqInfo.Size) + ((info.Total % reqInfo.Size) ? 1 : 0);
  43. for (const auto& mem : tinfos)
  44. {
  45. DetectorInfo info;
  46. ProtoCvt::Tbl2Json(mem, info);
  47. infos.emplace_back(info);
  48. }
  49. TZLogInfo("query detector size %d~~~", infos.size());
  50. return MEC_OK;
  51. }
  52. // 构建数据流管道
  53. SPtr<IStreamPipe> DetectorStation::BuildPipe(TZ_INT rscID, TZ_INT detId)
  54. {
  55. TblDetectorInfoItem tInfo;
  56. tInfo.Tb_Id = detId;
  57. auto irst = DBMGR->QueryTblDetectorInfoByKey(tInfo);
  58. if (irst)
  59. {
  60. TZLogWarn("query det by id %d failed!!!", detId);
  61. return nullptr;
  62. }
  63. auto iter = m_detLib.find(detId);
  64. if (m_detLib.end() == iter)
  65. {
  66. TZLogWarn("can't find det lib by %d!!!", detId);
  67. return nullptr;
  68. }
  69. ExcepInfo excepinfo(tInfo.Tb_Name, 3000);
  70. auto pipe = std::make_shared<DetStreamPipe>(rscID, detId, tInfo.Tb_InputType, excepinfo);
  71. irst = pipe->Initialize(iter->second, tInfo.Tb_InitParam);
  72. if (irst)
  73. {
  74. TZLogWarn("det pipe %d initialize failed!!!", detId);
  75. return nullptr;
  76. }
  77. TZLogInfo("build detpipe %d success~~~", detId);
  78. return std::move(pipe);
  79. }
  80. TZ_INT DetectorStation::SetInitParam(const DetectorInfo & info)
  81. {
  82. TblDetectorInfoItem tInfo;
  83. tInfo.Tb_Id = info.Id;
  84. auto irst = DBMGR->QueryTblDetectorInfoByKey(tInfo);
  85. if (irst) return irst;
  86. tInfo.Tb_InitParam = info.InitParam;
  87. return DBMGR->AddOrUpdateTblDetectorInfo(tInfo);
  88. }
  89. TZ_INT DetectorStation::loadDetLibs()
  90. {
  91. std::set<std::string> paths;
  92. FileHelper::GetComponentFilePaths(DET_LIB_PATH, paths);
  93. for (auto & libpath : paths)
  94. {
  95. DetectorInfo det;
  96. auto pdet = this->genDetLibIns(libpath, det);
  97. if (!pdet) continue;
  98. this->updateDbByDetector(det, libpath);
  99. m_detLib.emplace(det.Id, pdet);
  100. }
  101. return MEC_OK;
  102. }
  103. // 更新数据库
  104. TZ_INT DetectorStation::updateDbByDetector(const DetectorInfo & det, const std::string & libpath)
  105. {
  106. std::list<TblDetectorInfoItem> infos;
  107. ORDER_PAIR order;
  108. std::vector<sdb::ValuePair> cds;
  109. PUSH_VALUE_PAIR(cds, TblDetectorInfo::COL_KEY, det.Key);
  110. TblDetectorInfoItem tinfo;
  111. ProtoCvt::Json2Tbl(det, tinfo);
  112. tinfo.Tb_Path = libpath;
  113. auto irst = DBMGR->QueryTblDetectorInfoByCd(cds, order, infos);
  114. if (!irst) tinfo.Tb_Id = infos.front().Tb_Id;
  115. else tinfo.Tb_Id = INVALID_PRIMARY_KEY;
  116. rst = DBMGR->AddOrUpdateTblDetectorInfo(tinfo);
  117. det.Id = tinfo.Tb_Id;
  118. TZLogInfo("load det %s - %s succeed, update db irst %d~~~", det.Key.c_str(), det.Name.c_str(), rst);
  119. return irst;
  120. }
  121. // 生成检测器库实例
  122. SPtr<DetectorLib> DetectorStation::genDetLibIns(const std::string & libpath, DetectorInfo & det)
  123. {
  124. auto pdet = std::make_shared<DetectorLib>(libpath);
  125. TZ_INT irst = pdet->Initialize(det);
  126. if (irst)
  127. {
  128. TZLogWarn("det lib %s init failed!!!", libpath.c_str());
  129. return nullptr;
  130. }
  131. return pdet;
  132. }
  133. NAMESPACE_MAS_END