PushHeartRateKeeperV2.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 = 1;
  15. private readonly string _token;
  16. private readonly ManualResetEvent _waitEvent = new ManualResetEvent(false);
  17. private readonly object _lock = new object();
  18. private readonly ILiveConsultationService _liveConsultationService;
  19. private readonly IEducationService _educationService;
  20. private readonly EnumHeartRateType _heartRateType;
  21. private readonly int _checkInterval;
  22. private string _heartRateCode;
  23. private volatile bool _stopped;
  24. /// <summary>
  25. /// The envent connection off line when a wrong eche result happened
  26. /// </summary>
  27. public event EventHandler Offlined;
  28. public PushHeartRateKeeperV2(string token, string consultationCode, int checkInterval, ILiveConsultationService liveConsultationService)
  29. {
  30. _heartRateType = EnumHeartRateType.LiveConsultation;
  31. _token = token;
  32. _heartRateCode = consultationCode;
  33. if (checkInterval <= 0)
  34. {
  35. checkInterval = 5;
  36. }
  37. _checkInterval = checkInterval;
  38. _liveConsultationService = liveConsultationService;
  39. }
  40. public PushHeartRateKeeperV2(string token, string consultationCode, int checkInterval, IEducationService educationService)
  41. {
  42. _heartRateType = EnumHeartRateType.Education;
  43. _token = token;
  44. _heartRateCode = consultationCode;
  45. _checkInterval = checkInterval;
  46. _educationService = educationService;
  47. }
  48. /// <summary>
  49. /// Start the Heartrate to keep the session available.
  50. /// </summary>
  51. public void Start()
  52. {
  53. _stopped = false;
  54. Logger.WriteLineInfo($"PushHeartRateKeeperV2 is started,Consultation Code :{_heartRateCode},Token:{_token}");
  55. Task.Run(() =>
  56. {
  57. DoCheck();
  58. });
  59. }
  60. public void SetHeartRateCode(string heartRateCode)
  61. {
  62. lock (_lock)
  63. {
  64. _heartRateCode = heartRateCode;
  65. }
  66. Logger.WriteLineInfo($"PushHeartRateKeeperV2 SetHeartRateCode:{heartRateCode},Token:{_token}");
  67. }
  68. /// <summary>
  69. /// Stop the Heartrate
  70. /// </summary>
  71. public void Stop()
  72. {
  73. _stopped = true;
  74. _waitEvent.Set();
  75. Logger.WriteLineInfo($"PushHeartRateKeeperV2 is stopped,Consultation Code :{_heartRateCode},Token:{_token}");
  76. }
  77. private void ExecuteDoCheck()
  78. {
  79. if (_stopped)
  80. {
  81. return;
  82. }
  83. try
  84. {
  85. switch (_heartRateType)
  86. {
  87. case EnumHeartRateType.LiveConsultation:
  88. var liveConsultationHeartRateRequest = new LiveConsultationHeartRateRequest
  89. {
  90. Token = _token,
  91. };
  92. lock (_lock)
  93. {
  94. liveConsultationHeartRateRequest.ConsultationCode = _heartRateCode;
  95. }
  96. var result = JsonRpcHelper.HeartRate(_liveConsultationService, liveConsultationHeartRateRequest);
  97. Logger.WriteLineInfo($"PushHeartRateKeeperV2 HeartRateAsync Send,Token:{_token},HeartRateType:{_heartRateType},ConsultationCode:{_heartRateCode} ");
  98. if (result == null)
  99. {
  100. Logger.WriteLineError($"PushHeartRateKeeperV2 HeartRateAsync Error Once,Result is null");
  101. Thread.Sleep(1000);
  102. lock (_lock)
  103. {
  104. liveConsultationHeartRateRequest.ConsultationCode = _heartRateCode;//切换病人时,会变更
  105. }
  106. result = JsonRpcHelper.HeartRate(_liveConsultationService, liveConsultationHeartRateRequest);
  107. if (result == null)
  108. {
  109. Logger.WriteLineError($"PushHeartRateKeeperV2 HeartRateAsync Error Second,Result is null");
  110. _stopped = true;
  111. OnOfflined();
  112. }
  113. }
  114. break;
  115. case EnumHeartRateType.Education:
  116. var liveHeartRateRequest = new LiveHeartRateRequest
  117. {
  118. Token = _token,
  119. };
  120. lock (_lock)
  121. {
  122. liveHeartRateRequest.LiveCode = _heartRateCode;
  123. }
  124. var liveConsultationHeartRateResult = JsonRpcHelper.HeartRate(_educationService, liveHeartRateRequest);
  125. Logger.WriteLineInfo($"PushHeartRateKeeperV2 HeartRateAsync Send,Token:{_token},HeartRateType:{_heartRateType},LiveCode:{_heartRateCode} ");
  126. if (liveConsultationHeartRateResult == null)
  127. {
  128. Logger.WriteLineError($"PushHeartRateKeeperV2 HeartRateAsync Error Once,Result is null");
  129. Thread.Sleep(1000);
  130. lock (_lock)
  131. {
  132. liveHeartRateRequest.LiveCode = _heartRateCode;
  133. }
  134. liveConsultationHeartRateResult = JsonRpcHelper.HeartRate(_educationService, liveHeartRateRequest);
  135. if (liveConsultationHeartRateResult == null)
  136. {
  137. Logger.WriteLineError($"PushHeartRateKeeperV2 HeartRateAsync Error Second,Result is null");
  138. _stopped = true;
  139. OnOfflined();
  140. }
  141. }
  142. break;
  143. }
  144. }
  145. catch (Exception ex)
  146. {
  147. Logger.WriteLineError($"PushHeartRateKeeperV2 error: {ex}");
  148. OnOfflined();
  149. }
  150. if (!_stopped)
  151. {
  152. _waitEvent.WaitOne(TimeSpan.FromSeconds(_checkInterval));
  153. if (!_stopped)
  154. {
  155. DoCheck();
  156. }
  157. }
  158. }
  159. /// <summary>
  160. /// Do HartRate
  161. /// </summary>
  162. /// <returns></returns>
  163. private void DoCheck()
  164. {
  165. Task.Run(new Action(ExecuteDoCheck));
  166. }
  167. private void OnOfflined()
  168. {
  169. Offlined?.Invoke(this, EventArgs.Empty);
  170. }
  171. }
  172. }