net-service.cpp 5.4 KB

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