module_meta_date_changed.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. #include "module_meta_date_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 <boost/format.hpp>
  12. #include <boost/tokenizer.hpp>
  13. #include <boost/algorithm/string/split.hpp>
  14. #include <boost/algorithm/string.hpp>
  15. ///基础数据
  16. void module_meta_date_changed::accept(sio::message::ptr const& data)
  17. {
  18. std::string name="";
  19. if(!tool_map::try_get_value(name, JSON_KEY_NAME, data))
  20. {
  21. log_error("web发来的数据: 基础数据找不到name字段");
  22. return;
  23. }
  24. int64_t id=-1;
  25. tool_map::try_get_value(id, JSON_KEY_ID, data);
  26. std::string szParam = "0";
  27. tool_map::try_get_value(szParam,JSON_KEY_ID,data);
  28. std::string op_type="";
  29. tool_map::try_get_value(op_type, JSON_KEY_OP_TYPE, data);
  30. if(-1 != id && !op_type.empty())
  31. {
  32. EDIT_TYPE_ID edit_type_id;
  33. if(!try_get_edit_type_id(op_type, edit_type_id))
  34. {
  35. log_error("web发来的数据: 基础数据op_type字段错误:op_type=%s", op_type.c_str());
  36. return;
  37. }
  38. log_info("基础数据 receive meta_data_changed: %s, id=%d, op_type=%d", name.c_str(), id, edit_type_id);
  39. if(JSON_KEY_NAME_VEHICLE == name || JSON_KEY_NAME_VEHICLE_EXTEND == name)
  40. {
  41. deal_call_edit_vehicle_or_staff(szParam, edit_type_id);
  42. }
  43. else if(JSON_KEY_NAME_STAFF == name || JSON_KEY_NAME_STAFF_EXTEND == name)
  44. {
  45. deal_call_edit_vehicle_or_staff(szParam, edit_type_id);
  46. }
  47. else if(JSON_KEY_NAME_CARD == name)
  48. {
  49. deal_call_edit_card(szParam, edit_type_id);
  50. }
  51. else if(JSON_KEY_NAME_AREA == name)
  52. {
  53. deal_call_edit_area(id, edit_type_id);
  54. }
  55. else if(JSON_KEY_NAME_READER == name)
  56. {
  57. deal_call_edit_reader(id, edit_type_id);
  58. }
  59. else if ("antenna" == name)
  60. {
  61. deal_call_edit_antenna(id,edit_type_id);
  62. }
  63. else if(JSON_KEY_NAME_PATH == name)
  64. {
  65. deal_call_edit_path(id, edit_type_id);
  66. }
  67. else if(JSON_KEY_NAME_MAP == name)
  68. {
  69. deal_call_edit_map(id, edit_type_id);
  70. }
  71. else if (JSON_KEY_NAME_LIGHT == name)
  72. {
  73. deal_call_edit_light(id,edit_type_id);///待实现
  74. }
  75. else if ("lights_group" == name)
  76. {
  77. deal_call_edit_lights_group(id,edit_type_id);///待实现
  78. }
  79. else
  80. {
  81. log_error("web发来的数据: 基础数据name字段错误:name=%s", name.c_str());
  82. }
  83. }
  84. else
  85. {
  86. if(JSON_KEY_NAME_SETTING == name)
  87. {
  88. //阈值限制接口
  89. init_setting(); ///待实现
  90. }
  91. else
  92. {
  93. log_error("web发来的数据: 基础数据name字段错误:name=%s", name.c_str());
  94. }
  95. }
  96. }
  97. ///3.清除卡相关信息,区域相关,分站相关,考勤相关,清除定时器,
  98. ///速度,状态 呼救 呼叫 告警相关。即保留基础信息,其他的重置。
  99. void module_meta_date_changed::clear_card(std::shared_ptr<card_location_base> card_ptr)
  100. {
  101. auto rea_tool = card_ptr->get_area_tool();
  102. rea_tool->on_leave(card_ptr);
  103. //card_ptr->clear();
  104. // //删除
  105. // //card_ptr->del_card_pos();
  106. // 升井 删除所有报警信息
  107. for(int i=0; i < CARD_EVENT_COUNT_MAX; i++ )
  108. {
  109. if (i == EVENT_TYPE::ET_CARD_LOW_POWER_SERIOUS)
  110. {
  111. continue;
  112. }
  113. uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
  114. event_tool::instance()->handle_event(OT_CARD, static_cast<EVENT_TYPE>(i), id, 0, 0, false);
  115. // auto ev_ptr = event_list::instance()->get_event_card(
  116. // card_ptr->m_id, card_ptr->m_type, static_cast<EVENT_TYPE>(i));
  117. // if(ev_ptr && !ev_ptr->is_end()) //
  118. // {
  119. // event_list::copy_event(card_ptr, ev_ptr);
  120. // ev_ptr->m_status = ES_END;
  121. // event_list::save_event(ev_ptr);
  122. // }
  123. }
  124. }
  125. /*
  126. * 修改车及卡的数据
  127. * 在 ET_INSERT | ET_DELETE id64 = 卡ID =10000001198
  128. * ET_UPDATE id64 = 卡ID;员工ID或者车ID=10000001198;101 分号间隔
  129. */
  130. void module_meta_date_changed::deal_call_edit_vehicle_or_staff(const std::string & id64, EDIT_TYPE_ID edit_type_id)
  131. {
  132. // 在 ET_INSERT | ET_DELETE id64 = 卡ID =10000001198
  133. //ET_UPDATE id64 = 卡ID;员工ID或者车ID=10000001198;101 分号间隔
  134. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  135. {
  136. int64_t card_id = std::stoll(id64);
  137. if (ET_UPDATE == edit_type_id)
  138. {
  139. std::vector<std::string> vecSegTag;
  140. boost::split(vecSegTag, id64,boost::is_any_of(";"));
  141. if (vecSegTag.size() == 1) //数据发生错误
  142. {
  143. log_errno("Web Send Data Error!(%s)",id64.c_str());
  144. return;
  145. }
  146. auto c = card_list::instance()->get_card_by_cid(std::stoi(vecSegTag[1].c_str()));
  147. if (nullptr != c && tool_other::type_id_to_str(c->m_type,c->m_id) != tool_other::to13str(vecSegTag[0]))
  148. {
  149. //如果修改了卡号,则删除原来的卡的数据
  150. remove_card(c);
  151. }
  152. card_id = std::stoll(vecSegTag[0]);
  153. }
  154. card_list::instance()->init_vehicle(card_id);
  155. }
  156. else
  157. {
  158. std::string card_id = id64;
  159. auto str = tool_other::to13str(card_id);
  160. remove_card(tool_other::id64_to_id(str), tool_other::id64_to_type(str));
  161. }
  162. }
  163. void module_meta_date_changed::deal_call_edit_card(std::string & id64, EDIT_TYPE_ID edit_type_id)
  164. {
  165. std::string lsz = id64;
  166. std::string card_id64_str = tool_other::to13str(lsz);
  167. int type = tool_other::id64_to_type(card_id64_str);
  168. if(tool_other::is_person(type) || tool_other::is_vehicle(type))
  169. {
  170. if (edit_type_id == ET_UPDATE)
  171. {
  172. auto c = card_list::instance()->get(tool_other::card_id_to_u64(card_id64_str));
  173. if (nullptr != c)
  174. {
  175. char id[100] = {0};
  176. sprintf(id,"%s;%d",lsz.c_str(),c->m_cid);
  177. lsz = id;
  178. }
  179. }
  180. deal_call_edit_vehicle_or_staff(lsz,edit_type_id);
  181. }
  182. else
  183. {
  184. log_error("基础数据 删除卡type不对:type=%d", type);
  185. }
  186. }
  187. void module_meta_date_changed::deal_call_edit_area(int id, EDIT_TYPE_ID edit_type_id)
  188. {
  189. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  190. {
  191. if(is_monkeycar_area(id))
  192. {
  193. area_list::instance()->init_monkeycar_area(id);
  194. }
  195. else
  196. {
  197. area_list::instance()->init_from_db(id);
  198. }
  199. }
  200. else if(ET_DELETE == edit_type_id)
  201. {
  202. auto area_ptr = area_list::instance()->get(id);
  203. if(!area_ptr)
  204. {
  205. log_info("区域已经删除:areaid=%d", id);
  206. return;
  207. }
  208. //删除告警
  209. delete_area_event(area_ptr);
  210. area_list::instance()->remove(id);
  211. }
  212. }
  213. void module_meta_date_changed::deal_call_edit_reader(int id, EDIT_TYPE_ID edit_type_id)
  214. {
  215. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  216. {
  217. sit_list::instance()->init_site(id);
  218. }
  219. else if(ET_DELETE == edit_type_id)
  220. {
  221. //删除告警
  222. sit_list::instance()->remove(id);
  223. }
  224. }
  225. void module_meta_date_changed::deal_call_edit_antenna(int id,EDIT_TYPE_ID edit_type_id)
  226. {
  227. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  228. {
  229. sit_list::instance()->read_sit_list(id);
  230. }
  231. else if(ET_DELETE == edit_type_id)
  232. {
  233. delete_antenna(id);
  234. }
  235. }
  236. void module_meta_date_changed::deal_call_edit_path(int id, EDIT_TYPE_ID edit_type_id)
  237. {
  238. if(ET_INSERT == edit_type_id)
  239. {
  240. log_error("path不支持增加操作");
  241. std_debug("path不支持增加操作");
  242. }
  243. else if(ET_UPDATE == edit_type_id)
  244. {
  245. sit_list::instance()->read_ant_path(id);
  246. }
  247. else if(ET_DELETE == edit_type_id)
  248. {
  249. auto sit_ptr = sit_list::instance()->get(id);
  250. if(sit_ptr)
  251. {
  252. sit_ptr->clear_path();
  253. }
  254. log_info("path删除成功,分站id=%d", id);
  255. std_debug("path删除成功,分站id=%d", id);
  256. }
  257. }
  258. void module_meta_date_changed::deal_call_edit_map(int id, EDIT_TYPE_ID edit_type_id)
  259. {
  260. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  261. {
  262. if(!is_cur_map(id))
  263. {
  264. log_info("修改的不是当前使用的地图,要修改地图id=%d,当前使用地图id=%d", id, cur_map_id());
  265. return;
  266. }
  267. update_map_info(id);
  268. }
  269. else if(ET_DELETE == edit_type_id)
  270. {
  271. if(is_cur_map(id))
  272. {
  273. log_error("删除了当前使用的地图");
  274. return;
  275. }
  276. }
  277. }
  278. void module_meta_date_changed::deal_call_edit_light(int id, EDIT_TYPE_ID edit_type_id)
  279. {
  280. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  281. {
  282. }
  283. else if(ET_DELETE == edit_type_id)
  284. {
  285. }
  286. }
  287. void module_meta_date_changed::deal_call_edit_lights_group(int id, EDIT_TYPE_ID edit_type_id)
  288. {
  289. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  290. {
  291. }
  292. else if(ET_DELETE == edit_type_id)
  293. {
  294. }
  295. }
  296. void module_meta_date_changed::init_setting()
  297. {
  298. //pRes = getMysqlRes("select setting_id, name, type, value from dat_setting;");
  299. }
  300. void module_meta_date_changed::remove_card(uint32_t id, int32_t type) {
  301. uint64_t card_id = tool_other::type_id_to_u64(type, id);
  302. auto card_ptr = card_list::instance()->get(card_id);
  303. if (!card_ptr) {
  304. log_error("基础数据, 在全局列表中删除卡,全局列表中已经不存在此卡, id=%d, type=%d", id, type);
  305. return;
  306. }
  307. remove_card(card_ptr);
  308. }
  309. void module_meta_date_changed::remove_card(std::shared_ptr<card_location_base> card_ptr)
  310. {
  311. if (!card_ptr)
  312. {
  313. return;
  314. }
  315. log_info("基础数据, 在全局列表中删除卡成功, id=%d, type=%d",card_ptr->m_type, card_ptr->m_id);
  316. // auto area_hover_ptr = card_ptr->get_area_hover();
  317. // if(area_hover_ptr && 0!=area_hover_ptr->id() && 0!=area_hover_ptr->mapid())
  318. // {
  319. // module_area::on_leave(card_ptr->m_id, area_hover_ptr, card_ptr->m_type);
  320. // }
  321. // if(card_ptr->is_person())
  322. // {
  323. // module_attendance_person::up_mine(card_ptr, nullptr);
  324. // }
  325. // else
  326. // {
  327. // module_attendance_vehicle::save_attendance(card_ptr, nullptr);
  328. // module_meta_date_changed::clear_card(card_ptr);
  329. // }
  330. module_meta_date_changed::clear_card(card_ptr);
  331. // 避免状态重置
  332. uint64_t card_id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
  333. card_list::instance()->remove(card_id);
  334. }
  335. bool module_meta_date_changed::try_get_edit_type_id(const std::string& op_type, EDIT_TYPE_ID& out_edit_type_id)
  336. {
  337. if(JSON_KEY_OP_TYPE_UPDATE == op_type)
  338. {
  339. out_edit_type_id = ET_UPDATE;
  340. }
  341. else if(JSON_KEY_OP_TYPE_DELETE == op_type)
  342. {
  343. out_edit_type_id = ET_DELETE;
  344. }
  345. else if(JSON_KEY_OP_TYPE_INSERT == op_type)
  346. {
  347. out_edit_type_id = ET_INSERT;
  348. }
  349. else
  350. {
  351. return false;
  352. }
  353. return true;
  354. }
  355. bool module_meta_date_changed::is_monkeycar_area(int area_id)
  356. {
  357. char sql[1024] = {'\0'};
  358. sprintf(sql,"select monkeycar_areaid from dat_monkeycar_base_info where monkeycar_areaid=%d;", area_id);
  359. std::string Error;
  360. YADB::CDBResultSet DBRes;
  361. sDBConnPool.Query(sql,DBRes,Error);
  362. int64_t nCount = DBRes.GetRecordCount( Error );
  363. log_info("monkeycar_area区域查询:nCount=%d,sql=%s", nCount, sql);
  364. std_debug("monkeycar_area区域查询:nCount=%d,sql=%s", nCount, sql);
  365. return (nCount > 0);
  366. }
  367. ///删除区域所有报警信息
  368. void module_meta_date_changed::delete_area_event(std::shared_ptr<area> area_ptr)
  369. {
  370. for(int i=0; i < CARD_EVENT_COUNT_MAX; i++ )
  371. {
  372. event_tool::instance()->handle_event(OT_AREA, static_cast<EVENT_TYPE>(i), area_ptr->id(), 0, 0, false);
  373. }
  374. }
  375. void module_meta_date_changed::delete_antenna(int id)
  376. {
  377. int sitid=-8;
  378. auto map = sit_list::instance()->m_map;
  379. for(auto&it:map)
  380. {
  381. auto sit_ptr = it.second;
  382. if(sit_ptr->m_ant[0].m_id == id)
  383. {
  384. sitid=sit_ptr->m_id;
  385. sit_ptr->delete_antenna(0);
  386. break;
  387. }
  388. if(sit_ptr->m_ant[1].m_id == id)
  389. {
  390. sitid=sit_ptr->m_id;
  391. sit_ptr->delete_antenna(1);
  392. break;
  393. }
  394. }
  395. if(sitid==-8)
  396. {
  397. std_debug("天线已经删除了,在分站列表中找不到该天线:天线id=%d", id);
  398. log_info("天线已经删除了,在分站列表中找不到该天线:天线id=%d", id);
  399. }
  400. else
  401. {
  402. std_debug("删除天线成功:天线id=%d,分站id=%d", id, sitid);
  403. log_info("删除天线成功:天线id=%d,分站id=%d", id, sitid);
  404. }
  405. }
  406. bool module_meta_date_changed::is_cur_map(int id)
  407. {
  408. return cur_map_id()==id;
  409. }
  410. int module_meta_date_changed::cur_map_id()
  411. {
  412. auto tmp = area_list::instance()->m_map;
  413. for(auto&it:tmp)
  414. {
  415. if(0!=it.second->m_mapid)
  416. {
  417. return it.second->m_mapid;
  418. }
  419. }
  420. return -1;
  421. }
  422. void module_meta_date_changed::update_map_info(int id)
  423. {
  424. std::string sql = "SELECT scale FROM dat_map WHERE map_id=";
  425. sql.append(std::to_string(id));
  426. sql.append(";");
  427. std_debug("修改地图 sql=%s", sql.c_str());
  428. log_info("修改地图 sql=%s", sql.c_str());
  429. std::string Error;
  430. YADB::CDBResultSet DBRes;
  431. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  432. int nCount = DBRes.GetRecordCount( Error );
  433. if (nCount < 1)
  434. {
  435. log_error("修改地图失败,数据库中找不到: map_id=%d", id);
  436. return ;
  437. }
  438. double scale = 0;
  439. DBRes.GetField( "scale",scale, Error );
  440. auto area = area_list::instance()->m_map;
  441. for(auto& it : area)
  442. {
  443. it.second->m_scale = scale;
  444. }
  445. auto site = sit_list::instance()->m_map;
  446. for(auto& it : site)
  447. {
  448. it.second->m_scale = scale;
  449. }
  450. }