card.cpp 15 KB

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