net-service.cpp 13 KB

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