123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- #include <thread>
- #include <mutex>
- #include <atomic>
- #include <iostream>
- #include <memory>
- #include <string.h>
- #include <math.h>
- #include <log.h>
- #include <znet.h>
- //#include <zstream.h>
- #include <sys/time.h>
- #include "worker.h"
- #include "tdoa_sync.h"
- #include "protocol.h"
- #include "net-service.h"
- #include "ant.h"
- #include "card.h"
- #include "crc.h"
- #include "mine_business.h"
- #include "tool_time.h"
- #include "module_service/module_traffic_light_manager.h"
- net_service::net_service()
- {
- m_loc_worker=worker::instance();
- m_sync_worker=tdoa_sync::instance();
- }
- net_service::~net_service()
- {
- if(m_loc_worker)
- m_loc_worker->stop();
- }
- void net_service::on_timer()
- {
- visit_site_status vss;
- sit_list::instance()->accept(vss);
- }
- void net_service::on_connect(const std::shared_ptr<client>& clt)
- {
-
- }
- static bool check_message_time(const std::shared_ptr<client> &clt, const std::shared_ptr<site> &site_ptr, struct tm*p_site_tm, int site_ct, int power)
- {
- bool result=true;
- char timebuf[64]{0};
- strftime(timebuf,64,"%F %T",p_site_tm);
- logn_info(1,"分站数据信息:net=%s,site=%d,tm=%s,sct=%d,power=%s", clt->name().c_str(), site_ptr->m_id,timebuf,
- site_ct, site_ptr->m_power_check_enable?((power&1)?"ac":"dc"):"??");
- time_t site_time=mktime(p_site_tm);
- double diff=difftime(site_time, site_ptr->last_site_time());
- if(diff<-3) //允许2秒的时间抖动
- {
- logn_error(1,"分站时间回退,数据将被丢弃:net=%s,site=%d,diff=%d",clt->name().c_str(),site_ptr->m_id,(int)diff);
- result=false;
- }
- diff=difftime(site_time, time(nullptr));
- if(fabs(diff)>3)
- {
- logn_error(1,"分站时间与服务器时间相差太大:%s,site=%d,diff=%d",clt->name().c_str(),site_ptr->m_id,(int)diff);
- result=false;
- }
- site_ptr->set_site_time(site_time);
- return result;
- }
- void net_service::on_message(const std::shared_ptr<client> &clt,const char*data,size_t len)
- {
- bool message_handled=true;
- try
- {
- zistream is(data,len-2);
- uint16_t cmd;
- is>>skip(2)>>cmd;
- switch(cmd)
- {
- case CHAR_LOCATEDATA_TOF_EXTEND://tof-扩展
- {
- uint32_t site_id=-1;
- uint16_t site_ct=-1;
- uint8_t power=-1;
- struct tm site_tm={0};
- is>>site_id>>site_ct
- >>*(uint8_t*)&site_tm.tm_sec >>*(uint8_t*)&site_tm.tm_min >>*(uint8_t*)&site_tm.tm_hour >>*(uint8_t*)&site_tm.tm_mday
- >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_year
- >>skip(2)>>power;
- site_tm.tm_year+=2000-1900;
- site_tm.tm_mon-=1;
- const auto &site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
- if(!site_ptr)
- {
- logn_error(1,"未定义分站:net=%s,site=%d", clt->name().c_str(), site_id);
- break;
- }
- if(site_ptr->is_abnormal_site())
- {
- logn_error(1,"分站[%d]天线异常", site_id);
- break;
- }
- check_message_time(clt,site_ptr,&site_tm,site_ct,power);
- site_ptr->m_algo = LDT_TOF;
- if(clt->type()!=2){
- site_ptr->set_client(clt);
- site_ptr->on_power_status((power&1)==0);
- //clt->set_site_id(site_id);
- }
- struct timeval tv;
- gettimeofday(&tv,NULL);
- uint64_t t = tv.tv_sec*1000 + tv.tv_usec/1000;
- int index=0;
- while(!is.eof())
- {
- uint64_t tstamp = t -1000 + 50 + index* 45;
- index++;
- task*t=task::alloc<message_locinfo>();
- message_locinfo&m=t->body<message_locinfo>();
- m.m_loc_type = LDT_TOF;
- m.m_site_id=site_id;
- m.m_time_stamp=tstamp;
- m.load(is,false);
- //t_site->m_site_data = 0;
- t->m_cmd_code=cmd;
- t->m_hash_id=m.m_card_id;
- m_loc_worker->request(t);
- }
- }
- break;
- case CHAR_LOCATEDATA_TOF_OPTIMIZE://tof-优化,大小分站
- {
- uint16_t site_id=-1;
- uint16_t site_ct=-1;
- uint32_t site_time_s=0;
- uint8_t power=-1;
- is>>site_id>>site_ct >>site_time_s >>power;
- power=power>>6;
- time_t site_time=site_time_s;
- auto site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
- if(!site_ptr)
- {
- logn_error(1,"在全局分站列表中找不到分站:%s,%d", clt->name().c_str(), site_id);
- break;
- }
- if(site_ptr->is_abnormal_site())
- {
- logn_error(1,"分站[%d]天线异常", site_id);
- break;
- }
- struct tm site_tm={0};
- localtime_r(&site_time, &site_tm);
- check_message_time(clt,site_ptr,&site_tm,site_ct,power);
- if(clt->type()!=2){
- site_ptr->set_client(clt);
- site_ptr->on_power_status((power&1)==0);
- //clt->set_site_id(site_id);
- }
- struct timeval tv;
- gettimeofday(&tv,NULL);
- uint64_t t = tv.tv_sec*1000 + tv.tv_usec/1000;
- int index=0;
- while(!is.eof())
- {
- uint64_t tstamp = t -1000 + 50 + index* 45;
- index++;
-
- for(auto & tk : message_locinfo::load_opt(is))
- {
- message_locinfo&m=tk->body<message_locinfo>();
- m.m_site_id=site_id;
- m.m_time_stamp=tstamp;
- tk->m_hash_id=m.m_card_id;
- tk->m_cmd_code=cmd;
- m_loc_worker->request(tk);
- }
- }
- }
- break;
- case CHAR_LOCATEDATA_PDOA:
- {
- int32_t site_id = parse_data_anchor_opt(clt, is);
- if(site_id < 0){
- break;
- }
- const auto& site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
- if(!site_ptr){
- logn_error(1,"在全局分站列表中找不到分站:%d", site_id);
- break;
- }
-
- if(clt->type() != 2){
- site_ptr->set_client(clt);
- //site_ptr->on_power_status((power&1)==0);
- }
- site_ptr->set_algo(LDT_PDOA);
- //log_info("[scale_test] %.2f", site_ptr->m_scale);
- struct timeval tv;
- gettimeofday(&tv, NULL);
- uint64_t cur_time = tv.tv_sec*1000 + tv.tv_usec/1000;
- int index = 0;
- while(!is.eof()){
- uint64_t tstamp = cur_time - 1000 + 50 + index*45;
- ++index;
- task* t = task::alloc<message_pdoa_locinfo>();
- message_pdoa_locinfo& m = t->body<message_pdoa_locinfo>();
- m.load(is);
- float pdoa = 10.0;
- float dist = m.m_tof*15.65*2.996*1e-4;
- m.m_site_id = site_id;
- m.m_time_stamp = tstamp;
- m.m_loc_type = LDT_PDOA;
- t->m_cmd_code = cmd;
- t->m_hash_id = m.m_card_id;
- pdoa = get_pdoa(m.m_poa, site_ptr->m_pdoa_offset);
- m_loc_worker->request(t);
- logn_info(3, "[pdoa] site_id=%d, card_id=%d, ct=%d, dist=%.3f, rav=%d, poa1=%.4f, poa2=%.4f, poa3=%.4f, pdoa=%.4f, pdoa_offset=%.4f", site_id, m.m_card_id, m.m_card_ct, dist, m.m_rav, m.m_poa[0], m.m_poa[1], m.m_poa[2], pdoa, site_ptr->m_pdoa_offset);
- }
- }
- break;
- case 0x783A://tof 分站时间同步
- {
- // 从第一个字节开始,分别表示毫秒(2字节)、秒、分、时、天、月、年
- unsigned char buf[20]={0,13,0x78,0x3b};
- struct timeval tv;
- gettimeofday(&tv,0);
- struct tm buff={0};
- const struct tm*t=localtime_r(&tv.tv_sec,&buff);
- int p=4;
- buf[p++]=(tv.tv_usec/1000)&0xFF;
- buf[p++]=((tv.tv_usec/1000)>>8)&0xFF;
- buf[p++]=t->tm_sec;
- buf[p++]=t->tm_min;
- buf[p++]=t->tm_hour;
- buf[p++]=t->tm_mday;
- buf[p++]=t->tm_wday;
- buf[p++]=t->tm_mon+1;
- buf[p++]=t->tm_year-100;
- uint16_t ccrc=do_crc(buf+2,11);
- buf[p++]=ccrc>>8;
- buf[p++]=ccrc&0xff;
- std::vector<char> tmp((char*)buf,(char*)buf+15);
- clt->send(std::move(tmp));
- logn_info(1,"分站时间同步:ip=%s,time=%d-%02d-%02d %02d:%02d:%02d.%03d",
- clt->name().c_str(),buf[12]+2000,buf[11],buf[9],buf[8],buf[7],buf[6],buf[5]*256+buf[4]);
- }
- break;
- case CHAR_LOCATEDATA_TDOA_EXTEND: // 0x863b
- case CHAR_LOCATEDATA_TDOA_EXTEND_INS: // 0x901b
- {
- int32_t site_id = parse_data_anchor(clt, is);
- if(site_id < 0){
- break;
- }
- const auto& site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
- if(!site_ptr){
- logn_error(1,"在全局分站列表中找不到分站:%d", site_id);
- break;
- }
- site_ptr->set_algo(LDT_TDOA);
- while(!is.eof())
- {
- task* t = task::alloc<message_tdoa_locinfo>();
- message_tdoa_locinfo& m = t->body<message_tdoa_locinfo>();
- m.m_loc_type = LDT_TDOA;
- m.load(is, cmd);
- t->m_cmd_code = cmd;
- t->m_hash_id = m.m_card_msg.m_id;
- m_sync_worker->translate(m);
- m_loc_worker->request(t);
- }
- }
- break;
- case CHAR_TDOA_READER_SYNC_TIME://time sync 0xa78d
- {
- message_tdoasync m;
- m.load(is);
- m_sync_worker->on_message(m);
- }
- //site_message::on_sync(this,t.m_param1);
- break;
- case CHAR_VIRTUAL_DATA_PUSH_CMD://虚拟数据链接
- {
- logn_info(1,"接收到虚拟链接:%s",clt->name().c_str());
- clt->set_conn_type(2);
- }
- break;
- case CHAR_READER_HEART:
- {
- uint32_t id = 0;
- uint16_t site_ct = 0;
- uint16_t reserve = 0;
- is>>id>>site_ct>>reserve;
- auto site_ptr = sit_list::instance()->get(id);
- if(!site_ptr){
- logn_error(1,"在全局分站列表中找不到分站:%d", id);
- break;
- }
- }
- break;
- case 0x793B: //虚拟推送的心跳测试
- case CHAR_LOCATEDATAHIS_TOF_EXTEND://tof his
- case CHAR_LOCATEDATAHIS_TDOA_EXTEND://tdoa his
- case CHAR_CTRL_READER_CMD://ctrl site message
- break;
- case THIRD_PARTY_CHAR_LIGHT_REQ_STATE:
- {
- // 红绿灯请求状态指令
- uint32_t id = 0;
- uint8_t dev_type = 0;
- is>>id>>dev_type;
- logn_info(4, "[traffic_light] light request state, light_id=%d, dev_type=%d", id, dev_type);
- // 找灯,并下发指令
- auto light_ptr = traffic_light_manager::instance()->get(id);
- if(light_ptr)
- {
- //traffic_light_manager::instance()->send_light_ctrl(id, DT_LIGHT, light_ptr->m_state);
- traffic_light_manager::instance()->send_light_data(id, DT_LIGHT, light_shape::green_all_on);
- }else{
- traffic_light_manager::instance()->send_light_data(id, DT_LIGHT, light_shape::green_spark);
- }
- }
- break;
- case THIRD_PARTY_CHAR_LIGHT_HEART:
- {
- // 红绿灯心跳
- uint32_t id = 0;
- uint16_t stamp = 0;
- uint8_t status = 0;
- is>>id>>stamp>>status;
- logn_info(4, "[traffic_light] light heart message, light_id=%d, stamp=%d, status=%d", id, stamp, status);
-
- auto light_ptr = traffic_light_manager::instance()->get(id);
- if(!light_ptr)
- {
- logn_error(1,"在全局信号灯列表中找不到信号灯:%d", id);
- break;
- }
-
- //log_info("[traffic_light] light client type=%d", clt->type());
- if(clt->type() != 2){
- light_ptr->set_client(clt);
- }
- light_ptr->m_state = status;
- light_ptr->m_rec_time = time(0);
- /*if((stamp%10) == 0){
- int shape = (status == light_shape::red_spark)?light_shape::green_spark:light_shape::red_spark;
- traffic_light_manager::instance()->send_light_ctrl(id, 0x05, shape);
- }*/
- }
- break;
- default:
- message_handled=false;
- }
- }
- catch(const std::exception&e)
- {
- logn_error(1,"分站数据处理失败,将关闭分站连接:%s [%s]",clt->name().c_str(),e.what());
- clt->close();
- }
- if(!message_handled)
- {
- logn_error(1,"分站数据未被处理,site=%s",clt->name().c_str());
- }
- }
- int32_t net_service::parse_data_anchor(const std::shared_ptr<client>& clt, zistream& s)
- {
- uint32_t site_id=-1;
- uint16_t site_ct=-1;
- uint8_t power=-1;
- struct tm site_tm={0};
- // 4字节分站号
- // 2字节时间戳
- // 7字节分站时间
- // 2字节保留字节
- // 1字节电源状态
- s>>site_id>>site_ct
- >>*(uint8_t*)&site_tm.tm_sec >>*(uint8_t*)&site_tm.tm_min >>*(uint8_t*)&site_tm.tm_hour >>*(uint8_t*)&site_tm.tm_mday
- >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_year
- >>skip(2)>>power;
- site_tm.tm_year+=2000-1900;
- site_tm.tm_mon-=1;
- const auto &site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
- if(!site_ptr)
- {
- logn_error(1,"未定义分站:net=%s,site=%d", clt->name().c_str(), site_id);
- return -1;
- }
- if(site_ptr->is_abnormal_site())
- {
- logn_error(1,"分站[%d]天线异常", site_id);
- return -1;
- }
- check_message_time(clt, site_ptr, &site_tm, site_ct, power);
- if(clt->type()!=2){
- site_ptr->set_client(clt);
- site_ptr->on_power_status((power&1)==0);
- }
- logn_info(1,"分站数据:site_id: %d, site_ct: %d, time: %s, power: %d", site_id, site_ct, tool_time::to_str_ex(&site_tm).c_str(), power);
- return site_id;
- }
- // 分站优化协议
- int32_t net_service::parse_data_anchor_opt(const std::shared_ptr<client>& clt, zistream& s)
- {
- // 分站号,2字节
- uint16_t site_id = 0;
- // 分站时间戳,2字节
- uint16_t site_ct = 0;
- // 分站时间,4字节
- uint32_t site_time_stamp = 0;
- // 大小分站所属关系,1字节
- uint8_t reverse = 0;
- // 状态字节,1字节,包括3部分:供电状态,设备类型,保留
- uint8_t power = 0;
- struct tm site_tm={0};
- s>>site_id>>site_ct>>site_time_stamp>>reverse>>power;
- time_t _sts = site_time_stamp;
- localtime_r(&_sts, &site_tm);
- const auto &site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
- if(!site_ptr)
- {
- logn_error(1,"未定义分站:net=%s, site=%d", clt->name().c_str(), site_id);
- return -1;
- }
- // pdoa分站不检查天线是否相等
- /*if(site_ptr->is_abnormal_site())
- {
- logn_error(1,"分站[%d]天线异常", site_id);
- return -1;
- }*/
- //check_message_time(clt, site_ptr, &site_tm, site_ct, power);
- site_ptr->m_device_type_id = (power>>3)&0x07;
- if(clt->type()!=2){
- site_ptr->set_client(clt);
- site_ptr->on_power_status((power&1)==0);
- }
- logn_info(1,"分站数据:site_id: %d, site_ct: %d, time: %s, power: %d", site_id, site_ct, tool_time::to_str_ex(&site_tm).c_str(), power);
- return site_id;
- }
- void net_service::parse_data_card()
- {
- }
- float net_service::get_pdoa(float poa[], const double& offset)
- {
- if(poa == nullptr){
- return -10.0;
- }
- float poa1 = poa[0];
- float poa2 = poa[1];
- float pdoa = poa2 - poa1 - offset;
- while(pdoa >= TPI){
- pdoa -= TPI;
- }
- while(pdoa < 0){
- pdoa += TPI;
- }
- pdoa -= PI;
- return pdoa;
- }
|