RemoteConnectManager.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. using System.ComponentModel;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Threading.Tasks;
  5. using WingServerCommon.Log;
  6. using WingInterfaceLibrary.Interface;
  7. using WingInterfaceLibrary.Enum;
  8. using System.Collections.Generic;
  9. using WingServerCommon.Config;
  10. using WingServerCommon.Config.Parameters;
  11. namespace WingDeviceService.Common
  12. {
  13. internal class RemoteConnectManager
  14. {
  15. private ConcurrentDictionary<string, RemoteConnectDTO> _remoteConnects = new ConcurrentDictionary<string, RemoteConnectDTO>();
  16. private ConcurrentDictionary<string, DeviceRemoteConnectDTO> _deviceRemoteConnects = new ConcurrentDictionary<string, DeviceRemoteConnectDTO>();
  17. private Func<RemoteConnectDTO, bool, bool, bool> OnRemoteConnectOnlineState;
  18. private Func<DeviceRemoteConnectDTO, bool, bool, bool> OnDeviceRemoteConnectOnlineState;
  19. private int _rateSeconds => EnvironmentConfigManager.GetParammeter<IntParameter>("Device", "RemoteControlHeartRateSeconds").Value;
  20. private IAuthenticationService _authenticationService;
  21. public RemoteConnectManager(Func<RemoteConnectDTO, bool, bool, bool> setOnlineState, Func<DeviceRemoteConnectDTO, bool, bool, bool> deviceSetOnlineState, IAuthenticationService _authService)
  22. {
  23. _authenticationService = _authService;
  24. OnRemoteConnectOnlineState = setOnlineState;
  25. OnDeviceRemoteConnectOnlineState = deviceSetOnlineState;
  26. StartCheckClients();
  27. }
  28. /// <summary>
  29. /// 加入检测集合
  30. /// </summary>
  31. /// <param name="key"></param>
  32. /// <param name="userToken"></param>
  33. /// <param name="deviceCode"></param>
  34. /// <param name="roomId"></param>
  35. public void AddOrUpdate(string key, string userToken, string deviceToken, string deviceCode, string userCode, string roomId, ConnectStatusEnum statusEnum, LoginSource loginSource, TransactionTypeEnum transactionType = TransactionTypeEnum.AfterSales)
  36. {
  37. _remoteConnects.AddOrUpdate(key, new RemoteConnectDTO(userToken, deviceToken, deviceCode, userCode, roomId, statusEnum, transactionType, loginSource), (k, v) =>
  38. {
  39. v.Activate();
  40. v.StatusEnum = statusEnum;
  41. v.DeviceToken = deviceToken;
  42. v.LoginSource = loginSource;
  43. v.UserToken = userToken;
  44. v.IsNeedSyn = v.IsNeedSyn;
  45. return v;
  46. });
  47. }
  48. /// <summary>
  49. /// 加入检测集合
  50. /// </summary>
  51. /// <param name="key"></param>
  52. /// <param name="userToken"></param>
  53. /// <param name="deviceCode"></param>
  54. /// <param name="roomId"></param>
  55. public void AddOrUpdateHeart(string key, bool isNeedSyn, string userToken, string deviceToken, string deviceCode, string userCode, string roomId, ConnectStatusEnum statusEnum, LoginSource loginSource, TransactionTypeEnum transactionType = TransactionTypeEnum.AfterSales)
  56. {
  57. _remoteConnects.AddOrUpdate(key, new RemoteConnectDTO(isNeedSyn, userToken, deviceToken, deviceCode, userCode, roomId, statusEnum, transactionType, loginSource), (k, v) =>
  58. {
  59. v.Activate();
  60. v.StatusEnum = statusEnum;
  61. v.DeviceToken = deviceToken;
  62. v.LoginSource = loginSource;
  63. v.UserToken = userToken;
  64. v.IsNeedSyn = isNeedSyn;
  65. return v;
  66. });
  67. }
  68. /// <summary>
  69. /// 加入检测集合
  70. /// </summary>
  71. /// <param name="key"></param>
  72. /// <param name="userToken"></param>
  73. /// <param name="deviceCode"></param>
  74. /// <param name="roomId"></param>
  75. public void DeviceAddOrUpdate(string key, string deviceToken, string userToken, string userCode, string deviceCode, TransactionTypeEnum transactionType)
  76. {
  77. _deviceRemoteConnects.AddOrUpdate(key, new DeviceRemoteConnectDTO(deviceToken, userToken, userCode, deviceCode, transactionType), (k, v) =>
  78. {
  79. v.UserToken = userToken;
  80. v.DeviceToken = deviceToken;
  81. v.IsNeedSyn = v.IsNeedSyn;
  82. v.Activate();
  83. return v;
  84. });
  85. }
  86. /// <summary>
  87. /// 加入检测集合
  88. /// </summary>
  89. /// <param name="key"></param>
  90. /// <param name="userToken"></param>
  91. /// <param name="deviceCode"></param>
  92. /// <param name="roomId"></param>
  93. public void DeviceAddOrUpdateHeart(string key, bool isNeedSyn, string deviceToken, string userToken, string userCode, string deviceCode, TransactionTypeEnum transactionType)
  94. {
  95. _deviceRemoteConnects.AddOrUpdate(key, new DeviceRemoteConnectDTO(isNeedSyn, deviceToken, userToken, userCode, deviceCode, transactionType), (k, v) =>
  96. {
  97. v.UserToken = userToken;
  98. v.DeviceToken = deviceToken;
  99. v.IsNeedSyn = isNeedSyn;
  100. v.Activate();
  101. return v;
  102. });
  103. }
  104. /// <summary>
  105. /// 是否存在token
  106. /// </summary>
  107. /// <param name="key"></param>
  108. /// <returns></returns>
  109. public TransactionTypeEnum GetConnectTransactionType(string key)
  110. {
  111. TransactionTypeEnum transactionType = TransactionTypeEnum.AfterSales;
  112. if (_remoteConnects?.Count > 0)
  113. {
  114. var res = _remoteConnects.ContainsKey(key);
  115. if (res)
  116. {
  117. transactionType = _remoteConnects[key].TransactionType;
  118. }
  119. }
  120. return transactionType;
  121. }
  122. /// <summary>
  123. /// 获取
  124. /// </summary>
  125. /// <param name="key"></param>
  126. /// <returns></returns>
  127. public string GetRoomId(string key)
  128. {
  129. string result = "";
  130. if (_remoteConnects?.Count > 0)
  131. {
  132. var res = _remoteConnects.ContainsKey(key);
  133. if (res)
  134. {
  135. result = _remoteConnects[key]?.RoomId ?? string.Empty;
  136. }
  137. }
  138. return result;
  139. }
  140. /// <summary>
  141. /// 获取
  142. /// </summary>
  143. /// <param name="key"></param>
  144. /// <returns></returns>
  145. public bool GetExistDeviceRoomId(string key)
  146. {
  147. bool res = false;
  148. if (_deviceRemoteConnects?.Count > 0)
  149. {
  150. res = _deviceRemoteConnects.ContainsKey(key);
  151. }
  152. return res;
  153. }
  154. /// <summary>
  155. /// 获取
  156. /// </summary>
  157. /// <param name="key"></param>
  158. /// <returns></returns>
  159. public RemoteConnectDTO GetRemoteConnectDTO(string key)
  160. {
  161. RemoteConnectDTO result = null;
  162. if (_remoteConnects?.Count > 0)
  163. {
  164. var res = _remoteConnects.ContainsKey(key);
  165. if (res)
  166. {
  167. result = _remoteConnects[key];
  168. }
  169. }
  170. return result;
  171. }
  172. /// <summary>
  173. /// 获取
  174. /// </summary>
  175. /// <param name="key"></param>
  176. /// <returns></returns>
  177. public DeviceRemoteConnectDTO GetDeviceRemoteConnectDTO(string key)
  178. {
  179. DeviceRemoteConnectDTO result = null;
  180. if (_deviceRemoteConnects?.Count > 0)
  181. {
  182. var res = _deviceRemoteConnects.ContainsKey(key);
  183. if (res)
  184. {
  185. result = _deviceRemoteConnects[key];
  186. }
  187. }
  188. return result;
  189. }
  190. /// <summary>
  191. /// 根据用户token查询所有的连接状态
  192. /// </summary>
  193. /// <param name="userToken"></param>
  194. /// <returns></returns>
  195. public List<RemoteConnectDTO> GetAllRemoteConnectByUserToken(string userToken)
  196. {
  197. var result = new List<RemoteConnectDTO>();
  198. if (_remoteConnects?.Count > 0)
  199. {
  200. foreach (var item in _remoteConnects.Values)
  201. {
  202. if (item.UserToken == userToken)
  203. {
  204. result.Add(item);
  205. }
  206. }
  207. }
  208. return result;
  209. }
  210. /// <summary>
  211. /// 根据deviceCode查询所有的连接状态
  212. /// </summary>
  213. /// <param name="userToken"></param>
  214. /// <returns></returns>
  215. public List<DeviceRemoteConnectDTO> GetAllRemoteConnectByDeviceToken(string deviceToken)
  216. {
  217. var result = new List<DeviceRemoteConnectDTO>();
  218. if (_deviceRemoteConnects?.Count > 0)
  219. {
  220. foreach (var item in _deviceRemoteConnects.Values)
  221. {
  222. if (item.DeviceToken == deviceToken)
  223. {
  224. result.Add(item);
  225. }
  226. }
  227. }
  228. return result;
  229. }
  230. public IList<RemoteConnectDTO> GetAllRemoteConnectList(string deviceCode)
  231. {
  232. var dataList = new List<RemoteConnectDTO>();
  233. if (_remoteConnects?.Count > 0)
  234. {
  235. foreach (var item in _remoteConnects.Values)
  236. {
  237. if (item.DeviceCode == deviceCode)
  238. {
  239. dataList.Add(item);
  240. }
  241. }
  242. }
  243. return dataList;
  244. }
  245. public IList<DeviceRemoteConnectDTO> GetAllDeviceRemoteConnectList(string deviceCode)
  246. {
  247. var dataList = new List<DeviceRemoteConnectDTO>();
  248. if (_deviceRemoteConnects?.Count > 0)
  249. {
  250. foreach (var item in _deviceRemoteConnects.Values)
  251. {
  252. if (item.DeviceCode == deviceCode)
  253. {
  254. dataList.Add(item);
  255. }
  256. }
  257. }
  258. return dataList;
  259. }
  260. public RemoteConnectDTO GetDeviceRemoteConnectInfo(string deviceCode)
  261. {
  262. RemoteConnectDTO info = null;
  263. if (_remoteConnects?.Count > 0)
  264. {
  265. foreach (var item in _remoteConnects.Values)
  266. {
  267. if (item.DeviceCode == deviceCode)
  268. {
  269. info = item;
  270. break;
  271. }
  272. }
  273. }
  274. return info;
  275. }
  276. /// <summary>
  277. /// 查询设备是否空闲
  278. /// </summary>
  279. /// <param name="userToken"></param>
  280. /// <param name="deviceCode"></param>
  281. /// <returns>是否被他人专用</returns>
  282. public int IsIdle(string userToken, string deviceCode, TransactionTypeEnum type = TransactionTypeEnum.AfterSales)
  283. {
  284. int res = 0;
  285. if (_remoteConnects?.Count > 0)
  286. {
  287. RemoteConnectDTO checkItem = null;
  288. foreach (var item in _remoteConnects.Values)
  289. {
  290. if (item.DeviceCode == deviceCode)
  291. {
  292. checkItem = item;
  293. break;
  294. }
  295. }
  296. if (checkItem != null)
  297. {
  298. //找到,判断usertoken是否一样, 如果不一样,则表示不是同一个人,则为他人已连接
  299. if (checkItem.UserToken != userToken)
  300. {
  301. //判断如果是同一个人,不同端
  302. var checkItemReq = new WingInterfaceLibrary.Request.TokenRequest() { Token = checkItem.UserToken };
  303. var checkItemToken = _authenticationService.GetTokenAsync(checkItemReq).Result;
  304. var userReq = new WingInterfaceLibrary.Request.TokenRequest() { Token = userToken };
  305. var userTokenInfo = _authenticationService.GetTokenAsync(userReq).Result;
  306. //这两个一样,表示不同端登录
  307. if (checkItemToken.ClientId == userTokenInfo.ClientId)
  308. {
  309. res = 2;
  310. }
  311. else
  312. {
  313. res = 1;
  314. }
  315. }
  316. //如果是同一个人,但是针对不同的模块也是不行的,比如正在会诊调参,是不能进行远程连接的
  317. else if (checkItem.UserToken == userToken && checkItem.TransactionType != type)
  318. {
  319. res = 1;
  320. }
  321. }
  322. }
  323. return res;
  324. }
  325. /// <summary>
  326. /// 尝试关闭
  327. /// </summary>
  328. /// <param name="key"></param>
  329. public bool Close(string key)
  330. {
  331. if (_remoteConnects.ContainsKey(key))
  332. {
  333. var res = _remoteConnects.TryRemove(key, out _);
  334. return res;
  335. }
  336. else
  337. {
  338. return false;
  339. }
  340. }
  341. /// <summary>
  342. /// 尝试关闭
  343. /// </summary>
  344. /// <param name="key"></param>
  345. public bool DeviceClose(string key)
  346. {
  347. if (_deviceRemoteConnects.ContainsKey(key))
  348. {
  349. var res = _deviceRemoteConnects.TryRemove(key, out _);
  350. return res;
  351. }
  352. else
  353. {
  354. return false;
  355. }
  356. }
  357. /// <summary>
  358. /// 设备心跳有效期验证
  359. /// </summary>
  360. private void StartCheckClients()
  361. {
  362. Logger.WriteLineInfo($"RemoteConnectManager start check clients");
  363. Task.Run(async () =>
  364. {
  365. while (true)
  366. {
  367. await Task.Delay(1 * 1000 * _rateSeconds);
  368. foreach (var token in _remoteConnects.Keys)
  369. {
  370. var remoteConnectDTO = _remoteConnects[token];
  371. remoteConnectDTO.DeActivate();
  372. try
  373. {
  374. if (remoteConnectDTO.LeftTime < 0)
  375. {
  376. Logger.WriteLineInfo($"RemoteConnectManager StartCheckClients, user is not online, Token:{ remoteConnectDTO.UserToken }, DeviceCode:{ remoteConnectDTO.DeviceCode }");
  377. OnRemoteConnectOnlineState?.Invoke(remoteConnectDTO, remoteConnectDTO.IsNeedSyn, false);
  378. }
  379. }
  380. catch (Exception ex)
  381. {
  382. Logger.WriteLineWarn($"check remote connect token {token} err, {ex}");
  383. }
  384. }
  385. foreach (var token in _deviceRemoteConnects.Keys)
  386. {
  387. var deviceRemoteConnectDTO = _deviceRemoteConnects[token];
  388. deviceRemoteConnectDTO.DeActivate();
  389. try
  390. {
  391. if (deviceRemoteConnectDTO.LeftTime < 0)
  392. {
  393. Logger.WriteLineInfo($"RemoteConnectManager StartCheckClients, device is not online, Token:{ deviceRemoteConnectDTO.DeviceToken }, UserCode:{ deviceRemoteConnectDTO.UserCode }");
  394. OnDeviceRemoteConnectOnlineState?.Invoke(deviceRemoteConnectDTO, deviceRemoteConnectDTO.IsNeedSyn, false);
  395. }
  396. }
  397. catch (Exception ex)
  398. {
  399. Logger.WriteLineWarn($"check remote connect token {token} err, {ex}");
  400. }
  401. }
  402. }
  403. });
  404. Logger.WriteLineInfo($"RemoteConnectManager finished");
  405. }
  406. }
  407. /// <summary>
  408. /// 远程连接管理实体
  409. /// </summary>
  410. internal class RemoteConnectDTO
  411. {
  412. /// <summary>
  413. /// 有参构造
  414. /// </summary>
  415. public RemoteConnectDTO(bool isNeedSyn, string userToken, string deviceToken, string deviceCode, string userCode, string roomId, ConnectStatusEnum statusEnum, TransactionTypeEnum transactionType, LoginSource loginSource)
  416. {
  417. UserToken = userToken;
  418. DeviceToken = deviceToken;
  419. DeviceCode = deviceCode;
  420. UserCode = userCode;
  421. RoomId = roomId;
  422. TransactionType = transactionType;
  423. StatusEnum = statusEnum;
  424. LoginSource = loginSource;
  425. IsNeedSyn = isNeedSyn;
  426. }
  427. /// <summary>
  428. /// 有参构造
  429. /// </summary>
  430. public RemoteConnectDTO(string userToken, string deviceToken, string deviceCode, string userCode, string roomId, ConnectStatusEnum statusEnum, TransactionTypeEnum transactionType, LoginSource loginSource)
  431. {
  432. UserToken = userToken;
  433. DeviceToken = deviceToken;
  434. DeviceCode = deviceCode;
  435. UserCode = userCode;
  436. RoomId = roomId;
  437. TransactionType = transactionType;
  438. StatusEnum = statusEnum;
  439. LoginSource = loginSource;
  440. }
  441. /// <summary>
  442. /// 类型
  443. /// </summary>
  444. public TransactionTypeEnum TransactionType { get; set; }
  445. /// <summary>
  446. /// 状态
  447. /// </summary>
  448. public ConnectStatusEnum StatusEnum { get; set; }
  449. /// <summary>
  450. /// 用户Token
  451. /// </summary>
  452. public string UserToken { get; set; }
  453. /// <summary>
  454. /// 设备Token
  455. /// </summary>
  456. public string DeviceToken { get; set; }
  457. /// <summary>
  458. /// 用户code
  459. /// </summary>
  460. public string UserCode { get; set; }
  461. /// <summary>
  462. /// 房间id
  463. /// </summary>
  464. public string RoomId { get; set; }
  465. /// <summary>
  466. /// 设备code
  467. /// </summary>
  468. public string DeviceCode { get; set; }
  469. /// <summary>
  470. /// 是否需要同步数据处理
  471. /// </summary>
  472. public bool IsNeedSyn { get; set; }
  473. /// <summary>
  474. /// 用户来源
  475. /// </summary>
  476. /// <value></value>
  477. public LoginSource LoginSource { get; set; }
  478. private volatile int _livingTime = 0;
  479. private readonly int _clientLifeCycle = 3; //3次无心跳就默认断开
  480. /// <summary>
  481. /// 离开次数
  482. /// </summary>
  483. public int LeftTime => _clientLifeCycle - _livingTime;
  484. /// <summary>
  485. /// Reset the living time.
  486. /// </summary>
  487. public virtual void Activate()
  488. {
  489. _livingTime = 0;
  490. }
  491. /// <summary>
  492. /// Deactive the session.
  493. /// </summary>
  494. public virtual void DeActivate()
  495. {
  496. _livingTime++;
  497. }
  498. }
  499. /// <summary>
  500. /// 设备远程连接管理实体
  501. /// </summary>
  502. internal class DeviceRemoteConnectDTO
  503. {
  504. /// <summary>
  505. /// 有参构造
  506. /// </summary>
  507. public DeviceRemoteConnectDTO(bool isNeedSyn, string deviceToken, string userToken, string userCode, string deviceCode, TransactionTypeEnum transactionType)
  508. {
  509. DeviceToken = deviceToken;
  510. UserToken = userToken;
  511. UserCode = userCode;
  512. DeviceCode = deviceCode;
  513. TransactionType = transactionType;
  514. IsNeedSyn = isNeedSyn;
  515. }
  516. /// <summary>
  517. /// 有参构造
  518. /// </summary>
  519. public DeviceRemoteConnectDTO(string deviceToken, string userToken, string userCode, string deviceCode, TransactionTypeEnum transactionType)
  520. {
  521. DeviceToken = deviceToken;
  522. UserToken = userToken;
  523. UserCode = userCode;
  524. DeviceCode = deviceCode;
  525. TransactionType = transactionType;
  526. }
  527. /// <summary>
  528. /// 类型
  529. /// </summary>
  530. public TransactionTypeEnum TransactionType { get; set; }
  531. /// <summary>
  532. /// 设备Token
  533. /// </summary>
  534. public string DeviceToken { get; set; }
  535. /// <summary>
  536. /// 用户Token
  537. /// </summary>
  538. public string UserToken { get; set; }
  539. /// <summary>
  540. /// 设备code
  541. /// </summary>
  542. public string DeviceCode { get; set; }
  543. /// <summary>
  544. /// 设备code
  545. /// </summary>
  546. public string UserCode { get; set; }
  547. /// <summary>
  548. /// 是否需要同步数据处理
  549. /// </summary>
  550. public bool IsNeedSyn { get; set; }
  551. private volatile int _livingTime = 0;
  552. private readonly int _clientLifeCycle = 3; //3次无心跳就默认断开
  553. /// <summary>
  554. /// 离开次数
  555. /// </summary>
  556. public int LeftTime => _clientLifeCycle - _livingTime;
  557. /// <summary>
  558. /// Reset the living time.
  559. /// </summary>
  560. public virtual void Activate()
  561. {
  562. _livingTime = 0;
  563. }
  564. /// <summary>
  565. /// Deactive the session.
  566. /// </summary>
  567. public virtual void DeActivate()
  568. {
  569. _livingTime++;
  570. }
  571. }
  572. }