123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724 |
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include "ant.h"
- #include "db_api/CDBSingletonDefine.h"
- template<> std::shared_ptr<sit_list>
- single_base<sit_list, int, std::shared_ptr<site>>::m_instance=std::make_shared<sit_list>();
- int site::index()const
- {
- return m_algo+(m_num_dims<<1);
- }
- site::site(int id)
- :m_algo(0)
- ,m_num_dims(0)
- ,m_id(id)
- ,m_path_empty(true)
- {
- }
- const algo_config&site::config()const
- {
- return g_config[index()];
- }
- #if 0
- static int str_split(char*s,char**rc)
- {
- char**o=rc;
- for(;*s;)
- {
- *o=strtok_r(s,",",&s);
- o++;
- }
- return o-rc;
- }
- void sit_list::read_ant_path(const char*fname)
- {
- FILE*fp=fopen(fname,"r");
- char buf[512],*p;
- int t,id,pid;
- char* s[20];
- while((p=fgets(buf,sizeof(buf),fp)))
- {
- t=str_split(buf,&s[0]);
- if(t<9)
- continue;
- id=atoi(s[0]);
- auto s_ = sit_list::instance()->get(id);
- if(s_==nullptr)
- continue;
- pid=atoi(s[1]);
- if(pid>2)
- continue;
- point p1(atof(s[2]),-atof(s[3]));
- point p2(atof(s[5]),-atof(s[6]));
- auto &sit_ = *s_;
- if(pid == 0)
- {
- line_v l(p1,p2);
- {
- point px = l.line::projection(sit_);
- sit_.set(px);
- for(int i=0;i<2;i++)
- {
- path p;
- p.m_slope[0] = atof(s[8]);
- p.m_line[0] = line_v(px,l[i]);
- sit_.m_ant[i].m_path.push_back(p);
- }
- }
- }
- else
- {
- ant &a = pid<0?sit_.m_ant[0]:sit_.m_ant[1];
- if(a.m_path.size()!=0)
- {
- path &p = a.m_path[0];
- p.m_line[abs(pid)-1] = line_v(p1,p2);
- p.m_slope[abs(pid)-1] = atof(s[8]);
- }
- else
- {
- path p;
- p.m_line[abs(pid)-1] = line_v(p1,p2);
- p.m_slope[abs(pid)-1] = atof(s[8]);
- a.m_path.push_back(p);
- }
- if(abs(pid)==1)
- sit_.set(p1);
- }
- }
- fclose(fp);
- for(auto&_s:sit_list::instance()->m_map)
- {
- auto & s = *(_s.second);
- if(s.m_id==-1)
- continue;
- s.swap();
- if((s.path(0).empty() && s.path(1).empty()))
- continue;
- s.m_path_empty=false;
- for(auto &a:s.m_ant)
- for(auto &p:a.m_path)
- {
- if(!p.m_line[0].empty())
- {
- point px = p.m_line[0].line::projection(a);
- p.m_line[0]=line_v(px,p.m_line[0][1]);
- }
- }
- //std_info("%s",s.to_string().c_str());
- log_info("%s",s.to_string().c_str());
- //std_info("%f----%f",s.x,s.y);
- }
- }
- //1, 101, 1, '101-1', 4727, 75, 0, 0, '2017-08-29 10:21:14'
- void sit_list::read_sit_list(const char*fname)
- {
- FILE*fp=fopen(fname,"r");
- char buf[512];
- int t,id;
- char* s[20];
- while(fgets(buf,sizeof(buf),fp))
- {
- t=str_split(buf,&s[0]);
- if(t<8)
- continue;
- id=atoi(s[1]);
- int antid=atoi(s[2])-1;
- if(antid>=2)
- continue;
- auto tmp = m_instance->get(id);
- if(tmp==nullptr)
- tmp = std::make_shared<site>(id);
- tmp->m_ant[antid].set(atof(s[4]),-atof(s[5]));
- sit_list::instance()->add(id,tmp);
- //std_info("211111 id=%dsize = %d",id,instance()->m_map.size());
- //m_list[id].m_id=id;
- //m_list[id].m_ant[antid].set(atof(s[4]),-atof(s[5]));
- }
-
- for(auto&sit_:sit_list::instance()->m_map)
- {
- auto & sit = *(sit_.second);
- if(sit.m_id==-1)
- continue;
- if(sit.m_ant[0]==sit.m_ant[1])
- {
- log_warn("%d分站天线坐标相等.",sit.m_id);
- }
- sit.set( (sit.m_ant[0].x+sit.m_ant[1].x)/2,(sit.m_ant[0].y+sit.m_ant[1].y)/2);
- }
- fclose(fp);
- }
- #endif
- #if 0
- void sit_list::read_sit_list()
- {
- std::unordered_map<int,std::shared_ptr<site>> map;
- const char *sql = "SELECT antenna_id, a.reader_id, idx, a.x, a.y, a.z, a.angle \
- FROM dat_antenna a, dat_reader r \
- WHERE a.reader_id = r.reader_id;";
- std::string Error;
- YADB::CDBResultSet DBRes;
- sDBConnPool.Query(sql,DBRes,Error);
- uint64_t nCount = DBRes.GetRecordCount( Error );
- if (nCount > 0)
- {
- log_info( "init_antenna. The record count=%ld\n", nCount );
- while ( DBRes.GetNextRecod(Error) )
- {
- int reader_id = 0;
- DBRes.GetField( "reader_id",reader_id, Error );
- int idx=0;
- DBRes.GetField( "idx",idx, Error );
- int antid = idx-1;
- if(antid >= 2)
- continue;
- double x= 0;
- DBRes.GetField( "x",x, Error );
- double y= 0;
- DBRes.GetField( "y",y, Error );
- double z= 0;
- DBRes.GetField( "z",z, Error );
- std::shared_ptr<site> site_ptr=nullptr;
- auto it = map.find(reader_id);
- if(it==map.end())
- {
- site_ptr = std::make_shared<site>(reader_id);
- map.insert({reader_id,site_ptr});
- }
- else
- site_ptr=it->second;
- site_ptr->m_ant[antid].set(x,-y);
- log_info("reand_ant..%d,%d,%.2f,%.2f",reader_id,antid,x,y);
- }
- }
- sit_list::instance()->add(map);
- for(auto&sit_:map)
- {
- auto & sit = *(sit_.second);
- if(sit.m_id==-1)
- continue;
- if(sit.m_ant[0]==sit.m_ant[1])
- {
- log_warn("%d分站天线坐标相等.",sit.m_id);
- }
- sit.set( (sit.m_ant[0].x+sit.m_ant[1].x)/2,(sit.m_ant[0].y+sit.m_ant[1].y)/2);
- }
- }
- void sit_list::read_ant_path()
- {
- std::unordered_map<int,std::shared_ptr<site>> map;
- const char *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;";
- std::string Error;
- YADB::CDBResultSet DBRes;
- sDBConnPool.Query(sql,DBRes,Error);
- uint64_t nCount = DBRes.GetRecordCount( Error );
- if(nCount<=0)
- log_error("init_ant_path error :%s",Error.c_str());
- if (nCount > 0)
- {
- log_info( "init_ant_path. The record count=%ld\n", nCount );
- while ( DBRes.GetNextRecod(Error) )
- {
- int reader_id = 0;
- DBRes.GetField( "reader_id",reader_id, Error );
- auto site_ptr=sit_list::instance()->get(reader_id);
- if(nullptr==site_ptr)
- continue;
- int pid=0;
- DBRes.GetField( "tof_flag",pid, Error );
- double b_x= 0;
- DBRes.GetField( "b_x",b_x, Error );
- double b_y= 0;
- DBRes.GetField( "b_y",b_y, Error );
- double b_z= 0;
- DBRes.GetField( "b_z",b_z, Error );
- double e_x= 0;
- DBRes.GetField( "e_x",e_x, Error );
- double e_y= 0;
- DBRes.GetField( "e_y",e_y, Error );
- double e_z= 0;
- DBRes.GetField( "e_z",e_z, Error );
- double spacing_ratio = 0;
- DBRes.GetField( "spacing_ratio",spacing_ratio, Error );
- log_info("ant_path:%d,%.2f,%.2f,%.2f,%.2f",reader_id,b_x,b_y,e_x,e_y);
- point p1(b_x,-b_y);
- point p2(e_x,-e_y);
- auto &sit_ = *site_ptr;
- if(pid == 0)
- {
- line_v l(p1,p2);
- {
- point px = l.line::projection(sit_);
- sit_.set(px);
- for(int i=0;i<2;i++)
- {
- path p;
- p.m_slope[0] = spacing_ratio;
- p.m_line[0] = line_v(px,l[i]);
- sit_.m_ant[i].m_path.push_back(p);
- }
- }
- }
- else
- {
- ant &a = pid<0?sit_.m_ant[0]:sit_.m_ant[1];
- if(a.m_path.size()!=0)
- {
- path &p = a.m_path[0];
- p.m_line[abs(pid)-1] = line_v(p1,p2);
- p.m_slope[abs(pid)-1] = spacing_ratio;
- }
- else
- {
- path p;
- p.m_line[abs(pid)-1] = line_v(p1,p2);
- p.m_slope[abs(pid)-1] = spacing_ratio;
- a.m_path.push_back(p);
- }
- if(abs(pid)==1)
- sit_.set(p1);
- }
- }
- }
- for(auto&_s:sit_list::instance()->m_map)
- {
- auto & s = *(_s.second);
- if(s.m_id==-1)
- continue;
- s.swap();
- if((s.path(0).empty() && s.path(1).empty()))
- continue;
- s.m_path_empty=false;
- for(auto &a:s.m_ant)
- for(auto &p:a.m_path)
- {
- if(!p.m_line[0].empty())
- {
- point px = p.m_line[0].line::projection(a);
- p.m_line[0]=line_v(px,p.m_line[0][1]);
- }
- }
- //std_info("%s",s.to_string().c_str());
- log_info("%s",s.to_string().c_str());
- //std_info("%f----%f",s.x,s.y);
- }
- }
- #endif
- void sit_list::read_ant_path(int id)
- {
- 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";
- if(-1 == id)
- {
- sql.append(";");
- }
- else
- {
- sql.append(" AND reader_id=");
- sql.append(std::to_string(id));
- sql.append(";");
- std_debug("修改path sql=%s", sql.c_str());
- log_info("修改path sql=%s", sql.c_str());
- }
- std::string Error;
- YADB::CDBResultSet DBRes;
- sDBConnPool.Query(sql.c_str(),DBRes,Error);
- int nCount = DBRes.GetRecordCount( Error );
- if (nCount < 1)
- {
- log_error("修改path失败,数据库中找不到(dat_reader_path_tof_n): 分站id=%d", id);
- return ;
- }
- while ( DBRes.GetNextRecod(Error) )
- {
- int reader_id = 0;
- DBRes.GetField( "reader_id",reader_id, Error );
- auto site_ptr=sit_list::instance()->get(reader_id);
- if(nullptr==site_ptr)
- continue;
- int pid=0;
- DBRes.GetField( "tof_flag",pid, Error );
- double b_x= 0;
- DBRes.GetField( "b_x",b_x, Error );
- double b_y= 0;
- DBRes.GetField( "b_y",b_y, Error );
- double b_z= 0;
- DBRes.GetField( "b_z",b_z, Error );
- double e_x= 0;
- DBRes.GetField( "e_x",e_x, Error );
- double e_y= 0;
- DBRes.GetField( "e_y",e_y, Error );
- double e_z= 0;
- DBRes.GetField( "e_z",e_z, Error );
- double spacing_ratio = 0;
- DBRes.GetField( "spacing_ratio",spacing_ratio, Error );
- log_info("ant_path:%d,%.2f,%.2f,%.2f,%.2f",reader_id,b_x,b_y,e_x,e_y);
- point p1(b_x,-b_y);
- point p2(e_x,-e_y);
- auto &sit_ = *site_ptr;
- if(-1!=id)//清空path
- {
- sit_.clear_path();
- log_info( "修改path 清空path,分站id=%d\n", id );
- std_debug( "修改path 清空path,分站id=%d\n", id );
- }
- if(pid == 0)
- {
- line_v l(p1,p2);
- {
- point px = l.line::projection(sit_);
- sit_.set(px);
- for(int i=0;i<2;i++)
- {
- path p;
- p.m_slope[0] = spacing_ratio;
- p.m_line[0] = line_v(px,l[i]);
- sit_.m_ant[i].m_path.push_back(p);
- }
- }
- }
- else
- {
- ant &a = pid<0?sit_.m_ant[0]:sit_.m_ant[1];
- if(a.m_path.size()!=0)
- {
- path &p = a.m_path[0];
- p.m_line[abs(pid)-1] = line_v(p1,p2);
- p.m_slope[abs(pid)-1] = spacing_ratio;
- }
- else
- {
- path p;
- p.m_line[abs(pid)-1] = line_v(p1,p2);
- p.m_slope[abs(pid)-1] = spacing_ratio;
- a.m_path.push_back(p);
- }
- if(abs(pid)==1)
- sit_.set(p1);
- }
- }
- if(-1==id)
- {
- log_info( "read_ant_path. The record count=%ld\n", nCount );
- for(auto&_s:sit_list::instance()->m_map)
- {
- _s.second->deal_path();
- }
- }
- else
- {
- auto sit_ptr=sit_list::instance()->get(id);
- if(sit_ptr)
- {
- sit_ptr->deal_path();
- log_info( "修改path成功,分站id=%d\n", id );
- std_debug( "修改path成功,分站id=%d\n", id );
- }
- }
- }
- void sit_list::init_site(int id)
- {
- std::string sql = "SELECT reader_id, reader_type_id, dat_reader.map_id, \
- area_id, device_type_id, dimension, dat_map.scale\
- FROM dat_reader, dat_map where \
- dat_reader.map_id=dat_map.map_id and state=0";
- if(-1 == id)
- {
- sql.append(";");
- }
- else
- {
- sql.append(" AND reader_id=");
- sql.append(std::to_string(id));
- sql.append(";");
- std_debug("增加或修改分站 sql=%s", sql.c_str());
- log_info("增加或修改分站 sql=%s", sql.c_str());
- }
- std::string Error;
- YADB::CDBResultSet DBRes;
- sDBConnPool.Query(sql.c_str(),DBRes,Error);
- int nCount = DBRes.GetRecordCount( Error );
- if (nCount < 1)
- {
- log_error("增加或修改失败,数据库中找不到: 分站id=%d", id);
- return ;
- }
- log_info( "增加或修改 init_site. The record count=%ld\n", nCount );
- while ( DBRes.GetNextRecod(Error) )
- {
- int reader_id = 0;
- DBRes.GetField( "reader_id",reader_id, Error );
- auto site_ptr=sit_list::instance()->get(reader_id);
- if(nullptr==site_ptr)
- {
- site_ptr = std::make_shared<site>(reader_id);
- sit_list::instance()->add(reader_id,site_ptr);
- }
- int reader_type_id = 0;
- DBRes.GetField( "reader_type_id",reader_type_id, Error );
- int map_id = 0;
- DBRes.GetField( "map_id",map_id, Error );
- int area_id = 0;
- DBRes.GetField( "area_id",area_id, Error );
- int device_type_id = 0;
- DBRes.GetField( "device_type_id",device_type_id, Error );
- int dimension = 0;
- DBRes.GetField( "dimension",dimension, Error );
- double scale= 0;
- DBRes.GetField( "scale",scale, Error );
- site_ptr->m_reader_type_id = reader_type_id;
- site_ptr->m_map_id = map_id;
- site_ptr->m_area_id = area_id;
- site_ptr->m_device_type_id = device_type_id;
- site_ptr->m_dimension = dimension;
- site_ptr->m_scale = scale;
- }
- }
- void sit_list::read_sit_list(int id)
- {
- std::string sql = "SELECT antenna_id, a.reader_id, idx, a.x, a.y, a.z, a.angle \
- FROM dat_antenna a, dat_reader r \
- WHERE a.reader_id = r.reader_id";
- if(-1 == id)
- {
- sql.append(";");
- }
- else
- {
- sql.append(" AND antenna_id=");
- sql.append(std::to_string(id));
- sql.append(";");
- std_debug("基础数据 增加或修改天线 sql=%s", sql.c_str());
- log_info("基础数据 增加或修改天线 sql=%s", sql.c_str());
- }
- std::string Error;
- YADB::CDBResultSet DBRes;
- sDBConnPool.Query(sql.c_str(),DBRes,Error);
- int nCount = DBRes.GetRecordCount( Error );
- if (nCount < 1)
- {
- log_error("基础数据 增加或修改失败,数据库中找不到: 天线id=%d", id);
- return ;
- }
- std::unordered_map<int,std::shared_ptr<site>> map;
- while ( DBRes.GetNextRecod(Error) )
- {
- int antenna_id = 0;
- DBRes.GetField( "antenna_id",antenna_id, Error );
- int reader_id = 0;
- DBRes.GetField( "reader_id",reader_id, Error );
- int idx=0;
- DBRes.GetField( "idx",idx, Error );
- int antid = idx-1;
- if(antid >= 2)
- continue;
- double x= 0;
- DBRes.GetField( "x",x, Error );
- double y= 0;
- DBRes.GetField( "y",y, Error );
- double z= 0;
- DBRes.GetField( "z",z, Error );
- if(-1 == id)
- {
- std::shared_ptr<site> site_ptr=nullptr;
- auto it = map.find(reader_id);
- if(it==map.end())
- {
- site_ptr = std::make_shared<site>(reader_id);
- map.insert({reader_id,site_ptr});
- }
- else
- site_ptr=it->second;
- site_ptr->m_ant[antid].m_id = antenna_id;
- site_ptr->m_ant[antid].set(x,-y);
- log_info("reand_ant..%d,%d,%.2f,%.2f",reader_id,antid,x,y);
- }
- else
- {
- auto site_ptr = sit_list::instance()->get(reader_id);
- if(site_ptr)
- {
- site_ptr->m_ant[antid].set(x,-y);
- site_ptr->set_ex();
- }
- log_info("基础数据 增加或修改天线成功:天线id:%d,分站id:%d",id,reader_id);
- std_debug("基础数据 增加或修改天线成功:天线id:%d,分站id:%d",id,reader_id);
- }
- }
- if(-1 == id)
- {
- log_info( "init_antenna. The record count=%ld\n", nCount );
- sit_list::instance()->add(map);
- for(auto&sit_:map)
- {
- sit_.second->set_ex();
- }
- }
- }
- #if 0
- void sit_list::init_site()
- {
- const char *sql = "SELECT reader_id, reader_type_id, dat_reader.map_id, \
- area_id, device_type_id, dimension, dat_map.scale\
- FROM dat_reader, dat_map where \
- dat_reader.map_id=dat_map.map_id and state=0;";
- std::string Error;
- YADB::CDBResultSet DBRes;
- sDBConnPool.Query(sql,DBRes,Error);
- uint64_t nCount = DBRes.GetRecordCount( Error );
- if (nCount > 0)
- {
- log_info( "init_site. The record count=%ld\n", nCount );
- while ( DBRes.GetNextRecod(Error) )
- {
- int reader_id = 0;
- DBRes.GetField( "reader_id",reader_id, Error );
- auto site_ptr=sit_list::instance()->get(reader_id);
- if(nullptr==site_ptr)
- {
- site_ptr = std::make_shared<site>(reader_id);
- sit_list::instance()->add(reader_id,site_ptr);
- }
- int reader_type_id = 0;
- DBRes.GetField( "reader_type_id",reader_type_id, Error );
- int map_id = 0;
- DBRes.GetField( "map_id",map_id, Error );
- int area_id = 0;
- DBRes.GetField( "area_id",area_id, Error );
- int device_type_id = 0;
- DBRes.GetField( "device_type_id",device_type_id, Error );
- int dimension = 0;
- DBRes.GetField( "dimension",dimension, Error );
- double scale= 0;
- DBRes.GetField( "scale",scale, Error );
- site_ptr->m_reader_type_id = reader_type_id;
- site_ptr->m_map_id = map_id;
- site_ptr->m_area_id = area_id;
- site_ptr->m_device_type_id = device_type_id;
- site_ptr->m_dimension = dimension;
- site_ptr->m_scale = scale;
- }
- }
- }
- #endif
- algo_config site::g_config[]=
- {
- { "tof-1", 1, 2, 0.1, 1 },
- { "tdoa-1", 2, 2, 0.1, 1 },
- { "tof-2", 2, 3, 0.1, 1 },
- { "tdoa-2", 3, 3, 0.1, 1 },
- { "tof-3", 3, 4, 0.1, 1 },
- { "tdoa-3", 4, 4, 0.1, 1 }
- };
- #ifdef _TEST
- int main()
- {
- log_init("./log.ini");
- //sit_list *sl = sit_list::instance();
- sit_list::instance()->load("data_reader_antenna.txt","path_tof.txt");
- sit_list::instance()->get(209)->solving(0,100);
- sit_list::instance()->get(209)->solving(1,100.5);
- //std_info("---%d",(*sl)[209].m_ant[0].m_path.size());
- //std_info("---%d",(*sl)[209][0].size());
- //std_info("---%s",(*sl)[209][0][0][0].to_string().c_str());
- }
- #endif
|