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;
}
///
/// Start the Heartrate to keep the session available.
///
public void Start()
{
DoHeartRate();
}
///
/// Stop the Heartrate
///
public void Stop()
{
_waitEvent.Set();
}
///
/// Do HartRate
///
///
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}");
}
}
});
}
}
}