PushHeartRateKeeperV2.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 WingInterfaceLibrary.LiveConsultation;
  7. namespace Vinno.vCloud.Common.FIS.LiveVideos
  8. {
  9. internal class PushHeartRateKeeperV2
  10. {
  11. private readonly string _token;
  12. private string _heartRateCode;
  13. private readonly ILiveConsultationService _liveConsultationService;
  14. private readonly int _checkInterval;
  15. private volatile bool _stopped;
  16. private readonly ManualResetEvent _waitEvent = new ManualResetEvent(false);
  17. private readonly object _lock = new object();
  18. /// <summary>
  19. /// The envent connection off line when a wrong eche result happened
  20. /// </summary>
  21. public event EventHandler Offlined;
  22. public PushHeartRateKeeperV2(string token, string consultationCode, int checkInterval, ILiveConsultationService liveConsultationService)
  23. {
  24. _token = token;
  25. _heartRateCode = consultationCode;
  26. _checkInterval = checkInterval;
  27. _liveConsultationService = liveConsultationService;
  28. }
  29. /// <summary>
  30. /// Start the Heartrate to keep the session available.
  31. /// </summary>
  32. public void Start()
  33. {
  34. _stopped = false;
  35. Logger.WriteLineInfo($"PushHeartRateKeeperV2 is started,Consultation Code :{_heartRateCode}");
  36. Task.Run(() =>
  37. {
  38. DoCheck();
  39. });
  40. }
  41. public void SetHeartRateCode(string heartRateCode)
  42. {
  43. lock (_lock)
  44. {
  45. _heartRateCode = heartRateCode;
  46. }
  47. Logger.WriteLineInfo($"PushHeartRateKeeperV2 SetHeartRateCode:{heartRateCode}");
  48. }
  49. /// <summary>
  50. /// Stop the Heartrate
  51. /// </summary>
  52. public void Stop()
  53. {
  54. _stopped = true;
  55. _waitEvent.Set();
  56. Logger.WriteLineInfo($"PushHeartRateKeeperV2 is stopped,Consultation Code :{_heartRateCode}");
  57. }
  58. private void ExecuteDoCheck()
  59. {
  60. if (_stopped)
  61. {
  62. return;
  63. }
  64. try
  65. {
  66. var liveConsultationHeartRateRequest = new LiveConsultationHeartRateRequest
  67. {
  68. Token = _token,
  69. };
  70. lock (_lock)
  71. {
  72. liveConsultationHeartRateRequest.ConsultationCode = _heartRateCode;
  73. }
  74. var result = JsonRpcHelper.HeartRate(_liveConsultationService, liveConsultationHeartRateRequest);
  75. Logger.WriteLineInfo($"PushHeartRateKeeperV2 HeartRateAsync Send,Token:{_token},ConsultationCode:{_heartRateCode} ");
  76. if (result == null)
  77. {
  78. Logger.WriteLineError($"PushHeartRateKeeperV2 HeartRateAsync Error,Result is null");
  79. OnOfflined();
  80. _stopped = true;
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. Logger.WriteLineError($"PushHeartRateKeeperV2 error: {ex}");
  86. OnOfflined();
  87. _stopped = true;
  88. }
  89. if (!_stopped)
  90. {
  91. _waitEvent.WaitOne(TimeSpan.FromSeconds(_checkInterval));
  92. if (!_stopped)
  93. {
  94. DoCheck();
  95. }
  96. }
  97. }
  98. /// <summary>
  99. /// Do HartRate
  100. /// </summary>
  101. /// <returns></returns>
  102. private void DoCheck()
  103. {
  104. Task.Run(new Action(ExecuteDoCheck));
  105. }
  106. private void OnOfflined()
  107. {
  108. Offlined?.Invoke(this, EventArgs.Empty);
  109. }
  110. }
  111. }