net-service.cpp 13 KB

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