net-service.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 "protocol.h"
  15. #include "net-service.h"
  16. #include "ant.h"
  17. #include "card.h"
  18. #include "crc.h"
  19. #include "mine_business.h"
  20. #include "tool_time.h"
  21. net_service::net_service()
  22. {
  23. m_loc_worker=worker::instance();
  24. m_sync_worker=tdoa_sync::instance();
  25. }
  26. net_service::~net_service()
  27. {
  28. if(m_loc_worker)
  29. m_loc_worker->stop();
  30. }
  31. void net_service::on_timer()
  32. {
  33. visit_site_status vss;
  34. sit_list::instance()->accept(vss);
  35. }
  36. void net_service::on_connect(const std::shared_ptr<client>& clt)
  37. {
  38. }
  39. 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)
  40. {
  41. bool result=true;
  42. char timebuf[64]{0};
  43. strftime(timebuf,64,"%F %T",p_site_tm);
  44. logn_info(1,"分站数据信息:net=%s,site=%d,tm=%s,sct=%d,power=%s", clt->name().c_str(), site_ptr->m_id,timebuf,
  45. site_ct, site_ptr->m_power_check_enable?((power&1)?"ac":"dc"):"??");
  46. time_t site_time=mktime(p_site_tm);
  47. double diff=difftime(site_time, site_ptr->last_site_time());
  48. if(diff<-3) //允许2秒的时间抖动
  49. {
  50. logn_error(1,"分站时间回退,数据将被丢弃:net=%s,site=%d,diff=%d",clt->name().c_str(),site_ptr->m_id,(int)diff);
  51. result=false;
  52. }
  53. diff=difftime(site_time, time(nullptr));
  54. if(fabs(diff)>3)
  55. {
  56. logn_error(1,"分站时间与服务器时间相差太大:%s,site=%d,diff=%d",clt->name().c_str(),site_ptr->m_id,(int)diff);
  57. result=false;
  58. }
  59. site_ptr->set_site_time(site_time);
  60. return result;
  61. }
  62. void net_service::on_message(const std::shared_ptr<client> &clt,const char*data,size_t len)
  63. {
  64. bool message_handled=true;
  65. try
  66. {
  67. zistream is(data,len-2);
  68. uint16_t cmd;
  69. is>>skip(2)>>cmd;
  70. switch(cmd)
  71. {
  72. case CHAR_LOCATEDATA_TOF_EXTEND://tof-扩展
  73. {
  74. uint32_t site_id=-1;
  75. uint16_t site_ct=-1;
  76. uint8_t power=-1;
  77. struct tm site_tm={0};
  78. is>>site_id>>site_ct
  79. >>*(uint8_t*)&site_tm.tm_sec >>*(uint8_t*)&site_tm.tm_min >>*(uint8_t*)&site_tm.tm_hour >>*(uint8_t*)&site_tm.tm_mday
  80. >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_year
  81. >>skip(2)>>power;
  82. site_tm.tm_year+=2000-1900;
  83. site_tm.tm_mon-=1;
  84. const auto &site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  85. if(!site_ptr)
  86. {
  87. logn_error(1,"未定义分站:net=%s,site=%d", clt->name().c_str(), site_id);
  88. break;
  89. }
  90. if(site_ptr->is_abnormal_site())
  91. {
  92. logn_error(1,"分站[%d]天线异常", site_id);
  93. break;
  94. }
  95. check_message_time(clt,site_ptr,&site_tm,site_ct,power);
  96. if(clt->type()!=2){
  97. site_ptr->set_client(clt);
  98. site_ptr->on_power_status((power&1)==0);
  99. //clt->set_site_id(site_id);
  100. }
  101. struct timeval tv;
  102. gettimeofday(&tv,NULL);
  103. uint64_t t = tv.tv_sec*1000 + tv.tv_usec/1000;
  104. int index=0;
  105. while(!is.eof())
  106. {
  107. uint64_t tstamp = t -1000 + 50 + index* 45;
  108. index++;
  109. task*t=task::alloc<message_locinfo>();
  110. message_locinfo&m=t->body<message_locinfo>();
  111. m.m_loc_type = LDT_TOF;
  112. m.m_site_id=site_id;
  113. m.m_time_stamp=tstamp;
  114. m.load(is,false);
  115. //t_site->m_site_data = 0;
  116. t->m_cmd_code=cmd;
  117. t->m_hash_id=m.m_card_id;
  118. m_loc_worker->request(t);
  119. }
  120. }
  121. break;
  122. case CHAR_LOCATEDATA_TOF_OPTIMIZE://tof-优化,大小分站
  123. {
  124. uint16_t site_id=-1;
  125. uint16_t site_ct=-1;
  126. uint32_t site_time_s=0;
  127. uint8_t power=-1;
  128. is>>site_id>>site_ct >>site_time_s >>power;
  129. power=power>>6;
  130. time_t site_time=site_time_s;
  131. auto site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  132. if(!site_ptr)
  133. {
  134. logn_error(1,"在全局分站列表中找不到分站:%s,%d", clt->name().c_str(), site_id);
  135. break;
  136. }
  137. if(site_ptr->is_abnormal_site())
  138. {
  139. logn_error(1,"分站[%d]天线异常", site_id);
  140. break;
  141. }
  142. struct tm site_tm={0};
  143. localtime_r(&site_time, &site_tm);
  144. check_message_time(clt,site_ptr,&site_tm,site_ct,power);
  145. if(clt->type()!=2){
  146. site_ptr->set_client(clt);
  147. site_ptr->on_power_status((power&1)==0);
  148. //clt->set_site_id(site_id);
  149. }
  150. struct timeval tv;
  151. gettimeofday(&tv,NULL);
  152. uint64_t t = tv.tv_sec*1000 + tv.tv_usec/1000;
  153. int index=0;
  154. while(!is.eof())
  155. {
  156. uint64_t tstamp = t -1000 + 50 + index* 45;
  157. index++;
  158. for(auto & tk : message_locinfo::load_opt(is))
  159. {
  160. message_locinfo&m=tk->body<message_locinfo>();
  161. m.m_site_id=site_id;
  162. m.m_time_stamp=tstamp;
  163. tk->m_hash_id=m.m_card_id;
  164. tk->m_cmd_code=cmd;
  165. m_loc_worker->request(tk);
  166. }
  167. }
  168. }
  169. break;
  170. case CHAR_LOCATEDATA_PDOA:
  171. {
  172. int32_t site_id = parse_data_anchor(clt, is);
  173. if(site_id < 0){
  174. return;
  175. }
  176. const auto &site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  177. struct timeval tv;
  178. gettimeofday(&tv, NULL);
  179. uint64_t t = tv.tv_sec*1000 + tv.tv_usec/1000;
  180. int index = 0;
  181. while(!is.eof()){
  182. uint64_t tstamp = t - 1000 + 50 + index*45;
  183. ++index;
  184. task* t = task::alloc<message_pdoa_locinfo>();
  185. message_pdoa_locinfo& m = t->body<message_pdoa_locinfo>();
  186. m.load(is);
  187. m.m_loc_type = LDT_PDOA;
  188. //m.m_site_id = site_id;
  189. m.m_time_stamp = tstamp;
  190. t->m_cmd_code = cmd;
  191. t->m_hash_id = m.m_card_id;
  192. site_ptr->m_pdoa = get_pdoa(m.m_poa, site_ptr->m_pdoa_offset);
  193. }
  194. }
  195. break;
  196. case 0x783A://tof 分站时间同步
  197. {
  198. // 从第一个字节开始,分别表示毫秒(2字节)、秒、分、时、天、月、年
  199. unsigned char buf[20]={0,13,0x78,0x3b};
  200. struct timeval tv;
  201. gettimeofday(&tv,0);
  202. struct tm buff={0};
  203. const struct tm*t=localtime_r(&tv.tv_sec,&buff);
  204. int p=4;
  205. buf[p++]=(tv.tv_usec/1000)&0xFF;
  206. buf[p++]=((tv.tv_usec/1000)>>8)&0xFF;
  207. buf[p++]=t->tm_sec;
  208. buf[p++]=t->tm_min;
  209. buf[p++]=t->tm_hour;
  210. buf[p++]=t->tm_mday;
  211. buf[p++]=t->tm_wday;
  212. buf[p++]=t->tm_mon+1;
  213. buf[p++]=t->tm_year-100;
  214. uint16_t ccrc=do_crc(buf+2,11);
  215. buf[p++]=ccrc>>8;
  216. buf[p++]=ccrc&0xff;
  217. std::vector<char> tmp((char*)buf,(char*)buf+15);
  218. clt->send(std::move(tmp));
  219. logn_info(1,"分站时间同步:ip=%s,time=%d-%02d-%02d %02d:%02d:%02d.%03d",
  220. clt->name().c_str(),buf[12]+2000,buf[11],buf[9],buf[8],buf[7],buf[6],buf[5]*256+buf[4]);
  221. }
  222. break;
  223. case CHAR_LOCATEDATA_TDOA_EXTEND: // 0x863b
  224. case CHAR_LOCATEDATA_TDOA_EXTEND_INS: // 0x901b
  225. {
  226. int32_t site_id = parse_data_anchor(clt, is);
  227. if(site_id < 0){
  228. return;
  229. }
  230. while(!is.eof())
  231. {
  232. task* t = task::alloc<message_tdoa_locinfo>();
  233. message_tdoa_locinfo& m = t->body<message_tdoa_locinfo>();
  234. m.m_loc_type = LDT_TDOA;
  235. m.load(is, cmd);
  236. t->m_cmd_code = cmd;
  237. t->m_hash_id = m.m_card_msg.m_id;
  238. m_sync_worker->translate(m);
  239. m_loc_worker->request(t);
  240. }
  241. }
  242. break;
  243. case CHAR_TDOA_READER_SYNC_TIME://time sync 0xa78d
  244. {
  245. message_tdoasync m;
  246. m.load(is);
  247. m_sync_worker->on_message(m);
  248. }
  249. //site_message::on_sync(this,t.m_param1);
  250. break;
  251. case CHAR_VIRTUAL_DATA_PUSH_CMD://虚拟数据链接
  252. {
  253. logn_info(1,"接收到虚拟链接:%s",clt->name().c_str());
  254. clt->set_conn_type(2);
  255. }
  256. break;
  257. case 0x793B: //虚拟推送的心跳测试
  258. case CHAR_LOCATEDATAHIS_TOF_EXTEND://tof his
  259. case CHAR_LOCATEDATAHIS_TDOA_EXTEND://tdoa his
  260. case CHAR_CTRL_READER_CMD://ctrl site message
  261. default:
  262. message_handled=false;
  263. }
  264. }
  265. catch(const std::exception&e)
  266. {
  267. logn_error(1,"分站数据处理失败,将关闭分站连接:%s [%s]",clt->name().c_str(),e.what());
  268. clt->close();
  269. }
  270. if(!message_handled)
  271. {
  272. logn_error(1,"分站数据未被处理,site=%s",clt->name().c_str());
  273. }
  274. }
  275. int32_t net_service::parse_data_anchor(const std::shared_ptr<client>& clt, zistream& s)
  276. {
  277. uint32_t site_id=-1;
  278. uint16_t site_ct=-1;
  279. uint8_t power=-1;
  280. struct tm site_tm={0};
  281. // 4字节分站号
  282. // 2字节时间戳
  283. // 7字节分站时间
  284. // 2字节保留字节
  285. // 1字节电源状态
  286. s>>site_id>>site_ct
  287. >>*(uint8_t*)&site_tm.tm_sec >>*(uint8_t*)&site_tm.tm_min >>*(uint8_t*)&site_tm.tm_hour >>*(uint8_t*)&site_tm.tm_mday
  288. >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_year
  289. >>skip(2)>>power;
  290. site_tm.tm_year+=2000-1900;
  291. site_tm.tm_mon-=1;
  292. const auto &site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  293. if(!site_ptr)
  294. {
  295. logn_error(1,"未定义分站:net=%s,site=%d", clt->name().c_str(), site_id);
  296. return -1;
  297. }
  298. if(site_ptr->is_abnormal_site())
  299. {
  300. logn_error(1,"分站[%d]天线异常", site_id);
  301. return -1;
  302. }
  303. check_message_time(clt, site_ptr, &site_tm, site_ct, power);
  304. if(clt->type()!=2){
  305. site_ptr->set_client(clt);
  306. site_ptr->on_power_status((power&1)==0);
  307. }
  308. logn_info(1,"分站数据:site_id: %d, site_ct: %d, time: %s, power: %d", site_id, site_ct, tool_time::to_str_ex(&site_tm).c_str(), power);
  309. return site_id;
  310. }
  311. void net_service::parse_data_card()
  312. {
  313. }
  314. float net_service::get_pdoa(int16_t poa[], const double& offset)
  315. {
  316. if(poa == nullptr){
  317. return -10.0;
  318. }
  319. float poa1 = poa[0]*0.001;
  320. float poa2 = poa[1]*0.001;
  321. float poa3 = poa[2]*0.001;
  322. float pdoa_raw = poa2 - poa1 + PI;
  323. while(pdoa_raw >= TPI){
  324. pdoa_raw -= TPI;
  325. }
  326. while(pdoa_raw < 0){
  327. pdoa_raw += TPI;
  328. }
  329. pdoa_raw -= PI;
  330. //90 adjust
  331. float pdoa = pdoa_raw - offset;
  332. if(pdoa < -1*PI){
  333. pdoa += TPI;
  334. }
  335. if(pdoa > PI){
  336. pdoa -= TPI;
  337. }
  338. logn_info(3, "[pdoa] poa1=%.4f, poa2=%.4f, poa3=%.4f, poa_diff=%.4f", poa1, poa2, poa3, pdoa);
  339. return pdoa;
  340. }