sync_manager.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #ifndef sync_time_manager_h
  2. #define sync_time_manager_h
  3. #include <boost/serialization/singleton.hpp>
  4. #include <unordered_map>
  5. #include <map>
  6. #include <deque>
  7. #include <iostream>
  8. #include <mutex>
  9. #include "sync_time_message.h"
  10. #include "position.h"
  11. #include "common.h"
  12. #include "sync_time.h"
  13. #include "sync_helper.h"
  14. #include "tag_message.h"
  15. using namespace std;
  16. namespace host_server {
  17. class sync_manager
  18. {
  19. public:
  20. static std::unordered_map<int, sync_manager> map_id_manager;
  21. unsigned long long sRootIdCode;
  22. struct sync_time_msg_item
  23. {
  24. unsigned short sync_num;
  25. unordered_map<unsigned long long, sync_time_message> ump_sync_time_msg_item;
  26. int nDataCount = 0;
  27. };
  28. struct sync_time_item
  29. {
  30. unsigned short sync_num;
  31. unordered_map<unsigned long long, sync_time> hist_sync;
  32. };
  33. std::recursive_mutex m_mu_sync_time;
  34. // 是否输出日志标志
  35. bool m_log_status;
  36. public:
  37. sync_manager();
  38. sync_manager(bool status);
  39. ~sync_manager();
  40. // unsigned long long 代表anchor编码 ID+AntNum
  41. unordered_map<unsigned long long, position> ump_anchors;
  42. // unsigned long long 代表anchor编码 ID+AntNum
  43. unordered_map<unsigned long long, unordered_map<unsigned long long, double>> ump_distance;
  44. // with root
  45. // 使用_syncTimeMsgs[SyncNum][LocalIdCode]访问对应的SyncTimeMsg,即同步时间消息
  46. unordered_map<unsigned long long, deque<sync_time_msg_item>> ump_sync_time_msg;
  47. // 使用_historySync[SyncNum][LocalIdCode]访问对应的SyncTime即计算后的时间同步
  48. unordered_map<unsigned long long, deque<sync_time_item>> ump_history_sync;
  49. public:
  50. //************************************
  51. // Description: 用于初始化所有成员
  52. // Method: init
  53. // Returns: void
  54. //************************************
  55. void init();
  56. //************************************
  57. // Description: 用于解析时间同步msg中的内容
  58. // Method: analyze_sync_msg
  59. // Returns: void
  60. // Parameter: SyncTimeMsg & msg 从socket获取的消息
  61. //************************************
  62. void analyze_sync_msg(sync_time_message& msg);
  63. //************************************
  64. // Description: 更新距离信息
  65. // Method: update_distance
  66. // Returns: void
  67. // Parameter: unsigned int local_id 当前分站号
  68. // Parameter: unsigned char local_ant_num 当前分站的天线号
  69. // Parameter: unsigned int upper_id 上级分站号
  70. // Parameter: unsigned char uppder_ant_num 上级分站的天线号
  71. // Parameter: int d 距离
  72. //************************************
  73. void update_distance(unsigned int local_id, uint8_t local_ant_num, unsigned int upper_id, uint8_t upper_ant_num, double d);
  74. //************************************
  75. // Description: 更新anchor信息
  76. // Method: update_anchor
  77. // Returns: void
  78. // Parameter: unsigned int local_id 当前分站号
  79. // Parameter: unsigned char local_ant_num 当前分站的天线号
  80. // Parameter: int x
  81. // Parameter: int y
  82. // Parameter: int z
  83. //************************************
  84. void update_anchor(unsigned int local_id, uint8_t local_ant_num, double x, double y, double z);
  85. //void update_anchor(unsigned int local_id, unsigned char local_ant_num, const position& p);
  86. //************************************
  87. // Description: 删除anchor
  88. // Method: delete_anchor
  89. // Returns: void
  90. // Parameter: unsigned int local_id 当前分站号
  91. // Parameter: unsigned char local_ant_num 当前分站的天线号
  92. //************************************
  93. void delete_anchor(unsigned int local_id, uint8_t local_ant_num);
  94. //************************************
  95. // Description: 根据Tag的记录和历史msg,利用线性插值算法计算Tag的预估时间
  96. // Method: cal_time_by_linar
  97. // Returns: unsigned long long
  98. // Parameter: unsigned long long tagReceiveTime tag的接收时间
  99. // Parameter: unsigned short SyncNum 接收tag时的时间同步编号
  100. // Parameter: unsigned long long localIdCode 接收tag的anchorID编码
  101. //************************************
  102. unsigned long long cal_time_by_linear(tag_message& tag);
  103. unsigned long long cal_time_by_inter_linear(tag_message& tag);
  104. //************************************
  105. // Method: update_sync
  106. // Returns: bool
  107. // Parameter: unsigned long long root_id_code
  108. // Parameter: unsigned short sync_num
  109. // Parameter: unsigned long long local_id_code
  110. //************************************
  111. bool update_sync(unsigned long long root_id_code, int idx, unsigned short sync_num, unsigned long long local_id_code);
  112. int find_sync_time_msg(unsigned long long root_id_code, unsigned short sync_num);
  113. int find_his_sync_time(unsigned long long root_id_code, unsigned short sync_num);
  114. void set_log_status(bool status)
  115. {
  116. this->m_log_status = status;
  117. }
  118. bool get_log_status() const
  119. {
  120. return m_log_status;
  121. }
  122. long long sub(unsigned long long t1, unsigned long long t2) {
  123. return ((t1 >= t2) ? (t1 - t2) : (t1 - t2 + TIME_MAX_ADD));
  124. }
  125. };
  126. };
  127. using singleton_sync_manager = boost::serialization::singleton<host_server::sync_manager>;
  128. #define ssync_manager singleton_sync_manager::get_mutable_instance()
  129. #endif