net-service.cpp 5.7 KB

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