#include "stdafx.h" #include "MonitorObject.h" #include #include #include #include #include "Device.h" using namespace std; CMonitorObject::CMonitorObject() { } CMonitorObject::~CMonitorObject() { for (auto& it : m_lstMo) { delete it; it = nullptr; } m_lstMo.clear(); } CMonitorObjectMng::CMonitorObjectMng() { Clear(); } CMonitorObjectMng::~CMonitorObjectMng() { if (m_treeroot) delete m_treeroot; m_treeroot = nullptr; m_mapImeiIdxMoMp.clear(); m_mapMoMpImeiIdx.clear(); } void CMonitorObjectMng::GetChild(std::list& lst, std::list& child, const std::string& id) { for (auto it = lst.begin(); it != lst.end(); ++it) { auto p = *it; if (p == nullptr) continue; if (p->id.compare(p->up) == 0) continue; if (p->up.compare(id) == 0) { if (p->type.compare("mo") == 0) { for (auto& it : CDeviceMng::Instance()->m_map_devices) { if (it.second->m_mo.compare(p->id) == 0) { auto pObject = new CMonitorObject; pObject->id = it.second->taskid; pObject->name = fmt::format("{}.{}", it.second->mo_name, it.second->mp_name); pObject->type = "mo.mp"; pObject->up = id; child.push_back(pObject); } } } else { child.push_back(p); *it = nullptr; GetChild(lst, p->m_lstMo, p->id); } } } } bool CMonitorObjectMng::GetStationNameByMomP(CMonitorObject* pInfo, const std::string& momp, std::string& station, std::string& momp_name) { if (pInfo == nullptr) return false; if (pInfo->type.compare("mo.mp") == 0 && pInfo->id.compare(momp) == 0) { station = pInfo->up; momp_name = pInfo->name; return true; } for (const auto& it : pInfo->m_lstMo) { if (GetStationNameByMomP(it, momp, station, momp_name)) return true; } return false; } void CMonitorObjectMng::Clear() { if (m_treeroot) delete m_treeroot; m_treeroot = nullptr; } BOOL CMonitorObjectMng::LoadMonitorTree() { Clear(); char id[100]; char type[50]; char up[100]; char name[100]; CString sql = "SELECT [id],[type],[up],[name] FROM [rm_mo]"; COdbcStatement stmt; if (!CDBConnectPool::Instance()->DBQuery(stmt, sql)) { CSimpleLog::Error("Ö´ÐÐÓï¾äʧ°Ü" + sql); return FALSE; } int nCol = 1; stmt.BindCharCol(nCol++, id, sizeof(id)); stmt.BindCharCol(nCol++, type, sizeof(type)); stmt.BindCharCol(nCol++, up, sizeof(up)); stmt.BindCharCol(nCol++, name, sizeof(name)); list lstMo; while (true) { if (stmt.FetchNext() != 0) { break; } auto p = new CMonitorObject; p->id = id; p->type = type; p->up = up; p->name = name; if (p->id.compare(up) == 0) { swap(m_treeroot, p); delete p; p = nullptr; } else { lstMo.push_back(p); } } bool bret = true; if (m_treeroot == nullptr) { assert(0); bret = false; goto l; } GetChild(lstMo, m_treeroot->m_lstMo, m_treeroot->id); l: for (auto it = lstMo.begin(); it != lstMo.end(); ++it) delete (*it); lstMo.clear(); //test //{ // string momp = "HZHDJD21.X"; // string name, name2; // GetStationNameByMomP(momp, name, name2); //} return TRUE; } BOOL CMonitorObjectMng::LoadHistoryData() { //for (auto& it : m_mapMoMpImeiIdx) //{ // string imei, idx; // spiltByPoint(it.second, imei, idx); // if (imei.length() > 5) CDeviceMng::Instance()->Insert(imei); //} return TRUE; } bool CMonitorObjectMng::GetStationNameByMomP(const std::string& momp, std::string& station, std::string& momp_name) { return GetStationNameByMomP(m_treeroot, momp, station, momp_name); } bool CMonitorObjectMng::GetStationNameByMomP(const std::string& mo, const std::string& mp, std::string& station, std::string& momp_name) { return GetStationNameByMomP(m_treeroot, mo + "." + mp, station, momp_name); } bool CMonitorObjectMng::GetNameByMoMp(const std::string& momp, std::string& name1, std::string& name2, std::string& name3) { auto& it = m_mapMoMpName.find(momp); if (it == m_mapMoMpName.end()) return false; if (it->second.size() < 3) return false; name1 = it->second[0]; name2 = it->second[1]; name3 = it->second[2]; return true; } bool CMonitorObjectMng::GetNameByMoMp(const std::string& momp, std::string& name1, std::string& name2, std::string& name3, std::string& out_name, std::string& in_name) { auto& it = m_mapMoMpName.find(momp); if (it == m_mapMoMpName.end()) return false; if (it->second.size() < 5) return false; name1 = it->second[0]; name2 = it->second[1]; name3 = it->second[2]; out_name = it->second[3]; in_name = it->second[4]; return true; } void CMonitorObjectMng::SetNameByMoMp(const std::string& momp, std::string& name1, std::string& name2, std::string& name3) { auto& it = m_mapMoMpName.find(momp); if (it == m_mapMoMpName.end()) { std::vector vct; vct.push_back(name1); vct.push_back(name2); vct.push_back(name3); m_mapMoMpName[momp] = vct; } else { it->second[0] = name1; it->second[1] = name2; it->second[2] = name3; } } bool CMonitorObjectMng::GetDirectByMoMp(const std::string& momp, std::string& direct1, std::string& direct2) { auto& it = m_mapMoMpDirect.find(momp); if (it == m_mapMoMpDirect.end()) return false; if (it->second.size() < 2) return false; direct1 = it->second[0]; direct2 = it->second[1]; return true; } void CMonitorObjectMng::SetDirectByMoMp(const std::string& momp, std::string& direct1, std::string& direct2) { auto& it = m_mapMoMpDirect.find(momp); if (it == m_mapMoMpDirect.end()) { std::vector vct; vct.push_back(direct1); vct.push_back(direct2); m_mapMoMpDirect[momp] = vct; } else { it->second[0] = direct1; it->second[1] = direct2; } } bool CMonitorObjectMng::spiltByPoint(std::string&src, std::string& dst1, std::string& dst2) { auto pos = src.find('.'); if (pos == -1) return false; dst1 = src.substr(0, pos); dst2 = src.substr(pos + 1); return true; } std::string CMonitorObjectMng::concatStringByPoint(const std::string& src1, const std::string& src2) { stringstream ss; ss << src1 << "." << src2; return ss.str(); } CMonitorObject* CMonitorObjectMng::GetTreeByID(CMonitorObject* p, string& id) { if (p->id.compare(id) == 0) { return p; } else { for (const auto& it : p->m_lstMo) { auto pObj = GetTreeByID(it, id); if (pObj) return pObj; } } return nullptr; } CMonitorObject* CMonitorObjectMng::GetTreeByID(string& id) { CMonitorObject* pObject = nullptr; do { if (m_treeroot == nullptr) break; return GetTreeByID(m_treeroot, id); } while (false); return pObject; } bool CMonitorObjectMng::ReNameMO(string id, string name) { CString sql = fmt::format("UPDATE rm_mo SET name = '{}' WHERE id = '{}'", id, name).c_str(); if (!CDBConnectPool::Instance()->DBExecuteSQL(sql)) { SPDLOG_ERROR("sql error:{}", sql); ASSERT(0); return false; } return true; } bool CMonitorObjectMng::AddMO(string id, string name, string type, string up) { CString sql = fmt::format("INSERT INTO rm_mo(id, name, type, up) VALUES ('{}', '{}','{}','{}')", id, name, type, up).c_str(); if (!CDBConnectPool::Instance()->DBExecuteSQL(sql)) { SPDLOG_ERROR("sql error:{}", sql); ASSERT(0); return false; } return true; } bool CMonitorObjectMng::AddMO2(string id, string name, string type, string up) { auto nPos = id.find('.'); if (nPos != -1) id = id.substr(0, nPos); nPos = name.find('.'); if (nPos != -1) name = name.substr(0, nPos); CString sql = fmt::format("if not exists (select 1 from [rm_mo] where [id] = '{0}' )\ BEGIN\ INSERT INTO [rm_mo] ([id],[type],[up],[name],[updatetime]) VALUES ('{0}' ,'mo' ,'{1}', '{2}',GETDATE());\ END", id, up, name).c_str(); if (!CDBConnectPool::Instance()->DBExecuteSQL(sql)) { SPDLOG_ERROR("sql error:{}", sql); ASSERT(0); return false; } return true; } CMonitorObjectMng CMonitorObjectMng::obj;