net-service.cpp 19 KB

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