1
0

net-service.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. net_service::net_service()
  19. {
  20. m_loc_worker=worker::instance();
  21. m_sync_worker=tdoa_sync::instance();
  22. }
  23. net_service::~net_service()
  24. {
  25. if(m_loc_worker)
  26. m_loc_worker->stop();
  27. }
  28. void net_service::on_timer()
  29. {
  30. static int version = -1;
  31. visit_site_status vss;
  32. sit_list::instance()->accept(vss);
  33. int v = card_list::instance()->version();
  34. card_list_visit clv;
  35. if(v != version)
  36. {
  37. version=v;
  38. clv._flag=true;
  39. mine_business::inst()->clear_vehicle();
  40. }
  41. card_list::instance()->accept(clv);
  42. mine_business::inst()->run_business();
  43. }
  44. void net_service::on_connect(const std::shared_ptr<client>& clt)
  45. {
  46. }
  47. void net_service::on_message(const std::shared_ptr<client> &clt,const char*data,size_t len)
  48. {
  49. bool message_handled=true;
  50. try
  51. {
  52. zistream is(data,len-2);
  53. uint16_t cmd;
  54. is>>skip(2)>>cmd;
  55. switch(cmd)
  56. {
  57. case CHAR_LOCATEDATA_TOF_EXTEND://tof-扩展
  58. case CHAR_LOCATEDATASMALL_TOF_EXTEND:
  59. {
  60. uint32_t site_id=-1;
  61. uint8_t power=-1;
  62. is>>site_id>>skip(11)>>power;
  63. auto site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  64. if(!site_ptr)
  65. {
  66. logn_error(1,"在全局分站列表中找不到分站:%d", site_id);
  67. break;
  68. }
  69. if(!site_ptr->check_timestamp(data+10))
  70. {
  71. logn_error(1,"分站数据时间戳错误:%s",clt->name().c_str());
  72. break;
  73. }
  74. {
  75. char timebuf[64]{0};
  76. unsigned char*t=(unsigned char*)data+10;
  77. sprintf(timebuf,"%d-%d %d:%d:%d",*(t+4)+1,*(t+3),*(t+2),*(t+1),*(t+0));
  78. logn_info(1,"分站数据信息:%s net=%s,sid=%d,tm=%s,sct=%d",(power&1)==0?"true":"false",clt->name().c_str(),site_id,timebuf,t[-2]*256+t[-1]);
  79. }
  80. site_ptr->set_client(clt);
  81. site_ptr->on_power_status((power&1)==0);
  82. //clt->set_site_id(site_id);
  83. struct timeval tv;
  84. gettimeofday(&tv,NULL);
  85. uint64_t t = tv.tv_sec*1000 + tv.tv_usec/1000;
  86. int index=0;
  87. while(!is.eof())
  88. {
  89. uint64_t tstamp = t -1000 + 50 + index* 45;
  90. index++;
  91. task*t=task::alloc<message_locinfo>();
  92. message_locinfo&m=t->body<message_locinfo>();
  93. m.m_site_id=site_id;
  94. m.m_time_stamp=tstamp;
  95. m.load(is,false);
  96. //t_site->m_site_data = 0;
  97. t->m_cmd_code=cmd;
  98. t->m_hash_id=m.m_card_id;
  99. if(m.m_card_type==5)
  100. 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);
  101. m_loc_worker->request(t);
  102. }
  103. }
  104. break;
  105. case CHAR_LOCATEDATA_TDOA_EXTEND://tdoa
  106. {
  107. uint32_t site_id;
  108. is>>site_id>>skip(12);
  109. while(!is.eof())
  110. {
  111. task*t=task::alloc<message_locinfo>();
  112. message_locinfo&m=t->body<message_locinfo>();
  113. m.load(is,true);
  114. m.m_site_id=site_id;
  115. t->m_cmd_code=cmd;
  116. t->m_hash_id=m.m_card_id;
  117. m_sync_worker->translate(m);
  118. m_loc_worker->request(t);
  119. }
  120. }
  121. break;
  122. case CHAR_TDOA_READER_SYNC_TIME://time sync
  123. {
  124. message_tdoasync m;
  125. m.load(is);
  126. m_sync_worker->on_message(m);
  127. }
  128. //site_message::on_sync(this,t.m_param1);
  129. break;
  130. case CHAR_LOCATEDATAHIS_TOF_EXTEND://tof his
  131. case CHAR_LOCATEDATAHIS_TDOA_EXTEND://tdoa his
  132. case CHAR_CTRL_READER_CMD://ctrl site message
  133. case CHAR_ADHOC://自组网数据
  134. default:
  135. message_handled=false;
  136. }
  137. }
  138. catch(const std::exception&e)
  139. {
  140. logn_error(1,"分站数据处理失败,将关闭分站连接:%s [%s]",clt->name().c_str(),e.what());
  141. clt->close();
  142. }
  143. if(!message_handled)
  144. {
  145. logn_error(1,"分站数据未被处理,site=%s",clt->name().c_str());
  146. }
  147. }