| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- #include "stdafx.h"
- #include "MonitorObject.h"
- #include <sstream>
- #include <ODBC/DBConnectPool.h>
- #include <Simplelog.h>
- #include <assert.h>
- #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<CMonitorObject*>& lst, std::list<CMonitorObject*>& 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<CMonitorObject*> 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<string> 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<string> 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;
|