123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- #include "DetStreamPipe.h"
- #include "DetectorStation.h"
- #include "DBMgr.h"
- #include "ProtoCvt.h"
- #include "FileHelper.h"
- NAMESPACE_MAS_BEGIN
- DetectorStation::DetectorStation() {}
- DetectorStation::~DetectorStation()
- {
- this->Dispose();
- }
- TZ_INT DetectorStation::Initialize()
- {
- this->loadDetLibs();
- TZLogInfo("detector station init~~");
- return MEC_OK;
- }
- TZ_INT DetectorStation::Dispose()
- {
- TZLogInfo("detector station dispose~~");
- return MEC_OK;
- }
- TZ_INT DetectorStation::QueryDetectorUnit(
- const QueryCondition & reqInfo,
- PageResult & info,
- std::list<DetectorInfo> & infos)
- {
- std::list<TblDetectorInfoItem> tinfos;
- std::vector<sdb::ValuePair> cds;
- ORDER_PAIR order;
- ProtoCvt::Cvt(reqInfo, order, cds);
- DBMGR->QueryTblDetectorInfoByCd(cds, order, tinfos);
- // 查询数据库中的检测器信息
- auto irst = DBMGR->QueryTblDetectorInfoCount(cds, info.Total);
- if (irst)
- {
- TZLogWarn("query detector count from db failed, rst: %d!!!", count_rst);
- return irst;
- }
- info.Page = reqInfo.Page;
- info.Num = reqInfo.Size;
- info.TotalPage = (info.Total / reqInfo.Size) + ((info.Total % reqInfo.Size) ? 1 : 0);
- for (const auto& mem : tinfos)
- {
- DetectorInfo info;
- ProtoCvt::Tbl2Json(mem, info);
- infos.emplace_back(info);
- }
- TZLogInfo("query detector size %d~~~", infos.size());
- return MEC_OK;
- }
- // 构建数据流管道
- SPtr<IStreamPipe> DetectorStation::BuildPipe(TZ_INT rscID, TZ_INT detId)
- {
- TblDetectorInfoItem tInfo;
- tInfo.Tb_Id = detId;
- auto irst = DBMGR->QueryTblDetectorInfoByKey(tInfo);
- if (irst)
- {
- TZLogWarn("query det by id %d failed!!!", detId);
- return nullptr;
- }
- auto iter = m_detLib.find(detId);
- if (m_detLib.end() == iter)
- {
- TZLogWarn("can't find det lib by %d!!!", detId);
- return nullptr;
- }
- ExcepInfo excepinfo(tInfo.Tb_Name, 3000);
- auto pipe = std::make_shared<DetStreamPipe>(rscID, detId, tInfo.Tb_InputType, excepinfo);
- irst = pipe->Initialize(iter->second, tInfo.Tb_InitParam);
- if (irst)
- {
- TZLogWarn("det pipe %d initialize failed!!!", detId);
- return nullptr;
- }
- TZLogInfo("build detpipe %d success~~~", detId);
- return std::move(pipe);
- }
- TZ_INT DetectorStation::SetInitParam(const DetectorInfo & info)
- {
- TblDetectorInfoItem tInfo;
- tInfo.Tb_Id = info.Id;
- auto irst = DBMGR->QueryTblDetectorInfoByKey(tInfo);
- if (irst) return irst;
- tInfo.Tb_InitParam = info.InitParam;
- return DBMGR->AddOrUpdateTblDetectorInfo(tInfo);
- }
- TZ_INT DetectorStation::loadDetLibs()
- {
- std::set<std::string> paths;
- FileHelper::GetComponentFilePaths(DET_LIB_PATH, paths);
- for (auto & libpath : paths)
- {
- DetectorInfo det;
- auto pdet = this->genDetLibIns(libpath, det);
- if (!pdet) continue;
- this->updateDbByDetector(det, libpath);
- m_detLib.emplace(det.Id, pdet);
- }
- return MEC_OK;
- }
- // 更新数据库
- TZ_INT DetectorStation::updateDbByDetector(const DetectorInfo & det, const std::string & libpath)
- {
- std::list<TblDetectorInfoItem> infos;
- ORDER_PAIR order;
- std::vector<sdb::ValuePair> cds;
- PUSH_VALUE_PAIR(cds, TblDetectorInfo::COL_KEY, det.Key);
- TblDetectorInfoItem tinfo;
- ProtoCvt::Json2Tbl(det, tinfo);
- tinfo.Tb_Path = libpath;
- auto irst = DBMGR->QueryTblDetectorInfoByCd(cds, order, infos);
- if (!irst) tinfo.Tb_Id = infos.front().Tb_Id;
- else tinfo.Tb_Id = INVALID_PRIMARY_KEY;
- rst = DBMGR->AddOrUpdateTblDetectorInfo(tinfo);
- det.Id = tinfo.Tb_Id;
- TZLogInfo("load det %s - %s succeed, update db irst %d~~~", det.Key.c_str(), det.Name.c_str(), rst);
- return irst;
- }
- // 生成检测器库实例
- SPtr<DetectorLib> DetectorStation::genDetLibIns(const std::string & libpath, DetectorInfo & det)
- {
- auto pdet = std::make_shared<DetectorLib>(libpath);
- TZ_INT irst = pdet->Initialize(det);
- if (irst)
- {
- TZLogWarn("det lib %s init failed!!!", libpath.c_str());
- return nullptr;
- }
- return pdet;
- }
- NAMESPACE_MAS_END
|