net-service.cpp 16 KB

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