1
0

net-service.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <thread>
  2. #include <mutex>
  3. #include <atomic>
  4. #include <iostream>
  5. #include <memory>
  6. #include <string.h>
  7. #include <math.h>
  8. #include <log.h>
  9. #include <znet.h>
  10. #include <zstream.h>
  11. #include <sys/time.h>
  12. #include "worker.h"
  13. #include "tdoa_sync.h"
  14. #include "net-service.h"
  15. #include "ant.h"
  16. #include "card.h"
  17. #include "crc.h"
  18. #include "mine_business.h"
  19. net_service::net_service()
  20. {
  21. m_loc_worker=worker::instance();
  22. m_sync_worker=tdoa_sync::instance();
  23. }
  24. net_service::~net_service()
  25. {
  26. if(m_loc_worker)
  27. m_loc_worker->stop();
  28. }
  29. void net_service::on_timer()
  30. {
  31. visit_site_status vss;
  32. sit_list::instance()->accept(vss);
  33. }
  34. void net_service::on_connect(const std::shared_ptr<client>& clt)
  35. {
  36. }
  37. void net_service::on_message(const std::shared_ptr<client> &clt,const char*data,size_t len)
  38. {
  39. bool message_handled=true;
  40. try
  41. {
  42. zistream is(data,len-2);
  43. uint16_t cmd;
  44. is>>skip(2)>>cmd;
  45. switch(cmd)
  46. {
  47. case CHAR_LOCATEDATA_TOF_EXTEND://tof-扩展
  48. {
  49. uint32_t site_id=-1;
  50. uint16_t site_ct=-1;
  51. uint8_t power=-1;
  52. struct tm site_tm={0};
  53. is>>site_id>>site_ct
  54. >>*(uint8_t*)&site_tm.tm_sec >>*(uint8_t*)&site_tm.tm_min >>*(uint8_t*)&site_tm.tm_hour >>*(uint8_t*)&site_tm.tm_mday
  55. >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_year
  56. >>skip(2)>>power;
  57. site_tm.tm_year+=2000-1900;
  58. site_tm.tm_mon-=1;
  59. auto site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  60. if(!site_ptr)
  61. {
  62. logn_error(1,"在全局分站列表中找不到分站:%s,%d", site_id);
  63. break;
  64. }
  65. if(site_ptr->is_abnormal_site())
  66. {
  67. logn_error(1,"分站[%d]天线异常", site_id);
  68. break;
  69. }
  70. {
  71. char timebuf[64]{0};
  72. strftime(timebuf,64,"%F %T",&site_tm);
  73. logn_info(1,"分站数据信息:net=%s,sid=%d,tm=%s,sct=%d,power=%s", clt->name().c_str(), site_id,timebuf,
  74. site_ct, site_ptr->m_power_check_enable?((power&1)?"ac":"dc"):"??");
  75. time_t site_time=mktime(&site_tm);
  76. double diff=difftime(site_time, site_ptr->last_site_time());
  77. if(diff<-3) //允许2秒的时间抖动
  78. {
  79. logn_error(1,"分站时间回退,数据将被丢弃:%s,diff=%d",clt->name().c_str(),(int)diff);
  80. }
  81. site_ptr->set_site_time(site_time);
  82. }
  83. if(clt->type()!=2){
  84. site_ptr->set_client(clt);
  85. site_ptr->on_power_status((power&1)==0);
  86. //clt->set_site_id(site_id);
  87. }
  88. struct timeval tv;
  89. gettimeofday(&tv,NULL);
  90. uint64_t t = tv.tv_sec*1000 + tv.tv_usec/1000;
  91. int index=0;
  92. while(!is.eof())
  93. {
  94. uint64_t tstamp = t -1000 + 50 + index* 45;
  95. index++;
  96. task*t=task::alloc<message_locinfo>();
  97. message_locinfo&m=t->body<message_locinfo>();
  98. m.m_site_id=site_id;
  99. m.m_time_stamp=tstamp;
  100. m.load(is,false);
  101. //t_site->m_site_data = 0;
  102. t->m_cmd_code=cmd;
  103. t->m_hash_id=m.m_card_id;
  104. if(m.m_card_type==5)
  105. 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);
  106. m_loc_worker->request(t);
  107. }
  108. }
  109. break;
  110. case 0x783A://tof 分站时间同步
  111. {
  112. // 从第一个字节开始,分别表示毫秒(2字节)、秒、分、时、天、月、年
  113. unsigned char buf[20]={0,13,0x78,0x3b};
  114. struct timeval tv;
  115. gettimeofday(&tv,0);
  116. struct tm buff={0};
  117. const struct tm*t=localtime_r(&tv.tv_sec,&buff);
  118. int p=4;
  119. buf[p++]=(tv.tv_usec/1000)&0xFF;
  120. buf[p++]=((tv.tv_usec/1000)>>8)&0xFF;
  121. buf[p++]=t->tm_sec;
  122. buf[p++]=t->tm_min;
  123. buf[p++]=t->tm_hour;
  124. buf[p++]=t->tm_mday;
  125. buf[p++]=t->tm_wday;
  126. buf[p++]=t->tm_mon+1;
  127. buf[p++]=t->tm_year-100;
  128. uint16_t ccrc=do_crc(buf+2,11);
  129. buf[p++]=ccrc>>8;
  130. buf[p++]=ccrc&0xff;
  131. std::vector<char> tmp((char*)buf,(char*)buf+15);
  132. clt->send(std::move(tmp));
  133. logn_info(1,"分站时间同步:ip=%s,time=%d-%02d-%02d %02d:%02d:%02d.%03d",
  134. clt->name().c_str(),buf[12]+2000,buf[11],buf[9],buf[8],buf[7],buf[6],buf[5]*256+buf[4]);
  135. }
  136. break;
  137. case CHAR_LOCATEDATA_TDOA_EXTEND://tdoa
  138. {
  139. uint32_t site_id;
  140. is>>site_id>>skip(12);
  141. while(!is.eof())
  142. {
  143. task*t=task::alloc<message_locinfo>();
  144. message_locinfo&m=t->body<message_locinfo>();
  145. m.load(is,true);
  146. m.m_site_id=site_id;
  147. t->m_cmd_code=cmd;
  148. t->m_hash_id=m.m_card_id;
  149. m_sync_worker->translate(m);
  150. m_loc_worker->request(t);
  151. }
  152. }
  153. break;
  154. case CHAR_TDOA_READER_SYNC_TIME://time sync
  155. {
  156. message_tdoasync m;
  157. m.load(is);
  158. m_sync_worker->on_message(m);
  159. }
  160. //site_message::on_sync(this,t.m_param1);
  161. break;
  162. case CHAR_LOCATEDATAHIS_TOF_EXTEND://tof his
  163. case CHAR_LOCATEDATAHIS_TDOA_EXTEND://tdoa his
  164. case CHAR_CTRL_READER_CMD://ctrl site message
  165. case CHAR_ADHOC://自组网数据
  166. case CHAR_VIRTUAL_DATA_PUSH_CMD://虚拟数据链接
  167. {
  168. logn_info(1,"接收到虚拟链接:%s",clt->name().c_str());
  169. clt->set_conn_type(2);
  170. }
  171. break;
  172. default:
  173. message_handled=false;
  174. }
  175. }
  176. catch(const std::exception&e)
  177. {
  178. logn_error(1,"分站数据处理失败,将关闭分站连接:%s [%s]",clt->name().c_str(),e.what());
  179. clt->close();
  180. }
  181. if(!message_handled)
  182. {
  183. logn_error(1,"分站数据未被处理,site=%s",clt->name().c_str());
  184. }
  185. }