123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- #include <string>
- #include <thread>
- #include "card_person.h"
- #include "loc_point.h"
- #include "card_message_handle.h"
- #include "area.h"
- #include "mine.h"
- #include "his_location.h"
- #include "tool_time.h"
- #include "three_rates.h"
- #include "select_tool.h"
- #include "monkey_car/monkeycar_person.h"
- #include "websocket/ws_common.h"
- #include "event.h"
- #include "mine_business.h"
- #include "common_tool.h"
- #include "db/db_tool.h"
- #include "forbid_staff_down_mine.h"
- #include <config_file.h>
- #include "cardMgr.h"
- #include "card.h"
- #include "db_api/CDBSingletonDefine.h"
- extern config_file config;
- int person::m_limit_detained_time=-1;
- int person::m_auto_up_mine_time=-1;
- int person::m_person_cards_flag=-1;
- person::person(const std::string &type,uint32_t cardid,uint16_t needdisplay,int16_t t,int32_t deptid,int32_t level_id,uint32_t cid,int wl,const std::string &sname,const std::string &dname,int worketype_id)
- :card_location_base(type,cardid,needdisplay,t,deptid,level_id,cid)
- ,m_workLine(wl)
- ,m_worktype_id(worketype_id)
- ,m_stafferName(sname)
- ,m_deptName(dname)
- {
- if(person::m_auto_up_mine_time==-1){
- //显示超时的界限 h
- person::m_limit_detained_time=config.get("service.detained_time",10);
- //打印输出超过该时长的数据信息
- person::m_auto_up_mine_time=config.get("service.auto_up_mine_time",20);
- person::m_person_cards_flag=config.get("service.person_cards_flag",0);
- }
- m_message_handle.reset(new card_message_handle(this));
- //m_his_location_card.reset(new location_staff(m_id,m_type,cid));
- }
- person::~person()
- {
- }
- void person::clear()
- {
- log_info("make_his_location clear");
- make_his_location(m_time,*this,true);
- //初始化区域和井下部分基础信息
- m_mine_tool->clear();
- m_area_tool->clear();
- //清除部分信息
- card_location_base::clear();
- //清除告警信息
- uint64_t id = tool_other::type_id_to_u64(m_type,m_id);
- for(std::uint8_t i=0;i<CARD_EVENT_COUNT_MAX;i++)
- {
- if(m_event[i]!=0)
- {
- log_info("clear_person_card_event:%d,%d",m_id,i);
- event_tool::instance()->handle_event(OT_CARD,static_cast<EVENT_TYPE>(i),id,0,0,false);
- m_event[i]=0;
- }
- }
- //清除一人多卡功能内存中的人卡相关数据,
- if(m_person_cards_flag){
- Msg m;
- m.cmd=CMD_CLEAR;m.cardid=m_type<<32|m_id;
- cardMgr::instance()->tryPut(m);
- }
- }
- //设置区域和地标信息
- void person::set_area_info(int mapid,double scale,int areaid,uint64_t t,int type)
- {
- m_area_tool->set_area_info(mapid,scale,areaid,*this,t,type);
- }
- void person::site_hover(int sid)
- {
- IKSDK_DB(sid);
- }
- //虹膜识别入库功能 输入到his_att_interface表.目前主要最小系统输入这个表,与井口闸机联动
- void person::IKSDK_DB(int sid)
- {
- time_t now = time(0);
- if(now - m_iris_recognition_timeval > 5)
- {
- if (forbid_staff_down_mine::instance()->IsForbid(m_cid,now))
- {
- log_info("Staff:%d forbid down mine",m_cid); //禁止指定人员下井
- return;
- }
- int f=0;
- if(event_list::instance()->get_event_card(m_id, m_type, ET_CARD_LOW_POWER_SERIOUS)) {
- f = 1;
- }
- std::string card_id = tool_other::type_id_to_str(m_type,m_id);
- std::string st = tool_time::to_str(now);
- char nsql[256]={0};
- const char*sql="REPLACE INTO his_att_interface (staff_id,card_id,reader_id,staff_name,dept_id,dept_name,upt_time,low_power_warn)"
- " VALUES (%d,%s,%d,'%s',%d,'%s','%s',%d);";
- snprintf(nsql,256,sql,m_cid,card_id.c_str(),sid,m_stafferName.c_str(),m_deptid,m_deptName.c_str(),st.c_str(),f);
- db_tool::PushAsync(nsql);
- m_iris_recognition_timeval = now;
- }
- }
- std::shared_ptr<mine_tool> person::get_mine_tool()
- {
- return m_mine_tool;
- }
- void person::do_business(const std::shared_ptr<site>&site,const point &pt,double acc)
- {
- m_mine_tool->m_is_attendance = true;
- //区域相关逻辑驱动
- m_area_tool->on_point(shared_from_this(),pt);
- //处理历史轨迹
- log_info("make_his_location person");
- make_his_location(m_time,pt);
- m_timeval=m_time;
- //三率数据处理
- handle_three_rates(pt);
- //一人多卡数据输入
- if(m_person_cards_flag){
- Msg m;
- m.type=m_type;
- m.x=(int)x;
- m.y=(int)y;
- m.cmd=CMD_HANDLE;
- m.cardid=m_type<<32|m_id;
- cardMgr::instance()->tryPut(m);
- }
- }
- //设置人员上猴车
- void person::reset(std::shared_ptr<monkey_person> mp)
- {
- m_monkeyPerson = mp;
- }
- void person::handle_three_rates(const point & pt)
- {
- card_pos cp;
- cp.work_line=m_workLine;
- cp.work_type_id = m_worktype_id;
- m_biz_stat=get_stat();
- cp.biz_stat = m_biz_stat;
- cp.x=pt.x;cp.y=pt.y;cp.z=pt.z;
- cp.work_type_id=m_worktype_id;
- put_three_rates(cp);
- }
- //定时推送到web函数,
- void person::on_timer()
- {
- if(!m_mine_tool->m_is_attendance)
- {
- if(m_upmine_flag.load())
- {
- log_warn("up_mine:att=false,upmine_flag=%d",m_upmine_flag.load());
- m_upmine_flag=0;
- }
-
- //log_info("[person] person is not attendance");
- return;
- }
- YA::_CARD_POS_ cp;
- uint64_t _time=0;
- point pt = getSmoothPoint(_time);
-
- cp.area_info=m_area_tool->m_area_info;
- cp.map_id =m_area_tool->get_mapid();
- cp.biz_stat = m_biz_stat;
- cp.down_time = m_mine_tool->get_down_time();
- cp.work_time = m_mine_tool->get_work_time();
- cp.is_on_duty= m_mine_tool->is_on_duty();
- cp.m_freq = m_freq;
- cp.speed = m_speed;
- upt_card_pos(cp,pt);
- log_info("on_timer here ...%d,%lld,%.2f,%.2f,%d,%d--%d,speed=%.2f",m_id,_time,pt.x,pt.y,cp.map_id,cp.area_info.size(),person::m_limit_detained_time,m_speed);
- //int sid=0; if(auto st=m_area_tool->m_site)sid=st->m_area_id; m_his_location_card->push(_time,pt,sid,cp.map_id);
- uint64_t _now=tool_time::now_to_ms();
- uint64_t t=_now>m_timeval?_now-m_timeval:m_timeval-_now;
- if(t>10*1000)
- {
- m_area_tool->on_point(shared_from_this(),pt);
- m_biz_stat=get_stat();
- }
- print_card_detained();
- if(m_upmine_flag.load())
- {
- log_info("card=%d user-id=%d up_mine:upmine_flag=%d",m_id,m_cid, m_upmine_flag.load());
- m_area_tool->on_leave(shared_from_this());
- m_upmine_flag=0;
- }
- }
- //***********************************************
- //整点打印入井超过设定时长(默认10h)
- //进入盲区超过2h的卡
- void person::print_card_detained()
- {
- const point p(4677,-193.7);
- const double limit_dist=45.0;
- int h=tool_time::get_hour();
- uint64_t _now=tool_time::now_to_ms();
- uint64_t work_time=m_mine_tool->get_work_time();
- uint64_t lost_time=_now>m_time?_now-m_time:m_time-_now;
- if(m_thour != h && (work_time > static_cast<uint64_t>(person::m_limit_detained_time*60*60*1000)) && lost_time>2*60*60*1000)
- {
- m_thour=h;
- auto start = m_mine_tool->m_attendance_start_time;
- std::string enter_time=tool_time::to_str(start);
- uint32_t min=work_time/1000/60;
- uint32_t lmin=lost_time/1000/60;
- double dist=p.dist(*this);
- std::string last_time=tool_time::to_str_ex(m_time);
- std::string alarm_time{"No Alarm."};
- auto event_power=event_list::instance()->get_event_card(m_id,m_type,ET_CARD_LOW_POWER_SERIOUS);
- if(event_power)
- alarm_time=tool_time::to_str(event_power->m_cur_time);
- log_warn("[card_detained]卡号:%03d%010d,分站:%d,入井时间点:%s,入井时长:%d小时%d分钟,(%.2f,%.2f)是否在处在井底范围内:%s,最后接收时间点:%s,进入盲区时长:%d小时%d分钟,是否低电量告警:%s,低电量开始时间点:%s",
- m_type,m_id,m_area_tool->get_site_id(),enter_time.c_str(),min/60,min%60,x,y,dist<limit_dist?"True":"False",last_time.c_str(),lmin/60,lmin%60,event_power?"Ture":"False",alarm_time.c_str());
- //超过设置的时长后,开始自动升井
- if(work_time > static_cast<uint64_t>(person::m_auto_up_mine_time*60*60*1000))
- inc_upmine_flag(2);
- }
- }
- //获取web输入坐标点
- point person::getSmoothPoint(uint64_t& t)
- {
- point pt;
- loc_point lp = m_smo_tool->smooth_strategy();
- m_speed = lp.m_speed;
- m_stat = lp.m_stat;
- pt.x = lp.x;
- pt.y = -lp.y;
- t=lp.m_time;
- if(auto p = m_monkeyPerson.lock() ){
- if(p->is_on_bus()){
- m_stat = 7;
- pt = p->getPoint(t,m_speed);
- log_info("getpoint_oncar:%d,%lld,speed:%.2f",m_id,t,m_speed);
- if(m_speed>1.85*3.6*2){log_error("monkey_speed_error...%d",m_id);m_speed=1.85*3.6;}
- }
- }
- return pt;
- }
- //人员显示输入
- void person::get_card(bool f)
- {
- if (!m_mine_tool->m_is_attendance)
- return ;
- mine_business::inst()->fetch_add(m_display);
- }
- //新协议处理逻辑。
- //0-4包为一组处理
- //其中0-1为充电时间 2-3 为放电时间 4为电量标识
- #define CHARGE_MASK 0X03
- #define DISCHARGE_MASK 0X0C
- #define POWER_MASK 0X10
- void person::handle_message(uint16_t ct,uint8_t &value)
- {
- //判断是新卡还是老卡
- if(m_card_generation==0){
- uint8_t remainder=ct%5;
- if(remainder==4){
- uint8_t v=value>>4;
- if(v>0)m_card_generation=2;
- else m_card_generation=1;
- }
- //没判定结果之前使用之前的状态
- value=m_pwr_stat;
- return;
- }
- //老卡
- if(m_card_generation==1){
- value=value&0x03;
- return;
- }
- //新卡继续往下走,获取数据
- uint16_t cttmp = ct/5;
- if(m_ctflag != cttmp){
- uint8_t power=m_power;
- if((m_index&CHARGE_MASK) == CHARGE_MASK)
- m_charge_value=(m_value[0]<<8)|m_value[1];
- if((m_index&DISCHARGE_MASK) == DISCHARGE_MASK)
- m_discharge_value=(m_value[2]<<8)|m_value[3];
- if((m_index&POWER_MASK) == POWER_MASK)
- power=m_value[4]>>4;
- //插入sql
- if(power !=m_power){
- m_power=power;
- char nsql[128]={0};
- const char*sql="insert into his_card_batlog (card_id,time,percent) values (%d,'%s',%d);";
- snprintf(nsql,128,sql,m_id,tool_time::now_to_str().c_str(),m_power*10);
- sDBConnPool.PushAsync(nsql);
- logn_info(2,"%s",nsql);
- }
- log_info("card_id:%d,充电时间:%u,放电时间:%u,电量%d%",m_id,m_charge_value,m_discharge_value,m_power*10);
- m_index=0;m_ctflag=cttmp;memset(&m_value,0,sizeof(m_value));
- }
- uint8_t remainder=ct%5;
- m_value[remainder]=value;
- m_index|=(1<<remainder);
- log_info("card_id:%d,ctflag:%u,index:%#X,value:%#X,bitmask:%#X",m_id,m_ctflag,remainder,value,m_index);
- if(remainder==4){
- value>>=4;
- if(value>=5)value=0;
- else if(value >2 && value<5) value=1;
- else value=2;
- }else{
- value=m_pwr_stat;
- }
- }
|