net-service.cpp 18 KB

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