#include #include #include #include #include #include #include #include #include #include #include #include "worker.h" #include "tdoa_sync.h" #include "net-service.h" #include "ant.h" #include "card.h" #include "crc.h" #include "mine_business.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& clt) { } void net_service::on_message(const std::shared_ptr &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; auto site_ptr = sit_list::instance()->get(static_cast(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; } { char timebuf[64]{0}; strftime(timebuf,64,"%F %T",&site_tm); logn_info(1,"分站数据信息:net=%s,sid=%d,tm=%s,sct=%d,power=%s", clt->name().c_str(), site_id,timebuf, site_ct, site_ptr->m_power_check_enable?((power&1)?"ac":"dc"):"??"); time_t site_time=mktime(&site_tm); double diff=difftime(site_time, site_ptr->last_site_time()); if(diff<-3) //允许2秒的时间抖动 { logn_error(1,"分站时间回退,数据将被丢弃:%s,diff=%d",clt->name().c_str(),(int)diff); } site_ptr->set_site_time(site_time); } 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&m=t->body(); 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; if(m.m_card_type==5) mine_business::inst()->make_reverse_condition(m.m_card_type,m.m_card_id,m.m_ant_id,m.m_card_ct,m.m_tof,m.m_site_id); m_loc_worker->request(t); } } 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 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://tdoa { uint32_t site_id; is>>site_id>>skip(12); while(!is.eof()) { task*t=task::alloc(); message_locinfo&m=t->body(); m.load(is,true); m.m_site_id=site_id; t->m_cmd_code=cmd; t->m_hash_id=m.m_card_id; m_sync_worker->translate(m); m_loc_worker->request(t); } } break; case CHAR_TDOA_READER_SYNC_TIME://time sync { message_tdoasync m; m.load(is); m_sync_worker->on_message(m); } //site_message::on_sync(this,t.m_param1); break; case CHAR_LOCATEDATAHIS_TOF_EXTEND://tof his case CHAR_LOCATEDATAHIS_TDOA_EXTEND://tdoa his case CHAR_CTRL_READER_CMD://ctrl site message case CHAR_ADHOC://自组网数据 case CHAR_VIRTUAL_DATA_PUSH_CMD://虚拟数据链接 { logn_info(1,"接收到虚拟链接:%s",clt->name().c_str()); clt->set_conn_type(2); } 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()); } }