LiveConsultationHeartRateManager.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System.Collections.Concurrent;
  2. using WingInterfaceLibrary.DTO.Consultation;
  3. using WingInterfaceLibrary.DTO.User;
  4. using WingInterfaceLibrary.Enum;
  5. using WingInterfaceLibrary.LiveConsultation;
  6. using WingLiveConsultationService.Utilities;
  7. using WingServerCommon.Interfaces.Cache;
  8. using WingInterfaceLibrary.Interface.DBInterface;
  9. using WingInterfaceLibrary.DB.Request;
  10. using WingInterfaceLibrary.Request.DBRequest;
  11. using WingInterfaceLibrary.DTO.LiveRoom;
  12. using WingServerCommon.Log;
  13. using WingServerCommon.Config;
  14. using WingServerCommon.Config.Parameters;
  15. namespace WingLiveConsultationService
  16. {
  17. internal class LiveConsultationHeartRateManager
  18. {
  19. private int _rateSeconds => EnvironmentConfigManager.GetParammeter<IntParameter>("LiveConsultation", "HeartRateSeconds").Value;//秒
  20. private int _networkErrTimes => EnvironmentConfigManager.GetParammeter<IntParameter>("LiveConsultation", "NetworkErrTimes").Value;//3次间隔无心跳,网络不佳
  21. private int _leaveRoomTimes => EnvironmentConfigManager.GetParammeter<IntParameter>("LiveConsultation", "LeaveRoomTimes").Value;//10次间隔无心跳,离开房间
  22. private ConcurrentDictionary<string, LiveConsultationHeartRateDTO> _clientTokens = new ConcurrentDictionary<string, LiveConsultationHeartRateDTO>();
  23. private Action<string, string, string, bool> OnClientJoined;
  24. private Action<string, string, string> OnClientNetworkErr;
  25. private Action<string, string, string> OnClientLeave;
  26. public LiveConsultationHeartRateManager(Action<string, string, string, bool> handleClientJoined, Action<string, string, string> handleClientNetworkErr, Action<string, string, string> handleClientLeave)
  27. {
  28. OnClientJoined = handleClientJoined;
  29. OnClientNetworkErr = handleClientNetworkErr;
  30. OnClientLeave = handleClientLeave;
  31. StartCheckClients();
  32. }
  33. /// <summary>
  34. /// 加入检测集合
  35. /// </summary>
  36. /// <param name="token"></param>
  37. /// <param name="clientId"></param>
  38. /// <param name="name"></param>
  39. /// <param name="consultationCode"></param>
  40. public void AddOrUpdate(string token, string clientId, string name, string consultationCode)
  41. {
  42. var uniqueId = GetUniqueId(token, consultationCode);
  43. _clientTokens.AddOrUpdate(uniqueId, (k) =>
  44. {
  45. var v = new LiveConsultationHeartRateDTO(token, clientId, name, consultationCode);
  46. OnClientJoined.Invoke(consultationCode, v.ClientId, name, false);
  47. return v;
  48. }, (k, v) =>
  49. {
  50. var sendMessage = v.LivingTimes >= _networkErrTimes;
  51. v.Activate();
  52. OnClientJoined.Invoke(consultationCode, v.ClientId, name, sendMessage);
  53. return v;
  54. });
  55. }
  56. private string GetUniqueId(string token, string consultationCode)
  57. {
  58. var uniqueId = $"{consultationCode}_{token}";
  59. return uniqueId;
  60. }
  61. /// <summary>
  62. /// 会诊心跳有效期验证
  63. /// </summary>
  64. private void StartCheckClients()
  65. {
  66. Task.Run(async () =>
  67. {
  68. Logger.WriteLineInfo($"LiveConsultationHeartRateManager start check clients");
  69. try
  70. {
  71. while (true)
  72. {
  73. await Task.Delay(1 * 1000 * _rateSeconds);
  74. foreach (var tokenInfo in _clientTokens.Values)
  75. {
  76. tokenInfo.DeActivate();
  77. var uniqueId = GetUniqueId(tokenInfo.Token, tokenInfo.ConsultationCode);
  78. try
  79. {
  80. if (tokenInfo.LivingTimes == _networkErrTimes)
  81. {
  82. OnClientNetworkErr.Invoke(tokenInfo.ConsultationCode, tokenInfo.ClientId, tokenInfo.Name);
  83. }
  84. else if (tokenInfo.LivingTimes == _leaveRoomTimes)
  85. {
  86. if (!_clientTokens.Values.Any(x => x.ClientId == tokenInfo.ClientId && x.ConsultationCode == tokenInfo.ConsultationCode && x.LivingTimes < _leaveRoomTimes))
  87. {
  88. OnClientLeave.Invoke(tokenInfo.ConsultationCode, tokenInfo.ClientId, tokenInfo.Name);
  89. }
  90. _clientTokens.TryRemove(uniqueId, out _);
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. Logger.WriteLineWarn($"check user[{tokenInfo.Name}] token {tokenInfo.Token} err, {ex}");
  96. }
  97. }
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. Logger.WriteLineError($"LiveConsultationHeartRateManager err, ex:{ex}");
  103. }
  104. Logger.WriteLineInfo($"LiveConsultationHeartRateManager finished");
  105. });
  106. }
  107. }
  108. internal class LiveConsultationHeartRateDTO
  109. {
  110. /// <summary>
  111. /// 登录Token
  112. /// </summary>
  113. /// <value></value>
  114. public string Token { get; set; }
  115. /// <summary>
  116. /// 用户or设备编码
  117. /// </summary>
  118. /// <value></value>
  119. public string ClientId { get; set; }
  120. /// <summary>
  121. /// 用户or设备名称
  122. /// </summary>
  123. /// <value></value>
  124. public string Name { get; set; }
  125. /// <summary>
  126. /// 会诊编码
  127. /// </summary>
  128. /// <value></value>
  129. public string ConsultationCode { get; set; }
  130. /// <summary>
  131. /// 已检查次数,下一个心跳会重置
  132. /// </summary>
  133. /// <value></value>
  134. public int LivingTimes { get; private set; }
  135. public LiveConsultationHeartRateDTO(string token, string clientId, string name, string consultationCode)
  136. {
  137. Token = token;
  138. ClientId = clientId;
  139. Name = name;
  140. ConsultationCode = consultationCode;
  141. }
  142. /// <summary>
  143. /// Reset the living time.
  144. /// </summary>
  145. public virtual void Activate()
  146. {
  147. LivingTimes = 0;
  148. }
  149. /// <summary>
  150. /// Deactive the session.
  151. /// </summary>
  152. public virtual void DeActivate()
  153. {
  154. LivingTimes++;
  155. }
  156. }
  157. }