message.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. #include <math.h>
  2. #include "zstream.h"
  3. #include "message.h"
  4. #include "log.h"
  5. #include "common_tool.h"
  6. #include "card.h"
  7. //找到一个办法进行兼容
  8. void message_locinfo::zero_this()
  9. {
  10. m_site_time=
  11. m_tof=
  12. m_card_type=
  13. m_card_id=
  14. m_card_ct=
  15. m_batty_status=
  16. m_callinfo=
  17. m_rav=
  18. m_acc=
  19. m_ant_id=
  20. m_sync_ct=
  21. m_rssi=0;
  22. m_distance = 0.0;
  23. }
  24. task* message_locinfo::clone(message_locinfo* ml)
  25. {
  26. task* t = task::alloc<message_locinfo>();
  27. message_locinfo& m = t->body<message_locinfo>();
  28. m.zero_this();
  29. m.m_time_stamp = ml->m_time_stamp;
  30. m.m_site_time = ml->m_site_time;
  31. m.m_tof = ml->m_tof;
  32. m.m_site_id = ml->m_site_id;
  33. m.m_card_type = ml->m_card_type;
  34. m.m_card_id = ml->m_card_id;
  35. m.m_card_ct = ml->m_card_ct;
  36. m.m_batty_status = ml->m_batty_status;
  37. m.m_callinfo = ml->m_callinfo;
  38. m.m_rav = ml->m_rav;
  39. m.m_acc = ml->m_acc;
  40. m.m_ant_id = ml->m_ant_id;
  41. m.m_sync_ct = ml->m_sync_ct;
  42. m.m_rssi = ml->m_rssi;
  43. return std::move(t);
  44. }
  45. void message_locinfo::load(zistream&is,bool tdoa)
  46. {
  47. zero_this();
  48. uint8_t b;
  49. uint32_t i;
  50. //卡类型、卡号、CT、电池状态
  51. is>>b>>m_card_id>>m_card_ct>>m_batty_status;
  52. if(m_card_id & 0xFFFF0000)
  53. log_warn("card_id=%#X,CT=%d,大于0xFFFF.",m_card_id,m_card_ct);
  54. m_card_id &= 0xFFFF; //卡号不会大于65535,分站上传数据的bug
  55. m_card_type=b;
  56. //m_batty_status&=0x03;
  57. is>>b;
  58. if(m_card_type==1)
  59. {
  60. m_callinfo=b;
  61. }
  62. else
  63. {
  64. m_rav=((b&0x80)?-1.:1.)*(b&0x7f)*3;
  65. }
  66. //加速度
  67. is>>b;
  68. const auto &c=card_list::instance()->get(tool_other::type_id_to_u64(m_card_type,m_card_id));
  69. if(m_card_type == 1 ||(c && tool_other::is_coal_or_driving(m_card_type,c->get_vehicle_type_id())))
  70. m_acc=b;
  71. else
  72. m_acc=((b&0x80)?-1.:1.)*(b&0x7f)*0.01;
  73. //TOF
  74. is>>b>>i;
  75. m_tof=b;
  76. m_tof=(m_tof<<32)|i;
  77. //天线号
  78. is>>m_ant_id;
  79. --m_ant_id;
  80. if(tdoa)
  81. {
  82. //同步序号,冲击响应丢弃
  83. is>>m_sync_ct>>skip(2);
  84. }
  85. //信号电平值
  86. uint16_t sp1=0,sp2=0;
  87. is>>sp1>>sp2;
  88. m_rssi=10*log10(1.*sp1*(1<<17)/pow(sp2-64.,2))-121.74;
  89. logn_info(3, "[tof] timestamp=%llu, type=%d, card=%d, site=%d, ct=%d, bat=%#X, acc=%d, tof=%llu, ant_id=%d, spq=%d",
  90. m_time_stamp, m_card_type, m_card_id, m_site_id, m_card_ct, m_batty_status, m_acc, m_tof, m_ant_id, m_rssi);
  91. }
  92. //优化协议数据解析
  93. std::vector<task*> message_locinfo::load_opt(zistream&is)
  94. {
  95. std::vector<task*> rc;
  96. task* t=task::alloc<message_locinfo>();
  97. message_locinfo&m=t->body<message_locinfo>();
  98. m.zero_this();
  99. uint8_t b;
  100. uint16_t card_id;
  101. //卡类型、卡号、CT、电池状态
  102. is>>b>>card_id>>m.m_card_ct;
  103. m.m_card_type=b&0xF;
  104. m.m_batty_status=b>>4;
  105. m.m_card_id = card_id;
  106. is>>b;
  107. if(m.m_card_type==1)
  108. {
  109. m.m_callinfo=b;
  110. }
  111. else
  112. {
  113. m.m_rav=((b&0x80)?-1.:1.)*(b&0x7f)*3;
  114. }
  115. //加速度
  116. is>>b;
  117. auto c=card_list::instance()->get(tool_other::type_id_to_u64(m.m_card_type,m.m_card_id));
  118. if(m.m_card_type == 1 ||(c && tool_other::is_coal_or_driving(m.m_card_type,c->get_vehicle_type_id())))
  119. m.m_acc=b;
  120. else
  121. m.m_acc=((b&0x80)?-1.:1.)*(b&0x7f)*0.01;
  122. //天线
  123. is>>b;
  124. m.m_ant_id = b-1;
  125. //分站RSSI
  126. int8_t val;
  127. is>>val;
  128. m.m_rssi = val;
  129. //天线1距离
  130. uint16_t d;
  131. is>>d;
  132. m.m_distance = d*0.02;
  133. task* t2 = message_locinfo::clone(&m);
  134. message_locinfo& m2 = t2->body<message_locinfo>();
  135. m2.m_ant_id = m.m_ant_id==0?1:0;
  136. //分站RSSI
  137. is>>val;
  138. m2.m_rssi = val;
  139. //天线2距离
  140. is>>d;
  141. m2.m_distance = d*0.02;
  142. log_info("load_opt: type=%d, card_id=%d,ct=%d, callinfo=%d, rav=%d, acc=%d, ant1=%d, tof1=%.2f, spq1=%d, ant2=%d, tof2=%.2f, sqp2=%d",m.m_card_type,m.m_card_id,m.m_card_ct,m.m_callinfo,m.m_rav,m.m_acc,m.m_ant_id,m.m_distance,m.m_rssi,m2.m_ant_id,m2.m_distance,m2.m_rssi);
  143. rc.push_back(t);
  144. rc.push_back(t2);
  145. return std::move(rc);
  146. }
  147. void message_tdoasync::zero_this()
  148. {
  149. m_local_site_id=
  150. m_parent_site_id=
  151. m_local_ant_id=
  152. m_parent_ant_id=
  153. m_sync_num=
  154. m_local_level=
  155. m_recv_time=
  156. m_root_site_id =
  157. m_root_ant_id =
  158. m_send_time=0;
  159. }
  160. void message_tdoasync::load(zistream&is)
  161. {
  162. zero_this();
  163. uint8_t b;
  164. //本地分站ID和天线ID
  165. is>>m_local_site_id>>b;
  166. m_local_ant_id=b;
  167. //上级分站ID和天线ID
  168. is>>m_parent_site_id>>b;
  169. m_parent_ant_id=b;
  170. //根分站CT和本机级别
  171. is>>m_sync_num>>m_local_level;
  172. uint32_t i;
  173. //本级发出的时间
  174. is>>b>>i;
  175. m_send_time=b;
  176. m_send_time=(m_send_time<<32)|i;
  177. //本级收到的时间
  178. is>>b>>i;
  179. m_recv_time=b;
  180. m_recv_time=(m_recv_time<<32)|i;
  181. logn_info(4, "local_id: %d, upper_id: %d, sync_num: %d, level: %d, send: %llu, recv: %llu",
  182. m_local_site_id,
  183. m_parent_site_id,
  184. m_sync_num,
  185. m_local_level,
  186. m_send_time,
  187. m_recv_time
  188. );
  189. }
  190. void message_tdoa_locinfo::zero_this()
  191. {
  192. m_site_msg.clear();
  193. m_card_msg.clear();
  194. }
  195. void message_tdoa_locinfo::load(zistream& is, uint16_t& cmd)
  196. {
  197. zero_this();
  198. uint8_t b;
  199. uint32_t i;
  200. //卡类型、卡号、CT、电池状态
  201. is>>b>>m_card_msg.m_id>>m_card_msg.m_time_stamp>>m_card_msg.m_battery_status;
  202. if(m_card_msg.m_id & 0xFFFF0000)
  203. log_warn("card_id=%#X,CT=%d,大于0xFFFF.",m_card_msg.m_id,m_card_msg.m_time_stamp);
  204. m_card_msg.m_id &= 0xFFFF; //卡号不会大于65535,分站上传数据的bug
  205. m_card_msg.m_type = b;
  206. is>>b;
  207. if(m_card_msg.m_type == 1)
  208. {
  209. m_card_msg.m_call_info = b;
  210. }
  211. else
  212. {
  213. m_card_msg.m_rav=((b&0x80)?-1.:1.)*(b&0x7f)*3;
  214. }
  215. //加速度
  216. is>>b;
  217. const auto &c=card_list::instance()->get(tool_other::type_id_to_u64(m_card_msg.m_type, m_card_msg.m_id));
  218. if(m_card_msg.m_type == 1 ||(c && tool_other::is_coal_or_driving(m_card_msg.m_type, c->get_vehicle_type_id())))
  219. m_card_msg.m_acc = b;
  220. else
  221. m_card_msg.m_acc = ((b&0x80)?-1.:1.)*(b&0x7f)*0.01;
  222. //定位时间戳
  223. is>>b>>i;
  224. m_card_msg.m_loc_stamp = b;
  225. m_card_msg.m_loc_stamp = (m_card_msg.m_loc_stamp<<32)|i;
  226. //天线号
  227. is>>m_card_msg.m_ant_id;
  228. --m_card_msg.m_ant_id;
  229. //序列号
  230. is>>m_card_msg.m_sync_num;
  231. //脉冲信道响应值
  232. is>>m_card_msg.m_rssi;
  233. if(CHAR_LOCATEDATA_TDOA_EXTEND == cmd || CHAR_LOCATEDATA_TDOA_EXTEND_INS == cmd){
  234. //信号电平值
  235. uint16_t sp1 = 0, sp2 = 0;
  236. is>>sp1>>sp2;
  237. m_card_msg.m_rssi = 10*log10(1.*sp1*(1<<17)/pow(sp2-64.,2))-121.74;
  238. m_card_msg.m_strength = sp1;
  239. m_card_msg.m_rxpacc = sp2;
  240. }
  241. if(CHAR_LOCATEDATA_TDOA_EXTEND_INS == cmd){
  242. is>>m_card_msg.m_acc_data[0]
  243. >>m_card_msg.m_acc_data[1]
  244. >>m_card_msg.m_acc_data[2]
  245. >>m_card_msg.m_rav_data[0]
  246. >>m_card_msg.m_rav_data[1]
  247. >>m_card_msg.m_rav_data[2]
  248. >>m_card_msg.m_walk_steps
  249. >>m_card_msg.m_jump_counts
  250. >>m_card_msg.m_hang_time
  251. >>m_card_msg.m_hang_height;
  252. }
  253. log_info("[tdoa] type=%d, card=%d, site=%d, ct=%d, bat=%#X, acc=%d, fly_time=%llu, ant_id=%d, spq=%d",
  254. m_card_msg.m_type,
  255. m_card_msg.m_id,
  256. m_site_msg.m_site_id,
  257. m_card_msg.m_time_stamp,
  258. m_card_msg.m_battery_status,
  259. m_card_msg.m_acc,
  260. m_card_msg.m_loc_stamp,
  261. m_card_msg.m_ant_id,
  262. m_card_msg.m_rssi
  263. );
  264. }
  265. void message_pdoa_locinfo::zero_this()
  266. {
  267. m_time_stamp =
  268. m_site_time =
  269. m_tof =
  270. m_site_id =
  271. m_card_type =
  272. m_card_id =
  273. m_card_ct =
  274. m_batty_status =
  275. m_callinfo =
  276. m_acc =
  277. m_ant_id =
  278. m_rssi = 0;
  279. m_rav = 0;
  280. m_poa[0] =
  281. m_poa[1] =
  282. m_poa[2] = 0.0;
  283. }
  284. void message_pdoa_locinfo::load(zistream& is)
  285. {
  286. zero_this();
  287. uint8_t b;
  288. // 1字节,低4位为卡类型
  289. is>>b;
  290. m_card_type = b&0x0F;
  291. //m_batty_status = (b>>4)&0xFF;
  292. // 电量,1字节
  293. is>>b;
  294. m_batty_status = b;
  295. //2字节卡号,2字节卡的ct号
  296. uint16_t id;
  297. is>>id>>m_card_ct;
  298. m_card_id = id;
  299. /*if(m_card_ct % 5 == 4){
  300. m_batty_status = (m_batty_status>>4)&0xFF;
  301. }*/
  302. //角速度值:人卡车卡表示含义不同
  303. is>>b;
  304. if(m_card_type == 1){
  305. // 1表示呼救
  306. m_callinfo = ((b&0x80) >> 7);
  307. m_rav = m_callinfo;
  308. }else{
  309. m_rav = ((b&0x80)?-1.:1.)*(b&0x7F)*3;
  310. }
  311. //加速度
  312. is>>b;
  313. if(m_card_type == 1 || m_card_type == 4 || m_card_type == 5){
  314. m_acc = b;
  315. }else{
  316. m_acc = ((b&0x80)?-1.:1.)*(b&0x7F)*0.01;
  317. }
  318. //信号电平值,tof测距结果
  319. int8_t rssi = 0;
  320. uint32_t tof = 0;
  321. uint8_t high_tof = 0;
  322. is>>rssi>>high_tof>>tof;
  323. m_rssi = rssi;
  324. m_tof = high_tof;
  325. m_tof = (m_tof<<32) | tof;
  326. is>>m_ant_id;
  327. --m_ant_id;
  328. uint16_t _poa = 0;
  329. for(int i = 0; i < 3; ++i){
  330. is>>_poa;
  331. m_poa[i] = (float)((((short)_poa)*1.0)/1000.0);
  332. }
  333. if(tof > 0){
  334. logn_info(3,"[pdoa] card_type=%d, card_id=%d, ct=%d, status=%d, call_type=%d, rav=%d, acc=%d, tof=%llu, ant_id=%d, rssi=%d, poa1=%.2f, poa2=%.2f, poa3=%.2f", m_card_type, m_card_id, m_card_ct, m_batty_status, m_callinfo, m_rav, m_acc, m_tof, m_ant_id, m_rssi, m_poa[0], m_poa[1], m_poa[2]);
  335. }
  336. }
  337. // 数据重置
  338. void message_ins_locinfo::zero_this()
  339. {
  340. m_time_stamp =
  341. m_site_time =
  342. m_tof =
  343. m_site_id =
  344. m_card_type =
  345. m_card_id =
  346. m_card_ct =
  347. m_batty_status =
  348. m_callinfo =
  349. m_rav =
  350. m_acc =
  351. m_ant_id =
  352. m_sync_ct =
  353. m_loc_type =
  354. m_biz_type =
  355. m_rssi = 0;
  356. m_distance = 0.0;
  357. m_ins_data.reset();
  358. }
  359. void message_ins_locinfo::load(zistream& is)
  360. {
  361. zero_this();
  362. uint8_t bt = 0;
  363. is >> bt;
  364. // 卡类型,1字节
  365. m_card_type = bt;
  366. uint16_t id = 0;
  367. // 卡号 2字节
  368. // 卡ct 2字节
  369. is >> id >> m_card_ct;
  370. m_card_id = id;
  371. // 报文+电池,1字节
  372. // 预留,2字节
  373. is >> bt >> id;
  374. uint32_t tof = 0;
  375. is >> bt >> tof;
  376. m_tof = bt;
  377. m_tof = (m_tof << 32) | tof;
  378. // 天线号,1字节
  379. // 信号接收功率,1字节
  380. is >> m_ant_id >> bt;
  381. m_rssi = bt;
  382. --m_ant_id;
  383. // 加速度三轴,x,y,z
  384. for(size_t i = 0;i < 3; ++i){
  385. is >> id;
  386. m_ins_data.m_acc_data[i] = id;
  387. }
  388. // 角速度三轴,x,y,z
  389. for(size_t i = 0;i < 3; ++i){
  390. is >> id;
  391. m_ins_data.m_ang_data[i] = id;
  392. }
  393. logn_info(3, "[solid_ball] cid=%d, ct=%d, tof=%d, ant_id=%d, rssi=%d, acc[0]=%d, acc[1]=%d, acc[2]=%d, ang[0]=%d, ang[1]=%d, ang[2]=%d", m_card_id, m_card_ct, tof, m_ant_id, m_rssi, m_ins_data.m_acc_data[0], m_ins_data.m_acc_data[1], m_ins_data.m_acc_data[2], m_ins_data.m_ang_data[0], m_ins_data.m_ang_data[1], m_ins_data.m_ang_data[2]);
  394. }