MonitorObject.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #include "stdafx.h"
  2. #include "MonitorObject.h"
  3. #include <sstream>
  4. #include <ODBC/DBConnectPool.h>
  5. #include <Simplelog.h>
  6. #include <assert.h>
  7. #include "Device.h"
  8. using namespace std;
  9. CMonitorObject::CMonitorObject()
  10. {
  11. }
  12. CMonitorObject::~CMonitorObject()
  13. {
  14. for (auto& it : m_lstMo)
  15. {
  16. delete it;
  17. it = nullptr;
  18. }
  19. m_lstMo.clear();
  20. }
  21. CMonitorObjectMng::CMonitorObjectMng()
  22. {
  23. Clear();
  24. }
  25. CMonitorObjectMng::~CMonitorObjectMng()
  26. {
  27. if (m_treeroot) delete m_treeroot;
  28. m_treeroot = nullptr;
  29. m_mapImeiIdxMoMp.clear();
  30. m_mapMoMpImeiIdx.clear();
  31. }
  32. void CMonitorObjectMng::GetChild(std::list<CMonitorObject*>& lst, std::list<CMonitorObject*>& child, const std::string& id)
  33. {
  34. for (auto it = lst.begin(); it != lst.end(); ++it)
  35. {
  36. auto p = *it;
  37. if (p == nullptr) continue;
  38. if (p->id.compare(p->up) == 0)
  39. continue;
  40. if (p->up.compare(id) == 0)
  41. {
  42. if (p->type.compare("mo") == 0)
  43. {
  44. for (auto& it : CDeviceMng::Instance()->m_map_devices)
  45. {
  46. if (it.second->m_mo.compare(p->id) == 0)
  47. {
  48. auto pObject = new CMonitorObject;
  49. pObject->id = it.second->taskid;
  50. pObject->name = fmt::format("{}.{}", it.second->mo_name, it.second->mp_name);
  51. pObject->type = "mo.mp";
  52. pObject->up = id;
  53. child.push_back(pObject);
  54. }
  55. }
  56. }
  57. else
  58. {
  59. child.push_back(p);
  60. *it = nullptr;
  61. GetChild(lst, p->m_lstMo, p->id);
  62. }
  63. }
  64. }
  65. }
  66. bool CMonitorObjectMng::GetStationNameByMomP(CMonitorObject* pInfo, const std::string& momp, std::string& station, std::string& momp_name)
  67. {
  68. if (pInfo == nullptr) return false;
  69. if (pInfo->type.compare("mo.mp") == 0 && pInfo->id.compare(momp) == 0)
  70. {
  71. station = pInfo->up;
  72. momp_name = pInfo->name;
  73. return true;
  74. }
  75. for (const auto& it : pInfo->m_lstMo)
  76. {
  77. if (GetStationNameByMomP(it, momp, station, momp_name))
  78. return true;
  79. }
  80. return false;
  81. }
  82. void CMonitorObjectMng::Clear()
  83. {
  84. if (m_treeroot) delete m_treeroot;
  85. m_treeroot = nullptr;
  86. }
  87. BOOL CMonitorObjectMng::LoadMonitorTree()
  88. {
  89. Clear();
  90. char id[100];
  91. char type[50];
  92. char up[100];
  93. char name[100];
  94. CString sql = "SELECT [id],[type],[up],[name] FROM [rm_mo]";
  95. COdbcStatement stmt;
  96. if (!CDBConnectPool::Instance()->DBQuery(stmt, sql))
  97. {
  98. CSimpleLog::Error("Ö´ÐÐÓï¾äʧ°Ü" + sql);
  99. return FALSE;
  100. }
  101. int nCol = 1;
  102. stmt.BindCharCol(nCol++, id, sizeof(id));
  103. stmt.BindCharCol(nCol++, type, sizeof(type));
  104. stmt.BindCharCol(nCol++, up, sizeof(up));
  105. stmt.BindCharCol(nCol++, name, sizeof(name));
  106. list<CMonitorObject*> lstMo;
  107. while (true)
  108. {
  109. if (stmt.FetchNext() != 0)
  110. {
  111. break;
  112. }
  113. auto p = new CMonitorObject;
  114. p->id = id;
  115. p->type = type;
  116. p->up = up;
  117. p->name = name;
  118. if (p->id.compare(up) == 0)
  119. {
  120. swap(m_treeroot, p);
  121. delete p;
  122. p = nullptr;
  123. }
  124. else
  125. {
  126. lstMo.push_back(p);
  127. }
  128. }
  129. bool bret = true;
  130. if (m_treeroot == nullptr)
  131. {
  132. assert(0);
  133. bret = false;
  134. goto l;
  135. }
  136. GetChild(lstMo, m_treeroot->m_lstMo, m_treeroot->id);
  137. l:
  138. for (auto it = lstMo.begin(); it != lstMo.end(); ++it)
  139. delete (*it);
  140. lstMo.clear();
  141. //test
  142. //{
  143. // string momp = "HZHDJD21.X";
  144. // string name, name2;
  145. // GetStationNameByMomP(momp, name, name2);
  146. //}
  147. return TRUE;
  148. }
  149. BOOL CMonitorObjectMng::LoadHistoryData()
  150. {
  151. //for (auto& it : m_mapMoMpImeiIdx)
  152. //{
  153. // string imei, idx;
  154. // spiltByPoint(it.second, imei, idx);
  155. // if (imei.length() > 5) CDeviceMng::Instance()->Insert(imei);
  156. //}
  157. return TRUE;
  158. }
  159. bool CMonitorObjectMng::GetStationNameByMomP(const std::string& momp, std::string& station, std::string& momp_name)
  160. {
  161. return GetStationNameByMomP(m_treeroot, momp, station, momp_name);
  162. }
  163. bool CMonitorObjectMng::GetStationNameByMomP(const std::string& mo, const std::string& mp, std::string& station, std::string& momp_name)
  164. {
  165. return GetStationNameByMomP(m_treeroot, mo + "." + mp, station, momp_name);
  166. }
  167. bool CMonitorObjectMng::GetNameByMoMp(const std::string& momp, std::string& name1, std::string& name2, std::string& name3)
  168. {
  169. auto& it = m_mapMoMpName.find(momp);
  170. if (it == m_mapMoMpName.end()) return false;
  171. if (it->second.size() < 3) return false;
  172. name1 = it->second[0];
  173. name2 = it->second[1];
  174. name3 = it->second[2];
  175. return true;
  176. }
  177. bool CMonitorObjectMng::GetNameByMoMp(const std::string& momp, std::string& name1, std::string& name2, std::string& name3, std::string& out_name, std::string& in_name)
  178. {
  179. auto& it = m_mapMoMpName.find(momp);
  180. if (it == m_mapMoMpName.end()) return false;
  181. if (it->second.size() < 5) return false;
  182. name1 = it->second[0];
  183. name2 = it->second[1];
  184. name3 = it->second[2];
  185. out_name = it->second[3];
  186. in_name = it->second[4];
  187. return true;
  188. }
  189. void CMonitorObjectMng::SetNameByMoMp(const std::string& momp, std::string& name1, std::string& name2, std::string& name3)
  190. {
  191. auto& it = m_mapMoMpName.find(momp);
  192. if (it == m_mapMoMpName.end())
  193. {
  194. std::vector<string> vct;
  195. vct.push_back(name1);
  196. vct.push_back(name2);
  197. vct.push_back(name3);
  198. m_mapMoMpName[momp] = vct;
  199. }
  200. else
  201. {
  202. it->second[0] = name1;
  203. it->second[1] = name2;
  204. it->second[2] = name3;
  205. }
  206. }
  207. bool CMonitorObjectMng::GetDirectByMoMp(const std::string& momp, std::string& direct1, std::string& direct2)
  208. {
  209. auto& it = m_mapMoMpDirect.find(momp);
  210. if (it == m_mapMoMpDirect.end()) return false;
  211. if (it->second.size() < 2) return false;
  212. direct1 = it->second[0];
  213. direct2 = it->second[1];
  214. return true;
  215. }
  216. void CMonitorObjectMng::SetDirectByMoMp(const std::string& momp, std::string& direct1, std::string& direct2)
  217. {
  218. auto& it = m_mapMoMpDirect.find(momp);
  219. if (it == m_mapMoMpDirect.end())
  220. {
  221. std::vector<string> vct;
  222. vct.push_back(direct1);
  223. vct.push_back(direct2);
  224. m_mapMoMpDirect[momp] = vct;
  225. }
  226. else
  227. {
  228. it->second[0] = direct1;
  229. it->second[1] = direct2;
  230. }
  231. }
  232. bool CMonitorObjectMng::spiltByPoint(std::string&src, std::string& dst1, std::string& dst2)
  233. {
  234. auto pos = src.find('.');
  235. if (pos == -1) return false;
  236. dst1 = src.substr(0, pos);
  237. dst2 = src.substr(pos + 1);
  238. return true;
  239. }
  240. std::string CMonitorObjectMng::concatStringByPoint(const std::string& src1, const std::string& src2)
  241. {
  242. stringstream ss;
  243. ss << src1 << "." << src2;
  244. return ss.str();
  245. }
  246. CMonitorObject* CMonitorObjectMng::GetTreeByID(CMonitorObject* p, string& id)
  247. {
  248. if (p->id.compare(id) == 0)
  249. {
  250. return p;
  251. }
  252. else
  253. {
  254. for (const auto& it : p->m_lstMo)
  255. {
  256. auto pObj = GetTreeByID(it, id);
  257. if (pObj) return pObj;
  258. }
  259. }
  260. return nullptr;
  261. }
  262. CMonitorObject* CMonitorObjectMng::GetTreeByID(string& id)
  263. {
  264. CMonitorObject* pObject = nullptr;
  265. do {
  266. if (m_treeroot == nullptr) break;
  267. return GetTreeByID(m_treeroot, id);
  268. } while (false);
  269. return pObject;
  270. }
  271. bool CMonitorObjectMng::ReNameMO(string id, string name)
  272. {
  273. CString sql = fmt::format("UPDATE rm_mo SET name = '{}' WHERE id = '{}'", id, name).c_str();
  274. if (!CDBConnectPool::Instance()->DBExecuteSQL(sql))
  275. {
  276. SPDLOG_ERROR("sql error:{}", sql);
  277. ASSERT(0);
  278. return false;
  279. }
  280. return true;
  281. }
  282. bool CMonitorObjectMng::AddMO(string id, string name, string type, string up)
  283. {
  284. CString sql = fmt::format("INSERT INTO rm_mo(id, name, type, up) VALUES ('{}', '{}','{}','{}')", id, name, type, up).c_str();
  285. if (!CDBConnectPool::Instance()->DBExecuteSQL(sql))
  286. {
  287. SPDLOG_ERROR("sql error:{}", sql);
  288. ASSERT(0);
  289. return false;
  290. }
  291. return true;
  292. }
  293. bool CMonitorObjectMng::AddMO2(string id, string name, string type, string up)
  294. {
  295. auto nPos = id.find('.');
  296. if (nPos != -1) id = id.substr(0, nPos);
  297. nPos = name.find('.');
  298. if (nPos != -1) name = name.substr(0, nPos);
  299. CString sql = fmt::format("if not exists (select 1 from [rm_mo] where [id] = '{0}' )\
  300. BEGIN\
  301. INSERT INTO [rm_mo] ([id],[type],[up],[name],[updatetime]) VALUES ('{0}' ,'mo' ,'{1}', '{2}',GETDATE());\
  302. END", id, up, name).c_str();
  303. if (!CDBConnectPool::Instance()->DBExecuteSQL(sql))
  304. {
  305. SPDLOG_ERROR("sql error:{}", sql);
  306. ASSERT(0);
  307. return false;
  308. }
  309. return true;
  310. }
  311. CMonitorObjectMng CMonitorObjectMng::obj;