AfterSalesHeartRateKeeper.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.Enum;
  7. using WingInterfaceLibrary.Interface;
  8. using WingInterfaceLibrary.Request.Remote;
  9. namespace Vinno.vCloud.Common.FIS.AfterSales
  10. {
  11. internal class AfterSalesHeartRateKeeperV2
  12. {
  13. private readonly IDeviceService _deviceService;
  14. private readonly string _token;
  15. private readonly int _heartRate;
  16. private readonly ManualResetEvent _waitEvent = new ManualResetEvent(false);
  17. public AfterSalesHeartRateKeeperV2(IDeviceService deviceService, string token, int heartRate = 5)
  18. {
  19. _deviceService = deviceService;
  20. _heartRate = heartRate;
  21. _token = token;
  22. }
  23. /// <summary>
  24. /// Start the Heartrate to keep the session available.
  25. /// </summary>
  26. public void Start()
  27. {
  28. DoHeartRate();
  29. }
  30. /// <summary>
  31. /// Stop the Heartrate
  32. /// </summary>
  33. public void Stop()
  34. {
  35. _waitEvent.Set();
  36. }
  37. /// <summary>
  38. /// Do HartRate
  39. /// </summary>
  40. /// <returns></returns>
  41. private void DoHeartRate()
  42. {
  43. Task.Run(() =>
  44. {
  45. while (!_waitEvent.WaitOne(TimeSpan.FromSeconds(_heartRate)))
  46. {
  47. try
  48. {
  49. bool result = false;
  50. var remoteConnectHeartRateRequest = new RemoteConnectHeartRateRequest()
  51. {
  52. Token = _token,
  53. TransactionType = TransactionTypeEnum.AfterSales,
  54. };
  55. result = JsonRpcHelper.DeviceRemoteConnectHeartRate(_deviceService, remoteConnectHeartRateRequest);
  56. if (!result)
  57. {
  58. Logger.WriteLineError($"AfterSales HeartRateKeeperV2 error,The Result is False");
  59. }
  60. }
  61. catch (Exception ex)
  62. {
  63. Logger.WriteLineError($"AfterSales HeartRateKeeperV2 Do HeartRate Error:{ex}");
  64. }
  65. }
  66. });
  67. }
  68. }
  69. }