MonitorObject.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. #include <ZlDataDefine.h>
  9. using namespace std;
  10. CMonitorObject::CMonitorObject()
  11. {
  12. }
  13. CMonitorObject::~CMonitorObject()
  14. {
  15. check = 0x00;
  16. for (auto& it : m_lstMo)
  17. {
  18. delete it;
  19. it = nullptr;
  20. }
  21. m_lstMo.clear();
  22. }
  23. CMonitorObjectMng::CMonitorObjectMng()
  24. {
  25. Clear();
  26. }
  27. CMonitorObjectMng::~CMonitorObjectMng()
  28. {
  29. if (m_treeroot) delete m_treeroot;
  30. m_treeroot = nullptr;
  31. m_mapImeiIdxMoMp.clear();
  32. m_mapMoMpImeiIdx.clear();
  33. }
  34. BOOL CMonitorObjectMng::LoadMoMap()
  35. {
  36. char mo[50];
  37. char mp[50];
  38. char imei[50];
  39. char name1[20];
  40. char name2[20];
  41. char name3[20];
  42. char direct1[20];
  43. char direct2[20];
  44. short idx;
  45. int zzj_no = 0;
  46. int epos = 0;
  47. uint8_t binstall_1 = 0;
  48. uint8_t binstall_2 = 0;
  49. uint8_t binstall_3 = 0;
  50. //CString sql = "SELECT mo,mp,ISNULL(IMEI, ''),ISNULL(idx, 0),ISNULL(zl_1_loca, ''),ISNULL(zl_2_loca, ''), '转换阻力曲线',ISNULL(direct1, ''),ISNULL(direct2, ''),zzjno,epos,install_1,install_2,install_3 FROM rm_map";
  51. CString sql = "SELECT mo,mp,ISNULL(IMEI, ''),ISNULL(idx, 0),ISNULL(zl_1_loca, ''),ISNULL(zl_2_loca, ''), '转换阻力曲线',ISNULL(direct1, ''),ISNULL(direct2, '') FROM rm_map";
  52. COdbcStatement stmt;
  53. if (!CDBConnectPool::Instance()->DBQuery(stmt, sql))
  54. {
  55. ASSERT(FALSE);
  56. CSimpleLog::Error("执行语句失败" + sql);
  57. return FALSE;
  58. }
  59. int nCol = 1;
  60. stmt.BindCharCol(nCol++, mo, sizeof(mo));
  61. stmt.BindCharCol(nCol++, mp, sizeof(mp));
  62. stmt.BindCharCol(nCol++, imei, sizeof(imei));
  63. stmt.BindSmallIntCol(nCol++, &idx);
  64. stmt.BindCharCol(nCol++, name1, sizeof(name1));
  65. stmt.BindCharCol(nCol++, name2, sizeof(name2));
  66. stmt.BindCharCol(nCol++, name3, sizeof(name3));
  67. stmt.BindCharCol(nCol++, direct1, sizeof(direct1));
  68. stmt.BindCharCol(nCol++, direct2, sizeof(direct2));
  69. //stmt.BindIntCol(nCol++, &zzj_no);
  70. //stmt.BindIntCol(nCol++, &epos);
  71. //stmt.BindTinyIntCol(nCol++, &binstall_1);
  72. //stmt.BindTinyIntCol(nCol++, &binstall_2);
  73. //stmt.BindTinyIntCol(nCol++, &binstall_3);
  74. stringstream smomp;
  75. stringstream simei;
  76. while (true)
  77. {
  78. memset(imei, 0, sizeof(imei));
  79. if (stmt.FetchNext() != 0)
  80. {
  81. break;
  82. }
  83. if (name1[0x00] != 0)
  84. sprintf_s(name1, 20, "%s%s", name1, "测力曲线");
  85. if (name2[0x00] != 0)
  86. sprintf_s(name2, 20, "%s%s", name2, "测力曲线");
  87. for (int i = 0x00; i < 16; i++) if (imei[i] == 0x20) imei[i] = 0x00; //0x20 代表空格
  88. smomp.str("");
  89. simei.str("");
  90. smomp << mo << '.' << mp;
  91. if (imei[0x00] != 0x00) simei << imei << '.' << idx;
  92. string momp = smomp.str();
  93. m_mapMoMpZZJNO[momp] = zzj_no;
  94. m_mapMoMpZZJEPOS[momp] = (DAOCHA_POSITION)epos;
  95. m_mapMoMpImeiIdx[momp] = simei.str();
  96. if (imei[0x00] != 0x00) m_mapImeiIdxMoMp[simei.str()] = smomp.str();
  97. {
  98. auto& it = m_mapMoMpInfo[momp];
  99. if (it == nullptr) it = new ST_MOMP_INFO;
  100. //it->binstall_1 = binstall_1;
  101. //it->binstall_2 = binstall_2;
  102. //it->binstall_3 = binstall_3;
  103. it->name1 = name1;
  104. it->name2 = name2;
  105. it->name3 = name3;
  106. it->out_name = direct1;
  107. it->in_name = direct2;
  108. it->imei = imei;
  109. it->idx = idx;
  110. //it->zzjno = zzj_no;
  111. it->mo = mo;
  112. it->mp = mp;
  113. if (!it->imei.empty())
  114. it->pDeivce = CDeviceMng::Instance()->Insert(imei);
  115. }
  116. }
  117. return m_mapMoMpImeiIdx.size() + 1;
  118. }
  119. void CMonitorObjectMng::GetChild(std::list<CMonitorObject*>& lst, CMonitorObject* parent)
  120. {
  121. for (auto it = lst.begin(); it != lst.end(); ++it)
  122. {
  123. auto p = *it;
  124. if (p == nullptr) continue;
  125. if (p->id.compare(p->up) == 0)
  126. continue;
  127. if (p->up.compare(parent->id) == 0)
  128. {
  129. if (p->type.compare("mo") == 0)
  130. {
  131. for (auto& ik : m_mapMoMpImeiIdx)
  132. {
  133. //int ii = ik.first.find('.');
  134. //int jj = ik.first.find(p->id);
  135. string strName = p->id + '.';
  136. if (ik.first.find(strName) != -1)
  137. {
  138. auto pObject = new CMonitorObject;
  139. pObject->id = ik.first;
  140. pObject->name = p->name + "." + ik.first.substr(ik.first.find('.') + 1);
  141. pObject->type = "mo.mp";
  142. pObject->up = parent->id;
  143. pObject->parent = parent;
  144. pObject->eqpno = m_mapMoMpZZJNO[pObject->id];
  145. parent->m_lstMo.push_back(pObject);
  146. }
  147. }
  148. }
  149. else
  150. {
  151. p->parent = parent;
  152. parent->m_lstMo.push_back(p);
  153. *it = nullptr;
  154. GetChild(lst, p);
  155. }
  156. }
  157. }
  158. }
  159. bool CMonitorObjectMng::GetStationNameByMomP(CMonitorObject* pInfo, const std::string& momp, std::string& station, std::string& momp_name)
  160. {
  161. if (pInfo == nullptr) return false;
  162. if (pInfo->type.compare("mo.mp") == 0 && pInfo->id.compare(momp) == 0)
  163. {
  164. station = pInfo->up;
  165. momp_name = pInfo->name;
  166. return true;
  167. }
  168. for (const auto& it : pInfo->m_lstMo)
  169. {
  170. if (GetStationNameByMomP(it, momp, station, momp_name))
  171. return true;
  172. }
  173. return false;
  174. }
  175. void CMonitorObjectMng::Clear()
  176. {
  177. if (m_treeroot) delete m_treeroot;
  178. m_treeroot = nullptr;
  179. m_mapImeiIdxMoMp.clear();
  180. m_mapMoMpImeiIdx.clear();
  181. for (auto& it : m_mapMoMpInfo)
  182. delete it.second;
  183. }
  184. BOOL CMonitorObjectMng::LoadMonitorTree()
  185. {
  186. Clear();
  187. if (!LoadMoMap()) return FALSE;
  188. char id[100];
  189. char type[50];
  190. char up[100];
  191. char name[100];
  192. CString sql = "SELECT [id],[type],[up],[name] FROM [rm_mo]";
  193. COdbcStatement stmt;
  194. if (!CDBConnectPool::Instance()->DBQuery(stmt, sql))
  195. {
  196. CSimpleLog::Error("执行语句失败" + sql);
  197. return FALSE;
  198. }
  199. int nCol = 1;
  200. stmt.BindCharCol(nCol++, id, sizeof(id));
  201. stmt.BindCharCol(nCol++, type, sizeof(type));
  202. stmt.BindCharCol(nCol++, up, sizeof(up));
  203. stmt.BindCharCol(nCol++, name, sizeof(name));
  204. list<CMonitorObject*> lstMo;
  205. while (true)
  206. {
  207. if (stmt.FetchNext() != 0)
  208. {
  209. break;
  210. }
  211. auto p = new CMonitorObject;
  212. p->id = id;
  213. p->type = type;
  214. p->up = up;
  215. p->name = name;
  216. //赋值节点名称
  217. if (p->type.compare("mo") == 0)
  218. {
  219. for (auto& it : m_mapMoMpInfo)
  220. {
  221. if (it.second->mo.compare(id) == 0)
  222. it.second->name = fmt::format("{}.{}", name, it.second->mp);
  223. }
  224. }
  225. if (p->id.compare(up) == 0)
  226. {
  227. swap(m_treeroot, p);
  228. delete p;
  229. p = nullptr;
  230. }
  231. else
  232. {
  233. lstMo.push_back(p);
  234. }
  235. }
  236. bool bret = true;
  237. if (m_treeroot == nullptr)
  238. {
  239. assert(0);
  240. bret = false;
  241. goto l;
  242. }
  243. GetChild(lstMo, m_treeroot);
  244. l:
  245. for (auto it = lstMo.begin(); it != lstMo.end(); ++it)
  246. delete (*it);
  247. lstMo.clear();
  248. //test
  249. //{
  250. // string momp = "HZHDJD21.X";
  251. // string name, name2;
  252. // GetStationNameByMomP(momp, name, name2);
  253. //}
  254. return TRUE;
  255. }
  256. BOOL CMonitorObjectMng::LoadHistoryData()
  257. {
  258. for (auto& it : m_mapMoMpImeiIdx)
  259. {
  260. string imei, idx;
  261. spiltByPoint(it.second, imei, idx);
  262. if (imei.length() > 5) CDeviceMng::Instance()->Insert(imei);
  263. }
  264. return TRUE;
  265. }
  266. bool CMonitorObjectMng::IMEI2MOMP(const std::string imei, const int idx, std::string& mo, std::string& mp)
  267. {
  268. auto& it = m_mapImeiIdxMoMp.find(concatStringByPoint(imei, to_string(idx)));
  269. if (it == m_mapImeiIdxMoMp.end()) return false;
  270. return spiltByPoint(it->second, mo, mp);
  271. }
  272. bool CMonitorObjectMng::IMEI2MOMP(const std::string& imei_idx, std::string& mo_mp)
  273. {
  274. auto& it = m_mapImeiIdxMoMp.find(imei_idx);
  275. if (it == m_mapImeiIdxMoMp.end()) return false;
  276. mo_mp = it->second;
  277. return true;
  278. }
  279. bool CMonitorObjectMng::MOMP2IMEI(const std::string& mo, const std::string& mp, std::string& imei, int& idx)
  280. {
  281. auto& it = m_mapMoMpImeiIdx.find(concatStringByPoint(mo, mp));
  282. if (it == m_mapMoMpImeiIdx.end()) return false;
  283. string idx2;
  284. if (!spiltByPoint(it->second, imei, idx2)) return false;
  285. idx = atoi(idx2.c_str());
  286. return true;
  287. }
  288. bool CMonitorObjectMng::MOMP2IMEI(const std::string& momp, std::string& imeiidx)
  289. {
  290. auto& it = m_mapMoMpImeiIdx.find(momp);
  291. if (it == m_mapMoMpImeiIdx.end()) return false;
  292. if (it->second.length() <= 0) return false;
  293. imeiidx = it->second;
  294. return true;
  295. }
  296. bool CMonitorObjectMng::MOMP2IMEI(const std::string& momp, std::string& imei, int& idx)
  297. {
  298. auto& it = m_mapMoMpImeiIdx.find(momp);
  299. if (it == m_mapMoMpImeiIdx.end()) return false;
  300. string idx2;
  301. if (!spiltByPoint(it->second, imei, idx2)) return false;
  302. idx = atoi(idx2.c_str());
  303. return true;
  304. }
  305. bool CMonitorObjectMng::GetStationNameByMomP(const std::string& momp, std::string& station, std::string& momp_name)
  306. {
  307. return GetStationNameByMomP(m_treeroot, momp, station, momp_name);
  308. }
  309. bool CMonitorObjectMng::GetStationNameByMomP(const std::string& mo, const std::string& mp, std::string& station, std::string& momp_name)
  310. {
  311. return GetStationNameByMomP(m_treeroot, mo + "." + mp, station, momp_name);
  312. }
  313. bool CMonitorObjectMng::spiltByPoint(const std::string&src, std::string& dst1, std::string& dst2)
  314. {
  315. auto pos = src.find('.');
  316. if (pos == -1) return false;
  317. dst1 = src.substr(0, pos);
  318. dst2 = src.substr(pos + 1);
  319. return true;
  320. }
  321. std::string CMonitorObjectMng::concatStringByPoint(const std::string& src1, const std::string& src2)
  322. {
  323. stringstream ss;
  324. ss << src1 << "." << src2;
  325. return ss.str();
  326. }
  327. CMonitorObject* CMonitorObjectMng::GetTreeByID(CMonitorObject* p, const string& id)
  328. {
  329. if (p->id.compare(id) == 0)
  330. {
  331. return p;
  332. }
  333. else
  334. {
  335. for (const auto& it : p->m_lstMo)
  336. {
  337. auto pObj = GetTreeByID(it, id);
  338. if (pObj) return pObj;
  339. }
  340. }
  341. return nullptr;
  342. }
  343. CMonitorObject* CMonitorObjectMng::GetTreeByEpqID(CMonitorObject* p, uint16_t id)
  344. {
  345. if (p->eqpno == id)
  346. {
  347. return p;
  348. }
  349. else
  350. {
  351. for (const auto& it : p->m_lstMo)
  352. {
  353. auto pObj = GetTreeByEpqID(it, id);
  354. if (pObj) return pObj;
  355. }
  356. }
  357. return nullptr;
  358. }
  359. CMonitorObject* CMonitorObjectMng::GetTreeByID(const string& id)
  360. {
  361. CMonitorObject* pObject = nullptr;
  362. do {
  363. if (m_treeroot == nullptr) break;
  364. return GetTreeByID(m_treeroot, id);
  365. } while (false);
  366. return pObject;
  367. }
  368. CMonitorObject* CMonitorObjectMng::GetTreeByEpqID(uint16_t id)
  369. {
  370. CMonitorObject* pObject = nullptr;
  371. do {
  372. if (m_treeroot == nullptr) break;
  373. return GetTreeByEpqID(m_treeroot, id);
  374. } while (false);
  375. return pObject;
  376. }
  377. void CMonitorObjectMng::GetAllObjByType(CMonitorObject* p, std::vector<CMonitorObject*>& vctObj, const string& type)
  378. {
  379. if (p->type.compare(type) == 0)
  380. {
  381. vctObj.push_back(p);
  382. }
  383. for (auto& it : p->m_lstMo)
  384. {
  385. GetAllObjByType(it, vctObj, type);
  386. }
  387. }
  388. //获取所有Type对象
  389. bool CMonitorObjectMng::GetAllObjByType(std::vector<CMonitorObject*>& vctObj, const string& type)
  390. {
  391. do {
  392. if (m_treeroot == nullptr) break;
  393. GetAllObjByType(m_treeroot, vctObj, type);
  394. } while (false);
  395. return !vctObj.empty();
  396. }
  397. void CMonitorObjectMng::GeneralMoPack(CMonitorObject* pObject, yyjson_mut_doc* doc, yyjson_mut_val* val)
  398. {
  399. yyjson_mut_obj_add_str(doc, val, "id", pObject->id.c_str());
  400. yyjson_mut_obj_add_str(doc, val, "name", pObject->name.c_str());
  401. yyjson_mut_obj_add_str(doc, val, "type", pObject->type.c_str());
  402. yyjson_mut_obj_add_str(doc, val, "up", pObject->up.c_str());
  403. yyjson_mut_val* arr = nullptr;
  404. if (pObject->m_lstMo.size() > 0)
  405. {
  406. arr = yyjson_mut_arr(doc);
  407. yyjson_mut_obj_add_val(doc, val, "child", arr);
  408. }
  409. for (auto& it : pObject->m_lstMo)
  410. {
  411. auto object = yyjson_mut_obj(doc);
  412. yyjson_mut_arr_add_val(arr, object);
  413. GeneralMoPack(it, doc, object);
  414. }
  415. }
  416. void CMonitorObjectMng::UpdateZZJEPOS(const string& momp, DAOCHA_POSITION epos)
  417. {
  418. string mo, mp;
  419. spiltByPoint(momp, mo, mp);
  420. CString sql = fmt::format("UPDATE rm_map SET epos = {} where mo = '{}' and mp = '{}'", int(epos), mo, mp).c_str();
  421. auto ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
  422. if (ret)
  423. {
  424. m_mapMoMpZZJEPOS[momp] = epos;
  425. }
  426. else
  427. {
  428. CSimpleLog::Error("更新语句错误:" + sql);
  429. }
  430. }
  431. CMonitorObjectMng CMonitorObjectMng::obj;