123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- #include <memory>
- #include "common_tool.h"
- #include "message.h"
- #include "loc_message.h"
- #include "config_file.h"
- #include "card_person.h"
- #include "card_car.h"
- #include "db/db_card.h"
- #include "db/db_api/CDBSingletonDefine.h"
- #include "db/db_api/CDBResultSet.h"
- #include "tool_time.h"
- #include "mine.h"
- #include "log.h"
- #include "area.h"
- #include "card.h"
- #include "mine_business.h"
- #include "worker.h"
- #include "sys_setting.h"
- extern config_file config;
- int three_rates_flag = 0;
- int traffic_light_flag = 0;
- int anti_coll_flag = 0;
- int use_original_algo = 0;
- static void request(uint64_t id,uint32_t hash_id,uint16_t dis)
- {
- task * t = task::alloc<message_change_card_display>();
- auto&mcb=t->body<message_change_card_display>();
- t->m_hash_id=hash_id;
- t->m_cmd_code=0x10002;
- mcb.m_card_id=id;
- mcb.m_display=dis;
- worker::instance()->request(t);
- }
- void card_list::init_staffer(const std::string & lszId64)
- {
- std::string strategy = config.get("person.strategy","PS_1");
- std::unordered_map<uint64_t,std::shared_ptr<card_location_base>>
- map=std::move(db_card::load_person(lszId64,strategy));
-
- if(map.empty())
- {
- log_error("增加或修改失败,数据库中找不到: card_id=%s", lszId64.c_str());
- return ;
- }
- if("" == lszId64)
- {
- log_info( "init_staffer. The record count=%d\n", map.size() );
- card_list::instance()->add(map);
- }
- else
- {
- auto db_person=map.begin()->second;
- uint64_t id64 = map.begin()->first;
- auto card_ptr = card_list::instance()->get(id64);
- if(card_ptr)
- {
- person* tmp_ptr= static_cast<person*>(card_ptr.get());
- person* db_person_ptr= static_cast<person*>(db_person.get());
- 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);
- //group_id
- //occ_id
- tmp_ptr->m_workLine = db_person_ptr->m_workLine;
- tmp_ptr->m_stafferName = db_person_ptr->m_stafferName;
- tmp_ptr->m_deptName = db_person_ptr->m_deptName;
- tmp_ptr->m_worktype_id = db_person_ptr->m_worktype_id;
- if(db_person_ptr->m_display!=tmp_ptr->m_display){
- request(id64, tmp_ptr->m_id, db_person_ptr->m_display);
- }
- }
- else
- {
- card_list::instance()->add(id64, db_person);
- }
- log_info("基础数据 增加或修改人卡成功:卡id:%ld, dept_id:%d,need_display:%d,occupation_level_id:%d",
- id64,db_person->m_deptid,db_person->m_display,db_person->m_level_id);
- }
- }
- void card_list::init_vehicle(const std::string & lszId64)
- {
- std::unordered_map<uint64_t,std::shared_ptr<card_location_base>>
- map=std::move(db_card::load_car(lszId64));
- if(map.empty())
- {
- log_error("增加或修改失败,数据库中找不到: card_id=%s", lszId64.c_str());
- return ;
- }
- if("" == lszId64)
- {
- log_info( "init_vehicle. The record count=%d\n", map.size() );
- card_list::instance()->add(map);
- }
- else
- {
- uint64_t id64 = map.begin()->first;
- auto card_ptr = card_list::instance()->get(id64);
- car* db_car= static_cast<car*>(map.begin()->second.get());
- if(card_ptr)
- {
- car* tmp_ptr= static_cast<car*>(card_ptr.get());
- card_ptr->set_base_data(db_car->m_cid,db_car->m_type,db_car->m_deptid,db_car->m_level_id);
- tmp_ptr->m_vehicle_category_id = db_car->m_vehicle_category_id;
- tmp_ptr->m_vehicle_type_id = db_car->m_vehicle_type_id;
- if(db_car->m_display!=tmp_ptr->m_display){
- request(id64,tmp_ptr->m_id,db_car->m_display);
- }
- }
- else
- {
- card_list::instance()->add(id64, map.begin()->second);
- }
- log_info("基础数据 增加或修改车卡成功:卡id:%ld, dept_id:%d,need_display:%d,categoryid:%d",
- id64,db_car->m_deptid,db_car->m_display,db_car->m_vehicle_category_id);
- }
- }
- void card_list::init_card_from_db()
- {
- init_staffer("");
- init_vehicle("");
- }
- void card_list::load_his_card_postion_from_db()
- {
- load_his_card_postion_vehicle();
- load_his_card_postion_staff();
- }
- void card_list::load_his_card_postion_vehicle()
- {
- const char *sql = "select l.card_id,l.site_id, l.cur_time, l.x, l.y, l.z, l.area_info, l.state, \
- att.start_time, att.end_time \
- from rt_location l, rt_att_vehicle att, dat_card c, dat_vehicle_extend v \
- where l.card_id = c.card_id and l.card_id = att.card_id and l.card_id=v.card_id \
- and c.state_id=0;";
- std::string Error;
- YADB::CDBResultSet DBRes;
- sDBConnPool.Query(sql,DBRes,Error);
- if(!Error.empty())
- log_error("load_his_card_postion_vehicle列表 Error,%s",Error.c_str());
- uint64_t nCount = DBRes.GetRecordCount( Error );
- if (nCount > 0)
- {
- log_info( "load_his_card_postion_vehicle. The record count=%ld\n", nCount );
- while ( DBRes.GetNextRecod(Error) )
- {
- std::string card_id = "";
- DBRes.GetField( "card_id",card_id, Error );
- uint64_t c_id = tool_other::card_id_to_u64(card_id);
- auto card_ptr = card_list::instance()->get(c_id);
- if(!card_ptr)
- {
- log_error("全局卡列表中找不到卡,%s",card_id.c_str());
- continue;
- }
- int site_id = -1;
- DBRes.GetField( "site_id",site_id, Error );
- double x = 0;
- DBRes.GetField( "x",x, Error );
- double y = 0;
- DBRes.GetField( "y",y, Error );
- double z = 0;
- DBRes.GetField( "z",z, Error );
- std::string strAreaInfo = "";
- DBRes.GetField( "area_info",strAreaInfo, Error );
- int state = 0;
- DBRes.GetField( "state",state, Error );
- double speed = 0;
- DBRes.GetField( "speed",speed, Error );
- std::string start_time = "";
- DBRes.GetField( "start_time",start_time, Error );
- std::string end_time = "";
- DBRes.GetField( "end_time",end_time, Error );
- std::string cur_time = "";
- DBRes.GetField( "cur_time",cur_time, Error );
-
- card_ptr->m_time = tool_time::to_time(cur_time)*1000;
- card_ptr->set(x,y,z);
- //考勤结束也要初始化该变量,用于统计今天出勤过得车卡数量
- auto mine_tool_ptr = card_ptr->get_mine_tool();
- mine_tool_ptr->m_attendance_start_time = tool_time::to_time_ex(start_time);
- if(end_time.empty())
- {
- mine_tool_ptr->m_is_attendance = true;
- log_info("vehicle加载考勤,考勤开始时间: %s, 卡id=%s", start_time.c_str(), card_id.c_str());
- }
- auto area_tool=card_ptr->get_area_tool();
- auto sit_ptr=sit_list::instance()->get(site_id);
- if(sit_ptr)
- {
- auto area_hover_ptr = std::make_shared<area_hover>(sit_ptr->get_area(),*card_ptr);
- area_tool->m_hover_list.push_back(area_hover_ptr);
- area_tool->set_site(sit_ptr);
- }
- //解析area_info
- const char * split = "|";
- char *p = strtok(const_cast<char *>(strAreaInfo.c_str()), split);
- while (p != NULL)
- {
- int area_id = 0;
- uint64_t enter_time = 0;
- sscanf(p, "%d,%lu", &area_id, &enter_time);
- p = strtok(NULL, split);
- if (area_id < 0)
- continue;
- auto area = area_list::instance()->get(area_id);
- if(area == nullptr)
- {
- log_error("load_vehicle...area_list 找不到区域:区域id=%d", area_id);
- continue;
- }
- log_info("load_vehicle_area:%s,%d,%lu",card_id.c_str(),area_id,enter_time);
- //要放到最后
- auto area_hover_ptr = std::make_shared<area_hover>();
- area_hover_ptr->m_area = area;
- area_hover_ptr->m_enter_time = enter_time;
- area_tool->m_hover_list.push_back(area_hover_ptr);
- area->on_load_his(area_hover_ptr, card_ptr);
- }
- }
- }
- }
- void card_list::load_his_card_postion_staff()
- {
- std::string init_limit_hour = config.get("service.init_limit_hour","48");
- /*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,\
- att.start_time, att.end_time \
- from rt_location l, rt_att_staff att, dat_card c, dat_staff_extend s \
- where l.card_id = c.card_id and l.card_id = att.card_id and l.card_id=s.card_id\
- and c.state_id=0 and end_time is null ";*/
- 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 \
- LEFT JOIN rt_location AS l ON l.card_id=att.card_id \
- LEFT JOIN dat_card AS c ON c.card_id=l.card_id \
- LEFT JOIN dat_staff_extend AS s ON s.card_id=l.card_id \
- WHERE c.state_id=0 and att.end_time is null ";
- sql += " and timestampdiff(hour ,att.start_time,now()) <= " + init_limit_hour + ";";
- log_info("load_his_card_postion_staff:%s ",sql.c_str());
- std::string Error;
- YADB::CDBResultSet DBRes;
- sDBConnPool.Query(sql.c_str(),DBRes,Error);
- if(!Error.empty())
- log_error("load_his_card_postion_staff列表 Error,%s",Error.c_str());
- uint64_t nCount = DBRes.GetRecordCount( Error );
- if (nCount > 0)
- {
- log_info( "load_his_card_postion_staff. The record count=%ld\n", nCount );
- while ( DBRes.GetNextRecod(Error) )
- {
- std::string end_time = "";
- DBRes.GetField( "end_time",end_time, Error );
- if(!end_time.empty()) // 已升井,不再读入
- continue;
- std::string card_id = "";
- DBRes.GetField( "card_id",card_id, Error );
- uint64_t c_id = tool_other::card_id_to_u64(card_id);
- auto card_ptr = card_list::instance()->get(c_id);
- if(!card_ptr)
- {
- log_error("全局卡列表中找不到卡,%s", card_id.c_str());
- continue;
- }
- int site_id = -1;
- DBRes.GetField( "site_id",site_id, Error );
- double x = 0;
- DBRes.GetField( "x",x, Error );
- double y = 0;
- DBRes.GetField( "y",y, Error );
- double z = 0;
- DBRes.GetField( "z",z, Error );
- std::string strAreaInfo = "";
- DBRes.GetField( "area_info",strAreaInfo, Error );
- int state = 0;
- DBRes.GetField( "state",state, Error );
- int power_status = 0;
- DBRes.GetField("power_status", power_status, Error);
- double speed = 0;
- DBRes.GetField( "speed",speed, Error );
- std::string start_time = "";
- DBRes.GetField( "start_time",start_time, Error );
- std::string cur_time = "";
- DBRes.GetField( "cur_time",cur_time, Error );
-
- card_ptr->set(x,y,z);
- card_ptr->m_time = tool_time::to_time(cur_time)*1000;
- card_ptr->m_battery_value = power_status;
- auto mine_tool_ptr = card_ptr->get_mine_tool();
- mine_tool_ptr->m_attendance_start_time = tool_time::to_time_ex(start_time);
- mine_tool_ptr->m_is_attendance = true;
- 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());
- //分站设置
- auto area_tool=card_ptr->get_area_tool();
- auto sit_ptr=sit_list::instance()->get(site_id);
- if(sit_ptr)
- {
- auto area_hover_ptr = std::make_shared<area_hover>(sit_ptr->get_area(),*card_ptr);
- area_tool->m_hover_list.push_back(area_hover_ptr);
- area_tool->set_site(sit_ptr);
- }
- //解析area_info
- const char * split = "|";
- char *p = strtok(const_cast<char *>(strAreaInfo.c_str()), split);
- while (p != NULL)
- {
- int area_id = 0;
- uint64_t enter_time = 0;
- sscanf(p, "%d,%lu", &area_id, &enter_time);
- p = strtok(NULL, split);
- if (area_id < 0)
- continue;
- auto area = area_list::instance()->get(area_id);
- if(area == nullptr)
- {
- log_error("area_list 找不到区域:区域id=%d", area_id);
- continue;
- }
- log_info("load_person_area:%s,%d,%lu",card_id.c_str(),area_id,enter_time);
- //要放到最后
- auto area_hover_ptr = std::make_shared<area_hover>();
- area_hover_ptr->m_area = area;
- area_hover_ptr->m_enter_time = enter_time;
- area_tool->m_hover_list.push_back(area_hover_ptr);
- area->on_load_his(area_hover_ptr, card_ptr);
- }
- }
- }
- }
- void card_list::on_message(zloop<task*> *loop, message_locinfo&loc,bool is_history)
- {
- uint64_t cardid = tool_other::type_id_to_u64(loc.m_card_type, loc.m_card_id);
- const auto &c=card_list::instance()->get(cardid);
- if(!c)
- {
- log_warn("数据库中未定义该卡的信息,card_id=%d, card_type=%d, cardid:%lld", loc.m_card_id, loc.m_card_type, cardid);
- return;
- }
- double dist_tof = loc.m_tof*15.65*2.996*1e-4;
- logn_info(3,"[card_message] site=%d, ant=%d, type=%d, card=%d, ct=%d, tof=%lld(%.2lfm), rav=%02X, acc=%02X, rssi=%d, bat=%02X, time=%llu",
- 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,
- (loc.m_card_type==1?loc.m_callinfo:loc.m_rav),
- loc.m_acc,loc.m_rssi,loc.m_batty_status,loc.m_time_stamp);
- if(loc.m_card_type != CT_PERSON && CYaSetting::m_sys_setting.check_rav(loc.m_card_type,loc.m_card_id))
- loc.set_rav(0);
-
- c->on_message(loop, loc, is_history);
- if(tool_other::is_driving(loc.m_card_type,c->get_vehicle_type_id()))
- 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);
- }
- void card_list::on_message(zloop<task*>* loop, message_tdoa_locinfo& loc, bool is_history)
- {
- uint64_t cardid = tool_other::type_id_to_u64(loc.m_card_msg.m_type, loc.m_card_msg.m_id);
- const auto &c=card_list::instance()->get(cardid);
- if(!c)
- {
- log_warn("[tdoa] 数据库中未定义该卡的信息,card_id=%d, card_type=%d, cardid:%lld", loc.m_card_msg.m_id, loc.m_card_msg.m_type, cardid);
- return;
- }
- c->on_message(loop, loc, is_history);
- }
- void card_list::on_message(zloop<task*>* loop, message_pdoa_locinfo& loc, bool is_history)
- {
- uint64_t cardid = tool_other::type_id_to_u64(loc.m_card_type, loc.m_card_id);
- const auto &c = card_list::instance()->get(cardid);
- if(!c)
- {
- log_warn("[pdoa] 数据库中未定义该卡的信息,card_id=%d, card_type=%d, cardid:%lld", loc.m_card_id, loc.m_card_type, cardid);
- return;
- }
- // 是否使用原始定位算法,0为否,1为是
- loc.m_sync_ct = use_original_algo;
- c->on_message(loop, loc, is_history);
- }
- // 实心球项目数据处理
- void card_list::on_message(zloop<task*>* loop, message_ins_locinfo& loc, bool is_history)
- {
- uint64_t cardid = tool_other::type_id_to_u64(loc.m_card_type, loc.m_card_id);
- const auto &c = card_list::instance()->get(cardid);
- if(!c)
- {
- log_warn("[pdoa] 数据库中未定义该卡的信息,card_id=%d, card_type=%d, cardid:%lld", loc.m_card_id, loc.m_card_type, cardid);
- return;
- }
- // 是否使用原始定位算法,0为否,1为是
- //loc.m_sync_ct = use_original_algo;
- c->on_message(loop, loc, is_history);
- }
- //获取卡数据 //标识id 人staff_id 车 vehicle_id
- const std::shared_ptr<card_location_base> card_list::get_card_by_cid(int cid)
- {
- auto t_map = card_list::instance()->m_map;
- for(const std::pair<int,std::shared_ptr<card_location_base>> &me:t_map)
- if ((int)me.second->m_cid == cid)
- return me.second;
- return nullptr;
- }
- bool card_list_visit::visit(std::shared_ptr<card_location_base> c)
- {
- c->get_card(_flag);
- return true;
- }
|