net-service.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. #include "module_service/module_traffic_light_manager.h"
  22. net_service::net_service()
  23. {
  24. m_loc_worker=worker::instance();
  25. m_sync_worker=tdoa_sync::instance();
  26. }
  27. net_service::~net_service()
  28. {
  29. if(m_loc_worker)
  30. m_loc_worker->stop();
  31. }
  32. void net_service::on_timer()
  33. {
  34. visit_site_status vss;
  35. sit_list::instance()->accept(vss);
  36. traffic_light_manager::instance()->visit_light_status();
  37. }
  38. void net_service::on_connect(const std::shared_ptr<client>& clt)
  39. {
  40. }
  41. 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)
  42. {
  43. bool result=true;
  44. char timebuf[64]{0};
  45. strftime(timebuf,64,"%F %T",p_site_tm);
  46. logn_info(1,"分站数据信息:net=%s,site=%d,tm=%s,sct=%d,power=%s", clt->name().c_str(), site_ptr->m_id,timebuf,
  47. site_ct, site_ptr->m_power_check_enable?((power&1)?"ac":"dc"):"??");
  48. time_t site_time=mktime(p_site_tm);
  49. double diff=difftime(site_time, site_ptr->last_site_time());
  50. if(diff<-6) //允许2秒的时间抖动 3
  51. {
  52. logn_error(1,"分站时间回退,数据将被丢弃:net=%s,site=%d,diff=%d",clt->name().c_str(),site_ptr->m_id,(int)diff);
  53. result=false;
  54. }
  55. diff=difftime(site_time, time(nullptr));
  56. if(fabs(diff)>6) //3
  57. {
  58. logn_error(1,"分站时间与服务器时间相差太大:%s,site=%d,diff=%d",clt->name().c_str(),site_ptr->m_id,(int)diff);
  59. result=false;
  60. }
  61. site_ptr->set_site_time(site_time);
  62. return result;
  63. }
  64. void net_service::on_message(const std::shared_ptr<client> &clt,const char*data,size_t len)
  65. {
  66. bool message_handled=true;
  67. try
  68. {
  69. zistream is(data,len-2);
  70. uint16_t cmd;
  71. is>>skip(2)>>cmd;
  72. switch(cmd)
  73. {
  74. case CHAR_LOCATEDATA_TOF_EXTEND://tof-扩展
  75. {
  76. uint32_t site_id=-1;
  77. uint16_t site_ct=-1;
  78. uint8_t power=-1;
  79. struct tm site_tm={0};
  80. is>>site_id>>site_ct
  81. >>*(uint8_t*)&site_tm.tm_sec >>*(uint8_t*)&site_tm.tm_min >>*(uint8_t*)&site_tm.tm_hour >>*(uint8_t*)&site_tm.tm_mday
  82. >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_year
  83. >>skip(2)>>power;
  84. site_tm.tm_year+=2000-1900;
  85. site_tm.tm_mon-=1;
  86. const auto &site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  87. if(!site_ptr)
  88. {
  89. logn_error(1,"未定义分站:net=%s,site=%d", clt->name().c_str(), site_id);
  90. break;
  91. }
  92. if(site_ptr->is_abnormal_site())
  93. {
  94. logn_error(1,"分站[%d]天线异常", site_id);
  95. break;
  96. }
  97. check_message_time(clt,site_ptr,&site_tm,site_ct,power);
  98. site_ptr->m_algo = LDT_TOF;
  99. if(clt->type()!=2){
  100. site_ptr->set_client(clt);
  101. site_ptr->on_power_status((power&1)==0);
  102. //clt->set_site_id(site_id);
  103. }
  104. struct timeval tv;
  105. gettimeofday(&tv,NULL);
  106. uint64_t t = tv.tv_sec*1000 + tv.tv_usec/1000;
  107. int index=0;
  108. while(!is.eof())
  109. {
  110. uint64_t tstamp = t -1000 + 50 + index* 45;
  111. index++;
  112. task*t=task::alloc<message_locinfo>();
  113. message_locinfo&m=t->body<message_locinfo>();
  114. m.m_loc_type = LDT_TOF;
  115. m.m_site_id=site_id;
  116. m.m_time_stamp=tstamp;
  117. m.load(is,false);
  118. //t_site->m_site_data = 0;
  119. t->m_cmd_code=cmd;
  120. t->m_hash_id=m.m_card_id;
  121. m_loc_worker->request(t);
  122. }
  123. }
  124. break;
  125. case CHAR_LOCATEDATA_TOF_OPTIMIZE://tof-优化,大小分站
  126. {
  127. uint16_t site_id=-1;
  128. uint16_t site_ct=-1;
  129. uint32_t site_time_s=0;
  130. uint8_t power=-1;
  131. is>>site_id>>site_ct >>site_time_s >>power;
  132. power=power>>6;
  133. time_t site_time=site_time_s;
  134. auto site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  135. if(!site_ptr)
  136. {
  137. logn_error(1,"在全局分站列表中找不到分站:%s,%d", clt->name().c_str(), site_id);
  138. break;
  139. }
  140. if(site_ptr->is_abnormal_site())
  141. {
  142. logn_error(1,"分站[%d]天线异常", site_id);
  143. break;
  144. }
  145. struct tm site_tm={0};
  146. localtime_r(&site_time, &site_tm);
  147. check_message_time(clt,site_ptr,&site_tm,site_ct,power);
  148. if(clt->type()!=2){
  149. site_ptr->set_client(clt);
  150. site_ptr->on_power_status((power&1)==0);
  151. //clt->set_site_id(site_id);
  152. }
  153. struct timeval tv;
  154. gettimeofday(&tv,NULL);
  155. uint64_t t = tv.tv_sec*1000 + tv.tv_usec/1000;
  156. int index=0;
  157. while(!is.eof())
  158. {
  159. uint64_t tstamp = t -1000 + 50 + index* 45;
  160. index++;
  161. for(auto & tk : message_locinfo::load_opt(is))
  162. {
  163. message_locinfo&m=tk->body<message_locinfo>();
  164. m.m_site_id=site_id;
  165. m.m_time_stamp=tstamp;
  166. tk->m_hash_id=m.m_card_id;
  167. tk->m_cmd_code=cmd;
  168. m_loc_worker->request(tk);
  169. }
  170. }
  171. }
  172. break;
  173. case CHAR_LOCATEDATA_PDOA:
  174. {
  175. int32_t site_id = parse_data_anchor_opt(clt, is);
  176. if(site_id < 0){
  177. break;
  178. }
  179. const auto& site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  180. if(!site_ptr){
  181. logn_error(1,"在全局分站列表中找不到分站:%d", site_id);
  182. break;
  183. }
  184. if(clt->type() != 2){
  185. site_ptr->set_client(clt);
  186. //site_ptr->on_power_status((power&1)==0);
  187. }
  188. site_ptr->set_algo(LDT_PDOA);
  189. //log_info("[scale_test] %.2f", site_ptr->m_scale);
  190. struct timeval tv;
  191. gettimeofday(&tv, NULL);
  192. uint64_t cur_time = tv.tv_sec*1000 + tv.tv_usec/1000;
  193. int index = 0;
  194. while(!is.eof()){
  195. uint64_t tstamp = cur_time - 1000 + 50 + index*45;
  196. ++index;
  197. task* t = task::alloc<message_pdoa_locinfo>();
  198. message_pdoa_locinfo& m = t->body<message_pdoa_locinfo>();
  199. m.load(is);
  200. float pdoa = 10.0;
  201. float dist = m.m_tof*15.65*2.996*1e-4;
  202. m.m_site_id = site_id;
  203. m.m_time_stamp = tstamp;
  204. m.m_loc_type = LDT_PDOA;
  205. t->m_cmd_code = cmd;
  206. t->m_hash_id = m.m_card_id;
  207. pdoa = tool_other::get_pdoa(m.m_poa, site_ptr->m_pdoa_offset);
  208. logn_info(3, "[pdoa] site_id=%d, card_id=%d, ct=%d, dist=%.3f, rav=%d, poa1=%.4f, poa2=%.4f, poa3=%.4f, pdoa=%.4f, pdoa_offset=%.4f", site_id, m.m_card_id, m.m_card_ct, dist, m.m_rav, m.m_poa[0], m.m_poa[1], m.m_poa[2], pdoa, site_ptr->m_pdoa_offset);
  209. if(m.m_poa[1] == 10.0 || dist < 0.0001 ){
  210. continue;
  211. }
  212. m_loc_worker->request(t);
  213. }
  214. }
  215. break;
  216. case CHAR_NET_CALI_TIME://tof 分站时间同步
  217. {
  218. // 网络设备校时
  219. net_cali_time(clt);
  220. }
  221. break;
  222. case CHAR_CAN_CALI_TIME:
  223. {
  224. // can设备校时
  225. uint32_t rid=0;
  226. is>>rid;
  227. can_cali_time(clt, rid);
  228. }
  229. break;
  230. case CHAR_LOCATEDATA_TDOA_EXTEND: // 0x863b
  231. case CHAR_LOCATEDATA_TDOA_EXTEND_INS: // 0x901b
  232. {
  233. int32_t site_id = parse_data_anchor(clt, is);
  234. if(site_id < 0){
  235. break;
  236. }
  237. const auto& site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  238. if(!site_ptr){
  239. logn_error(1,"在全局分站列表中找不到分站:%d", site_id);
  240. break;
  241. }
  242. site_ptr->set_algo(LDT_TDOA);
  243. while(!is.eof())
  244. {
  245. task* t = task::alloc<message_tdoa_locinfo>();
  246. message_tdoa_locinfo& m = t->body<message_tdoa_locinfo>();
  247. m.m_loc_type = LDT_TDOA;
  248. m.load(is, cmd);
  249. t->m_cmd_code = cmd;
  250. t->m_hash_id = m.m_card_msg.m_id;
  251. m_sync_worker->translate(m);
  252. m_loc_worker->request(t);
  253. }
  254. }
  255. break;
  256. case CHAR_TDOA_READER_SYNC_TIME://time sync 0xa78d
  257. {
  258. message_tdoasync m;
  259. m.load(is);
  260. m_sync_worker->on_message(m);
  261. }
  262. //site_message::on_sync(this,t.m_param1);
  263. break;
  264. case CHAR_VIRTUAL_DATA_PUSH_CMD://虚拟数据链接
  265. {
  266. logn_info(1,"接收到虚拟链接:%s",clt->name().c_str());
  267. clt->set_conn_type(2);
  268. }
  269. break;
  270. case CHAR_READER_HEART:
  271. {
  272. //通信基站心跳
  273. uint32_t id = 0;
  274. uint16_t site_ct = 0;
  275. uint16_t reserve = 0;
  276. is>>id>>site_ct>>reserve;
  277. auto site_ptr = sit_list::instance()->get(id);
  278. if(!site_ptr){
  279. logn_error(1,"在全局分站列表中找不到分站:%d", id);
  280. break;
  281. }
  282. site_ptr->m_device_type_id = 0x08;
  283. }
  284. break;
  285. case 0x793B: //虚拟推送的心跳测试
  286. case CHAR_LOCATEDATAHIS_TOF_EXTEND://tof his
  287. case CHAR_LOCATEDATAHIS_TDOA_EXTEND://tdoa his
  288. case CHAR_CTRL_READER_CMD://ctrl site message
  289. break;
  290. case THIRD_PARTY_CHAR_LIGHT_REQ_STATE:
  291. {
  292. // 红绿灯请求状态指令
  293. uint32_t id = 0;
  294. uint8_t dev_type = 0;
  295. is>>id>>dev_type;
  296. logn_info(4, "[traffic_light] light request state, light_id=%d, dev_type=%d", id, dev_type);
  297. // 找灯,并下发指令
  298. auto light_ptr = traffic_light_manager::instance()->get(id);
  299. if(light_ptr)
  300. {
  301. //traffic_light_manager::instance()->send_light_ctrl(id, DT_LIGHT, light_ptr->m_state);
  302. traffic_light_manager::instance()->send_light_data(id, DT_LIGHT, light_shape::green_all_on);
  303. }else{
  304. traffic_light_manager::instance()->send_light_data(id, DT_LIGHT, light_shape::green_spark);
  305. }
  306. }
  307. break;
  308. case THIRD_PARTY_CHAR_LIGHT_HEART:
  309. {
  310. // 红绿灯心跳
  311. uint32_t id = 0;
  312. uint16_t stamp = 0;
  313. uint8_t status = 0;
  314. is>>id>>stamp>>status;
  315. logn_info(4, "[traffic_light] light heart message, light_id=%d, stamp=%d, status=%d", id, stamp, status);
  316. logn_info(1, "红绿灯数据 : light_id=%d, ct=%d", id, stamp);
  317. auto light_ptr = traffic_light_manager::instance()->get(id);
  318. if(!light_ptr)
  319. {
  320. logn_error(1,"在全局信号灯列表中找不到信号灯:%d", id);
  321. break;
  322. }
  323. //log_info("[traffic_light] light client type=%d", clt->type());
  324. if(clt->type() != 2){
  325. light_ptr->set_client(clt);
  326. }
  327. light_ptr->m_state = status;
  328. light_ptr->m_rec_time = time(0);
  329. /*if((stamp%10) == 0){
  330. int shape = (status == light_shape::red_spark)?light_shape::green_spark:light_shape::red_spark;
  331. traffic_light_manager::instance()->send_light_ctrl(id, 0x05, shape);
  332. }*/
  333. }
  334. break;
  335. default:
  336. message_handled=false;
  337. }
  338. }
  339. catch(const std::exception&e)
  340. {
  341. logn_error(1,"分站数据处理失败,将关闭分站连接:%s [%s]",clt->name().c_str(),e.what());
  342. clt->close();
  343. }
  344. if(!message_handled)
  345. {
  346. logn_error(1,"分站数据未被处理,site=%s",clt->name().c_str());
  347. }
  348. }
  349. int32_t net_service::parse_data_anchor(const std::shared_ptr<client>& clt, zistream& s)
  350. {
  351. uint32_t site_id=-1;
  352. uint16_t site_ct=-1;
  353. uint8_t power=-1;
  354. struct tm site_tm={0};
  355. // 4字节分站号
  356. // 2字节时间戳
  357. // 7字节分站时间
  358. // 2字节保留字节
  359. // 1字节电源状态
  360. s>>site_id>>site_ct
  361. >>*(uint8_t*)&site_tm.tm_sec >>*(uint8_t*)&site_tm.tm_min >>*(uint8_t*)&site_tm.tm_hour >>*(uint8_t*)&site_tm.tm_mday
  362. >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_year
  363. >>skip(2)>>power;
  364. site_tm.tm_year+=2000-1900;
  365. site_tm.tm_mon-=1;
  366. const auto &site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  367. if(!site_ptr)
  368. {
  369. logn_error(1,"未定义分站:net=%s,site=%d", clt->name().c_str(), site_id);
  370. return -1;
  371. }
  372. if(site_ptr->is_abnormal_site())
  373. {
  374. logn_error(1,"分站[%d]天线异常", site_id);
  375. return -1;
  376. }
  377. check_message_time(clt, site_ptr, &site_tm, site_ct, power);
  378. if(clt->type()!=2){
  379. site_ptr->set_client(clt);
  380. site_ptr->on_power_status((power&1)==0);
  381. }
  382. 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);
  383. return site_id;
  384. }
  385. // 分站优化协议
  386. int32_t net_service::parse_data_anchor_opt(const std::shared_ptr<client>& clt, zistream& s)
  387. {
  388. // 分站号,2字节
  389. uint16_t site_id = 0;
  390. // 分站时间戳,2字节
  391. uint16_t site_ct = 0;
  392. // 分站时间,4字节
  393. uint32_t site_time_stamp = 0;
  394. // 大小分站所属关系,1字节
  395. uint8_t reverse = 0;
  396. // 状态字节,1字节,包括3部分:供电状态,设备类型,保留
  397. uint8_t power = 0;
  398. struct tm site_tm={0};
  399. s>>site_id>>site_ct>>site_time_stamp>>reverse>>power;
  400. time_t _sts = site_time_stamp;
  401. localtime_r(&_sts, &site_tm);
  402. const auto &site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
  403. if(!site_ptr)
  404. {
  405. logn_error(1,"未定义分站:net=%s, site=%d", clt->name().c_str(), site_id);
  406. return -1;
  407. }
  408. // pdoa分站不检查天线是否相等
  409. /*if(site_ptr->is_abnormal_site())
  410. {
  411. logn_error(1,"分站[%d]天线异常", site_id);
  412. return -1;
  413. }*/
  414. site_ptr->m_device_type_id = (power>>3)&0x07;
  415. if(!check_message_time(clt, site_ptr, &site_tm, site_ct, power)){
  416. // 分站时间异常,直接校时
  417. switch(site_ptr->m_device_type_id)
  418. {
  419. case 0:
  420. can_cali_time(clt, site_id);
  421. break;
  422. case 1:
  423. net_cali_time(clt);
  424. default:
  425. net_cali_time(clt);
  426. break;
  427. }
  428. return -1;
  429. }
  430. if(clt->type()!=2){
  431. site_ptr->set_client(clt);
  432. site_ptr->on_power_status((power&1)==0);
  433. }
  434. uint64_t tt = site_time_stamp;
  435. tt *= 1000;
  436. logn_info(1,"分站数据:site_id: %d, site_ct: %d, time_t: %llu, time: %s, power: %d, device_type: %d", site_id, site_ct, tt, tool_time::to_str_ex(tt).c_str(), power, site_ptr->m_device_type_id);
  437. return site_id;
  438. }
  439. void net_service::net_cali_time(const std::shared_ptr<client>& clt)
  440. {
  441. // 从第一个字节开始,分别表示毫秒(2字节)、秒、分、时、天、月、年
  442. unsigned char buf[20]={0,13,0x78,0x3b};
  443. struct timeval tv;
  444. gettimeofday(&tv,0);
  445. struct tm buff={0};
  446. const struct tm*t=localtime_r(&tv.tv_sec,&buff);
  447. int p=4;
  448. buf[p++]=(tv.tv_usec/1000)&0xFF;
  449. buf[p++]=((tv.tv_usec/1000)>>8)&0xFF;
  450. buf[p++]=t->tm_sec+1; // 由于硬件扫描方式会慢1秒,给补偿1秒
  451. buf[p++]=t->tm_min;
  452. buf[p++]=t->tm_hour;
  453. buf[p++]=t->tm_mday;
  454. buf[p++]=t->tm_wday;
  455. buf[p++]=t->tm_mon+1;
  456. buf[p++]=t->tm_year%100;
  457. uint16_t ccrc=do_crc(buf+2,11);
  458. buf[p++]=ccrc>>8;
  459. buf[p++]=ccrc&0xff;
  460. std::vector<char> tmp((char*)buf,(char*)buf+15);
  461. clt->send(std::move(tmp));
  462. logn_info(1,"分站时间同步:ip=%s,time=%d-%02d-%02d %02d:%02d:%02d.%03d",
  463. clt->name().c_str(),buf[12]+2000,buf[11],buf[9],buf[8],buf[7],buf[6],buf[5]*256+buf[4]);
  464. }
  465. void net_service::can_cali_time(const std::shared_ptr<client>& clt, const int& reader_id)
  466. {
  467. unsigned char buf[30]={0,17,0x78,0x4b};
  468. struct timeval tv;
  469. gettimeofday(&tv,0);
  470. struct tm buff = {0};
  471. const struct tm *t = localtime_r(&tv.tv_sec,&buff);
  472. int p=4;
  473. for(int i = sizeof(uint32_t) - 1; i >= 0; --i)
  474. {
  475. buf[p++] = ((reader_id>>(i*8))&0xFF);
  476. }
  477. buf[p++]=(tv.tv_usec/1000)&0xFF;
  478. buf[p++]=((tv.tv_usec/1000)>>8)&0xFF;
  479. buf[p++]=t->tm_sec;
  480. buf[p++]=t->tm_min;
  481. buf[p++]=t->tm_hour;
  482. buf[p++]=t->tm_mday;
  483. buf[p++]=t->tm_wday;
  484. buf[p++]=t->tm_mon+1;
  485. buf[p++]=t->tm_year%100;
  486. uint16_t ccrc=do_crc(buf+2,15);
  487. buf[p++]=ccrc>>8;
  488. buf[p++]=ccrc&0xff;
  489. std::vector<char> tmp((char*)buf,(char*)buf+19);
  490. clt->send(std::move(tmp));
  491. logn_info(1,"分站时间同步:site_id=%d, ip=%s, p=%d, time=%d-%02d-%02d %02d:%02d:%02d.%03d",
  492. reader_id, clt->name().c_str(), p, buf[16]+2000, buf[15], buf[13], buf[12], buf[11], buf[10], buf[9]*256+buf[8]);
  493. }