ClientService.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. namespace EEMSUIClient.Services;
  2. public class ClientService(
  3. AddressInfo address,
  4. RunningData runningData,
  5. IClientBaseProvider provider)
  6. {
  7. private IClientCaller? clientCaller;
  8. private Timer? _timer;
  9. public bool Initialize()
  10. {
  11. if (this.clientCaller is not null)
  12. return false;
  13. if (string.IsNullOrEmpty(address.Ip) ||
  14. address.Port < 0 ||
  15. string.IsNullOrEmpty(address.HubName))
  16. return false;
  17. ClientCaller temp = new(provider);
  18. if (!temp.Initialize())
  19. return false;
  20. if (!temp.Open(address.Ip, address.Port, address.HubName, 5))
  21. return false;
  22. this.clientCaller = temp;
  23. return true;
  24. }
  25. public Guid RegisterDevice(DeviceInfo deviceInfo)
  26. {
  27. if (clientCaller is null)
  28. return Guid.Empty;
  29. return clientCaller.RegisterDevice(deviceInfo).Result;
  30. }
  31. public bool UpdateRealTimeData(Dictionary<string, object> realtimeData)
  32. {
  33. if (clientCaller is null)
  34. return false;
  35. return clientCaller.UpdateRealTimeData(realtimeData).Result;
  36. }
  37. public bool StartDataCollectionService()
  38. {
  39. _fakeRecipe.DeviceId = runningData.DeviceInfo.Guid ?? Guid.Empty;
  40. _timer?.Dispose();
  41. _timer = new(TimerCallback, null, 0, 1000 * interval);
  42. //TimerCallback(null);
  43. return true;
  44. }
  45. Recipe _fakeRecipe = new()
  46. {
  47. DeviceId = runningData.DeviceInfo.Guid ?? Guid.Empty,
  48. RecipeInfo = [],
  49. CurrentStepName = $"Step {coutner}",
  50. NextStepName = $"Step {coutner + 1}",
  51. CurrentStepRemainTime = 0,
  52. CurrentStepTotalTime = max,
  53. TotalRemainTime = 0,
  54. TotalTime = max * cycle
  55. };
  56. static int interval = 1;
  57. static int coutner = 1;
  58. static int max = 10;
  59. static int cycle = 3;
  60. private void TimerCallback(object? state)
  61. {
  62. if (clientCaller is null)
  63. return;
  64. _fakeRecipe.CurrentStepRemainTime += interval;
  65. _fakeRecipe.TotalRemainTime += interval;
  66. clientCaller.UpdateRecipeInfo(_fakeRecipe);
  67. if (_fakeRecipe.CurrentStepRemainTime >= max)
  68. {
  69. coutner++;
  70. _fakeRecipe.CurrentStepRemainTime = 0;
  71. _fakeRecipe.CurrentStepName = $"Step {coutner}";
  72. if (coutner == 3)
  73. _fakeRecipe.NextStepName = "Finish";
  74. else
  75. _fakeRecipe.NextStepName = $"Step {coutner + 1}";
  76. }
  77. if (_fakeRecipe.TotalRemainTime >= max * cycle)
  78. {
  79. _timer?.Dispose();
  80. coutner = 1;
  81. _fakeRecipe = new()
  82. {
  83. DeviceId = runningData.DeviceInfo.Guid ?? Guid.Empty,
  84. RecipeInfo = [],
  85. CurrentStepName = $"Step {coutner}",
  86. NextStepName = $"Step {coutner + 1}",
  87. CurrentStepRemainTime = 0,
  88. CurrentStepTotalTime = max,
  89. TotalRemainTime = 0,
  90. TotalTime = max * cycle
  91. };
  92. return;
  93. }
  94. }
  95. public void Dispose()
  96. {
  97. clientCaller?.Dispose();
  98. clientCaller = null;
  99. }
  100. }