SyncManager.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include "stdafx.h"
  2. #include"SyncManager.h"
  3. #include <deque>
  4. #include <Eigen/Dense>
  5. #include "../ProcessRemodule.h"
  6. #include "../log_process_module.h"
  7. #include "./../system_basic_info/SystemAnalysis.h"
  8. #pragma warning(disable: 4244)
  9. HostServer::SyncManager::SyncManager()
  10. {
  11. init();
  12. }
  13. void HostServer::SyncManager::init()
  14. {
  15. InitializeCriticalSectionAndSpinCount(&m_csSyncTime, 4000);
  16. //InitializeCriticalSection(&m_csCalcLiner);
  17. _anchors.swap(unordered_map<unsigned long long, Position>());
  18. //_syncTimeMsgs.clear();
  19. //_historySync.clear();
  20. _distance.swap(unordered_map<unsigned long long,unordered_map<unsigned long long, double>>());
  21. }
  22. void HostServer::SyncManager::analyzeSyncMsg(SyncTimeMsg &msg)
  23. {
  24. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_10);
  25. EnterCriticalSection(&m_csSyncTime);
  26. int idx = FindSyncTimeMsg(msg.RootIdCode(), msg.SyncNum());
  27. if( -1 == idx){ // 没找到
  28. // 如果时间同步消息的版本数量超过最大限制,则删除最早添加的消息
  29. if(_syncTimeMsgs[msg.RootIdCode()].size() >= MAX_SYNCTIME_NUM){ // 删除第一个
  30. // 可能有泄露
  31. _syncTimeMsgs[msg.RootIdCode()].pop_front();
  32. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_12);
  33. }
  34. SyncTimeMsgItem it;
  35. it.SyncNum = msg.SyncNum();
  36. it.SyncTimeMsgs[msg.LocalIdCode()] = msg;
  37. _syncTimeMsgs[msg.RootIdCode()].push_back(it);
  38. idx = _syncTimeMsgs[msg.RootIdCode()].size() - 1;
  39. }else{
  40. _syncTimeMsgs[msg.RootIdCode()][idx].SyncTimeMsgs[msg.LocalIdCode()] = msg;
  41. }
  42. //
  43. while(_historySync[msg.RootIdCode()].size() > MAX_SYNCTIME_NUM){
  44. _historySync[msg.RootIdCode()].pop_front();
  45. }
  46. // 更新时间同步并计算
  47. for(auto it :_syncTimeMsgs[msg.RootIdCode()][idx].SyncTimeMsgs)
  48. {
  49. updateSync(msg.RootIdCode(), idx, msg.SyncNum(), it.first);
  50. }
  51. LeaveCriticalSection(&m_csSyncTime);
  52. }
  53. bool HostServer::SyncManager::updateSync(unsigned long long rootIdCode, int idx, unsigned short SyncNum, unsigned long long localIdCode)
  54. {
  55. if(-1 == idx){
  56. return false;
  57. }
  58. // 如果当前版本的时间同步消息未收到则返回false
  59. unordered_map<unsigned long long, SyncTimeMsg>::iterator itSyncTime = _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs.find(localIdCode);
  60. if(itSyncTime == _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs.end()){
  61. return false;
  62. }
  63. SyncTimeMsg &msg = _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs[localIdCode];
  64. // 如果当前的为root,则返回true
  65. if(msg.SyncLevel() == 0){
  66. return true;
  67. }
  68. int idx_synctime = FindSyncTime(rootIdCode, SyncNum);
  69. // 如果时间同步已经计算过,则返回true
  70. if(-1 != idx_synctime){
  71. unordered_map<unsigned long long, SyncTime>::iterator itHistSync = _historySync[rootIdCode][idx_synctime].HistSync.find(localIdCode);
  72. if(itHistSync != _historySync[rootIdCode][idx_synctime].HistSync.end()){
  73. if(itHistSync->second.TimeDelay())
  74. return true;
  75. }
  76. }
  77. // 如果已经收到了上一级的同步消息
  78. if(_syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs.count(msg.UpperIdCode())){
  79. if(!updateSync(rootIdCode, idx, SyncNum, msg.UpperIdCode())){
  80. return false;
  81. }
  82. SyncTimeMsg &upperMsg = _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs[msg.UpperIdCode()];
  83. SyncTime* s = NULL;
  84. for(auto it(_historySync[rootIdCode].rbegin()); it != _historySync[rootIdCode].rend(); ++it)
  85. {
  86. if(it->SyncNum != msg.SyncNum() && it->HistSync.count(localIdCode))
  87. {
  88. s = &(it->HistSync.find(localIdCode)->second);
  89. break;
  90. }
  91. }
  92. idx_synctime = FindSyncTime(rootIdCode, SyncNum);
  93. if(-1 == idx_synctime){
  94. SyncTimeItem it;
  95. it.SyncNum = msg.SyncNum();
  96. it.HistSync[localIdCode] = SyncTime(msg, upperMsg, s);
  97. _historySync[rootIdCode].push_back(it);
  98. idx_synctime = _historySync[rootIdCode].size() - 1;
  99. }else{
  100. _historySync[rootIdCode][idx_synctime].HistSync[localIdCode] = SyncTime(msg, upperMsg, s);
  101. }
  102. // 计算时间同步
  103. long long upperTimeDelay = 0;
  104. if(_syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs[msg.UpperIdCode()].SyncLevel() != 0)
  105. {
  106. upperTimeDelay = _historySync[rootIdCode][idx_synctime].HistSync[msg.UpperIdCode()].TimeDelay();
  107. }
  108. long long sendTime = _historySync[rootIdCode][idx_synctime].HistSync[localIdCode].SendTime();
  109. long long receiveTime = _historySync[rootIdCode][idx_synctime].HistSync[localIdCode].ReceiveTime();
  110. long long timeDelay = receiveTime - sendTime - _distance[localIdCode][msg.UpperIdCode()];
  111. timeDelay += upperTimeDelay;
  112. if(timeDelay > TIME_MAX){
  113. timeDelay -= TIME_MAX;
  114. }
  115. if(timeDelay + TIME_MAX < 0 ){
  116. timeDelay += TIME_MAX;
  117. }
  118. _historySync[rootIdCode][idx_synctime].HistSync[localIdCode].TimeDelay(timeDelay);
  119. debug_print_syslog(0, "Delay info, syncnum:%d, station id: 0x%x, timeDelay:%I64u",
  120. SyncNum, localIdCode, timeDelay);
  121. return true;
  122. }
  123. return false;
  124. }
  125. unsigned long long HostServer::SyncManager::calTimeByLinar(TagMsg &tag)
  126. {
  127. //EnterCriticalSection(&m_csCalcLiner);
  128. EnterCriticalSection(&m_csSyncTime);
  129. // 获取历史记录中与此tag最近的两条
  130. deque<SyncTime> hisSync;
  131. //ofstream fout("test/linar_513.txt",ios::app);
  132. unsigned long long rootIdCode = tag.SyncRootIdCode;
  133. int i = 0;
  134. int idx = FindSyncTimeMsg(rootIdCode, tag.SyncNum);
  135. if(-1 != idx){
  136. unordered_map<unsigned long long, SyncTimeMsg>::iterator it = _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs.find(tag.StationIdCode);
  137. if(it != _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs.end()){
  138. if(it->second.SyncLevel() == 0){
  139. //LeaveCriticalSection(&m_csCalcLiner);
  140. LeaveCriticalSection(&m_csSyncTime);
  141. return tag.ReceiveTime;
  142. }
  143. }
  144. }
  145. int idx_sync = -1;
  146. while(hisSync.size() < 2 && i < MAX_CALCLINER_NUM)
  147. {
  148. auto syncNum = tag.SyncNum - i;
  149. idx_sync = FindSyncTime(rootIdCode, syncNum);
  150. if(-1 != idx_sync){
  151. if(_historySync[rootIdCode][idx_sync].HistSync.count(tag.StationIdCode)){
  152. hisSync.push_front(_historySync[rootIdCode][idx_sync].HistSync[tag.StationIdCode]);
  153. }
  154. }
  155. i++;
  156. }
  157. // 如果满足条件的历史记录不足两个则返回
  158. if(hisSync.size() < 2){
  159. //LeaveCriticalSection(&m_csCalcLiner);
  160. LeaveCriticalSection(&m_csSyncTime);
  161. return LLONG_MAX;
  162. }
  163. // 计算预估值
  164. long long y1(hisSync.at(0).ReceiveTime() - hisSync.at(0).TimeDelay()),y2(hisSync.at(1).ReceiveTime() - hisSync.at(1).TimeDelay());
  165. long long x1(hisSync.at(0).RealReceiveTime()), x2(hisSync.at(1).RealReceiveTime()), x3(tag.ReceiveTime);
  166. LeaveCriticalSection(&m_csSyncTime);
  167. unsigned long long res;
  168. if(x1 > x2)
  169. {
  170. x2 += TIME_MAX;
  171. }
  172. if(x2 > x3)
  173. {
  174. x3 += TIME_MAX;
  175. }
  176. if(y1 < 0){ // 理论y值不能小于0
  177. y1 += TIME_MAX;
  178. }
  179. if(y2 < 0 ){
  180. y2 += TIME_MAX;
  181. }
  182. if(y1 > y2){
  183. y2 += TIME_MAX;
  184. }else if(y2-y1 > TIME_MAX){
  185. y2 -=TIME_MAX;
  186. }
  187. Eigen::Matrix3d a;
  188. a << x1 ,1 , 0,
  189. x2 ,1 , 0,
  190. x3, 1, -1;
  191. Eigen::Vector3d b(y1, y2, 0);
  192. Eigen::Vector3d X = a.colPivHouseholderQr().solve(b);
  193. res = X(2);
  194. res &= TIME_MAX;
  195. return res;
  196. }
  197. void HostServer::SyncManager::updateDistance(unsigned int localId, unsigned char localAntNum, unsigned int upperId, unsigned char uppderAntNum, double d)
  198. {
  199. unsigned long long lId = SyncHelper::parseId(localId, localAntNum);
  200. unsigned long long uId = SyncHelper::parseId(upperId, uppderAntNum);
  201. if(_anchors.count(lId) == 0)
  202. {
  203. _anchors[lId] = Position();
  204. }
  205. if(_anchors.count(uId) == 0)
  206. {
  207. _anchors[uId] = Position();
  208. }
  209. _distance[lId][uId] = d;
  210. _distance[uId][lId] = d;
  211. // 删除所有版本的消息记录
  212. _historySync.clear();
  213. _syncTimeMsgs.clear();
  214. //_historySync.swap(unordered_map<unsigned short, unordered_map<unsigned long long, SyncTime>>());
  215. //_syncTimeMsgs.swap(unordered_map<unsigned short, unordered_map<unsigned long long, SyncTimeMsg>>());
  216. }
  217. void HostServer::SyncManager::updateAnchor(unsigned int localId, unsigned char localAntNum, double x, double y, double z)
  218. {
  219. deleteAnchor(localId, localAntNum);
  220. unsigned long long lId = SyncHelper::parseId(localId, localAntNum);
  221. _anchors[lId] = Position( x, y, z);
  222. }
  223. void HostServer::SyncManager::deleteAnchor(unsigned int localId, unsigned char localAntNum)
  224. {
  225. unsigned long long lId = SyncHelper::parseId(localId, localAntNum);
  226. auto it = _anchors.find(lId);
  227. if(it == _anchors.end()) return;
  228. // 删除anchor
  229. _anchors.erase(it);
  230. // 删除与此anchor相关的距离信息
  231. _distance.erase(_distance.find(lId));
  232. for(auto it(_distance.begin()); it != _distance.end(); it++)
  233. {
  234. it->second.erase(it->second.find(lId));
  235. if(it->second.size() == 0)
  236. {
  237. it = _distance.erase(it);
  238. }
  239. }
  240. // 删除所有版本的消息记录
  241. _historySync.clear();
  242. _syncTimeMsgs.clear();
  243. //_historySync.swap(unordered_map<unsigned short, unordered_map<unsigned long long, SyncTime>>());
  244. //_syncTimeMsgs.swap(unordered_map<unsigned short, unordered_map<unsigned long long, SyncTimeMsg>>());
  245. }
  246. HostServer::SyncManager::~SyncManager()
  247. {
  248. //DeleteCriticalSection(&m_csCalcLiner);
  249. DeleteCriticalSection(&m_csSyncTime);
  250. }
  251. int HostServer::SyncManager::FindSyncTimeMsg(unsigned long long rootIdCode, unsigned short SyncNum )
  252. {
  253. int idx = -1;
  254. for(int i = _syncTimeMsgs[rootIdCode].size() - 1; i >= 0; i--){
  255. if(_syncTimeMsgs[rootIdCode][i].SyncNum == SyncNum ){
  256. return i;
  257. }
  258. }
  259. return idx;
  260. }
  261. int HostServer::SyncManager::FindSyncTime(unsigned long long rootIdCode, unsigned short SyncNum )
  262. {
  263. int idx = -1;
  264. for(int i = _historySync[rootIdCode].size() - 1; i >= 0; i--){
  265. if(_historySync[rootIdCode][i].SyncNum == SyncNum ){
  266. return i;
  267. }
  268. }
  269. return idx;
  270. }