sync_time.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef sync_time_base_h
  2. #define sync_time_base_h
  3. #include "sync_time_message.h"
  4. namespace host_server{
  5. class sync_time
  6. {
  7. private:
  8. unsigned long long m_upper_id{0}; // 上级分站id
  9. unsigned long long m_receive_time{0}; // 接收时间
  10. unsigned long long m_send_time{0}; // 发送时间
  11. unsigned short m_sync_level{0}; // 分站时间同步层级
  12. long long m_delay_time{0}; // 延迟时间
  13. bool m_rtime_over_status{false}; // 接收时间是否溢出,过周期
  14. bool m_stime_over_status{false}; // 发送时间是否溢出,过周期
  15. public:
  16. sync_time():m_upper_id(0),
  17. m_receive_time(0),
  18. m_send_time(0),
  19. m_sync_level(0),
  20. m_delay_time(0),
  21. m_rtime_over_status(false),
  22. m_stime_over_status(false)
  23. {}
  24. sync_time(unsigned long long upperIdCode, long long receiveTime,bool isRTimeOverflow, long long sendTime, bool isSTimeOverflow, unsigned short syncLevel):m_upper_id(upperIdCode),
  25. m_receive_time(receiveTime),
  26. m_send_time(sendTime),
  27. m_sync_level(syncLevel),
  28. m_delay_time(0),
  29. m_rtime_over_status(isRTimeOverflow),
  30. m_stime_over_status(isSTimeOverflow)
  31. {}
  32. sync_time(sync_time_message& msg, sync_time_message& upper_msg, sync_time* s = nullptr)
  33. :m_upper_id(msg.get_upper_id()),
  34. m_receive_time(msg.get_local_receive_time()),
  35. m_send_time(upper_msg.get_local_send_time()),
  36. m_sync_level(msg.get_sync_level()),
  37. m_delay_time(0)
  38. {
  39. // 根据历史记录解决时间戳溢出的问题
  40. if(s == nullptr)
  41. {
  42. m_rtime_over_status = false;
  43. m_stime_over_status = false;
  44. }
  45. else
  46. {
  47. m_rtime_over_status = (m_receive_time < s->get_real_receive_time()) ? !s->get_rtime_status() : s->get_rtime_status();
  48. m_stime_over_status = (m_send_time < s->get_real_send_time()) ? !s->get_stime_status() : s->get_stime_status();
  49. // 解决同时溢出的问题
  50. if(m_rtime_over_status && m_stime_over_status)
  51. {
  52. m_rtime_over_status = false;
  53. m_stime_over_status = false;
  54. }
  55. }
  56. }
  57. unsigned long long get_upper_id() const
  58. {
  59. return m_upper_id;
  60. }
  61. /*
  62. * 获得接收时间
  63. * */
  64. unsigned long long get_receive_time() const
  65. {
  66. return m_receive_time + (m_rtime_over_status ? TIME_MAX : 0);
  67. }
  68. unsigned long long get_real_receive_time() const
  69. {
  70. return m_receive_time;
  71. }
  72. void set_receive_time(unsigned long long& val)
  73. {
  74. m_receive_time = val;
  75. }
  76. bool get_rtime_status() const
  77. {
  78. return m_rtime_over_status;
  79. }
  80. void set_rtime_status(bool val)
  81. {
  82. m_rtime_over_status = val;
  83. }
  84. unsigned long long get_send_time() const
  85. {
  86. return m_send_time + (m_stime_over_status ? TIME_MAX : 0);
  87. }
  88. unsigned long long get_real_send_time() const
  89. {
  90. return m_send_time;
  91. }
  92. void set_send_time(const unsigned long long& val)
  93. {
  94. m_send_time = val;
  95. }
  96. bool get_stime_status() const
  97. {
  98. return m_stime_over_status;
  99. }
  100. void set_stime_status(bool val)
  101. {
  102. m_stime_over_status = val;
  103. }
  104. unsigned short get_sync_level() const
  105. {
  106. return m_sync_level;
  107. }
  108. void set_sync_level(unsigned short val)
  109. {
  110. m_sync_level = val;
  111. }
  112. long long get_delay_time() const
  113. {
  114. return m_delay_time;
  115. }
  116. void set_delay_time(const long long& val)
  117. {
  118. m_delay_time = val;
  119. }
  120. };
  121. }
  122. #endif