123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- #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 "net-service.h"
- #include "ant.h"
- #include "card.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()
- {
- static int version = -1;
- visit_site_status vss;
- sit_list::instance()->accept(vss);
- int v = card_list::instance()->version();
- card_list_visit clv;
- if(v != version)
- {
- version=v;
- clv._flag=true;
- mine_business::inst()->clear_vehicle();
- }
- card_list::instance()->accept(clv);
- mine_business::inst()->run_business();
- }
- void net_service::on_message(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-扩展
- case CHAR_LOCATEDATASMALL_TOF_EXTEND:
- {
- uint32_t site_id;
- uint8_t power;
- is>>site_id>>skip(11)>>power;
- auto site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
- if(!site_ptr)
- {
- logn_error(1,"在全局分站列表中找不到分站:%d", site_id);
- break;
- }
- if(!site_ptr->check_timestamp(data+10))
- {
- logn_error(1,"分站数据时间戳错误:%s",clt->name().c_str());
- break;
- }
- {
- char timebuf[64];
- unsigned char*t=(unsigned char*)data+10;
- sprintf(timebuf,"%d-%d %d:%d:%d",*(t+4)+1,*(t+3),*(t+2),*(t+1),*(t+0));
- logn_info(1,"分站数据信息: net=%s,sid=%d,tm=%s,sct=%d",clt->name().c_str(),site_id,timebuf,((*t-2)<<8)|*(t-1));
- }
- site_ptr->set_client(clt);
- site_ptr->on_power_status((power&1)==0);
- 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.load(is,false);
- m.m_site_id=site_id;
- m.m_time_stamp=tstamp;
- //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 CHAR_LOCATEDATA_TDOA_EXTEND://tdoa
- {
- uint32_t site_id;
- is>>site_id>>skip(12);
- while(!is.eof())
- {
- task*t=task::alloc<message_locinfo>();
- message_locinfo&m=t->body<message_locinfo>();
- 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://自组网数据
- default:
- message_handled=false;
- }
- }
- catch(const std::exception&e)
- {
- logn_error(1,"分站数据处理失败,将关闭分站连接:%s",clt->name().c_str());
- clt->close();
- }
- if(!message_handled)
- {
- logn_error(1,"分站数据未被处理,site=%s",clt->name().c_str());
- }
- }
|