SyncManager.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include "stdafx.h"
  2. #include"SyncManager.h"
  3. #include <deque>
  4. #include <Eigen/Dense>
  5. #include <fstream>
  6. #include <iostream>
  7. #include <direct.h>
  8. #include <io.h>
  9. #include "../ProcessRemodule.h"
  10. #include "../log_process_module.h"
  11. #include "./../system_basic_info/SystemAnalysis.h"
  12. #include "Functions/Functions.h"
  13. #pragma warning(disable: 4244)
  14. HostServer::SyncManager::SyncManager()
  15. {
  16. init();
  17. }
  18. void HostServer::SyncManager::init()
  19. {
  20. InitializeCriticalSectionAndSpinCount(&m_csSyncTime, 4000);
  21. _anchors.swap(unordered_map<unsigned long long, Position>());
  22. _distance.swap(unordered_map<unsigned long long,unordered_map<unsigned long long, double>>());
  23. std::string dir = "";
  24. TCHAR chpath[MAX_PATH];
  25. ::GetModuleFileName(NULL,chpath, MAX_PATH);
  26. (_tcsrchr(chpath, _T('\\')))[1] = 0;
  27. dir = CFunctions::TCHAR2string(chpath);
  28. logDir = dir + "\\synclog\\";
  29. if(0 != _access(logDir.c_str(), 0)){
  30. _mkdir(logDir.c_str());
  31. }
  32. isOutputLog = false;
  33. }
  34. void HostServer::SyncManager::analyzeSyncMsg(SyncTimeMsg &msg)
  35. {
  36. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_10);
  37. EnterCriticalSection(&m_csSyncTime);
  38. //查找root节点的时间同步序号
  39. int idx = FindSyncTimeMsg(msg.RootIdCode(), msg.SyncNum());
  40. if( -1 == idx){
  41. // 没找到
  42. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_72);
  43. // 如果时间同步消息的版本数量超过最大限制,则删除最早添加的消息
  44. if(_syncTimeMsgs[msg.RootIdCode()].size() >= MAX_SYNCTIME_NUM){
  45. // 删除第一个
  46. // 可能有泄露
  47. _syncTimeMsgs[msg.RootIdCode()].pop_front();
  48. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_12);
  49. }
  50. //构造此时间同步数据保存到队列中
  51. SyncTimeMsgItem it;
  52. it.SyncNum = msg.SyncNum();
  53. it.SyncTimeMsgs[msg.LocalIdCode()] = msg;
  54. _syncTimeMsgs[msg.RootIdCode()].push_back(it);
  55. idx = _syncTimeMsgs[msg.RootIdCode()].size() - 1;
  56. }else{
  57. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_73);
  58. _syncTimeMsgs[msg.RootIdCode()][idx].SyncTimeMsgs[msg.LocalIdCode()] = msg;
  59. }
  60. // 如果历史同步队列中的时间同步数据大于指定容量,清除数据直到满足指定容量
  61. while(_historySync[msg.RootIdCode()].size() > MAX_SYNCTIME_NUM){
  62. _historySync[msg.RootIdCode()].pop_front();
  63. }
  64. // 更新时间同步并计算
  65. for(auto it :_syncTimeMsgs[msg.RootIdCode()][idx].SyncTimeMsgs)
  66. {
  67. updateSync(msg.RootIdCode(), idx, msg.SyncNum(), it.first);
  68. }
  69. LeaveCriticalSection(&m_csSyncTime);
  70. }
  71. bool HostServer::SyncManager::updateSync(unsigned long long rootIdCode, int idx, unsigned short SyncNum, unsigned long long localIdCode)
  72. {
  73. if(-1 == idx){
  74. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_74);
  75. return false;
  76. }
  77. // 如果当前版本的时间同步消息未收到则返回false
  78. unordered_map<unsigned long long, SyncTimeMsg>::iterator itSyncTime = _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs.find(localIdCode);
  79. if(itSyncTime == _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs.end()){
  80. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_75);
  81. return false;
  82. }
  83. SyncTimeMsg &msg = _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs[localIdCode];
  84. // 如果当前的为root,则返回true
  85. if(msg.SyncLevel() == 0){
  86. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_76);
  87. return true;
  88. }
  89. int idx_synctime = FindHisSyncTime(rootIdCode, SyncNum);
  90. // 如果时间同步已经计算过,则返回true
  91. if(-1 != idx_synctime){
  92. unordered_map<unsigned long long, SyncTime>::iterator itHistSync = _historySync[rootIdCode][idx_synctime].HistSync.find(localIdCode);
  93. if(itHistSync != _historySync[rootIdCode][idx_synctime].HistSync.end()){
  94. if(itHistSync->second.TimeDelay())
  95. return true;
  96. }
  97. }
  98. // 如果已经收到了上一级的同步消息
  99. if(_syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs.count(msg.UpperIdCode())){
  100. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_77);
  101. if(!updateSync(rootIdCode, idx, SyncNum, msg.UpperIdCode())){
  102. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_78);
  103. return false;
  104. }
  105. SyncTimeMsg &upperMsg = _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs[msg.UpperIdCode()];
  106. SyncTime* s = NULL;
  107. for(auto it(_historySync[rootIdCode].rbegin()); it != _historySync[rootIdCode].rend(); ++it)
  108. {
  109. if(it->SyncNum != msg.SyncNum() && it->HistSync.count(localIdCode))
  110. {
  111. s = &(it->HistSync.find(localIdCode)->second);
  112. break;
  113. }
  114. }
  115. idx_synctime = FindHisSyncTime(rootIdCode, SyncNum);
  116. if(-1 == idx_synctime){
  117. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_79);
  118. SyncTimeItem it;
  119. it.SyncNum = msg.SyncNum();
  120. it.HistSync[localIdCode] = SyncTime(msg, upperMsg, s);
  121. _historySync[rootIdCode].push_back(it);
  122. idx_synctime = _historySync[rootIdCode].size() - 1;
  123. }else{
  124. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_80);
  125. _historySync[rootIdCode][idx_synctime].HistSync[localIdCode] = SyncTime(msg, upperMsg, s);
  126. }
  127. // 计算时间同步
  128. long long upperTimeDelay = 0;
  129. if(_syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs[msg.UpperIdCode()].SyncLevel() != 0)
  130. {
  131. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_81);
  132. upperTimeDelay = _historySync[rootIdCode][idx_synctime].HistSync[msg.UpperIdCode()].TimeDelay();
  133. }
  134. long long sendTime = _historySync[rootIdCode][idx_synctime].HistSync[localIdCode].SendTime();
  135. long long receiveTime = _historySync[rootIdCode][idx_synctime].HistSync[localIdCode].ReceiveTime();
  136. long long timeDelay = receiveTime - sendTime - _distance[localIdCode][msg.UpperIdCode()];
  137. timeDelay += upperTimeDelay;
  138. // 从不同的Upper来的同步数据,跨周期判断可能不正确,将时间差控制在一个周期内
  139. while(timeDelay > TIME_MAX){
  140. timeDelay -= TIME_MAX;
  141. }
  142. while(timeDelay + TIME_MAX < 0 ){
  143. timeDelay += TIME_MAX;
  144. }
  145. _historySync[rootIdCode][idx_synctime].HistSync[localIdCode].TimeDelay(timeDelay);
  146. if(isOutputLog){
  147. char filename[100];
  148. char temp[200];
  149. long long aa = timeDelay;
  150. if(aa < 0){
  151. aa += TIME_MAX;
  152. }
  153. int bb = (receiveTime > TIME_MAX) ? 1 : 0;
  154. int cc = (sendTime > TIME_MAX) ? 1 : 0;
  155. int dd = msg.UpperIdCode() >> 8;
  156. sprintf_s(filename, "%s\\%d.log", logDir.c_str(), localIdCode>>8);
  157. ofstream outfile(filename, ofstream::app);
  158. sprintf_s(temp, "sn:%d, up:%d, rec:%I64d, snd:%I64d, del:%I64d, crt:%I64d, r_crs:%d, s_crs:%d\r", SyncNum, dd, receiveTime, sendTime, timeDelay, aa, bb, cc);
  159. outfile << temp;
  160. }
  161. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_69);
  162. return true;
  163. }else{
  164. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_110);
  165. }
  166. return false;
  167. }
  168. unsigned long long HostServer::SyncManager::calTimeByLinar(TagMsg &tag)
  169. {
  170. EnterCriticalSection(&m_csSyncTime);
  171. // 获取历史记录中与此tag最近的两条
  172. deque<SyncTime> hisSync;
  173. unsigned long long rootIdCode = tag.SyncRootIdCode;
  174. int i = 0;
  175. int idx = FindSyncTimeMsg(rootIdCode, tag.SyncNum);
  176. if(-1 != idx){
  177. unordered_map<unsigned long long, SyncTimeMsg>::iterator it = _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs.find(tag.StationIdCode);
  178. if(it != _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs.end()){
  179. if(it->second.SyncLevel() == 0){
  180. LeaveCriticalSection(&m_csSyncTime);
  181. return tag.ReceiveTime;
  182. }
  183. }
  184. }
  185. int idx_sync = -1;
  186. while(hisSync.size() < 2 && i < MAX_CALCLINER_NUM)
  187. {
  188. auto syncNum = tag.SyncNum - i;
  189. idx_sync = FindHisSyncTime(rootIdCode, syncNum);
  190. if(-1 != idx_sync){
  191. if(_historySync[rootIdCode][idx_sync].HistSync.count(tag.StationIdCode)){
  192. hisSync.push_front(_historySync[rootIdCode][idx_sync].HistSync[tag.StationIdCode]);
  193. }
  194. }
  195. i++;
  196. }
  197. // 如果满足条件的历史记录不足两个则返回
  198. if(hisSync.size() < 2){
  199. LeaveCriticalSection(&m_csSyncTime);
  200. return LLONG_MAX;
  201. }
  202. // 计算预估值
  203. long long y1(hisSync.at(0).ReceiveTime() - hisSync.at(0).TimeDelay()),y2(hisSync.at(1).ReceiveTime() - hisSync.at(1).TimeDelay());
  204. long long x1(hisSync.at(0).RealReceiveTime()), x2(hisSync.at(1).RealReceiveTime()), x3(tag.ReceiveTime);
  205. LeaveCriticalSection(&m_csSyncTime);
  206. unsigned long long res;
  207. if(x1 > x2)
  208. {
  209. x2 += TIME_MAX;
  210. }
  211. if(x2 > x3)
  212. {
  213. x3 += TIME_MAX;
  214. }
  215. if(y1 < 0){ // 理论y值不能小于0
  216. y1 += TIME_MAX;
  217. }
  218. if(y2 < 0 ){
  219. y2 += TIME_MAX;
  220. }
  221. if(y1 > y2){
  222. y2 += TIME_MAX;
  223. }else if(y2-y1 > TIME_MAX){
  224. y2 -=TIME_MAX;
  225. }
  226. Eigen::Matrix3d a;
  227. a << x1 ,1 , 0,
  228. x2 ,1 , 0,
  229. x3, 1, -1;
  230. Eigen::Vector3d b(y1, y2, 0);
  231. Eigen::Vector3d X = a.colPivHouseholderQr().solve(b);
  232. res = X(2);
  233. res &= TIME_MAX;
  234. return res;
  235. }
  236. void HostServer::SyncManager::updateDistance(unsigned int localId, unsigned char localAntNum, unsigned int upperId, unsigned char uppderAntNum, double d)
  237. {
  238. unsigned long long lId = SyncHelper::parseId(localId, localAntNum);
  239. unsigned long long uId = SyncHelper::parseId(upperId, uppderAntNum);
  240. if(_anchors.count(lId) == 0)
  241. {
  242. _anchors[lId] = Position();
  243. }
  244. if(_anchors.count(uId) == 0)
  245. {
  246. _anchors[uId] = Position();
  247. }
  248. _distance[lId][uId] = d;
  249. _distance[uId][lId] = d;
  250. // 删除所有版本的消息记录
  251. _historySync.clear();
  252. _syncTimeMsgs.clear();
  253. }
  254. void HostServer::SyncManager::updateAnchor(unsigned int localId, unsigned char localAntNum, double x, double y, double z)
  255. {
  256. deleteAnchor(localId, localAntNum);
  257. unsigned long long lId = SyncHelper::parseId(localId, localAntNum);
  258. _anchors[lId] = Position( x, y, z);
  259. }
  260. void HostServer::SyncManager::deleteAnchor(unsigned int localId, unsigned char localAntNum)
  261. {
  262. unsigned long long lId = SyncHelper::parseId(localId, localAntNum);
  263. auto it = _anchors.find(lId);
  264. if(it == _anchors.end()) return;
  265. // 删除anchor
  266. _anchors.erase(it);
  267. // 删除与此anchor相关的距离信息
  268. _distance.erase(_distance.find(lId));
  269. for(auto it(_distance.begin()); it != _distance.end(); it++)
  270. {
  271. it->second.erase(it->second.find(lId));
  272. if(it->second.size() == 0)
  273. {
  274. it = _distance.erase(it);
  275. }
  276. }
  277. // 删除所有版本的消息记录
  278. _historySync.clear();
  279. _syncTimeMsgs.clear();
  280. }
  281. HostServer::SyncManager::~SyncManager()
  282. {
  283. DeleteCriticalSection(&m_csSyncTime);
  284. }
  285. int HostServer::SyncManager::FindSyncTimeMsg(unsigned long long rootIdCode, unsigned short SyncNum )
  286. {
  287. int idx = -1;
  288. for(int i = _syncTimeMsgs[rootIdCode].size() - 1; i >= 0; i--){
  289. if(_syncTimeMsgs[rootIdCode][i].SyncNum == SyncNum ){
  290. return i;
  291. }
  292. }
  293. return idx;
  294. }
  295. int HostServer::SyncManager::FindHisSyncTime(unsigned long long rootIdCode, unsigned short SyncNum )
  296. {
  297. int idx = -1;
  298. for(int i = _historySync[rootIdCode].size() - 1; i >= 0; i--){
  299. if(_historySync[rootIdCode][i].SyncNum == SyncNum ){
  300. return i;
  301. }
  302. }
  303. return idx;
  304. }