1
0

net-service.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. static bool check_message_time(const std::shared_ptr<client> &clt, const std::shared_ptr<site> &site_ptr, struct tm*p_site_tm, int site_ct, int power)
  38. {
  39. bool result=true;
  40. char timebuf[64]{0};
  41. strftime(timebuf,64,"%F %T",p_site_tm);
  42. logn_info(1,"分站数据信息:net=%s,site=%d,tm=%s,sct=%d,power=%s", clt->name().c_str(), site_ptr->m_id,timebuf,
  43. site_ct, site_ptr->m_power_check_enable?((power&1)?"ac":"dc"):"??");
  44. time_t site_time=mktime(p_site_tm);
  45. double diff=difftime(site_time, site_ptr->last_site_time());
  46. if(diff<-3) //允许2秒的时间抖动
  47. {
  48. logn_error(1,"分站时间回退,数据将被丢弃:net=%s,site=%d,diff=%d",clt->name().c_str(),site_ptr->m_id,(int)diff);
  49. result=false;
  50. }
  51. diff=difftime(site_time, time(nullptr));
  52. if(fabs(diff)>3)
  53. {
  54. logn_error(1,"分站时间与服务器时间相差太大:%s,site=%d,diff=%d",clt->name().c_str(),site_ptr->m_id,(int)diff);
  55. result=false;
  56. }
  57. site_ptr->set_site_time(site_time);
  58. return result;
  59. }
  60. void net_service::on_message(const std::shared_ptr<client> &clt,const char*data,size_t len)
  61. {
  62. bool message_handled=true;
  63. try
  64. {
  65. zistream is(data,len-2);
  66. uint16_t cmd;
  67. is>>skip(2)>>cmd;
  68. switch(cmd)
  69. {
  70. case CHAR_LOCATEDATA_TOF_EXTEND://tof-扩展
  71. {
  72. uint32_t site_id=-1;
  73. uint16_t site_ct=-1;
  74. uint8_t power=-1;
  75. struct tm site_tm={0};
  76. is>>site_id>>site_ct
  77. >>*(uint8_t*)&site_tm.tm_sec >>*(uint8_t*)&site_tm.tm_min >>*(uint8_t*)&site_tm.tm_hour >>*(uint8_t*)&site_tm.tm_mday
  78. >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_year
  79. >>skip(2)>>power;
  80. site_tm.tm_year+=2000-1900;
  81. site_tm.tm_mon-=1;
  82. const auto &site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  83. if(!site_ptr)
  84. {
  85. logn_error(1,"未定义分站:net=%s,site=%d", clt->name().c_str(), site_id);
  86. break;
  87. }
  88. if(site_ptr->is_abnormal_site())
  89. {
  90. logn_error(1,"分站[%d]天线异常", site_id);
  91. break;
  92. }
  93. check_message_time(clt,site_ptr,&site_tm,site_ct,power);
  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. m_loc_worker->request(t);
  116. }
  117. }
  118. break;
  119. case CHAR_LOCATEDATA_TOF_OPTIMIZE://tof-优化,大小分站
  120. {
  121. uint16_t site_id=-1;
  122. uint16_t site_ct=-1;
  123. uint32_t site_time_s=0;
  124. uint8_t power=-1;
  125. is>>site_id>>site_ct >>site_time_s >>power;
  126. power=power>>6;
  127. time_t site_time=site_time_s;
  128. auto site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  129. if(!site_ptr)
  130. {
  131. logn_error(1,"在全局分站列表中找不到分站:%s,%d", clt->name().c_str(), site_id);
  132. break;
  133. }
  134. if(site_ptr->is_abnormal_site())
  135. {
  136. logn_error(1,"分站[%d]天线异常", site_id);
  137. break;
  138. }
  139. struct tm site_tm={0};
  140. localtime_r(&site_time, &site_tm);
  141. check_message_time(clt,site_ptr,&site_tm,site_ct,power);
  142. if(clt->type()!=2){
  143. site_ptr->set_client(clt);
  144. site_ptr->on_power_status((power&1)==0);
  145. //clt->set_site_id(site_id);
  146. }
  147. struct timeval tv;
  148. gettimeofday(&tv,NULL);
  149. uint64_t t = tv.tv_sec*1000 + tv.tv_usec/1000;
  150. int index=0;
  151. while(!is.eof())
  152. {
  153. uint64_t tstamp = t -1000 + 50 + index* 45;
  154. index++;
  155. for(auto & tk : message_locinfo::load_opt(is))
  156. {
  157. message_locinfo&m=tk->body<message_locinfo>();
  158. m.m_site_id=site_id;
  159. m.m_time_stamp=tstamp;
  160. tk->m_hash_id=m.m_card_id;
  161. tk->m_cmd_code=cmd;
  162. m_loc_worker->request(tk);
  163. }
  164. }
  165. }
  166. break;
  167. case 0x783A://tof 分站时间同步
  168. {
  169. // 从第一个字节开始,分别表示毫秒(2字节)、秒、分、时、天、月、年
  170. unsigned char buf[20]={0,13,0x78,0x3b};
  171. struct timeval tv;
  172. gettimeofday(&tv,0);
  173. struct tm buff={0};
  174. const struct tm*t=localtime_r(&tv.tv_sec,&buff);
  175. int p=4;
  176. buf[p++]=(tv.tv_usec/1000)&0xFF;
  177. buf[p++]=((tv.tv_usec/1000)>>8)&0xFF;
  178. buf[p++]=t->tm_sec;
  179. buf[p++]=t->tm_min;
  180. buf[p++]=t->tm_hour;
  181. buf[p++]=t->tm_mday;
  182. buf[p++]=t->tm_wday;
  183. buf[p++]=t->tm_mon+1;
  184. buf[p++]=t->tm_year-100;
  185. uint16_t ccrc=do_crc(buf+2,11);
  186. buf[p++]=ccrc>>8;
  187. buf[p++]=ccrc&0xff;
  188. std::vector<char> tmp((char*)buf,(char*)buf+15);
  189. clt->send(std::move(tmp));
  190. logn_info(1,"分站时间同步:ip=%s,time=%d-%02d-%02d %02d:%02d:%02d.%03d",
  191. clt->name().c_str(),buf[12]+2000,buf[11],buf[9],buf[8],buf[7],buf[6],buf[5]*256+buf[4]);
  192. }
  193. break;
  194. case CHAR_LOCATEDATA_TDOA_EXTEND://tdoa
  195. {
  196. uint32_t site_id;
  197. is>>site_id>>skip(12);
  198. while(!is.eof())
  199. {
  200. task*t=task::alloc<message_locinfo>();
  201. message_locinfo&m=t->body<message_locinfo>();
  202. m.load(is,true);
  203. m.m_site_id=site_id;
  204. t->m_cmd_code=cmd;
  205. t->m_hash_id=m.m_card_id;
  206. m_sync_worker->translate(m);
  207. m_loc_worker->request(t);
  208. }
  209. }
  210. break;
  211. case CHAR_TDOA_READER_SYNC_TIME://time sync
  212. {
  213. message_tdoasync m;
  214. m.load(is);
  215. m_sync_worker->on_message(m);
  216. }
  217. //site_message::on_sync(this,t.m_param1);
  218. break;
  219. case CHAR_VIRTUAL_DATA_PUSH_CMD://虚拟数据链接
  220. {
  221. logn_info(1,"接收到虚拟链接:%s",clt->name().c_str());
  222. clt->set_conn_type(2);
  223. }
  224. break;
  225. case 0x793B: //虚拟推送的心跳测试
  226. case CHAR_LOCATEDATAHIS_TOF_EXTEND://tof his
  227. case CHAR_LOCATEDATAHIS_TDOA_EXTEND://tdoa his
  228. case CHAR_CTRL_READER_CMD://ctrl site message
  229. case CHAR_ADHOC://自组网数据
  230. default:
  231. message_handled=false;
  232. }
  233. }
  234. catch(const std::exception&e)
  235. {
  236. logn_error(1,"分站数据处理失败,将关闭分站连接:%s [%s]",clt->name().c_str(),e.what());
  237. clt->close();
  238. }
  239. if(!message_handled)
  240. {
  241. logn_error(1,"分站数据未被处理,site=%s",clt->name().c_str());
  242. }
  243. }