card.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #include <memory>
  2. #include "common_tool.h"
  3. #include "message.h"
  4. #include "loc_message.h"
  5. #include "config_file.h"
  6. #include "card_person.h"
  7. #include "card_car.h"
  8. #include "db/db_card.h"
  9. #include "db/db_api/CDBSingletonDefine.h"
  10. #include "db/db_api/CDBResultSet.h"
  11. #include "tool_time.h"
  12. #include "mine.h"
  13. #include "log.h"
  14. #include "area.h"
  15. #include "card.h"
  16. #include "mine_business.h"
  17. #include "worker.h"
  18. #include "sys_setting.h"
  19. extern config_file config;
  20. int three_rates_flag = 0;
  21. int traffic_light_flag = 0;
  22. int anti_coll_flag = 0;
  23. int use_original_algo = 0;
  24. static void request(uint64_t id,uint32_t hash_id,uint16_t dis)
  25. {
  26. task * t = task::alloc<message_change_card_display>();
  27. auto&mcb=t->body<message_change_card_display>();
  28. t->m_hash_id=hash_id;
  29. t->m_cmd_code=0x10002;
  30. mcb.m_card_id=id;
  31. mcb.m_display=dis;
  32. worker::instance()->request(t);
  33. }
  34. void card_list::init_staffer(const std::string & lszId64)
  35. {
  36. std::string strategy = config.get("person.strategy","PS_1");
  37. std::unordered_map<uint64_t,std::shared_ptr<card_location_base>>
  38. map=std::move(db_card::load_person(lszId64,strategy));
  39. if(map.empty())
  40. {
  41. log_error("增加或修改失败,数据库中找不到: card_id=%s", lszId64.c_str());
  42. return ;
  43. }
  44. if("" == lszId64)
  45. {
  46. log_info( "init_staffer. The record count=%d\n", map.size() );
  47. card_list::instance()->add(map);
  48. }
  49. else
  50. {
  51. auto db_person=map.begin()->second;
  52. uint64_t id64 = map.begin()->first;
  53. auto card_ptr = card_list::instance()->get(id64);
  54. if(card_ptr)
  55. {
  56. person* tmp_ptr= static_cast<person*>(card_ptr.get());
  57. person* db_person_ptr= static_cast<person*>(db_person.get());
  58. card_ptr->set_base_data(db_person_ptr->m_cid, db_person_ptr->m_type, db_person_ptr->m_deptid, db_person_ptr->m_level_id);
  59. //group_id
  60. //occ_id
  61. tmp_ptr->m_workLine = db_person_ptr->m_workLine;
  62. tmp_ptr->m_stafferName = db_person_ptr->m_stafferName;
  63. tmp_ptr->m_deptName = db_person_ptr->m_deptName;
  64. tmp_ptr->m_worktype_id = db_person_ptr->m_worktype_id;
  65. if(db_person_ptr->m_display!=tmp_ptr->m_display){
  66. request(id64, tmp_ptr->m_id, db_person_ptr->m_display);
  67. }
  68. }
  69. else
  70. {
  71. card_list::instance()->add(id64, db_person);
  72. }
  73. log_info("基础数据 增加或修改人卡成功:卡id:%ld, dept_id:%d,need_display:%d,occupation_level_id:%d",
  74. id64,db_person->m_deptid,db_person->m_display,db_person->m_level_id);
  75. }
  76. }
  77. void card_list::init_vehicle(const std::string & lszId64)
  78. {
  79. std::unordered_map<uint64_t,std::shared_ptr<card_location_base>>
  80. map=std::move(db_card::load_car(lszId64));
  81. if(map.empty())
  82. {
  83. log_error("增加或修改失败,数据库中找不到: card_id=%s", lszId64.c_str());
  84. return ;
  85. }
  86. if("" == lszId64)
  87. {
  88. log_info( "init_vehicle. The record count=%d\n", map.size() );
  89. card_list::instance()->add(map);
  90. }
  91. else
  92. {
  93. uint64_t id64 = map.begin()->first;
  94. auto card_ptr = card_list::instance()->get(id64);
  95. car* db_car= static_cast<car*>(map.begin()->second.get());
  96. if(card_ptr)
  97. {
  98. car* tmp_ptr= static_cast<car*>(card_ptr.get());
  99. card_ptr->set_base_data(db_car->m_cid,db_car->m_type,db_car->m_deptid,db_car->m_level_id);
  100. tmp_ptr->m_vehicle_category_id = db_car->m_vehicle_category_id;
  101. tmp_ptr->m_vehicle_type_id = db_car->m_vehicle_type_id;
  102. if(db_car->m_display!=tmp_ptr->m_display){
  103. request(id64,tmp_ptr->m_id,db_car->m_display);
  104. }
  105. }
  106. else
  107. {
  108. card_list::instance()->add(id64, map.begin()->second);
  109. }
  110. log_info("基础数据 增加或修改车卡成功:卡id:%ld, dept_id:%d,need_display:%d,categoryid:%d",
  111. id64,db_car->m_deptid,db_car->m_display,db_car->m_vehicle_category_id);
  112. }
  113. }
  114. void card_list::init_card_from_db()
  115. {
  116. init_staffer("");
  117. init_vehicle("");
  118. }
  119. void card_list::load_his_card_postion_from_db()
  120. {
  121. load_his_card_postion_vehicle();
  122. load_his_card_postion_staff();
  123. }
  124. void card_list::load_his_card_postion_vehicle()
  125. {
  126. const char *sql = "select l.card_id,l.site_id, l.cur_time, l.x, l.y, l.z, l.area_info, l.state, \
  127. att.start_time, att.end_time \
  128. from rt_location l, rt_att_vehicle att, dat_card c, dat_vehicle_extend v \
  129. where l.card_id = c.card_id and l.card_id = att.card_id and l.card_id=v.card_id \
  130. and c.state_id=0;";
  131. std::string Error;
  132. YADB::CDBResultSet DBRes;
  133. sDBConnPool.Query(sql,DBRes,Error);
  134. if(!Error.empty())
  135. log_error("load_his_card_postion_vehicle列表 Error,%s",Error.c_str());
  136. uint64_t nCount = DBRes.GetRecordCount( Error );
  137. if (nCount > 0)
  138. {
  139. log_info( "load_his_card_postion_vehicle. The record count=%ld\n", nCount );
  140. while ( DBRes.GetNextRecod(Error) )
  141. {
  142. std::string card_id = "";
  143. DBRes.GetField( "card_id",card_id, Error );
  144. uint64_t c_id = tool_other::card_id_to_u64(card_id);
  145. auto card_ptr = card_list::instance()->get(c_id);
  146. if(!card_ptr)
  147. {
  148. log_error("全局卡列表中找不到卡,%s",card_id.c_str());
  149. continue;
  150. }
  151. int site_id = -1;
  152. DBRes.GetField( "site_id",site_id, Error );
  153. double x = 0;
  154. DBRes.GetField( "x",x, Error );
  155. double y = 0;
  156. DBRes.GetField( "y",y, Error );
  157. double z = 0;
  158. DBRes.GetField( "z",z, Error );
  159. std::string strAreaInfo = "";
  160. DBRes.GetField( "area_info",strAreaInfo, Error );
  161. int state = 0;
  162. DBRes.GetField( "state",state, Error );
  163. double speed = 0;
  164. DBRes.GetField( "speed",speed, Error );
  165. std::string start_time = "";
  166. DBRes.GetField( "start_time",start_time, Error );
  167. std::string end_time = "";
  168. DBRes.GetField( "end_time",end_time, Error );
  169. std::string cur_time = "";
  170. DBRes.GetField( "cur_time",cur_time, Error );
  171. card_ptr->m_time = tool_time::to_time(cur_time)*1000;
  172. card_ptr->set(x,y,z);
  173. //考勤结束也要初始化该变量,用于统计今天出勤过得车卡数量
  174. auto mine_tool_ptr = card_ptr->get_mine_tool();
  175. mine_tool_ptr->m_attendance_start_time = tool_time::to_time_ex(start_time);
  176. if(end_time.empty())
  177. {
  178. mine_tool_ptr->m_is_attendance = true;
  179. log_info("vehicle加载考勤,考勤开始时间: %s, 卡id=%s", start_time.c_str(), card_id.c_str());
  180. }
  181. auto area_tool=card_ptr->get_area_tool();
  182. auto sit_ptr=sit_list::instance()->get(site_id);
  183. if(sit_ptr)
  184. {
  185. auto area_hover_ptr = std::make_shared<area_hover>(sit_ptr->get_area(),*card_ptr);
  186. area_tool->m_hover_list.push_back(area_hover_ptr);
  187. area_tool->set_site(sit_ptr);
  188. }
  189. //解析area_info
  190. const char * split = "|";
  191. char *p = strtok(const_cast<char *>(strAreaInfo.c_str()), split);
  192. while (p != NULL)
  193. {
  194. int area_id = 0;
  195. uint64_t enter_time = 0;
  196. sscanf(p, "%d,%lu", &area_id, &enter_time);
  197. p = strtok(NULL, split);
  198. if (area_id < 0)
  199. continue;
  200. auto area = area_list::instance()->get(area_id);
  201. if(area == nullptr)
  202. {
  203. log_error("load_vehicle...area_list 找不到区域:区域id=%d", area_id);
  204. continue;
  205. }
  206. log_info("load_vehicle_area:%s,%d,%lu",card_id.c_str(),area_id,enter_time);
  207. //要放到最后
  208. auto area_hover_ptr = std::make_shared<area_hover>();
  209. area_hover_ptr->m_area = area;
  210. area_hover_ptr->m_enter_time = enter_time;
  211. area_tool->m_hover_list.push_back(area_hover_ptr);
  212. area->on_load_his(area_hover_ptr, card_ptr);
  213. }
  214. }
  215. }
  216. }
  217. void card_list::load_his_card_postion_staff()
  218. {
  219. std::string init_limit_hour = config.get("service.init_limit_hour","48");
  220. /*std::string sql = "select l.card_id,l.site_id, l.cur_time, l.x, l.y, l.z, l.area_info, l.state, l.power_status,\
  221. att.start_time, att.end_time \
  222. from rt_location l, rt_att_staff att, dat_card c, dat_staff_extend s \
  223. where l.card_id = c.card_id and l.card_id = att.card_id and l.card_id=s.card_id\
  224. and c.state_id=0 and end_time is null ";*/
  225. std::string sql = "SELECT att.card_id,l.site_id, l.cur_time, l.x, l.y, l.z, l.area_info, l.state, l.power_status, att.start_time, att.end_time FROM rt_att_staff AS att \
  226. LEFT JOIN rt_location AS l ON l.card_id=att.card_id \
  227. LEFT JOIN dat_card AS c ON c.card_id=l.card_id \
  228. LEFT JOIN dat_staff_extend AS s ON s.card_id=l.card_id \
  229. WHERE c.state_id=0 and att.end_time is null ";
  230. sql += " and timestampdiff(hour ,att.start_time,now()) <= " + init_limit_hour + ";";
  231. log_info("load_his_card_postion_staff:%s ",sql.c_str());
  232. std::string Error;
  233. YADB::CDBResultSet DBRes;
  234. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  235. if(!Error.empty())
  236. log_error("load_his_card_postion_staff列表 Error,%s",Error.c_str());
  237. uint64_t nCount = DBRes.GetRecordCount( Error );
  238. if (nCount > 0)
  239. {
  240. log_info( "load_his_card_postion_staff. The record count=%ld\n", nCount );
  241. while ( DBRes.GetNextRecod(Error) )
  242. {
  243. std::string end_time = "";
  244. DBRes.GetField( "end_time",end_time, Error );
  245. if(!end_time.empty()) // 已升井,不再读入
  246. continue;
  247. std::string card_id = "";
  248. DBRes.GetField( "card_id",card_id, Error );
  249. uint64_t c_id = tool_other::card_id_to_u64(card_id);
  250. auto card_ptr = card_list::instance()->get(c_id);
  251. if(!card_ptr)
  252. {
  253. log_error("全局卡列表中找不到卡,%s", card_id.c_str());
  254. continue;
  255. }
  256. int site_id = -1;
  257. DBRes.GetField( "site_id",site_id, Error );
  258. double x = 0;
  259. DBRes.GetField( "x",x, Error );
  260. double y = 0;
  261. DBRes.GetField( "y",y, Error );
  262. double z = 0;
  263. DBRes.GetField( "z",z, Error );
  264. std::string strAreaInfo = "";
  265. DBRes.GetField( "area_info",strAreaInfo, Error );
  266. int state = 0;
  267. DBRes.GetField( "state",state, Error );
  268. int power_status = 0;
  269. DBRes.GetField("power_status", power_status, Error);
  270. double speed = 0;
  271. DBRes.GetField( "speed",speed, Error );
  272. std::string start_time = "";
  273. DBRes.GetField( "start_time",start_time, Error );
  274. std::string cur_time = "";
  275. DBRes.GetField( "cur_time",cur_time, Error );
  276. card_ptr->set(x,y,z);
  277. card_ptr->m_time = tool_time::to_time(cur_time)*1000;
  278. card_ptr->m_battery_value = power_status;
  279. auto mine_tool_ptr = card_ptr->get_mine_tool();
  280. mine_tool_ptr->m_attendance_start_time = tool_time::to_time_ex(start_time);
  281. mine_tool_ptr->m_is_attendance = true;
  282. log_info("[person_att] person加载考勤,考勤开始时间:%s,[%s]cur_time:%s 卡id=%s 考勤状态:%d, power_status: %d, areainfo=%s",start_time.c_str(),tool_time::to_str(mine_tool_ptr->m_attendance_start_time).c_str(),cur_time.c_str(), card_id.c_str(), (mine_tool_ptr->m_is_attendance?1:0), power_status, strAreaInfo.c_str());
  283. //分站设置
  284. auto area_tool=card_ptr->get_area_tool();
  285. auto sit_ptr=sit_list::instance()->get(site_id);
  286. if(sit_ptr)
  287. {
  288. auto area_hover_ptr = std::make_shared<area_hover>(sit_ptr->get_area(),*card_ptr);
  289. area_tool->m_hover_list.push_back(area_hover_ptr);
  290. area_tool->set_site(sit_ptr);
  291. }
  292. //解析area_info
  293. const char * split = "|";
  294. char *p = strtok(const_cast<char *>(strAreaInfo.c_str()), split);
  295. while (p != NULL)
  296. {
  297. int area_id = 0;
  298. uint64_t enter_time = 0;
  299. sscanf(p, "%d,%lu", &area_id, &enter_time);
  300. p = strtok(NULL, split);
  301. if (area_id < 0)
  302. continue;
  303. auto area = area_list::instance()->get(area_id);
  304. if(area == nullptr)
  305. {
  306. log_error("area_list 找不到区域:区域id=%d", area_id);
  307. continue;
  308. }
  309. log_info("load_person_area:%s,%d,%lu",card_id.c_str(),area_id,enter_time);
  310. //要放到最后
  311. auto area_hover_ptr = std::make_shared<area_hover>();
  312. area_hover_ptr->m_area = area;
  313. area_hover_ptr->m_enter_time = enter_time;
  314. area_tool->m_hover_list.push_back(area_hover_ptr);
  315. area->on_load_his(area_hover_ptr, card_ptr);
  316. }
  317. }
  318. }
  319. }
  320. void card_list::on_message(zloop<task*> *loop, message_locinfo&loc,bool is_history)
  321. {
  322. uint64_t cardid = tool_other::type_id_to_u64(loc.m_card_type, loc.m_card_id);
  323. const auto &c=card_list::instance()->get(cardid);
  324. if(!c)
  325. {
  326. log_warn("数据库中未定义该卡的信息,card_id=%d, card_type=%d, cardid:%lld", loc.m_card_id, loc.m_card_type, cardid);
  327. return;
  328. }
  329. double dist_tof = loc.m_tof*15.65*2.996*1e-4;
  330. logn_info(3,"card_message:site=%d,ant=%d,type=%lu,card=%d,ct=%d,tof=%lld(%.2lfm),rav=%02X,acc=%02X,rssi=%d,bat=%02X,stamp=%llu",
  331. loc.m_site_id,loc.m_ant_id,loc.m_card_type,loc.m_card_id,loc.m_card_ct,loc.m_tof,dist_tof,
  332. (loc.m_card_type==1?loc.m_callinfo:loc.m_rav),
  333. loc.m_acc,loc.m_rssi,loc.m_batty_status,loc.m_time_stamp);
  334. if(loc.m_card_type != CT_PERSON && CYaSetting::m_sys_setting.check_rav(loc.m_card_type,loc.m_card_id))
  335. loc.set_rav(0);
  336. c->on_message(loop,loc,is_history);
  337. if(tool_other::is_driving(loc.m_card_type,c->get_vehicle_type_id()))
  338. mine_business::inst()->make_reverse_condition(loc.m_card_type,loc.m_card_id,loc.m_ant_id,loc.m_card_ct,loc.m_tof,loc.m_site_id);
  339. }
  340. void card_list::on_message(zloop<task*>* loop, message_tdoa_locinfo& loc, bool is_history)
  341. {
  342. uint64_t cardid = tool_other::type_id_to_u64(loc.m_card_msg.m_type, loc.m_card_msg.m_id);
  343. const auto &c=card_list::instance()->get(cardid);
  344. if(!c)
  345. {
  346. log_warn("[tdoa] 数据库中未定义该卡的信息,card_id=%d, card_type=%d, cardid:%lld", loc.m_card_msg.m_id, loc.m_card_msg.m_type, cardid);
  347. return;
  348. }
  349. c->on_message(loop, loc, is_history);
  350. }
  351. void card_list::on_message(zloop<task*>* loop, message_pdoa_locinfo& loc, bool is_history)
  352. {
  353. uint64_t cardid = tool_other::type_id_to_u64(loc.m_card_type, loc.m_card_id);
  354. const auto &c = card_list::instance()->get(cardid);
  355. if(!c)
  356. {
  357. log_warn("[pdoa] 数据库中未定义该卡的信息,card_id=%d, card_type=%d, cardid:%lld", loc.m_card_id, loc.m_card_type, cardid);
  358. return;
  359. }
  360. // 是否使用原始定位算法,0为否,1为是
  361. loc.m_sync_ct = use_original_algo;
  362. c->on_message(loop, loc, is_history);
  363. }
  364. //获取卡数据 //标识id 人staff_id 车 vehicle_id
  365. const std::shared_ptr<card_location_base> card_list::get_card_by_cid(int cid)
  366. {
  367. auto t_map = card_list::instance()->m_map;
  368. for(const std::pair<int,std::shared_ptr<card_location_base>> &me:t_map)
  369. if ((int)me.second->m_cid == cid)
  370. return me.second;
  371. return nullptr;
  372. }
  373. bool card_list_visit::visit(std::shared_ptr<card_location_base> c)
  374. {
  375. c->get_card(_flag);
  376. return true;
  377. }