#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 & infos) { std::list tinfos; std::vector 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 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(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 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 infos; ORDER_PAIR order; std::vector 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 DetectorStation::genDetLibIns(const std::string & libpath, DetectorInfo & det) { auto pdet = std::make_shared(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