ant.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. template<> std::shared_ptr<sit_list>
  9. single_base<sit_list, int, std::shared_ptr<site>>::m_instance=std::make_shared<sit_list>();
  10. int site::index()const
  11. {
  12. return m_algo+(m_num_dims<<1);
  13. }
  14. site::site(int id)
  15. :m_algo(0)
  16. ,m_num_dims(0)
  17. ,m_id(id)
  18. ,m_path_empty(true)
  19. {
  20. m_time=time(0);
  21. }
  22. const algo_config&site::config()const
  23. {
  24. return g_config[index()];
  25. }
  26. bool visit_site_status::visit(std::shared_ptr<site> s)
  27. {
  28. time_t now=time(0);
  29. int diff = now-s->m_time;
  30. event_tool::instance()->handle_event(OT_DEVICE_READER,ET_READER_ERROR,s->m_id,READER_TIMEOUT,diff,[diff](){return diff>READER_TIMEOUT;});
  31. return true;
  32. }
  33. void sit_list::read_ant_path(int id)
  34. {
  35. 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";
  36. if(-1 == id)
  37. {
  38. sql.append(";");
  39. }
  40. else
  41. {
  42. sql.append(" AND reader_id=");
  43. sql.append(std::to_string(id));
  44. sql.append(";");
  45. std_debug("修改path sql=%s", sql.c_str());
  46. log_info("修改path sql=%s", sql.c_str());
  47. }
  48. std::string Error;
  49. YADB::CDBResultSet DBRes;
  50. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  51. int nCount = DBRes.GetRecordCount( Error );
  52. if (nCount < 1)
  53. {
  54. log_error("修改path失败,数据库中找不到(dat_reader_path_tof_n): 分站id=%d", id);
  55. return ;
  56. }
  57. while ( DBRes.GetNextRecod(Error) )
  58. {
  59. int reader_id = 0;
  60. DBRes.GetField( "reader_id",reader_id, Error );
  61. auto site_ptr=sit_list::instance()->get(reader_id);
  62. if(nullptr==site_ptr)
  63. continue;
  64. int pid=0;
  65. DBRes.GetField( "tof_flag",pid, Error );
  66. double b_x= 0;
  67. DBRes.GetField( "b_x",b_x, Error );
  68. double b_y= 0;
  69. DBRes.GetField( "b_y",b_y, Error );
  70. double b_z= 0;
  71. DBRes.GetField( "b_z",b_z, Error );
  72. double e_x= 0;
  73. DBRes.GetField( "e_x",e_x, Error );
  74. double e_y= 0;
  75. DBRes.GetField( "e_y",e_y, Error );
  76. double e_z= 0;
  77. DBRes.GetField( "e_z",e_z, Error );
  78. double spacing_ratio = 0;
  79. DBRes.GetField( "spacing_ratio",spacing_ratio, Error );
  80. log_info("ant_path:%d,%.2f,%.2f,%.2f,%.2f",reader_id,b_x,b_y,e_x,e_y);
  81. point p1(b_x,-b_y);
  82. point p2(e_x,-e_y);
  83. auto &sit_ = *site_ptr;
  84. if(-1!=id)//清空path
  85. {
  86. sit_.clear_path();
  87. log_info( "修改path 清空path,分站id=%d\n", id );
  88. std_debug( "修改path 清空path,分站id=%d\n", id );
  89. }
  90. if(pid == 0)
  91. {
  92. line_v l(p1,p2);
  93. {
  94. point px = l.line::projection(sit_);
  95. sit_.set(px);
  96. for(int i=0;i<2;i++)
  97. {
  98. path p;
  99. p.m_slope[0] = spacing_ratio;
  100. p.m_line[0] = line_v(px,l[i]);
  101. sit_.m_ant[i].m_path.push_back(p);
  102. }
  103. }
  104. }
  105. else
  106. {
  107. ant &a = pid<0?sit_.m_ant[0]:sit_.m_ant[1];
  108. if(a.m_path.size()!=0)
  109. {
  110. path &p = a.m_path[0];
  111. p.m_line[abs(pid)-1] = line_v(p1,p2);
  112. p.m_slope[abs(pid)-1] = spacing_ratio;
  113. }
  114. else
  115. {
  116. path p;
  117. p.m_line[abs(pid)-1] = line_v(p1,p2);
  118. p.m_slope[abs(pid)-1] = spacing_ratio;
  119. a.m_path.push_back(p);
  120. }
  121. if(abs(pid)==1)
  122. sit_.set(p1);
  123. }
  124. }
  125. if(-1==id)
  126. {
  127. log_info( "read_ant_path. The record count=%ld\n", nCount );
  128. for(auto&_s:sit_list::instance()->m_map)
  129. {
  130. _s.second->deal_path();
  131. }
  132. }
  133. else
  134. {
  135. auto sit_ptr=sit_list::instance()->get(id);
  136. if(sit_ptr)
  137. {
  138. sit_ptr->deal_path();
  139. log_info( "修改path成功,分站id=%d\n", id );
  140. std_debug( "修改path成功,分站id=%d\n", id );
  141. }
  142. }
  143. }
  144. void sit_list::init_site(int id)
  145. {
  146. std::string sql = "SELECT reader_id, reader_type_id, dat_reader.map_id, \
  147. area_id, device_type_id, dimension, dat_map.scale\
  148. FROM dat_reader, dat_map where \
  149. dat_reader.map_id=dat_map.map_id and state=0";
  150. if(-1 == id)
  151. {
  152. sql.append(";");
  153. }
  154. else
  155. {
  156. sql.append(" AND reader_id=");
  157. sql.append(std::to_string(id));
  158. sql.append(";");
  159. std_debug("增加或修改分站 sql=%s", sql.c_str());
  160. log_info("增加或修改分站 sql=%s", sql.c_str());
  161. }
  162. std::string Error;
  163. YADB::CDBResultSet DBRes;
  164. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  165. int nCount = DBRes.GetRecordCount( Error );
  166. if (nCount < 1)
  167. {
  168. log_error("增加或修改失败,数据库中找不到: 分站id=%d", id);
  169. return ;
  170. }
  171. log_info( "增加或修改 init_site. The record count=%ld\n", nCount );
  172. while ( DBRes.GetNextRecod(Error) )
  173. {
  174. int reader_id = 0;
  175. DBRes.GetField( "reader_id",reader_id, Error );
  176. auto site_ptr=sit_list::instance()->get(reader_id);
  177. if(nullptr==site_ptr)
  178. {
  179. site_ptr = std::make_shared<site>(reader_id);
  180. sit_list::instance()->add(reader_id,site_ptr);
  181. }
  182. int reader_type_id = 0;
  183. DBRes.GetField( "reader_type_id",reader_type_id, Error );
  184. int map_id = 0;
  185. DBRes.GetField( "map_id",map_id, Error );
  186. int area_id = 0;
  187. DBRes.GetField( "area_id",area_id, Error );
  188. int device_type_id = 0;
  189. DBRes.GetField( "device_type_id",device_type_id, Error );
  190. int dimension = 0;
  191. DBRes.GetField( "dimension",dimension, Error );
  192. double scale= 0;
  193. DBRes.GetField( "scale",scale, Error );
  194. site_ptr->m_reader_type_id = reader_type_id;
  195. site_ptr->m_map_id = map_id;
  196. site_ptr->m_area_id = area_id;
  197. site_ptr->m_device_type_id = device_type_id;
  198. site_ptr->m_dimension = dimension;
  199. site_ptr->m_scale = scale;
  200. }
  201. }
  202. void sit_list::read_sit_list(int id)
  203. {
  204. std::string sql = "SELECT antenna_id, a.reader_id, idx, a.x, a.y, a.z, a.angle \
  205. FROM dat_antenna a, dat_reader r \
  206. WHERE a.reader_id = r.reader_id";
  207. if(-1 == id)
  208. {
  209. sql.append(";");
  210. }
  211. else
  212. {
  213. sql.append(" AND antenna_id=");
  214. sql.append(std::to_string(id));
  215. sql.append(";");
  216. std_debug("基础数据 增加或修改天线 sql=%s", sql.c_str());
  217. log_info("基础数据 增加或修改天线 sql=%s", sql.c_str());
  218. }
  219. std::string Error;
  220. YADB::CDBResultSet DBRes;
  221. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  222. int nCount = DBRes.GetRecordCount( Error );
  223. if (nCount < 1)
  224. {
  225. log_error("基础数据 增加或修改失败,数据库中找不到: 天线id=%d", id);
  226. return ;
  227. }
  228. std::unordered_map<int,std::shared_ptr<site>> map;
  229. while ( DBRes.GetNextRecod(Error) )
  230. {
  231. int antenna_id = 0;
  232. DBRes.GetField( "antenna_id",antenna_id, Error );
  233. int reader_id = 0;
  234. DBRes.GetField( "reader_id",reader_id, Error );
  235. int idx=0;
  236. DBRes.GetField( "idx",idx, Error );
  237. int antid = idx-1;
  238. if(antid >= 2)
  239. continue;
  240. double x= 0;
  241. DBRes.GetField( "x",x, Error );
  242. double y= 0;
  243. DBRes.GetField( "y",y, Error );
  244. double z= 0;
  245. DBRes.GetField( "z",z, Error );
  246. if(-1 == id)
  247. {
  248. std::shared_ptr<site> site_ptr=nullptr;
  249. auto it = map.find(reader_id);
  250. if(it==map.end())
  251. {
  252. site_ptr = std::make_shared<site>(reader_id);
  253. map.insert({reader_id,site_ptr});
  254. }
  255. else
  256. site_ptr=it->second;
  257. site_ptr->m_ant[antid].m_id = antenna_id;
  258. site_ptr->m_ant[antid].set(x,-y);
  259. log_info("reand_ant..%d,%d,%.2f,%.2f",reader_id,antid,x,y);
  260. }
  261. else
  262. {
  263. auto site_ptr = sit_list::instance()->get(reader_id);
  264. if(site_ptr)
  265. {
  266. site_ptr->m_ant[antid].set(x,-y);
  267. site_ptr->set_ex();
  268. }
  269. log_info("基础数据 增加或修改天线成功:天线id:%d,分站id:%d",id,reader_id);
  270. std_debug("基础数据 增加或修改天线成功:天线id:%d,分站id:%d",id,reader_id);
  271. }
  272. }
  273. if(-1 == id)
  274. {
  275. log_info( "init_antenna. The record count=%ld\n", nCount );
  276. sit_list::instance()->add(map);
  277. for(auto&sit_:map)
  278. {
  279. sit_.second->set_ex();
  280. }
  281. }
  282. }
  283. /*
  284. 处理分站供电状态,交流供电时,ac_down=false,直流供电时,ac_down=true
  285. 目前只有大分站实现了这个功能,并且井下安装时是否接入了该电信号也不确定
  286. ,所以需要有张表定义某个ID是否需要告警
  287. */
  288. void site::on_power_status(bool ac_down)//电源状态
  289. {
  290. if(!m_power_check_enable || ac_down == m_power_ac_down)
  291. {
  292. return;
  293. }
  294. m_power_ac_down=ac_down;
  295. 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);
  296. log_info("[event warning: reader power supply by battery] reader_id: Power %d->%d.",id(),!m_power_ac_down,m_power_ac_down);
  297. }
  298. #if 0
  299. void sit_list::init_site()
  300. {
  301. const char *sql = "SELECT reader_id, reader_type_id, dat_reader.map_id, \
  302. area_id, device_type_id, dimension, dat_map.scale\
  303. FROM dat_reader, dat_map where \
  304. dat_reader.map_id=dat_map.map_id and state=0;";
  305. std::string Error;
  306. YADB::CDBResultSet DBRes;
  307. sDBConnPool.Query(sql,DBRes,Error);
  308. uint64_t nCount = DBRes.GetRecordCount( Error );
  309. if (nCount > 0)
  310. {
  311. log_info( "init_site. The record count=%ld\n", nCount );
  312. while ( DBRes.GetNextRecod(Error) )
  313. {
  314. int reader_id = 0;
  315. DBRes.GetField( "reader_id",reader_id, Error );
  316. auto site_ptr=sit_list::instance()->get(reader_id);
  317. if(nullptr==site_ptr)
  318. {
  319. site_ptr = std::make_shared<site>(reader_id);
  320. sit_list::instance()->add(reader_id,site_ptr);
  321. }
  322. int reader_type_id = 0;
  323. DBRes.GetField( "reader_type_id",reader_type_id, Error );
  324. int map_id = 0;
  325. DBRes.GetField( "map_id",map_id, Error );
  326. int area_id = 0;
  327. DBRes.GetField( "area_id",area_id, Error );
  328. int device_type_id = 0;
  329. DBRes.GetField( "device_type_id",device_type_id, Error );
  330. int dimension = 0;
  331. DBRes.GetField( "dimension",dimension, Error );
  332. double scale= 0;
  333. DBRes.GetField( "scale",scale, Error );
  334. site_ptr->m_reader_type_id = reader_type_id;
  335. site_ptr->m_map_id = map_id;
  336. site_ptr->m_area_id = area_id;
  337. site_ptr->m_device_type_id = device_type_id;
  338. site_ptr->m_dimension = dimension;
  339. site_ptr->m_scale = scale;
  340. }
  341. }
  342. }
  343. #endif
  344. algo_config site::g_config[]=
  345. {
  346. { "tof-1", 1, 2, 0.1, 1 },
  347. { "tdoa-1", 2, 2, 0.1, 1 },
  348. { "tof-2", 2, 3, 0.1, 1 },
  349. { "tdoa-2", 3, 3, 0.1, 1 },
  350. { "tof-3", 3, 4, 0.1, 1 },
  351. { "tdoa-3", 4, 4, 0.1, 1 }
  352. };
  353. #ifdef _TEST
  354. int main()
  355. {
  356. log_init("./log.ini");
  357. //sit_list *sl = sit_list::instance();
  358. sit_list::instance()->load("data_reader_antenna.txt","path_tof.txt");
  359. sit_list::instance()->get(209)->solving(0,100);
  360. sit_list::instance()->get(209)->solving(1,100.5);
  361. //std_info("---%d",(*sl)[209].m_ant[0].m_path.size());
  362. //std_info("---%d",(*sl)[209][0].size());
  363. //std_info("---%s",(*sl)[209][0][0][0].to_string().c_str());
  364. }
  365. #endif