sync_manager.h 5.5 KB

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