net-service.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. site_ptr->m_algo = LDT_TOF;
  97. if(clt->type()!=2){
  98. site_ptr->set_client(clt);
  99. site_ptr->on_power_status((power&1)==0);
  100. //clt->set_site_id(site_id);
  101. }
  102. struct timeval tv;
  103. gettimeofday(&tv,NULL);
  104. uint64_t t = tv.tv_sec*1000 + tv.tv_usec/1000;
  105. int index=0;
  106. while(!is.eof())
  107. {
  108. uint64_t tstamp = t -1000 + 50 + index* 45;
  109. index++;
  110. task*t=task::alloc<message_locinfo>();
  111. message_locinfo&m=t->body<message_locinfo>();
  112. m.m_loc_type = LDT_TOF;
  113. m.m_site_id=site_id;
  114. m.m_time_stamp=tstamp;
  115. m.load(is,false);
  116. //t_site->m_site_data = 0;
  117. t->m_cmd_code=cmd;
  118. t->m_hash_id=m.m_card_id;
  119. m_loc_worker->request(t);
  120. }
  121. }
  122. break;
  123. case CHAR_LOCATEDATA_TOF_OPTIMIZE://tof-优化,大小分站
  124. {
  125. uint16_t site_id=-1;
  126. uint16_t site_ct=-1;
  127. uint32_t site_time_s=0;
  128. uint8_t power=-1;
  129. is>>site_id>>site_ct >>site_time_s >>power;
  130. power=power>>6;
  131. time_t site_time=site_time_s;
  132. auto site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  133. if(!site_ptr)
  134. {
  135. logn_error(1,"在全局分站列表中找不到分站:%s,%d", clt->name().c_str(), site_id);
  136. break;
  137. }
  138. if(site_ptr->is_abnormal_site())
  139. {
  140. logn_error(1,"分站[%d]天线异常", site_id);
  141. break;
  142. }
  143. struct tm site_tm={0};
  144. localtime_r(&site_time, &site_tm);
  145. check_message_time(clt,site_ptr,&site_tm,site_ct,power);
  146. if(clt->type()!=2){
  147. site_ptr->set_client(clt);
  148. site_ptr->on_power_status((power&1)==0);
  149. //clt->set_site_id(site_id);
  150. }
  151. struct timeval tv;
  152. gettimeofday(&tv,NULL);
  153. uint64_t t = tv.tv_sec*1000 + tv.tv_usec/1000;
  154. int index=0;
  155. while(!is.eof())
  156. {
  157. uint64_t tstamp = t -1000 + 50 + index* 45;
  158. index++;
  159. for(auto & tk : message_locinfo::load_opt(is))
  160. {
  161. message_locinfo&m=tk->body<message_locinfo>();
  162. m.m_site_id=site_id;
  163. m.m_time_stamp=tstamp;
  164. tk->m_hash_id=m.m_card_id;
  165. tk->m_cmd_code=cmd;
  166. m_loc_worker->request(tk);
  167. }
  168. }
  169. }
  170. break;
  171. case CHAR_LOCATEDATA_PDOA:
  172. {
  173. int32_t site_id = parse_data_anchor_opt(clt, is);
  174. if(site_id < 0){
  175. break;
  176. }
  177. const auto& site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  178. if(!site_ptr){
  179. logn_error(1,"在全局分站列表中找不到分站:%d", site_id);
  180. break;
  181. }
  182. site_ptr->set_algo(LDT_PDOA);
  183. struct timeval tv;
  184. gettimeofday(&tv, NULL);
  185. uint64_t t = tv.tv_sec*1000 + tv.tv_usec/1000;
  186. int index = 0;
  187. while(!is.eof()){
  188. uint64_t tstamp = t - 1000 + 50 + index*45;
  189. ++index;
  190. task* t = task::alloc<message_pdoa_locinfo>();
  191. message_pdoa_locinfo& m = t->body<message_pdoa_locinfo>();
  192. m.load(is);
  193. float pdoa = 10.0;
  194. float dist = m.m_tof*15.65*2.996*1e-4/site_ptr->m_scale;
  195. if(fabs(m.m_poa[0]) < 10.0 && fabs(m.m_poa[1]) < 10.0){
  196. m.m_site_id = site_id;
  197. m.m_time_stamp = tstamp;
  198. m.m_loc_type = LDT_PDOA;
  199. t->m_cmd_code = cmd;
  200. t->m_hash_id = m.m_card_id;
  201. pdoa = get_pdoa(m.m_poa, site_ptr->m_pdoa_offset);
  202. m_loc_worker->request(t);
  203. }
  204. logn_info(3, "[pdoa] site_id=%d, card_id=%d, dist=%.3f, poa1=%.4f, poa2=%.4f, poa3=%.4f, pdoa=%.4f, pdoa_offset=%.4f", site_id, m.m_card_id, dist, m.m_poa[0], m.m_poa[1], m.m_poa[2], pdoa, site_ptr->m_pdoa_offset);
  205. }
  206. }
  207. break;
  208. case 0x783A://tof 分站时间同步
  209. {
  210. // 从第一个字节开始,分别表示毫秒(2字节)、秒、分、时、天、月、年
  211. unsigned char buf[20]={0,13,0x78,0x3b};
  212. struct timeval tv;
  213. gettimeofday(&tv,0);
  214. struct tm buff={0};
  215. const struct tm*t=localtime_r(&tv.tv_sec,&buff);
  216. int p=4;
  217. buf[p++]=(tv.tv_usec/1000)&0xFF;
  218. buf[p++]=((tv.tv_usec/1000)>>8)&0xFF;
  219. buf[p++]=t->tm_sec;
  220. buf[p++]=t->tm_min;
  221. buf[p++]=t->tm_hour;
  222. buf[p++]=t->tm_mday;
  223. buf[p++]=t->tm_wday;
  224. buf[p++]=t->tm_mon+1;
  225. buf[p++]=t->tm_year-100;
  226. uint16_t ccrc=do_crc(buf+2,11);
  227. buf[p++]=ccrc>>8;
  228. buf[p++]=ccrc&0xff;
  229. std::vector<char> tmp((char*)buf,(char*)buf+15);
  230. clt->send(std::move(tmp));
  231. logn_info(1,"分站时间同步:ip=%s,time=%d-%02d-%02d %02d:%02d:%02d.%03d",
  232. clt->name().c_str(),buf[12]+2000,buf[11],buf[9],buf[8],buf[7],buf[6],buf[5]*256+buf[4]);
  233. }
  234. break;
  235. case CHAR_LOCATEDATA_TDOA_EXTEND: // 0x863b
  236. case CHAR_LOCATEDATA_TDOA_EXTEND_INS: // 0x901b
  237. {
  238. int32_t site_id = parse_data_anchor(clt, is);
  239. if(site_id < 0){
  240. break;
  241. }
  242. const auto& site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  243. if(!site_ptr){
  244. logn_error(1,"在全局分站列表中找不到分站:%d", site_id);
  245. break;
  246. }
  247. site_ptr->set_algo(LDT_TDOA);
  248. while(!is.eof())
  249. {
  250. task* t = task::alloc<message_tdoa_locinfo>();
  251. message_tdoa_locinfo& m = t->body<message_tdoa_locinfo>();
  252. m.m_loc_type = LDT_TDOA;
  253. m.load(is, cmd);
  254. t->m_cmd_code = cmd;
  255. t->m_hash_id = m.m_card_msg.m_id;
  256. m_sync_worker->translate(m);
  257. m_loc_worker->request(t);
  258. }
  259. }
  260. break;
  261. case CHAR_TDOA_READER_SYNC_TIME://time sync 0xa78d
  262. {
  263. message_tdoasync m;
  264. m.load(is);
  265. m_sync_worker->on_message(m);
  266. }
  267. //site_message::on_sync(this,t.m_param1);
  268. break;
  269. case CHAR_VIRTUAL_DATA_PUSH_CMD://虚拟数据链接
  270. {
  271. logn_info(1,"接收到虚拟链接:%s",clt->name().c_str());
  272. clt->set_conn_type(2);
  273. }
  274. break;
  275. case 0x793B: //虚拟推送的心跳测试
  276. case CHAR_LOCATEDATAHIS_TOF_EXTEND://tof his
  277. case CHAR_LOCATEDATAHIS_TDOA_EXTEND://tdoa his
  278. case CHAR_CTRL_READER_CMD://ctrl site message
  279. break;
  280. case CHAR_LIGHT_TCP_STATUS:
  281. {
  282. // 请求红绿灯状态
  283. uint32_t id = 0;
  284. uint8_t type = 0;
  285. is>>id>>type;
  286. logn_info(1, "light info: light_id=%d, type=%d", id, type);
  287. /*auto light_ptr = light_list::instance()->get(id);
  288. if(!light_ptr){
  289. logn_error(1,"在全局信号灯列表中找不到信号灯:%d", id);
  290. break;
  291. }*/
  292. task* t = task::alloc<message_light>();
  293. message_light& m = t->body<message_light>();
  294. t->m_cmd_code = cmd;
  295. m.m_light_id = id;
  296. m.m_type = type;
  297. m_loc_worker->request(t);
  298. }
  299. break;
  300. case CHAR_LIGHT_TCP_HEART:
  301. {
  302. // 红绿灯心跳
  303. uint32_t id = 0;
  304. uint16_t stamp = 0;
  305. uint8_t status = 0;
  306. is>>id>>stamp>>status;
  307. logn_info(1, "light info: light_id=%d, stamp=%d, status=%d", id, stamp, status);
  308. /*auto light_ptr = light_list::instance()->get(id);
  309. if(!light_ptr)
  310. {
  311. logn_error(1,"在全局信号灯列表中找不到信号灯:%d", id);
  312. break;
  313. }*/
  314. task* t = task::alloc<message_light>();
  315. message_light& m = t->body<message_light>();
  316. t->m_cmd_code = cmd;
  317. m.m_light_id = id;
  318. m.m_stamp = stamp;
  319. m.m_status = status;
  320. m_loc_worker->request(t);
  321. }
  322. break;
  323. default:
  324. message_handled=false;
  325. }
  326. }
  327. catch(const std::exception&e)
  328. {
  329. logn_error(1,"分站数据处理失败,将关闭分站连接:%s [%s]",clt->name().c_str(),e.what());
  330. clt->close();
  331. }
  332. if(!message_handled)
  333. {
  334. logn_error(1,"分站数据未被处理,site=%s",clt->name().c_str());
  335. }
  336. }
  337. int32_t net_service::parse_data_anchor(const std::shared_ptr<client>& clt, zistream& s)
  338. {
  339. uint32_t site_id=-1;
  340. uint16_t site_ct=-1;
  341. uint8_t power=-1;
  342. struct tm site_tm={0};
  343. // 4字节分站号
  344. // 2字节时间戳
  345. // 7字节分站时间
  346. // 2字节保留字节
  347. // 1字节电源状态
  348. s>>site_id>>site_ct
  349. >>*(uint8_t*)&site_tm.tm_sec >>*(uint8_t*)&site_tm.tm_min >>*(uint8_t*)&site_tm.tm_hour >>*(uint8_t*)&site_tm.tm_mday
  350. >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_year
  351. >>skip(2)>>power;
  352. site_tm.tm_year+=2000-1900;
  353. site_tm.tm_mon-=1;
  354. const auto &site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  355. if(!site_ptr)
  356. {
  357. logn_error(1,"未定义分站:net=%s,site=%d", clt->name().c_str(), site_id);
  358. return -1;
  359. }
  360. if(site_ptr->is_abnormal_site())
  361. {
  362. logn_error(1,"分站[%d]天线异常", site_id);
  363. return -1;
  364. }
  365. check_message_time(clt, site_ptr, &site_tm, site_ct, power);
  366. if(clt->type()!=2){
  367. site_ptr->set_client(clt);
  368. site_ptr->on_power_status((power&1)==0);
  369. }
  370. 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);
  371. return site_id;
  372. }
  373. // 分站优化协议
  374. int32_t net_service::parse_data_anchor_opt(const std::shared_ptr<client>& clt, zistream& s)
  375. {
  376. // 分站号,2字节
  377. uint16_t site_id = 0;
  378. // 分站时间戳,2字节
  379. uint16_t site_ct = 0;
  380. // 分站时间,4字节
  381. uint32_t site_time_stamp = 0;
  382. // 大小分站所属关系,1字节
  383. uint8_t reverse = 0;
  384. // 状态字节,1字节,包括3部分:供电状态,设备类型,保留
  385. uint8_t power = 0;
  386. struct tm site_tm={0};
  387. s>>site_id>>site_ct>>site_time_stamp>>reverse>>power;
  388. time_t _sts = site_time_stamp;
  389. localtime_r(&_sts, &site_tm);
  390. const auto &site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  391. if(!site_ptr)
  392. {
  393. logn_error(1,"未定义分站:net=%s, site=%d", clt->name().c_str(), site_id);
  394. return -1;
  395. }
  396. // pdoa分站不检查天线是否相等
  397. /*if(site_ptr->is_abnormal_site())
  398. {
  399. logn_error(1,"分站[%d]天线异常", site_id);
  400. return -1;
  401. }*/
  402. //check_message_time(clt, site_ptr, &site_tm, site_ct, power);
  403. site_ptr->m_device_type_id = (power>>3)&0x07;
  404. if(clt->type()!=2){
  405. site_ptr->set_client(clt);
  406. site_ptr->on_power_status((power&1)==0);
  407. }
  408. 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);
  409. return site_id;
  410. }
  411. void net_service::parse_data_card()
  412. {
  413. }
  414. float net_service::get_pdoa(float poa[], const double& offset)
  415. {
  416. if(poa == nullptr){
  417. return -10.0;
  418. }
  419. float poa1 = poa[0];
  420. float poa2 = poa[1];
  421. //float poa3 = poa[2];
  422. float pdoa = poa2 - poa1 + PI - offset;
  423. while(pdoa >= TPI){
  424. pdoa -= TPI;
  425. }
  426. while(pdoa < 0){
  427. pdoa += TPI;
  428. }
  429. pdoa -= PI;
  430. //90 adjust
  431. /*float pdoa = pdoa_raw - offset;
  432. if(pdoa < -1*PI){
  433. pdoa += TPI;
  434. }
  435. if(pdoa > PI){
  436. pdoa -= TPI;
  437. }*/
  438. //logn_info(3, "[pdoa] poa1=%.4f, poa2=%.4f, poa3=%.4f, pdoa=%.4f", poa1, poa2, poa3, pdoa);
  439. return pdoa;
  440. }