net-service.cpp 15 KB

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