net-service.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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", clt->name().c_str(), 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 0x753C://tof-优化,大小分站
  111. {
  112. uint16_t site_id=-1;
  113. uint16_t site_ct=-1;
  114. uint32_t site_time_s=0;
  115. uint8_t power=-1;
  116. is>>site_id>>site_ct >>site_time_s >>power;
  117. power=power>>6;
  118. time_t site_time=site_time_s;
  119. auto site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  120. if(!site_ptr)
  121. {
  122. logn_error(1,"在全局分站列表中找不到分站:%s,%d", clt->name().c_str(), site_id);
  123. break;
  124. }
  125. if(site_ptr->is_abnormal_site())
  126. {
  127. logn_error(1,"分站[%d]天线异常", site_id);
  128. break;
  129. }
  130. {
  131. char timebuf[64]{0};
  132. struct tm site_tm={0};
  133. localtime_r(&site_time, &site_tm);
  134. strftime(timebuf,64,"%F %T",&site_tm);
  135. const char*power_flag="??";
  136. if(site_ptr->m_power_check_enable)
  137. {
  138. if(power==1)
  139. power_flag="dc";
  140. if(power==2)
  141. power_flag="ac";
  142. }
  143. logn_info(1,"分站数据信息:net=%s,sid=%d,tm=%s,sct=%d,power=%s", clt->name().c_str(), site_id,timebuf, site_ct, power_flag);
  144. double diff=difftime(site_time, site_ptr->last_site_time());
  145. if(diff<-3) //允许2秒的时间抖动
  146. {
  147. logn_error(1,"分站时间回退,数据将被丢弃:%s,diff=%d",clt->name().c_str(),(int)diff);
  148. }
  149. site_ptr->set_site_time(site_time);
  150. }
  151. if(clt->type()!=2){
  152. site_ptr->set_client(clt);
  153. site_ptr->on_power_status((power&1)==0);
  154. //clt->set_site_id(site_id);
  155. }
  156. struct timeval tv;
  157. gettimeofday(&tv,NULL);
  158. uint64_t t = tv.tv_sec*1000 + tv.tv_usec/1000;
  159. int index=0;
  160. while(!is.eof())
  161. {
  162. uint64_t tstamp = t -1000 + 50 + index* 45;
  163. index++;
  164. for(auto&tk:message_locinfo::load_753C(is))//未写完
  165. {
  166. message_locinfo&m=tk->body<message_locinfo>();
  167. m.m_site_id=site_id;
  168. m.m_time_stamp=tstamp;
  169. tk->m_hash_id=m.m_card_id;
  170. tk->m_cmd_code=cmd;
  171. if(m.m_card_type==5)
  172. 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);
  173. m_loc_worker->request(tk);
  174. }
  175. }
  176. }
  177. break;
  178. case 0x783A://tof 分站时间同步
  179. {
  180. // 从第一个字节开始,分别表示毫秒(2字节)、秒、分、时、天、月、年
  181. unsigned char buf[20]={0,13,0x78,0x3b};
  182. struct timeval tv;
  183. gettimeofday(&tv,0);
  184. struct tm buff={0};
  185. const struct tm*t=localtime_r(&tv.tv_sec,&buff);
  186. int p=4;
  187. buf[p++]=(tv.tv_usec/1000)&0xFF;
  188. buf[p++]=((tv.tv_usec/1000)>>8)&0xFF;
  189. buf[p++]=t->tm_sec;
  190. buf[p++]=t->tm_min;
  191. buf[p++]=t->tm_hour;
  192. buf[p++]=t->tm_mday;
  193. buf[p++]=t->tm_wday;
  194. buf[p++]=t->tm_mon+1;
  195. buf[p++]=t->tm_year-100;
  196. uint16_t ccrc=do_crc(buf+2,11);
  197. buf[p++]=ccrc>>8;
  198. buf[p++]=ccrc&0xff;
  199. std::vector<char> tmp((char*)buf,(char*)buf+15);
  200. clt->send(std::move(tmp));
  201. logn_info(1,"分站时间同步:ip=%s,time=%d-%02d-%02d %02d:%02d:%02d.%03d",
  202. clt->name().c_str(),buf[12]+2000,buf[11],buf[9],buf[8],buf[7],buf[6],buf[5]*256+buf[4]);
  203. }
  204. break;
  205. case CHAR_LOCATEDATA_TDOA_EXTEND://tdoa
  206. {
  207. uint32_t site_id;
  208. is>>site_id>>skip(12);
  209. while(!is.eof())
  210. {
  211. task*t=task::alloc<message_locinfo>();
  212. message_locinfo&m=t->body<message_locinfo>();
  213. m.load(is,true);
  214. m.m_site_id=site_id;
  215. t->m_cmd_code=cmd;
  216. t->m_hash_id=m.m_card_id;
  217. m_sync_worker->translate(m);
  218. m_loc_worker->request(t);
  219. }
  220. }
  221. break;
  222. case CHAR_TDOA_READER_SYNC_TIME://time sync
  223. {
  224. message_tdoasync m;
  225. m.load(is);
  226. m_sync_worker->on_message(m);
  227. }
  228. //site_message::on_sync(this,t.m_param1);
  229. break;
  230. case CHAR_VIRTUAL_DATA_PUSH_CMD://虚拟数据链接
  231. {
  232. logn_info(1,"接收到虚拟链接:%s",clt->name().c_str());
  233. clt->set_conn_type(2);
  234. }
  235. break;
  236. case CHAR_LOCATEDATAHIS_TOF_EXTEND://tof his
  237. case CHAR_LOCATEDATAHIS_TDOA_EXTEND://tdoa his
  238. case CHAR_CTRL_READER_CMD://ctrl site message
  239. case CHAR_ADHOC://自组网数据
  240. default:
  241. message_handled=false;
  242. }
  243. }
  244. catch(const std::exception&e)
  245. {
  246. logn_error(1,"分站数据处理失败,将关闭分站连接:%s [%s]",clt->name().c_str(),e.what());
  247. clt->close();
  248. }
  249. if(!message_handled)
  250. {
  251. logn_error(1,"分站数据未被处理,site=%s",clt->name().c_str());
  252. }
  253. }