1
0

module_meta_data_changed.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. #include "module_meta_data_changed.h"
  2. #include "area_business_person_attendance.h"
  3. #include "area_business_car_attendance.h"
  4. #include "common_tool.h"
  5. #include "websocket/wsClientMgr.h"
  6. #include "db/db_api/CDBSingletonDefine.h"
  7. #include "event.h"
  8. #include "ant.h"
  9. #include "card.h"
  10. #include "area.h"
  11. #include "forbid_staff_down_mine.h"
  12. #include "landmark.h"
  13. #include "area_business_geofault.h"
  14. #include <boost/format.hpp>
  15. #include <boost/tokenizer.hpp>
  16. #include <boost/algorithm/string/split.hpp>
  17. #include <boost/algorithm/string.hpp>
  18. #include <card_path.h>
  19. #include "sys_setting.h"
  20. #include "module_traffic_light_manager.h"
  21. ///基础数据
  22. void module_meta_data_changed::accept(sio::message::ptr const& data)
  23. {
  24. std::string name="";
  25. if(!tool_map::try_get_value(name, JSON_KEY_NAME, data))
  26. {
  27. log_error("web发来的数据: 基础数据找不到name字段");
  28. return;
  29. }
  30. std::string szParam = "0";
  31. tool_map::try_get_value(szParam, JSON_KEY_ID, data);
  32. std::string op_type="";
  33. tool_map::try_get_value(op_type, JSON_KEY_OP_TYPE, data);
  34. log_info("web发来数据:cmd=meta_data_changed.name:%s Param:%s OpType:%s", name.c_str(), szParam.c_str(), op_type.c_str());
  35. if(!szParam.empty() && !op_type.empty())
  36. {
  37. EDIT_TYPE_ID edit_type_id;
  38. if(!try_get_edit_type_id(op_type, edit_type_id))
  39. {
  40. log_error("web发来的数据: 基础数据op_type字段错误:op_type=%s", op_type.c_str());
  41. return;
  42. }
  43. if(JSON_KEY_NAME_VEHICLE == name || JSON_KEY_NAME_VEHICLE_EXTEND == name)
  44. {
  45. deal_call_edit_vehicle_or_staff(szParam, edit_type_id);
  46. }
  47. else if(JSON_KEY_NAME_STAFF == name || JSON_KEY_NAME_STAFF_EXTEND == name)
  48. {
  49. deal_call_edit_vehicle_or_staff(szParam, edit_type_id);
  50. }
  51. else if(JSON_KEY_NAME_CARD == name)
  52. {
  53. deal_call_edit_card(szParam, edit_type_id);
  54. }
  55. else if(JSON_KEY_NAME_AREA == name)
  56. {
  57. deal_call_edit_area(szParam, edit_type_id);
  58. }
  59. else if(JSON_KEY_NAME_READER == name)
  60. {
  61. deal_call_edit_reader(szParam, edit_type_id);
  62. }
  63. else if (JSON_KEY_NAME_ANTENNA == name)
  64. {
  65. int id = std::stoi(szParam);
  66. deal_call_edit_antenna(id,edit_type_id);
  67. }
  68. else if(JSON_KEY_NAME_PATH == name)
  69. {
  70. int id = std::stoi(szParam);
  71. deal_call_edit_path(id, edit_type_id);
  72. }
  73. else if(JSON_KEY_NAME_MAP == name || JSON_KEY_NAME_MAP_GIS == name)
  74. {
  75. int id = std::stoi(szParam);
  76. deal_call_edit_map(id, edit_type_id);
  77. }
  78. else if (JSON_KEY_NAME_LIGHT == name)
  79. {
  80. int id = std::stoi(szParam);
  81. deal_call_edit_light(id,edit_type_id);///待实现
  82. }
  83. else if (JSON_KEY_NAME_LIGHTS_GROUP== name)
  84. {
  85. int id = std::stoi(szParam);
  86. deal_call_edit_lights_group(id,edit_type_id);///待实现
  87. }
  88. else if (JSON_KEY_NAME_FORBID_PERSON_DOWN_MINE == name)
  89. {
  90. deal_call_edit_forbid_person_down_mine(szParam,edit_type_id);
  91. }
  92. else if (JSON_KEY_NAME_PERSONS_DYNAMIC_THRE == name)
  93. {
  94. int id = std::stoi(szParam);
  95. deal_call_edit_persons_dynamic_thre(id,edit_type_id);
  96. }
  97. else if (JSON_KEY_NAME_GEO_FAULT == name)//地址断层更新
  98. {
  99. area_business_geofault::init_geofault_from_db();
  100. }
  101. else if (JSON_KEY_NAME_LANDMARK == name)//地标信息更新
  102. {
  103. deal_call_edit_landmark(szParam, edit_type_id);
  104. }
  105. else if (JSON_KEY_NAME_COALFACE==name|| JSON_KEY_NAME_COALFACE_VEHICLE == name ||
  106. JSON_KEY_NAME_DRIVING_FACE == name || JSON_KEY_NAME_DRIVINGFACE_VEHICLE == name
  107. || JSON_KEY_NAME_DRIVINGFACE_REF_POINT == name
  108. || JSON_KEY_NAME_DRIVINGFACE_WARNING_POINT == name)
  109. {
  110. //three_rates::get_instance()->update_db_data(name, szParam, edit_type_id);
  111. }else if(JSON_KEY_NAME_ANTI_COLLISION == name){
  112. deal_edit_anti_collision(szParam, edit_type_id);
  113. }
  114. else
  115. {
  116. log_error("web发来的数据: 基础数据name字段错误:name=%s", name.c_str());
  117. }
  118. }
  119. else
  120. {
  121. if(JSON_KEY_NAME_SETTING == name)
  122. {
  123. //阈值限制接口
  124. init_setting(); ///待实现
  125. }
  126. else
  127. {
  128. log_error("web发来的数据: 基础数据name字段错误:name=%s", name.c_str());
  129. }
  130. }
  131. }
  132. ///3.清除卡相关信息,区域相关,分站相关,考勤相关,清除定时器,
  133. ///速度,状态 呼救 呼叫 告警相关。即保留基础信息,其他的重置。
  134. void module_meta_data_changed::clear_card(const std::shared_ptr<card_location_base>& card_ptr)
  135. {
  136. card_ptr->get_area_tool()->on_leave(card_ptr);
  137. }
  138. /*
  139. * 修改车及卡的数据
  140. * id64 = ;员工ID或者车ID;卡ID=101;0010000001198 分号间隔
  141. */
  142. void module_meta_data_changed::deal_call_edit_vehicle_or_staff(const std::string & id64, EDIT_TYPE_ID edit_type_id)
  143. {
  144. std::vector<std::string> vecSegTag;
  145. boost::split(vecSegTag, id64, boost::is_any_of(";"));
  146. if (vecSegTag.size() == 1) //数据发生错误
  147. {
  148. log_errno("Web Send Data Error!(%s)", id64.c_str());
  149. return;
  150. }
  151. std::string lsz_card_id = vecSegTag[1];
  152. int cid = std::stoi(vecSegTag[0].c_str());
  153. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  154. {
  155. if (ET_UPDATE == edit_type_id)
  156. {
  157. auto c = card_list::instance()->get_card_by_cid(cid);
  158. if (nullptr != c && tool_other::type_id_to_str(c->m_type, c->m_id) != lsz_card_id)
  159. {
  160. //如果修改了卡号,则删除原来的卡的数据
  161. remove_card(c);
  162. }
  163. }
  164. int c_type = tool_other::id64_to_type(lsz_card_id);
  165. if (tool_other::is_vehicle(c_type))
  166. {
  167. card_list::instance()->init_vehicle(lsz_card_id);
  168. }
  169. else if (tool_other::is_person(c_type))
  170. {
  171. card_list::instance()->init_staffer(lsz_card_id);
  172. }
  173. }
  174. else
  175. {
  176. if(!lsz_card_id.empty())
  177. remove_card(tool_other::id64_to_id(lsz_card_id), tool_other::id64_to_type(lsz_card_id));
  178. }
  179. }
  180. void module_meta_data_changed::deal_call_edit_card(std::string & id64, EDIT_TYPE_ID edit_type_id)
  181. {
  182. std::string card_id64_str = tool_other::to13str(id64);
  183. int type = tool_other::id64_to_type(card_id64_str);
  184. if(tool_other::is_person(type) || tool_other::is_vehicle(type))
  185. {
  186. auto c = card_list::instance()->get(tool_other::card_id_to_u64(card_id64_str));
  187. if (nullptr == c) {
  188. log_info("web edit card:%s Not bind staff or vehicle. editType:%d", card_id64_str.c_str(), edit_type_id);
  189. return ;
  190. }
  191. std::string lsz = std::to_string(c->m_cid) + ";" + card_id64_str;
  192. deal_call_edit_vehicle_or_staff(lsz,edit_type_id);
  193. }
  194. else
  195. {
  196. log_error("基础数据 删除卡type不对:type=%d", type);
  197. }
  198. }
  199. void module_meta_data_changed::deal_call_edit_area(const std::string& id,EDIT_TYPE_ID edit_type_id)
  200. {
  201. int cid = std::stoi(id);
  202. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id){
  203. area_list::instance()->init_from_db(cid);
  204. }else if(ET_DELETE == edit_type_id)
  205. {
  206. if(auto area_ptr = area_list::instance()->get(cid)){
  207. log_info("区域删除:areaid=%d", cid);
  208. area_ptr->clear();
  209. area_list::instance()->remove(cid);
  210. }
  211. }
  212. }
  213. void module_meta_data_changed::deal_call_edit_reader(const std::string &ids, EDIT_TYPE_ID edit_type_id)
  214. {
  215. //新增发送的都是单个分站名称,所以
  216. if(ET_INSERT == edit_type_id){
  217. sit_list::instance()->load_from_db(ids);
  218. }
  219. else if(ET_UPDATE == edit_type_id){
  220. sit_list::instance()->init_site(ids);
  221. }else if(ET_DELETE == edit_type_id){
  222. int id =std::stoi(ids);
  223. if(auto sit_ptr=sit_list::instance()->get(id)){
  224. log_info("分站删除:sid=%d", id);
  225. sit_ptr->clear_event();
  226. sit_list::instance()->remove(id);
  227. card_path::init();
  228. }
  229. }
  230. }
  231. void module_meta_data_changed::deal_call_edit_antenna(int id,EDIT_TYPE_ID edit_type_id)
  232. {
  233. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  234. {
  235. sit_list::instance()->read_sit_list(id);
  236. }
  237. //else if(ET_DELETE == edit_type_id)
  238. //{
  239. //delete_antenna(id);
  240. // }
  241. }
  242. void module_meta_data_changed::deal_call_edit_path(int id, EDIT_TYPE_ID edit_type_id)
  243. {
  244. // if(ET_INSERT == edit_type_id)
  245. // {
  246. // log_error("path不支持增加操作");
  247. // std_debug("path不支持增加操作");
  248. // }
  249. // else if(ET_UPDATE == edit_type_id)
  250. // {
  251. // }
  252. if(sit_list::instance()->get(id))
  253. sit_list::instance()->read_ant_path(id);
  254. }
  255. void module_meta_data_changed::deal_call_edit_map(int id, EDIT_TYPE_ID edit_type_id)
  256. {
  257. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  258. {
  259. if(!is_cur_map(id))
  260. {
  261. log_info("修改的不是当前使用的地图,要修改地图id=%d,当前使用地图id=%d", id, cur_map_id());
  262. return;
  263. }
  264. update_map_info(id);
  265. }
  266. else if(ET_DELETE == edit_type_id)
  267. {
  268. if(is_cur_map(id))
  269. {
  270. log_error("删除了当前使用的地图");
  271. return;
  272. }
  273. }
  274. }
  275. void module_meta_data_changed::deal_call_edit_light(int id, EDIT_TYPE_ID edit_type_id)
  276. {
  277. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  278. {
  279. traffic_light_manager::instance()->init_light_from_db(id);
  280. }
  281. else if(ET_DELETE == edit_type_id)
  282. {
  283. traffic_light_manager::instance()->remove_light(id);
  284. }
  285. }
  286. void module_meta_data_changed::deal_call_edit_lights_group(int id, EDIT_TYPE_ID edit_type_id)
  287. {
  288. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  289. {
  290. traffic_light_manager::instance()->init_light_group_from_db(id);
  291. }
  292. else if(ET_DELETE == edit_type_id)
  293. {
  294. traffic_light_manager::instance()->remove_light_group(id);
  295. }
  296. }
  297. // 禁止指定人员下井 id = (rt_person_forbid_down_mine)数据库中自增长ID
  298. void module_meta_data_changed::deal_call_edit_forbid_person_down_mine(const std::string & lszId,EDIT_TYPE_ID edit_type_id)
  299. {
  300. std::vector<std::string> vecSegTag;
  301. boost::split(vecSegTag, lszId, boost::is_any_of(";"));
  302. if (vecSegTag.size() == 1) //数据发生错误
  303. {
  304. log_errno("Web Send Data Error!(%s)", lszId.c_str());
  305. return;
  306. }
  307. int db_id = std::stoi(vecSegTag[0].c_str());
  308. int staff_id = std::stoi(vecSegTag[1].c_str());
  309. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  310. {
  311. forbid_staff_down_mine::instance()->init_forbid_staff(db_id,edit_type_id);
  312. }
  313. else if(ET_DELETE == edit_type_id)
  314. {
  315. forbid_staff_down_mine::instance()->del_forbid_data(db_id,staff_id);
  316. }
  317. }
  318. // 设置区域中时间段人员超员阈值等数据
  319. void module_meta_data_changed::deal_call_edit_persons_dynamic_thre(int area_id,EDIT_TYPE_ID edit_type_id)
  320. {
  321. area_list::instance()->init_area_persons_dynamic_thre_from_db(area_id);
  322. }
  323. // 修改地标信息
  324. void module_meta_data_changed::deal_call_edit_landmark(const std::string & landmarkid, EDIT_TYPE_ID edit_type_id)
  325. {
  326. int id = atoi(landmarkid.c_str());
  327. if (ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  328. {
  329. Landmark_list::instance()->init_landmarkInfo(id);
  330. }
  331. else if (ET_DELETE == edit_type_id)
  332. {
  333. Landmark_list::instance()->remove(id);
  334. }
  335. }
  336. void module_meta_data_changed::init_setting()
  337. {
  338. CYaSetting::Init_sys_setting();
  339. }
  340. void module_meta_data_changed::remove_card(uint32_t id, int32_t type) {
  341. uint64_t card_id = tool_other::type_id_to_u64(type, id);
  342. auto card_ptr = card_list::instance()->get(card_id);
  343. if (!card_ptr) {
  344. log_error("基础数据, 在全局列表中删除卡,全局列表中已经不存在此卡, id=%d, type=%d", id, type);
  345. return;
  346. }
  347. remove_card(card_ptr);
  348. }
  349. /*
  350. * @brief 从内存中删除卡
  351. * @param std::shared_ptr<card_location_base> card_ptr 卡对象
  352. * @return 无
  353. * @note
  354. * @warning
  355. * @bug
  356. * */
  357. void module_meta_data_changed::remove_card(std::shared_ptr<card_location_base> card_ptr)
  358. {
  359. if (!card_ptr)
  360. {
  361. return;
  362. }
  363. log_info("基础数据, 在全局列表中删除卡成功, type=%d, id=%d",card_ptr->m_type, card_ptr->m_id);
  364. module_meta_data_changed::clear_card(card_ptr);
  365. // 避免状态重置
  366. uint64_t card_id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
  367. card_list::instance()->remove(card_id);
  368. }
  369. bool module_meta_data_changed::try_get_edit_type_id(const std::string& op_type, EDIT_TYPE_ID& out_edit_type_id)
  370. {
  371. if(JSON_KEY_OP_TYPE_UPDATE == op_type)
  372. {
  373. out_edit_type_id = ET_UPDATE;
  374. }
  375. else if(JSON_KEY_OP_TYPE_DELETE == op_type)
  376. {
  377. out_edit_type_id = ET_DELETE;
  378. }
  379. else if(JSON_KEY_OP_TYPE_INSERT == op_type)
  380. {
  381. out_edit_type_id = ET_INSERT;
  382. }
  383. else
  384. {
  385. return false;
  386. }
  387. return true;
  388. }
  389. ///删除区域所有报警信息
  390. //void module_meta_data_changed::delete_area_event(std::shared_ptr<area> area_ptr)
  391. //{
  392. // for(int i=0; i < CARD_EVENT_COUNT_MAX; i++ )
  393. // {
  394. // event_tool::instance()->handle_event(OT_AREA, static_cast<EVENT_TYPE>(i), area_ptr->id(), 0, 0, false);
  395. // }
  396. //}
  397. //void module_meta_data_changed::delete_antenna(int id)
  398. //{
  399. // int sitid=-8;
  400. // auto map = sit_list::instance()->m_map;
  401. // for(auto&it:map)
  402. // {
  403. // auto sit_ptr = it.second;
  404. // if(sit_ptr->m_ant[0].m_id == id)
  405. // {
  406. // sitid=sit_ptr->m_id;
  407. // sit_ptr->delete_antenna(0);
  408. // break;
  409. // }
  410. // if(sit_ptr->m_ant[1].m_id == id)
  411. // {
  412. // sitid=sit_ptr->m_id;
  413. // sit_ptr->delete_antenna(1);
  414. // break;
  415. // }
  416. // }
  417. //
  418. // if(sitid==-8)
  419. // {
  420. // std_debug("天线已经删除了,在分站列表中找不到该天线:天线id=%d", id);
  421. // log_info("天线已经删除了,在分站列表中找不到该天线:天线id=%d", id);
  422. // }
  423. // else
  424. // {
  425. // std_debug("删除天线成功:天线id=%d,分站id=%d", id, sitid);
  426. // log_info("删除天线成功:天线id=%d,分站id=%d", id, sitid);
  427. // }
  428. //}
  429. //
  430. bool module_meta_data_changed::is_cur_map(int id)
  431. {
  432. return cur_map_id()==id;
  433. }
  434. int module_meta_data_changed::cur_map_id()
  435. {
  436. auto tmp = area_list::instance()->m_map;
  437. for(auto&it:tmp)
  438. {
  439. if(0!=it.second->m_mapid)
  440. {
  441. return it.second->m_mapid;
  442. }
  443. }
  444. return -1;
  445. }
  446. void module_meta_data_changed::update_map_info(int id)
  447. {
  448. std::string sql = "SELECT scale FROM dat_map WHERE map_id=";
  449. sql.append(std::to_string(id));
  450. sql.append(";");
  451. std_debug("修改地图 sql=%s", sql.c_str());
  452. log_info("修改地图 sql=%s", sql.c_str());
  453. std::string Error;
  454. YADB::CDBResultSet DBRes;
  455. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  456. int nCount = DBRes.GetRecordCount( Error );
  457. if (nCount < 1)
  458. {
  459. log_error("修改地图失败,数据库中找不到: map_id=%d", id);
  460. return ;
  461. }
  462. double scale = 0;
  463. if (DBRes.GetNextRecod(Error))
  464. {
  465. DBRes.GetField("scale", scale, Error);
  466. }
  467. if (scale < 1e-4)
  468. {
  469. log_error("修改地图,数据库地图比例尺错误: map_id=%d,scale=%.2f", id,scale);
  470. return;
  471. }
  472. auto area = area_list::instance()->m_map;
  473. for(auto& it : area)
  474. {
  475. it.second->m_scale = scale;
  476. }
  477. auto site = sit_list::instance()->m_map;
  478. for(auto& it : site)
  479. {
  480. it.second->m_scale = scale;
  481. }
  482. }
  483. void module_meta_data_changed::deal_edit_anti_collision(const std::string& value, EDIT_TYPE_ID& edit_type_id)
  484. {
  485. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  486. {
  487. CYaSetting::m_sys_setting.init_anti_coll_value(value);
  488. }
  489. else if(ET_DELETE == edit_type_id)
  490. {
  491. CYaSetting::m_sys_setting.del_anti_coll_value();
  492. }
  493. }