1
0

card_person.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #include <string>
  2. #include <thread>
  3. #include "card_person.h"
  4. #include "loc_point.h"
  5. #include "card_message_handle.h"
  6. #include "area.h"
  7. #include "mine.h"
  8. #include "his_location.h"
  9. #include "tool_time.h"
  10. #include "three_rates.h"
  11. #include "select_tool.h"
  12. #include "monkey_car/monkeycar_person.h"
  13. #include "websocket/ws_common.h"
  14. #include "event.h"
  15. #include "mine_business.h"
  16. #include "common_tool.h"
  17. #include "db/db_tool.h"
  18. #include "forbid_staff_down_mine.h"
  19. #include <config_file.h>
  20. #include "cardMgr.h"
  21. #include "card.h"
  22. #include "db_api/CDBSingletonDefine.h"
  23. #include "module_service/module_screen.h"
  24. extern config_file config;
  25. int person::m_limit_detained_time=-1;
  26. int person::m_auto_up_mine_time=-1;
  27. int person::m_person_cards_flag=-1;
  28. 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)
  29. :card_location_base(type, cardid, needdisplay, t, deptid, level_id, cid)
  30. ,m_workLine(wl)
  31. ,m_worktype_id(worketype_id)
  32. ,m_stafferName(sname)
  33. ,m_deptName(dname)
  34. {
  35. if(person::m_auto_up_mine_time==-1){
  36. //显示超时的界限 h
  37. person::m_limit_detained_time = config.get("service.detained_time",10);
  38. //打印输出超过该时长的数据信息
  39. person::m_auto_up_mine_time = config.get("service.auto_up_mine_time",20);
  40. person::m_person_cards_flag = config.get("service.person_cards_flag",0);
  41. }
  42. m_message_handle.reset(new card_message_handle(this));
  43. //m_his_location_card.reset(new location_staff(m_id,m_type,cid));
  44. }
  45. person::~person()
  46. {
  47. }
  48. void person::clear()
  49. {
  50. log_info("%d make_his_location clear", m_id);
  51. make_his_location(m_time,*this,true);
  52. bool is_att = m_mine_tool->m_is_attendance;
  53. std::chrono::system_clock::time_point att_start_time = m_mine_tool->m_attendance_start_time;
  54. //初始化区域和井下部分基础信息
  55. m_mine_tool->clear();
  56. m_area_tool->clear();
  57. //清除部分信息
  58. card_location_base::clear();
  59. //清除告警信息
  60. uint64_t id = tool_other::type_id_to_u64(m_type,m_id);
  61. for(std::uint8_t i=0;i<CARD_EVENT_COUNT_MAX;i++)
  62. {
  63. if(m_event[i]!=0)
  64. {
  65. log_info("clear_person_card_event:%d,%d",m_id,i);
  66. event_tool::instance()->handle_event(OT_CARD,static_cast<EVENT_TYPE>(i),id,0,0,false);
  67. m_event[i]=0;
  68. }
  69. }
  70. //清除一人多卡功能内存中的人卡相关数据,
  71. if(m_person_cards_flag){
  72. Msg m;
  73. m.cmd = CMD_CLEAR;
  74. m.cardid = m_type<<32|m_id;
  75. cardMgr::instance()->tryPut(m);
  76. }
  77. m_mine_tool->m_is_attendance = is_att;
  78. m_mine_tool->m_attendance_start_time = att_start_time;
  79. }
  80. //设置区域和地标信息
  81. void person::set_area_info(int mapid,double scale,int areaid,uint64_t t,int type)
  82. {
  83. m_area_tool->set_area_info(mapid,scale,areaid,*this,t,type);
  84. }
  85. /*
  86. * @brief 井口虹膜考勤联动以及大屏事宜处理
  87. * @parma int sid 分站号
  88. * @return 无
  89. * @note
  90. * @warning
  91. * @bug
  92. * */
  93. void person::site_hover(int sid)
  94. {
  95. // 更新新城金矿大屏所需井口人员的时间信息
  96. mine_person p(m_id, m_battery_value);
  97. module_screen::instance()->put(p);
  98. IKSDK_DB(sid);
  99. }
  100. //虹膜识别入库功能 输入到his_att_interface表.目前主要最小系统输入这个表,与井口闸机联动
  101. void person::IKSDK_DB(int sid)
  102. {
  103. time_t now = time(0);
  104. if(now - m_iris_recognition_timeval > 5)
  105. {
  106. if (forbid_staff_down_mine::instance()->IsForbid(m_cid,now))
  107. {
  108. log_info("Staff:%d forbid down mine",m_cid); //禁止指定人员下井
  109. return;
  110. }
  111. int f=0;
  112. if(event_list::instance()->get_event_card(m_id, m_type, ET_CARD_LOW_POWER_SERIOUS)) {
  113. f = 1;
  114. }
  115. std::string card_id = tool_other::type_id_to_str(m_type,m_id);
  116. std::string st = tool_time::to_str(now);
  117. char nsql[256]={0};
  118. 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)"
  119. " VALUES (%d,%s,%d,'%s',%d,'%s','%s',%d);";
  120. 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);
  121. db_tool::PushAsync(nsql);
  122. m_iris_recognition_timeval = now;
  123. }
  124. }
  125. std::shared_ptr<mine_tool> person::get_mine_tool()
  126. {
  127. return m_mine_tool;
  128. }
  129. /*
  130. * @brief 人卡业务处理
  131. * @param const std::shared_ptr<site>& site 分站信息
  132. * @param const point& pt 定位点
  133. * @param double acc 加速度状态
  134. * @return 无
  135. * @note
  136. * @bug
  137. * @warning
  138. * */
  139. void person::do_business(const std::shared_ptr<site>&site,const point &pt,double acc)
  140. {
  141. //区域相关逻辑驱动
  142. m_area_tool->on_point(shared_from_this(), pt);
  143. //处理历史轨迹
  144. log_info("[person_att] %d is attendance: %d", m_id, (m_mine_tool->m_is_attendance?1:0));
  145. make_his_location(m_time, pt);
  146. m_timeval = m_time;
  147. //三率数据处理
  148. //handle_three_rates(pt);
  149. //一人多卡数据输入
  150. if(m_person_cards_flag){
  151. Msg m;
  152. m.type = m_type;
  153. m.x=(int)x;
  154. m.y=(int)y;
  155. m.cmd = CMD_HANDLE;
  156. m.cardid=m_type<<32|m_id;
  157. cardMgr::instance()->tryPut(m);
  158. }
  159. }
  160. //设置人员上猴车
  161. void person::reset(std::shared_ptr<monkey_person> mp)
  162. {
  163. m_monkeyPerson = mp;
  164. }
  165. /*
  166. * @brief 处理三率模块
  167. * @param const point& pt
  168. * @return 无
  169. * @note
  170. * @bug
  171. * @warning
  172. * */
  173. void person::handle_three_rates(const point & pt)
  174. {
  175. card_pos cp;
  176. cp.work_line = m_workLine;
  177. cp.work_type_id = m_worktype_id;
  178. m_biz_stat = get_stat();
  179. cp.biz_stat = m_biz_stat;
  180. cp.x = pt.x;
  181. cp.y = pt.y;
  182. cp.z = pt.z;
  183. cp.work_type_id = m_worktype_id;
  184. put_three_rates(cp);
  185. }
  186. //定时推送到web函数,
  187. void person::on_timer()
  188. {
  189. if(!m_mine_tool->m_is_attendance)
  190. {
  191. if(m_upmine_flag.load())
  192. {
  193. log_warn("up_mine:att=false,upmine_flag=%d", m_upmine_flag.load());
  194. m_upmine_flag=0;
  195. }
  196. //log_info("[person_att] person %d is not attendance", m_id);
  197. return;
  198. }
  199. sys::_CARD_POS_ cp;
  200. uint64_t _time = 0;
  201. //point pt = getSmoothPoint(_time);
  202. point pt = point(x, y);
  203. cp.area_info = m_area_tool->m_area_info;
  204. cp.map_id = m_area_tool->get_mapid();
  205. cp.biz_stat = m_biz_stat;
  206. cp.down_time = m_mine_tool->get_down_time();
  207. cp.work_time = m_mine_tool->get_work_time();
  208. cp.is_on_duty= m_mine_tool->is_on_duty();
  209. cp.m_freq = m_freq;
  210. cp.speed = m_speed;
  211. if(auto site_ptr = m_area_tool->m_site){
  212. cp.area_id = site_ptr->m_area_id;
  213. }
  214. cp.dept_id = m_deptid;
  215. //cp.stat = m_battery_value;
  216. upt_card_pos(cp,pt);
  217. 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);
  218. //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);
  219. uint64_t _now = tool_time::now_to_ms();
  220. uint64_t t = _now>m_timeval?_now-m_timeval:m_timeval-_now;
  221. if(t > 10*1000)
  222. {
  223. m_area_tool->on_point(shared_from_this(),pt);
  224. m_biz_stat=get_stat();
  225. }
  226. print_card_detained();
  227. // 自动升井
  228. if(m_upmine_flag.load())
  229. {
  230. log_info("card=%d user-id=%d up_mine:upmine_flag=%d",m_id, m_cid, m_upmine_flag.load());
  231. m_area_tool->on_leave(shared_from_this());
  232. m_upmine_flag = 0;
  233. }
  234. }
  235. //***********************************************
  236. //整点打印入井超过设定时长(默认10h)
  237. //进入盲区超过2h的卡
  238. void person::print_card_detained()
  239. {
  240. const point p(4677,-193.7);
  241. const double limit_dist=45.0;
  242. int h=tool_time::get_hour();
  243. uint64_t _now=tool_time::now_to_ms();
  244. uint64_t work_time=m_mine_tool->get_work_time();
  245. uint64_t lost_time=_now>m_time?_now-m_time:m_time-_now;
  246. if(m_thour != h && (work_time > static_cast<uint64_t>(person::m_limit_detained_time*60*60*1000)) && lost_time>2*60*60*1000)
  247. {
  248. m_thour=h;
  249. auto start = m_mine_tool->m_attendance_start_time;
  250. std::string enter_time=tool_time::to_str(start);
  251. uint32_t min=work_time/1000/60;
  252. uint32_t lmin=lost_time/1000/60;
  253. double dist=p.dist(*this);
  254. std::string last_time = tool_time::to_str_ex(m_time);
  255. std::string alarm_time{"No Alarm."};
  256. auto event_power=event_list::instance()->get_event_card(m_id,m_type,ET_CARD_LOW_POWER_SERIOUS);
  257. if(event_power)
  258. alarm_time=tool_time::to_str(event_power->m_cur_time);
  259. log_warn("[card_detained]卡号:%03d%010d,分站:%d,入井时间点:%s,入井时长:%d小时%d分钟,(%.2f,%.2f)是否在处在井底范围内:%s,最后接收时间点:%s,进入盲区时长:%d小时%d分钟,是否低电量告警:%s,低电量开始时间点:%s",
  260. 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());
  261. //超过设置的时长后,开始自动升井
  262. if(work_time > static_cast<uint64_t>(person::m_auto_up_mine_time*60*60*1000))
  263. inc_upmine_flag(2);
  264. }
  265. }
  266. //获取web输入坐标点
  267. point person::getSmoothPoint(uint64_t& t)
  268. {
  269. point pt;
  270. loc_point lp = m_smo_tool->smooth_strategy();
  271. m_speed = lp.m_speed;
  272. m_stat = lp.m_stat;
  273. pt.x = lp.x;
  274. pt.y = lp.y;
  275. t=lp.m_time;
  276. if(auto p = m_monkeyPerson.lock() ){
  277. if(p->is_on_bus()){
  278. m_stat = 7;
  279. pt = p->getPoint(t,m_speed);
  280. log_info("getpoint_oncar:%d,%lld,speed:%.2f",m_id,t,m_speed);
  281. if(m_speed>1.85*3.6*2){log_error("monkey_speed_error...%d",m_id);m_speed=1.85*3.6;}
  282. }
  283. }
  284. return pt;
  285. }
  286. //人员显示输入
  287. void person::get_card(bool f)
  288. {
  289. if (!m_mine_tool->m_is_attendance)
  290. return ;
  291. mine_business::inst()->fetch_add(m_display);
  292. }
  293. //新协议处理逻辑。
  294. //0-4包为一组处理
  295. //其中0-1为充电时间 2-3 为放电时间 4为电量标识
  296. #define CHARGE_MASK 0X03
  297. #define DISCHARGE_MASK 0X0C
  298. #define POWER_MASK 0X10
  299. void person::handle_message(uint16_t ct,uint8_t &value)
  300. {
  301. //判断是新卡还是老卡
  302. if(m_card_generation==0){
  303. uint8_t remainder=ct%5;
  304. if(remainder==4){
  305. uint8_t v=value>>4;
  306. if(v>0)m_card_generation=2;
  307. else m_card_generation=1;
  308. }
  309. //没判定结果之前使用之前的状态
  310. value=m_pwr_stat;
  311. return;
  312. }
  313. //老卡
  314. if(m_card_generation==1){
  315. value = value&0x03;
  316. return;
  317. }
  318. //新卡继续往下走,获取数据
  319. uint16_t cttmp = ct/5;
  320. if(m_ctflag != cttmp){
  321. uint8_t power=m_power;
  322. if((m_index&CHARGE_MASK) == CHARGE_MASK)
  323. m_charge_value=(m_value[0]<<8)|m_value[1];
  324. if((m_index&DISCHARGE_MASK) == DISCHARGE_MASK)
  325. m_discharge_value=(m_value[2]<<8)|m_value[3];
  326. if((m_index&POWER_MASK) == POWER_MASK)
  327. power=m_value[4]>>4;
  328. //插入sql
  329. if(power !=m_power){
  330. m_power=power;
  331. char nsql[128]={0};
  332. const char*sql="insert into his_card_batlog (card_id,time,percent) values (%d,'%s',%d);";
  333. snprintf(nsql,128,sql,m_id,tool_time::now_to_str().c_str(),m_power*10);
  334. sDBConnPool.PushAsync(nsql);
  335. logn_info(2,"%s",nsql);
  336. }
  337. log_info("card_id:%d,充电时间:%u,放电时间:%u,电量%d%",m_id,m_charge_value,m_discharge_value,m_power*10);
  338. m_index=0;m_ctflag=cttmp;memset(&m_value,0,sizeof(m_value));
  339. }
  340. uint8_t remainder=ct%5;
  341. m_value[remainder]=value;
  342. m_index|=(1<<remainder);
  343. log_info("card_id:%d,ctflag:%u,index:%#X,value:%#X,bitmask:%#X",m_id,m_ctflag,remainder,value,m_index);
  344. if(remainder==4){
  345. value>>=4;
  346. if(value>=5)value=0;
  347. else if(value >2 && value<5) value=1;
  348. else value=2;
  349. }else{
  350. value=m_pwr_stat;
  351. }
  352. }