net-service.cpp 23 KB

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