123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- using Vinno.IUS.Common.Log;
- using Vinno.vCloud.Common.FIS.Helper;
- using WingInterfaceLibrary.LiveConsultation;
- namespace Vinno.vCloud.Common.FIS.LiveVideos
- {
- internal class PushHeartRateKeeperV2
- {
- private readonly string _token;
- private string _heartRateCode;
- private readonly ILiveConsultationService _liveConsultationService;
- private readonly int _checkInterval;
- private volatile bool _stopped;
- private readonly ManualResetEvent _waitEvent = new ManualResetEvent(false);
- private readonly object _lock = new object();
- /// <summary>
- /// The envent connection off line when a wrong eche result happened
- /// </summary>
- public event EventHandler Offlined;
- public PushHeartRateKeeperV2(string token, string consultationCode, int checkInterval, ILiveConsultationService liveConsultationService)
- {
- _token = token;
- _heartRateCode = consultationCode;
- _checkInterval = checkInterval;
- _liveConsultationService = liveConsultationService;
- }
- /// <summary>
- /// Start the Heartrate to keep the session available.
- /// </summary>
- public void Start()
- {
- _stopped = false;
- Logger.WriteLineInfo($"PushHeartRateKeeperV2 is started,Consultation Code :{_heartRateCode}");
- Task.Run(() =>
- {
- DoCheck();
- });
- }
- public void SetHeartRateCode(string heartRateCode)
- {
- lock (_lock)
- {
- _heartRateCode = heartRateCode;
- }
- Logger.WriteLineInfo($"PushHeartRateKeeperV2 SetHeartRateCode:{heartRateCode}");
- }
- /// <summary>
- /// Stop the Heartrate
- /// </summary>
- public void Stop()
- {
- _stopped = true;
- _waitEvent.Set();
- Logger.WriteLineInfo($"PushHeartRateKeeperV2 is stopped,Consultation Code :{_heartRateCode}");
- }
- private void ExecuteDoCheck()
- {
- if (_stopped)
- {
- return;
- }
- try
- {
- var liveConsultationHeartRateRequest = new LiveConsultationHeartRateRequest
- {
- Token = _token,
- };
- lock (_lock)
- {
- liveConsultationHeartRateRequest.ConsultationCode = _heartRateCode;
- }
- var result = JsonRpcHelper.HeartRate(_liveConsultationService, liveConsultationHeartRateRequest);
- Logger.WriteLineInfo($"PushHeartRateKeeperV2 HeartRateAsync Send,Token:{_token},ConsultationCode:{_heartRateCode} ");
- if (result == null)
- {
- Logger.WriteLineError($"PushHeartRateKeeperV2 HeartRateAsync Error,Result is null");
- OnOfflined();
- _stopped = true;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"PushHeartRateKeeperV2 error: {ex}");
- OnOfflined();
- _stopped = true;
- }
- if (!_stopped)
- {
- _waitEvent.WaitOne(TimeSpan.FromSeconds(_checkInterval));
- if (!_stopped)
- {
- DoCheck();
- }
- }
- }
- /// <summary>
- /// Do HartRate
- /// </summary>
- /// <returns></returns>
- private void DoCheck()
- {
- Task.Run(new Action(ExecuteDoCheck));
- }
- private void OnOfflined()
- {
- Offlined?.Invoke(this, EventArgs.Empty);
- }
- }
- }
|