his_location.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #include "his_location.h"
  2. #include "log.h"
  3. #include "tool_time.h"
  4. #include "card_path.h"
  5. #include "db_api/CDBSingletonDefine.h"
  6. #include <config_file.h>
  7. extern config_file config;
  8. uint32_t location_card::m_difftime=0;
  9. int location_card::m_distance=-1;
  10. location_card::location_card(uint32_t id,uint64_t type,uint32_t objid)
  11. :m_cardid(id)
  12. ,m_type(type)
  13. ,m_objid(objid)
  14. {
  15. init();
  16. if(location_card::m_distance==-1)
  17. {
  18. location_card::m_difftime=config.get("service.difftime",300);
  19. location_card::m_distance=config.get("service.distance",30);
  20. log_info("his_location_init:%u,%d",m_difftime,m_distance);
  21. }
  22. }
  23. void location_card::init()
  24. {
  25. m_areaid=-1;
  26. m_mapid=-1;
  27. m_siteid = -1;
  28. m_timestamp=0;
  29. m_p.set(0,0);
  30. std::queue<mini_data> tmp;
  31. m_d.swap(tmp);
  32. m_direct_index=0;
  33. last_timestamp=0;
  34. set_invalid();
  35. }
  36. void location_card::init_att(const point &pt,uint64_t time)
  37. {
  38. std::queue<mini_data> tmp;
  39. m_d.swap(tmp);
  40. m_d.emplace(pt,time);
  41. }
  42. void location_card::set_invalid()
  43. {
  44. m_arg=0x12345678;
  45. }
  46. bool location_card::is_valid()
  47. {
  48. return m_arg!=0x12345678;
  49. }
  50. double location_card::make_arg(const point &pt,const point &p)
  51. {
  52. log_info("his_location arg[%d],(%.2f,%.2f)--->(%.2f,%.2f)---->(%.2f,%.2f)",m_cardid,m_p.x,m_p.y,p.x,p.y,pt.x,pt.y);
  53. return std::arg(std::complex<double>(pt.x,pt.y)-std::complex<double>(p.x,p.y));
  54. }
  55. void location_card::set(const point &pt,uint64_t time)
  56. {
  57. m_timestamp=time;
  58. m_p.set(pt);
  59. init_att(pt,time);
  60. }
  61. bool location_card::line_changed(const point &pt)//,int &df)
  62. {
  63. if(!is_valid())
  64. return false;
  65. point p;
  66. if(m_d.empty())
  67. p=m_p;
  68. else
  69. p=m_d.back().p;
  70. if(point::eq(p.x,pt.x,0.2) && point::eq(p.y,pt.y,0.2)) return false;
  71. double dis1=m_p.dist(pt);
  72. if(dis1<2)return false;
  73. //double dis = p.dist(pt);
  74. double dis = m_p.dist(pt);
  75. double arg = make_arg(pt,m_p);
  76. //查看路径方向是否改变
  77. bool change_flag=(arg-m_arg > -1e-10 && arg-m_arg<1e-10);
  78. //change_flag为true,标识没有发生变化
  79. // if(change_flag)m_direct_index=0;
  80. log_info("his_location:line_changed:%d,%f,m_arg:%f,%s,%f,%f",m_cardid,arg,m_arg, change_flag?"same":"not same",m_arg-arg,dis);
  81. //if(dis<0.5 && fabs(fabs(arg)+fabs(m_arg)-3.141593)< 0.000005)
  82. // return false;
  83. //判断10次反向才做下一步动作
  84. // if(fabs(fabs(arg)+fabs(m_arg)-3.141593)< 0.000005)
  85. // {
  86. // if(++m_direct_index<=10)
  87. // {
  88. // df=1;return false;
  89. // }
  90. // }
  91. //return !point::eq(arg,m_arg,1e-10);
  92. return !change_flag;
  93. }
  94. bool location_card::is_speed_changed(const point& pt,uint64_t time)
  95. {
  96. bool flag = false;
  97. double v=0.0;
  98. point ps=m_d.back().p;
  99. if(ps.dist(pt)<0.1)return flag;
  100. if(m_d.size()>=4)
  101. {
  102. mini_data d1 = m_d.front();
  103. double dist = d1.p.dist(pt);
  104. double t = time-d1.time;
  105. v = dist/t*1000;
  106. m_d.pop();
  107. }
  108. m_d.emplace(pt,time);
  109. if(v<=0.1)
  110. return flag;
  111. double dist = m_p.dist(pt);
  112. double t = time - m_timestamp;
  113. double avge_speed= dist/t*1000;
  114. log_info("his_location cardid:%d:v:%.2f,avge_v:%.2f,(%.2f--%.2f)",m_cardid,v,avge_speed,0.7*avge_speed,1.3*avge_speed);
  115. if(v<(1-0.3)*avge_speed || v>(1+0.3)*avge_speed)
  116. flag=true;
  117. return flag;
  118. }
  119. bool location_card::time_out(const point &p,uint64_t time)
  120. {
  121. uint64_t t=last_timestamp==0?m_timestamp:last_timestamp;
  122. if(time-t>=30*1000 && m_p.dist(p)>0.1)
  123. return true;
  124. return false;
  125. }
  126. //区域是否发生变化
  127. bool location_card::is_area_changed(int new_areaid)
  128. {
  129. bool flag =false;
  130. if(m_areaid != new_areaid)
  131. {
  132. m_areaid = new_areaid;
  133. flag=true;
  134. }
  135. return flag;
  136. }
  137. //地图是否发生变化
  138. bool location_card::is_map_changed(int new_mapid)
  139. {
  140. bool flag =false;
  141. if(m_mapid != new_mapid)
  142. {
  143. m_mapid = new_mapid;
  144. flag=true;
  145. }
  146. return flag;
  147. }
  148. void location_card::push(uint64_t timestamp,const point & p,int32_t areaid,int32_t mapid,int32_t siteid,bool bclose/* = false*/)
  149. {
  150. //log_info("his_location: 1111111111111111111; timestamp=%d, p.x=%.2f, p.y=%.2f", timestamp, p.x, p.y);
  151. if (bclose) //卡移除后直接更新his_location
  152. {
  153. update(p,timestamp);
  154. init();
  155. return;
  156. }
  157. //log_info("his_location: m_p.empty()=%d, m_timestamp=%d, m_areaid=%d, m_mapid=%d, m_siteid=%d", m_p.empty(), m_timestamp, m_areaid, m_mapid, m_siteid);
  158. if(m_p.empty() || m_timestamp==0||m_areaid<0||m_mapid<0 || m_siteid < 0)
  159. {
  160. set(p,timestamp);
  161. m_areaid = areaid;
  162. m_mapid = mapid;
  163. m_siteid = siteid;
  164. m_d.emplace(p,timestamp);
  165. return ;
  166. }
  167. //log_info("his_location: 3333333333333333333333333");
  168. if(!is_valid())
  169. {
  170. //if(p==m_p)
  171. if(point::eq(p.x,m_p.x,0.2) && point::eq(p.y,m_p.y,0.2))
  172. {
  173. set(p,timestamp);
  174. log_info("his_location:%d New Point Too close.Point[%.2f,%.2f]",m_cardid,p.x,p.y);
  175. return ;
  176. }
  177. auto v=find_path(m_p,p);
  178. if(v.empty())
  179. m_arg=make_arg(p,m_p);
  180. else
  181. {
  182. log_info("his_location:more_abnormal_point....%d,(%.2f,%.2f)---(%.2f,%.2f)",m_cardid,m_p.x,m_p.y,p.x,p.y);
  183. handle_path(v,timestamp,true);
  184. set_invalid();
  185. return;
  186. }
  187. log_info("his_location:%d arg:%f",m_cardid,m_arg);
  188. insert();
  189. return;
  190. }
  191. //log_info("his_location: 44444444444444444444444444444444444");
  192. bool flag=false;
  193. int iflag=0;
  194. //判断是否路径发生了变化
  195. //这里现在又判断,如果反向了也会返回true.但是不运作,依然走下面的逻辑就会有问题
  196. //比如速度
  197. flag=handle_message(p,timestamp);
  198. if(time_out(p,timestamp))
  199. if(!flag)iflag=1;
  200. if(is_speed_changed(p,timestamp))
  201. if(!flag)iflag=2;
  202. if(is_area_changed(areaid))
  203. if(!flag)iflag=3;
  204. if(is_map_changed(mapid))
  205. if(!flag)iflag=4;
  206. log_info("his_location cardid:%d:%d",m_cardid,iflag);
  207. if(iflag)
  208. {
  209. update(p,timestamp,iflag);
  210. //set_invalid();
  211. if(iflag>1){
  212. m_siteid = siteid;
  213. set(p,timestamp);
  214. insert();
  215. }
  216. }
  217. }
  218. void location_card::insert(uint64_t timestamp, const point & p, int32_t areaid, int32_t mapid, int32_t siteid, double scale)
  219. {
  220. double speed = 0.0;
  221. double dist = 0.0;
  222. if(m_timestamp != 0){
  223. dist = m_p.dist(p)*scale;
  224. if(fabs(dist) < 1){
  225. return;
  226. }
  227. double t = (timestamp - m_timestamp)/1000;
  228. speed = dist/t;
  229. if(std::isnan(speed)|| std::isinf(speed))
  230. speed=0;
  231. }
  232. m_timestamp = timestamp;
  233. m_p = p;
  234. char sql[512] = {0};
  235. snprintf(sql, 512, "replace into his_location_simplify(obj_id, card_type_id, ident, loc_time, map_id, area_id, begin_pt, reader_id, speed) values (%d, %d, %d, '%s', %d, %d, '%.2f,%.2f', %d, %.2f);", m_objid, m_type, m_cardid, tool_time::to_str(timestamp/1000).c_str(), mapid, areaid, p.x, p.y, siteid, speed);
  236. log_info("his_location_simplify: %d-%lu: %s", m_cardid, timestamp, sql);
  237. sDBConnPool.PushAsync(sql);
  238. }
  239. void location_card::insert()
  240. {
  241. //std::string tabName=getTabName();
  242. char nsql[512]={0};
  243. const char * sql = "replace into his_location (obj_id,card_type_id,ident,begin_time,map_id,area_id,begin_pt,direction,reader_id)"
  244. "values(%d,%d,%d,'%s',%d,%d,'%.2f,%.2f',%f,%d);";
  245. snprintf(nsql,512,sql,m_objid,m_type,m_cardid,tool_time::to_str(m_timestamp/1000).c_str(),m_mapid,m_areaid,m_p.x,m_p.y,m_arg,m_siteid);
  246. log_info("his_location[%d,%lu]:%s",m_cardid,m_timestamp,nsql);
  247. sDBConnPool.PushAsync(nsql);
  248. }
  249. void location_card::update(const point &p,uint64_t timestamp,int flag/*=0*/,int dflag/*=0*/)
  250. {
  251. //std::string tabName=getTabName();
  252. char nsql[512]={0};
  253. const char * sql = "update his_location set last_time='%s',speed=%.3f,direction=%f,location_flag=%d where obj_id=%d and begin_time='%s' and last_time is null;";
  254. double dist = m_p.dist(p);
  255. double t = (timestamp - m_timestamp)/1000;
  256. double avge_speed = dist/t;
  257. if(std::isnan(avge_speed)|| std::isinf(avge_speed))avge_speed=0;
  258. log_info("his_location_time[%d]:%d[%lu,%lu,%lu]",m_cardid,flag,last_timestamp,timestamp,m_timestamp);
  259. if(last_timestamp != 0)
  260. {
  261. const char * ss = "update his_location set last_time='%s',speed=%.3f,direction=%f,location_flag=%d where obj_id=%d and begin_time='%s' and last_time = '%s';";
  262. snprintf(nsql,512,ss,tool_time::to_str(timestamp/1000).c_str(),avge_speed,m_arg,dflag,m_objid,tool_time::to_str(m_timestamp/1000).c_str(),tool_time::to_str(last_timestamp/1000).c_str());
  263. }
  264. else
  265. snprintf(nsql,512,sql,tool_time::to_str(timestamp/1000).c_str(),avge_speed,m_arg,dflag,m_objid,tool_time::to_str(m_timestamp/1000).c_str());
  266. if(flag==1) last_timestamp=timestamp;
  267. else last_timestamp=0;
  268. log_info("his_location[%d]:%s[%lu,%lu]",m_cardid,nsql,timestamp,m_timestamp);
  269. sDBConnPool.PushAsync(nsql);
  270. }
  271. std::vector<point> location_card::find_path(const point &p1,const point &p2)
  272. {
  273. std::vector<point> rc=card_path::inst().find_path(point(p1.x,-p1.y),point(p2.x,-p2.y));
  274. return std::move(rc);
  275. }
  276. bool location_card::handle_message(const point &p,uint64_t timestamp)
  277. {
  278. bool flag = false;
  279. if(line_changed(p))
  280. {
  281. flag = true;
  282. std::vector<point> rc=find_path(m_p,p);
  283. if(rc.empty())
  284. {
  285. log_info("his_location:line_changed rc.empty() %d",m_cardid);
  286. update(m_d.back().p,m_d.back().time);
  287. set(m_d.back().p,m_d.back().time);
  288. }
  289. else
  290. {
  291. if(handle_path(rc,timestamp,false))
  292. return true;
  293. }
  294. //置m_arg非法
  295. set_invalid();
  296. }else{
  297. log_info("his_location: line not change");
  298. }
  299. return flag;
  300. }
  301. bool location_card::handle_path(std::vector<point> &rc,uint64_t timestamp,bool flag)
  302. {
  303. double dis=0;
  304. point _p=m_p;
  305. std::for_each(rc.begin(),rc.end(),[&dis,&_p](point &pt){
  306. pt.y = pt.y;
  307. dis+=_p.dist(pt);
  308. _p=pt;
  309. });
  310. uint64_t t=timestamp/1000 - m_timestamp/1000;
  311. if(t==0||dis<0.1){
  312. log_info("his_location: card_id=%d, t=%d, dist=%.2f, rc.size=%d", m_cardid, t, dis, rc.size());
  313. return true;
  314. }
  315. double avge_speed= dis/t;
  316. //有拐点 盲区时间差 距离
  317. uint64_t difftime = t;
  318. int dflag = 0;
  319. if(!m_d.empty())
  320. difftime = timestamp/1000-(m_d.back().time)/1000;
  321. if(difftime >= location_card::m_difftime && dis>location_card::m_distance)
  322. {
  323. log_info("his_location[%d]:abnormal_line difftime:%lu,ltime:%u,dis:%.2f,limit_dis:%d",m_cardid,difftime,location_card::m_difftime,dis,location_card::m_distance);
  324. dflag=1;
  325. }
  326. for(const point & pp:rc)
  327. {
  328. m_arg=make_arg(pp,m_p);
  329. if(flag)insert();
  330. log_info("his_location[%d]:line_changed_x point(%.2f,%.2f)--circle point(%.2f,%.2f),speed:%.2f",m_cardid,m_p.x,m_p.y,pp.x,pp.y,avge_speed);
  331. double dist=m_p.dist(pp);uint64_t tt=dist/avge_speed*1000;
  332. uint64_t etime=m_timestamp+tt;
  333. update(pp,etime,0,dflag);
  334. set(pp,etime);
  335. flag=true;
  336. }
  337. return false;
  338. }