12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- using Vinno.IUS.Common.Log;
- using Vinno.vCloud.Common.FIS.Helper;
- using WingInterfaceLibrary.Interface;
- using WingInterfaceLibrary.Request;
- namespace Vinno.vCloud.Common.FIS
- {
- internal class HeartRateKeeperV2
- {
- private readonly IDeviceService _deviceService;
- private readonly string _token;
- private readonly int _heartRate;
- private readonly ManualResetEvent _waitEvent = new ManualResetEvent(false);
- public HeartRateKeeperV2(IDeviceService deviceService, string token, int heartRate = 300)
- {
- _deviceService = deviceService;
- if (heartRate <= 0)
- {
- heartRate = 300;
- }
- _heartRate = heartRate;
- _token = token;
- }
- /// <summary>
- /// Start the Heartrate to keep the session available.
- /// </summary>
- public void Start()
- {
- DoHeartRate();
- }
- /// <summary>
- /// Stop the Heartrate
- /// </summary>
- public void Stop()
- {
- _waitEvent.Set();
- }
- /// <summary>
- /// Do HartRate
- /// </summary>
- /// <returns></returns>
- private void DoHeartRate()
- {
- Task.Run(() =>
- {
- while (!_waitEvent.WaitOne(TimeSpan.FromSeconds(_heartRate)))
- {
- try
- {
- bool result = false;
- var tokenRequest = new TokenRequest()
- {
- Token = _token,
- };
- result = JsonRpcHelper.HeartRate(_deviceService, tokenRequest);
- if (!result)
- {
- Logger.WriteLineError($"HeartRateKeeperV2 HeartRate Fail,Result is false");
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"HeartRateKeeperV2 Do HeartRate Error:{ex}");
- }
- }
- });
- }
- }
- }
|