PushHeartRateKeeperV2.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Vinno.IUS.Common.Log;
  5. using Vinno.vCloud.Common.FIS.Helper;
  6. using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
  7. using WingInterfaceLibrary.Interface;
  8. using WingInterfaceLibrary.LiveConsultation;
  9. using WingInterfaceLibrary.Request.Education;
  10. namespace Vinno.vCloud.Common.FIS.LiveVideos
  11. {
  12. internal class PushHeartRateKeeperV2
  13. {
  14. private const int _retryLimit = 3;
  15. private int _retryCount = 0;
  16. private readonly string _token;
  17. private readonly ManualResetEvent _waitEvent = new ManualResetEvent(false);
  18. private readonly object _lock = new object();
  19. private readonly ILiveConsultationService _liveConsultationService;
  20. private readonly IEducationService _educationService;
  21. private readonly EnumHeartRateType _heartRateType;
  22. private readonly int _checkInterval;
  23. private string _heartRateCode;
  24. private volatile bool _stopped;
  25. /// <summary>
  26. /// The envent connection off line when a wrong eche result happened
  27. /// </summary>
  28. public event EventHandler Offlined;
  29. public PushHeartRateKeeperV2(string token, string consultationCode, int checkInterval, ILiveConsultationService liveConsultationService)
  30. {
  31. _heartRateType = EnumHeartRateType.LiveConsultation;
  32. _token = token;
  33. _heartRateCode = consultationCode;
  34. if (checkInterval <= 0)
  35. {
  36. checkInterval = 5;
  37. }
  38. _checkInterval = checkInterval;
  39. _liveConsultationService = liveConsultationService;
  40. }
  41. public PushHeartRateKeeperV2(string token, string consultationCode, int checkInterval, IEducationService educationService)
  42. {
  43. _heartRateType = EnumHeartRateType.Education;
  44. _token = token;
  45. _heartRateCode = consultationCode;
  46. _checkInterval = checkInterval;
  47. _educationService = educationService;
  48. }
  49. /// <summary>
  50. /// Start the Heartrate to keep the session available.
  51. /// </summary>
  52. public void Start()
  53. {
  54. _stopped = false;
  55. Logger.WriteLineInfo($"PushHeartRateKeeperV2 is started,Consultation Code :{_heartRateCode},Token:{_token},Interval:{_checkInterval}");
  56. Task.Run(() =>
  57. {
  58. DoCheck();
  59. });
  60. }
  61. public void SetHeartRateCode(string heartRateCode)
  62. {
  63. lock (_lock)
  64. {
  65. _heartRateCode = heartRateCode;
  66. }
  67. Logger.WriteLineInfo($"PushHeartRateKeeperV2 SetHeartRateCode:{heartRateCode},Token:{_token}");
  68. }
  69. /// <summary>
  70. /// Stop the Heartrate
  71. /// </summary>
  72. public void Stop()
  73. {
  74. _stopped = true;
  75. _waitEvent.Set();
  76. Logger.WriteLineInfo($"PushHeartRateKeeperV2 is stopped,Consultation Code :{_heartRateCode},Token:{_token}");
  77. }
  78. private void ExecuteDoCheck()
  79. {
  80. if (_stopped)
  81. {
  82. return;
  83. }
  84. try
  85. {
  86. switch (_heartRateType)
  87. {
  88. case EnumHeartRateType.LiveConsultation:
  89. var liveConsultationHeartRateRequest = new LiveConsultationHeartRateRequest
  90. {
  91. Token = _token,
  92. };
  93. lock (_lock)
  94. {
  95. liveConsultationHeartRateRequest.ConsultationCode = _heartRateCode;
  96. }
  97. var result = JsonRpcHelper.HeartRate(_liveConsultationService, liveConsultationHeartRateRequest);
  98. Logger.WriteLineInfo($"PushHeartRateKeeperV2 HeartRateAsync Send,Token:{_token},HeartRateType:{_heartRateType},ConsultationCode:{_heartRateCode} ");
  99. if (!result.IsSuccess)
  100. {
  101. Logger.WriteLineError($"PushHeartRateKeeperV2 HeartRateAsync Error,Token:{_token},HeartRateType:{_heartRateType},ConsultationCode:{_heartRateCode} ,ErrorCode:{result.ErrorCode}");
  102. if (result.ErrorCode == 833)//ConsultationEnded
  103. {
  104. _stopped = true;
  105. OnOfflined();
  106. }
  107. else
  108. {
  109. if (_retryCount < _retryLimit)
  110. {
  111. _retryCount++;
  112. }
  113. else
  114. {
  115. _stopped = true;
  116. OnOfflined();
  117. }
  118. }
  119. }
  120. else
  121. {
  122. _retryCount = 0;
  123. }
  124. break;
  125. case EnumHeartRateType.Education:
  126. var liveHeartRateRequest = new LiveHeartRateRequest
  127. {
  128. Token = _token,
  129. };
  130. lock (_lock)
  131. {
  132. liveHeartRateRequest.LiveCode = _heartRateCode;
  133. }
  134. var liveConsultationHeartRateResult = JsonRpcHelper.HeartRate(_educationService, liveHeartRateRequest);
  135. Logger.WriteLineInfo($"PushHeartRateKeeperV2 HeartRateAsync Send,Token:{_token},HeartRateType:{_heartRateType},LiveCode:{_heartRateCode} ");
  136. if (!liveConsultationHeartRateResult.IsSuccess)
  137. {
  138. Logger.WriteLineError($"PushHeartRateKeeperV2 HeartRateAsync Error,Token:{_token},HeartRateType:{_heartRateType},LiveCode:{_heartRateCode},ErrorCode is {liveConsultationHeartRateResult.ErrorCode}");
  139. if (_retryCount < _retryLimit)
  140. {
  141. _retryCount++;
  142. }
  143. else
  144. {
  145. _stopped = true;
  146. OnOfflined();
  147. }
  148. }
  149. else
  150. {
  151. _retryCount = 0;
  152. }
  153. break;
  154. }
  155. }
  156. catch (Exception ex)
  157. {
  158. Logger.WriteLineError($"PushHeartRateKeeperV2 error: {ex}");
  159. if (_retryCount < _retryLimit)
  160. {
  161. _retryCount++;
  162. }
  163. else
  164. {
  165. _stopped = true;
  166. OnOfflined();
  167. }
  168. }
  169. if (!_stopped)
  170. {
  171. _waitEvent.WaitOne(TimeSpan.FromSeconds(_retryCount > 0 ? (double)_checkInterval / 2 : _checkInterval));//如果上一次失败,则等待一半时间再试一次
  172. if (!_stopped)
  173. {
  174. DoCheck();
  175. }
  176. }
  177. }
  178. /// <summary>
  179. /// Do HartRate
  180. /// </summary>
  181. /// <returns></returns>
  182. private void DoCheck()
  183. {
  184. Task.Run(new Action(ExecuteDoCheck));
  185. }
  186. private void OnOfflined()
  187. {
  188. Offlined?.Invoke(this, EventArgs.Empty);
  189. }
  190. }
  191. }