123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- #ifndef sync_time_manager_h
- #define sync_time_manager_h
- #include <boost/serialization/singleton.hpp>
- #include <unordered_map>
- #include <map>
- #include <deque>
- #include <iostream>
- #include <mutex>
- #include "sync_time_message.h"
- #include "position.h"
- #include "common.h"
- #include "sync_time.h"
- #include "sync_helper.h"
- #include "tag_message.h"
- using namespace std;
- namespace host_server{
- class sync_manager
- {
- struct sync_time_msg_item
- {
- unsigned short sync_num;
- unordered_map<unsigned long long, sync_time_message> ump_sync_time_msg;
- };
- struct sync_time_item
- {
- unsigned short sync_num;
- unordered_map<unsigned long long, sync_time> hist_sync;
- };
- std::recursive_mutex m_mu_sync_time;
- // 是否输出日志标志
- bool m_log_status;
- public:
- sync_manager();
- sync_manager(bool status);
- ~sync_manager();
- // unsigned long long 代表anchor编码 ID+AntNum
- unordered_map<unsigned long long, position> ump_anchors;
- // unsigned long long 代表anchor编码 ID+AntNum
- unordered_map<unsigned long long, unordered_map<unsigned long long, double>> ump_distance;
- // with root
- // 使用_syncTimeMsgs[SyncNum][LocalIdCode]访问对应的SyncTimeMsg,即同步时间消息
- unordered_map<unsigned long long, deque<sync_time_msg_item>> ump_sync_time_msg;
- // 使用_historySync[SyncNum][LocalIdCode]访问对应的SyncTime即计算后的时间同步
- unordered_map<unsigned long long, deque<sync_time_item>> ump_history_sync;
- public:
- //************************************
- // Description: 用于初始化所有成员
- // Method: init
- // Returns: void
- //************************************
- void init();
- //************************************
- // Description: 用于解析时间同步msg中的内容
- // Method: analyze_sync_msg
- // Returns: void
- // Parameter: SyncTimeMsg & msg 从socket获取的消息
- //************************************
- void analyze_sync_msg(sync_time_message& msg);
- //************************************
- // Description: 更新距离信息
- // Method: update_distance
- // Returns: void
- // Parameter: unsigned int local_id 当前分站号
- // Parameter: unsigned char local_ant_num 当前分站的天线号
- // Parameter: unsigned int upper_id 上级分站号
- // Parameter: unsigned char uppder_ant_num 上级分站的天线号
- // Parameter: int d 距离
- //************************************
- void update_distance(unsigned int local_id, uint8_t local_ant_num, unsigned int upper_id, uint8_t upper_ant_num, double d);
- //************************************
- // Description: 更新anchor信息
- // Method: update_anchor
- // Returns: void
- // Parameter: unsigned int local_id 当前分站号
- // Parameter: unsigned char local_ant_num 当前分站的天线号
- // Parameter: int x
- // Parameter: int y
- // Parameter: int z
- //************************************
- void update_anchor(unsigned int local_id, uint8_t local_ant_num, double x, double y, double z);
- //void update_anchor(unsigned int local_id, unsigned char local_ant_num, const position& p);
- //************************************
- // Description: 删除anchor
- // Method: delete_anchor
- // Returns: void
- // Parameter: unsigned int local_id 当前分站号
- // Parameter: unsigned char local_ant_num 当前分站的天线号
- //************************************
- void delete_anchor(unsigned int local_id, uint8_t local_ant_num);
- //************************************
- // Description: 根据Tag的记录和历史msg,利用线性插值算法计算Tag的预估时间
- // Method: cal_time_by_linar
- // Returns: unsigned long long
- // Parameter: unsigned long long tagReceiveTime tag的接收时间
- // Parameter: unsigned short SyncNum 接收tag时的时间同步编号
- // Parameter: unsigned long long localIdCode 接收tag的anchorID编码
- //************************************
- unsigned long long cal_time_by_linear(tag_message& tag);
- unsigned long long cal_time_by_inter_linear(tag_message& tag);
- //************************************
- // Method: update_sync
- // Returns: bool
- // Parameter: unsigned long long root_id_code
- // Parameter: unsigned short sync_num
- // Parameter: unsigned long long local_id_code
- //************************************
- bool update_sync(unsigned long long root_id_code, int idx, unsigned short sync_num, unsigned long long local_id_code);
- int find_sync_time_msg(unsigned long long root_id_code, unsigned short sync_num);
- int find_his_sync_time(unsigned long long root_id_code, unsigned short sync_num);
- void set_log_status(bool status)
- {
- this->m_log_status = status;
- }
- bool get_log_status() const
- {
- return m_log_status;
- }
-
- long long sub(unsigned long long t1, unsigned long long t2){
- return ((t1 >= t2)? (t1 - t2):(t1 - t2 + TIME_MAX_ADD));
- }
- };
- };
- using singleton_sync_manager = boost::serialization::singleton<host_server::sync_manager>;
- #define ssync_manager singleton_sync_manager::get_mutable_instance()
- #endif
|