1
0

net-service.cpp 6.3 KB

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