ant.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include "ant.h"
  5. #include "db_api/CDBSingletonDefine.h"
  6. #include "event.h"
  7. #include "tool_time.h"
  8. #include "area.h"
  9. #include "card_path.h"
  10. //template<> std::shared_ptr<sit_list> single_base<sit_list, int, std::shared_ptr<site>>::m_instance=std::make_shared<sit_list>();
  11. int site::index()const
  12. {
  13. return m_algo+(m_num_dims<<1);
  14. }
  15. site::site(int id)
  16. :m_algo(0)
  17. ,m_num_dims(0)
  18. ,m_id(id)
  19. ,m_path_empty(true)
  20. {
  21. m_time=time(0);
  22. for(uint32_t i=0;i<m_timeoff_count.size();i++)
  23. m_timeoff_count[i]=0;
  24. }
  25. const algo_config&site::config()const
  26. {
  27. return g_config[index()];
  28. }
  29. #if 0
  30. bool site::check_timestamp(const char*tm_buf)
  31. {
  32. //秒、分、时、天、周、月、年
  33. struct tm t={0};
  34. char buf[6];
  35. buf[0]=tm_buf[6];
  36. buf[1]=tm_buf[5];
  37. buf[2]=tm_buf[3];
  38. buf[3]=tm_buf[2];
  39. buf[4]=tm_buf[1];
  40. buf[5]=tm_buf[0];
  41. t.tm_year=buf[0]+100;
  42. t.tm_mon=buf[1]-1;
  43. t.tm_mday=buf[2];
  44. t.tm_hour=buf[3];
  45. t.tm_min=buf[4];
  46. t.tm_sec=buf[5];
  47. time_t now=time(0);
  48. time_t site_tm=mktime(&t);
  49. int off=abs(now-site_tm);
  50. if(off>0)
  51. {
  52. off=log(off)/log(2);
  53. }
  54. m_timeoff_count[off]++;
  55. if(m_package_count++ % 300 ==0 )
  56. {
  57. std::ostringstream stm;
  58. for(uint32_t i=0;i<m_timeoff_count.size();i++)
  59. {
  60. if(m_timeoff_count[i]==0)
  61. continue;
  62. stm<<"("<<i<<":"<<m_timeoff_count[i]<<"),";
  63. }
  64. log_info("site-time-stat:site_id=%d,%s",m_id,stm.str().c_str());
  65. }
  66. if(memcmp(m_timestamp,buf,6)<=0)
  67. {
  68. memcpy(m_timestamp,buf,6);
  69. return true;
  70. }
  71. return false;
  72. }
  73. #endif
  74. void ant::set_path(const std::vector<line_v>&v_line,std::vector<line_v>::const_iterator itm)
  75. {
  76. auto it=itm;
  77. for(int i=0;i<2 && it!=v_line.end();++it,++i)
  78. m_path[0][i]=*it;
  79. it=itm-1;
  80. for(int i=0;i<2 && it>=v_line.begin();--it,++i)
  81. {
  82. m_path[1][i]=*it;
  83. m_path[1][i].swap_point();
  84. }
  85. for(int i=0;i<2;i++)
  86. {
  87. point _p=m_path[i][0].line::projection(*this);
  88. m_path[i][0].set_point(0,_p);
  89. }
  90. for(const auto&p:m_path)
  91. {
  92. log_info("site-path: %s",p.to_str().c_str());
  93. }
  94. }
  95. void ant::set_path(const std::vector<line_v>&v_line_)
  96. {
  97. std::vector<line_v> vl(v_line_);
  98. vl.reserve(vl.size()+2);
  99. //找到距离天线最近的端点
  100. auto min_it=vl.begin();
  101. double dist=10;
  102. for(auto it=vl.begin();it!=vl.end();it++)
  103. {
  104. double d=it->as_line().dist(*this);
  105. if(d<dist)
  106. {
  107. dist=d;
  108. min_it=it;
  109. }
  110. }
  111. if(min_it==vl.end())
  112. {
  113. log_error("分站路径距离分站太远site_id=%d",m_id);
  114. return;
  115. }
  116. if(abs(min_it->v[0].dist(*this)-dist)<1)
  117. {
  118. set_path(vl,min_it);
  119. }
  120. else if(abs(min_it->v[1].dist(*this)-dist)<1)
  121. {
  122. set_path(vl,min_it+1);
  123. }
  124. else
  125. {
  126. point proj=min_it->projection(*this);
  127. vl.insert(min_it+1,line_v(proj,min_it->v[1]));
  128. min_it->set_point(1,proj);
  129. if(min_it->v[0].z)//slope ..555
  130. {
  131. double slope=min_it->v[0].z;
  132. double len_a=min_it->v[0].dist((min_it+1)->v[1]);
  133. double len_0=slope*min_it->length()/len_a;
  134. double len_1=slope-len_0;
  135. min_it->v[0].z=min_it->v[1].z=len_0;
  136. (min_it+1)->v[0].z=(min_it+1)->v[1].z=len_1;
  137. }
  138. set_path(vl,min_it+1);
  139. }
  140. }
  141. void site::set_path(const std::vector<line_v>&v_line)
  142. {
  143. if(v_line.empty())
  144. return;
  145. const auto&find_line=[](const point&pt,int first,std::vector<line_v>&vl){
  146. for(auto it=vl.begin();it!=vl.end();it++)
  147. {
  148. if(it->v[first].dist(pt)<1)
  149. return it;
  150. if(it->v[first?0:1].dist(pt)<1)
  151. {
  152. point p=it->v[0];
  153. it->v[0]=it->v[1];
  154. it->v[1]=p;
  155. return it;
  156. }
  157. }
  158. return vl.end();
  159. };
  160. //构造一个首尾相连的结构
  161. std::vector<line_v> vl(v_line.begin()+1,v_line.end());
  162. std::vector<line_v> target(v_line.begin(),v_line.begin()+1);
  163. #if 0
  164. target[0][0].z=target[0][1].z=slope[0];
  165. for(int i=0,c=vl.size();i<c;i++)
  166. vl[i][0].z=vl[i][1].z=slope[i+1];
  167. #endif
  168. for(;;)
  169. {
  170. auto it=find_line(target.back().v[1],0,vl);
  171. if(it==vl.end())
  172. break;
  173. target.insert(target.end(),it,it+1);
  174. vl.erase(it);
  175. }
  176. for(;;)
  177. {
  178. auto it=find_line(target.front().v[0],1,vl);
  179. if(it==vl.end())
  180. break;
  181. target.insert(target.begin(),it,it+1);
  182. vl.erase(it);
  183. }
  184. log_info("初始化分站路径:site_id=%d",m_id);
  185. for(int i=0,c=target.size();i<c;i++)
  186. {
  187. log_info("path-id=%d,line=%s",i,target[i].to_string().c_str());
  188. }
  189. for(int i=0,c=m_ant.size();i<c;i++)
  190. {
  191. if(m_ant[i].empty())
  192. {
  193. log_error("分站未设置天线坐标 site_id=%d,ant_index=%d",m_id,i);
  194. continue;
  195. }
  196. m_ant[i].set_path(target);
  197. }
  198. m_path_empty= !m_ant[0][0].valid() && !m_ant[0][1].valid() ;
  199. }
  200. std::vector<point> ant::getsol(const double &dist) const
  201. {
  202. std::vector<point> v;
  203. for(const auto & p : m_path)
  204. {
  205. if(!p.valid())
  206. continue;
  207. point pt;
  208. double d = dist;
  209. if(dist <= p.m_line[0].length())
  210. {
  211. d += d*p.m_line[0][0].z;
  212. pt = point(p.m_line[0][0].x + d*p.m_line[0].cos() , p.m_line[0][0].y + d*p.m_line[0].sin());
  213. }
  214. else if(p.m_line[1].length()>0)
  215. {
  216. d -= p.m_line[0].length()*(1-d*p.m_line[0][1].z);
  217. d += d*p.m_line[1][0].z;
  218. pt = point(p.m_line[1][0].x+d*p.m_line[1].cos(),p.m_line[1][0].y+d*p.m_line[1].sin());
  219. }
  220. else
  221. {
  222. continue;
  223. }
  224. v.push_back(pt);
  225. }
  226. return std::move(v);
  227. }
  228. bool visit_site_status::visit(std::shared_ptr<site> s)
  229. {
  230. time_t now=time(0);
  231. int diff = now-s->m_time;
  232. event_tool::instance()->handle_event(OT_DEVICE_READER,ET_READER_ERROR,s->m_id,READER_TIMEOUT,diff,diff>READER_TIMEOUT);
  233. return true;
  234. }
  235. static void test_find_path(card_path&cp,const point&p1,const point&p2)
  236. {
  237. printf("\nfind-path: from=(%.3lf,%.3lf),to=(%.3lf,%.3lf)\n",p1.x,p1.y,p2.x,p2.y);
  238. std::vector<point> rc=cp.find_path(p1,p2);
  239. for(uint32_t i=0;i<rc.size();i++)
  240. printf("x=%.3lf,y=%.3lf\n",rc[i].x,rc[i].y);
  241. }
  242. //static void test_find_poss_path(const card_path&cp ,const point&from)
  243. //{
  244. // log_info("\nfind-poss_path: from=(%.3lf,%.3lf)\n",from.x,from.y);
  245. // std::vector<line_v> rc=cp.find_possible_path(from,3);
  246. // for(uint32_t i=0;i<rc.size();i++)
  247. // {
  248. // log_info("poss path from=(%.3lf,%.3lf) to=(%.3lf,%.3lf)\n", rc[i][0].x,rc[i][0].y, rc[i][1].x,rc[i][1].y);
  249. // }
  250. //}
  251. void sit_list::read_ant_path(int id)
  252. {
  253. std::string sql = "SELECT reader_id,tof_flag,b_x,b_y,b_z,e_x,e_y,e_z,spacing_ratio FROM dat_reader_path_tof_n";
  254. std::map<int,std::vector<line_v>> map_path;
  255. if(-1 == id)
  256. {
  257. sql.append(";");
  258. }
  259. else
  260. {
  261. sql.append(" where reader_id=");
  262. sql.append(std::to_string(id));
  263. sql.append(";");
  264. std_debug("修改path sql=%s", sql.c_str());
  265. log_info("修改path sql=%s", sql.c_str());
  266. }
  267. std::string Error;
  268. YADB::CDBResultSet DBRes;
  269. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  270. int nCount = DBRes.GetRecordCount( Error );
  271. if (nCount < 1)
  272. {
  273. log_error("修改path失败,数据库中找不到(dat_reader_path_tof_n): 分站id=%d", id);
  274. return ;
  275. }
  276. log_info( "read_ant_path. The record count=%ld\n", nCount );
  277. while ( DBRes.GetNextRecod(Error) )
  278. {
  279. int reader_id = 0;
  280. DBRes.GetField( "reader_id",reader_id, Error );
  281. auto site_ptr=sit_list::instance()->get(reader_id);
  282. if(nullptr==site_ptr)
  283. {
  284. log_error("定义了分站路径,但是没有定义分站:%d",reader_id);
  285. continue;
  286. }
  287. int pid=0;
  288. DBRes.GetField( "tof_flag",pid, Error );
  289. double b_x= 0;
  290. DBRes.GetField( "b_x",b_x, Error );
  291. double b_y= 0;
  292. DBRes.GetField( "b_y",b_y, Error );
  293. double b_z= 0;
  294. DBRes.GetField( "b_z",b_z, Error );
  295. double e_x= 0;
  296. DBRes.GetField( "e_x",e_x, Error );
  297. double e_y= 0;
  298. DBRes.GetField( "e_y",e_y, Error );
  299. double e_z= 0;
  300. DBRes.GetField( "e_z",e_z, Error );
  301. double spacing_ratio = 0;
  302. DBRes.GetField( "spacing_ratio",spacing_ratio, Error );
  303. log_info("src-path:site=%d,x0=%.2f,y0=%.2f,x1=%.2f,y1=%.2f",reader_id,b_x,b_y,e_x,e_y);
  304. point p1(b_x,-b_y, spacing_ratio);
  305. point p2(e_x,-e_y, spacing_ratio);
  306. map_path.insert(std::make_pair(reader_id,std::vector<line_v>()));
  307. map_path.find(reader_id)->second.push_back(line_v(p1,p2));
  308. #if 0
  309. auto &sit_ = *site_ptr;
  310. if(-1!=id)//清空path
  311. {
  312. sit_.clear_path();
  313. log_info( "修改path 清空path,分站id=%d\n", id );
  314. }
  315. if(pid == 0)
  316. {
  317. line_v l(p1,p2);
  318. {
  319. point px = l.line::projection(sit_);
  320. sit_.set(px);
  321. for(int i=0;i<2;i++)
  322. {
  323. path p;
  324. p.m_slope[0] = spacing_ratio;
  325. p.m_line[0] = line_v(px,l[i]);
  326. sit_.m_ant[i].m_path.push_back(p);
  327. }
  328. }
  329. }
  330. else
  331. {
  332. ant &a = pid<0?sit_.m_ant[0]:sit_.m_ant[1];
  333. if(a.m_path.size()!=0)
  334. {
  335. path &p = a.m_path[0];
  336. p.m_line[abs(pid)-1] = line_v(p1,p2);
  337. p.m_slope[abs(pid)-1] = spacing_ratio;
  338. }
  339. else
  340. {
  341. path p;
  342. p.m_line[abs(pid)-1] = line_v(p1,p2);
  343. p.m_slope[abs(pid)-1] = spacing_ratio;
  344. a.m_path.push_back(p);
  345. }
  346. if(abs(pid)==1)
  347. sit_.set(p1);
  348. }
  349. #endif
  350. }
  351. #if 0
  352. if(-1==id)
  353. {
  354. for(auto&_s:sit_list::instance()->m_map)
  355. {
  356. _s.second->deal_path();
  357. }
  358. }
  359. else
  360. {
  361. auto sit_ptr=sit_list::instance()->get(id);
  362. if(sit_ptr)
  363. {
  364. sit_ptr->deal_path();
  365. log_info( "修改path成功,分站id=%d\n", id );
  366. }
  367. }
  368. #endif
  369. for(auto&vl:map_path)
  370. {
  371. auto sit_ptr=sit_list::instance()->get(vl.first);
  372. if(!sit_ptr)
  373. {
  374. log_error("定义了分站路径,但是没有定义分站:%d",vl.first);
  375. continue;
  376. }
  377. sit_ptr->set_path(vl.second);
  378. }
  379. card_path::init();
  380. test_find_path(card_path::inst(),point(4727.00,418.07),point(4724.73,598.32));
  381. test_find_path(card_path::inst(),point(4727,-8.06),point(2200,-75));
  382. test_find_path(card_path::inst(),point(4727,-8.06),point(2600,-100));
  383. test_find_path(card_path::inst(),point(2768.50,-75.00),point(2768.50,-161.58));
  384. test_find_path(card_path::inst(),point(4727,-8.06),point(5427.04,-304.02));
  385. test_find_path(card_path::inst(),point(4714.70,-8.06),point(5427.04,-304.02));
  386. test_find_path(card_path::inst(),point(4727,-75),point(2200,-75));
  387. test_find_path(card_path::inst(),point(4683.625,56.988),point(2200,-75));
  388. }
  389. void sit_list::init_site(int id)
  390. {
  391. std::string sql = "SELECT reader_id, reader_type_id, dat_reader.map_id, \
  392. area_id, device_type_id, dimension, dat_map.scale,need_power_alarm\
  393. ,x,y \
  394. FROM dat_reader, dat_map where \
  395. dat_reader.map_id=dat_map.map_id and state=0";
  396. if(-1 == id)
  397. {
  398. sql.append(";");
  399. }
  400. else
  401. {
  402. sql.append(" AND reader_id=");
  403. sql.append(std::to_string(id));
  404. sql.append(";");
  405. std_debug("增加或修改分站 sql=%s", sql.c_str());
  406. log_info("增加或修改分站 sql=%s", sql.c_str());
  407. }
  408. std::string Error;
  409. YADB::CDBResultSet DBRes;
  410. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  411. int nCount = DBRes.GetRecordCount( Error );
  412. if (nCount < 1)
  413. {
  414. log_error("增加或修改失败,数据库中找不到: 分站id=%d", id);
  415. return ;
  416. }
  417. log_info( "init_site. The record count=%ld\n", nCount );
  418. while ( DBRes.GetNextRecod(Error) )
  419. {
  420. int reader_id = 0;
  421. DBRes.GetField( "reader_id",reader_id, Error );
  422. auto site_ptr=sit_list::instance()->get(reader_id);
  423. if(nullptr==site_ptr)
  424. {
  425. site_ptr = std::make_shared<site>(reader_id);
  426. sit_list::instance()->add(reader_id,site_ptr);
  427. }
  428. int reader_type_id = 0;
  429. DBRes.GetField( "reader_type_id",reader_type_id, Error );
  430. int map_id = 0;
  431. DBRes.GetField( "map_id",map_id, Error );
  432. int area_id = 0;
  433. DBRes.GetField( "area_id",area_id, Error );
  434. int device_type_id = 0;
  435. DBRes.GetField( "device_type_id",device_type_id, Error );
  436. int dimension = 0;
  437. DBRes.GetField( "dimension",dimension, Error );
  438. double scale= 0;
  439. DBRes.GetField( "scale",scale, Error );
  440. int power_alarm = 0;
  441. DBRes.GetField( "need_power_alarm",power_alarm, Error );
  442. site_ptr->m_reader_type_id = reader_type_id;
  443. site_ptr->m_map_id = map_id;
  444. site_ptr->m_area_id = area_id;
  445. site_ptr->m_power_check_enable=power_alarm;
  446. site_ptr->m_device_type_id = device_type_id;
  447. site_ptr->m_dimension = dimension;
  448. site_ptr->m_scale = scale;
  449. site_ptr->create_area();
  450. double x=0,y=0;
  451. DBRes.GetField( "x",x, Error );
  452. DBRes.GetField( "y",y, Error );
  453. log_info ("site-position:site=%d,x=%.2lf,y=%.2lf",reader_id,x,y);
  454. }
  455. }
  456. void sit_list::read_sit_list(int id)
  457. {
  458. std::string sql = "SELECT antenna_id, a.reader_id, idx, a.x, a.y, a.z, a.angle \
  459. FROM dat_antenna a, dat_reader r \
  460. WHERE a.reader_id = r.reader_id";
  461. if(-1 == id)
  462. {
  463. sql.append(";");
  464. }
  465. else
  466. {
  467. sql.append(" AND antenna_id=");
  468. sql.append(std::to_string(id));
  469. sql.append(";");
  470. log_info("基础数据 增加或修改天线 sql=%s", sql.c_str());
  471. }
  472. std::string Error;
  473. YADB::CDBResultSet DBRes;
  474. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  475. int nCount = DBRes.GetRecordCount( Error );
  476. if (nCount < 1)
  477. {
  478. log_error("基础数据 增加或修改失败,数据库中找不到: 天线id=%d", id);
  479. return ;
  480. }
  481. log_info( "init_antenna. The record count=%ld\n", nCount );
  482. while ( DBRes.GetNextRecod(Error) )
  483. {
  484. int antenna_id = 0;
  485. DBRes.GetField( "antenna_id",antenna_id, Error );
  486. int reader_id = 0;
  487. DBRes.GetField( "reader_id",reader_id, Error );
  488. int idx=0;
  489. DBRes.GetField( "idx",idx, Error );
  490. int antid = idx-1;
  491. if(antid >= 2)
  492. continue;
  493. double x= 0;
  494. DBRes.GetField( "x",x, Error );
  495. double y= 0;
  496. DBRes.GetField( "y",y, Error );
  497. double z= 0;
  498. DBRes.GetField( "z",z, Error );
  499. log_info("ant-position:reader=%d,antid=%d,x=%.2lf,y=%.2lf",reader_id,antid,x,y);
  500. if(-1 == id)
  501. {
  502. std::shared_ptr<site> site_ptr=nullptr;
  503. site_ptr = sit_list::instance()->get(reader_id);
  504. if(!site_ptr)
  505. continue;
  506. site_ptr->m_ant[antid].m_id = antenna_id;
  507. site_ptr->m_ant[antid].set(x,-y);
  508. site_ptr->set_ex();
  509. }
  510. else
  511. {
  512. auto site_ptr = sit_list::instance()->get(reader_id);
  513. if(site_ptr)
  514. {
  515. site_ptr->m_ant[antid].set(x,-y);
  516. site_ptr->set_ex();
  517. }
  518. log_info("基础数据 增加或修改天线成功:天线id:%d,分站id:%d",id,reader_id);
  519. }
  520. }
  521. }
  522. void site::create_area()
  523. {
  524. m_area=std::make_shared<area>(-m_id,0,0,m_scale,m_map_id,1<<7);
  525. }
  526. /*
  527. 处理分站供电状态,交流供电时,ac_down=false,直流供电时,ac_down=true
  528. 目前只有大分站实现了这个功能,并且井下安装时是否接入了该电信号也不确定
  529. ,所以需要有张表定义某个ID是否需要告警
  530. */
  531. void site::on_power_status(bool ac_down)//电源状态
  532. {
  533. if(!m_power_check_enable || ac_down == m_power_ac_down)
  534. {
  535. return;
  536. }
  537. m_power_ac_down=ac_down;
  538. event_tool::instance()->handle_event(OT_DEVICE_READER,ET_READER_POWER_BY_BATTERY,id(),tool_time::now_to_seconds(),ac_down,m_power_ac_down);
  539. log_info("[event warning: reader power supply by battery] reader_id: Power %d->%d.",id(),!m_power_ac_down,m_power_ac_down);
  540. }
  541. algo_config site::g_config[]=
  542. {
  543. { "tof-1", 1, 2, 0.1, 1 },
  544. { "tdoa-1", 2, 2, 0.1, 1 },
  545. { "tof-2", 2, 3, 0.1, 1 },
  546. { "tdoa-2", 3, 3, 0.1, 1 },
  547. { "tof-3", 3, 4, 0.1, 1 },
  548. { "tdoa-3", 4, 4, 0.1, 1 }
  549. };
  550. #ifdef _TEST
  551. int main()
  552. {
  553. log_init("./log.ini");
  554. //sit_list *sl = sit_list::instance();
  555. sit_list::instance()->load("data_reader_antenna.txt","path_tof.txt");
  556. sit_list::instance()->get(209)->solving(0,100);
  557. sit_list::instance()->get(209)->solving(1,100.5);
  558. //std_info("---%d",(*sl)[209].m_ant[0].m_path.size());
  559. //std_info("---%d",(*sl)[209][0].size());
  560. //std_info("---%s",(*sl)[209][0][0][0].to_string().c_str());
  561. }
  562. #endif