123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include "ant.h"
- #include "db_api/CDBConnPool.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()];
- }
-
- static int str_split(char*s,char**rc)
- {
- char**o=rc;
- for(;*s;)
- {
- *o=strtok_r(s,",",&s);
- o++;
- }
- return o-rc;
- }
- //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);
- }
- */
- void sit_list::read_sit_list(const char*fname)
- {
- 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 );
- auto site_ptr=sit_list::instance()->get(reader_id);
- if(nullptr==site_ptr)
- {
- site_ptr = std::make_shared<site>(reader_id);
- map.insert({reader_id,site_ptr});
- }
- site_ptr->m_ant[antid].set(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(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);
- }
- }
- */
- 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);
- }
- }
- 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;
- }
- }
- }
- 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
|