MachineElectricEnergyDto.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using ProductionLineMonitor.Core.Models;
  2. using ProductionLineMonitor.Web.Services;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. namespace ProductionLineMonitor.Application.Services.EnergyConsumptionService.Dtos
  7. {
  8. public class MachineElectricEnergyDto : Machine
  9. {
  10. public int Floor { get; set; }
  11. public int Line { get; set; }
  12. public List<ElectricEnergyMeter> Meters { get; set; }
  13. = new List<ElectricEnergyMeter>();
  14. }
  15. public class MachineElectricEnergyMeterCheckDto : MachineElectricEnergyDto
  16. {
  17. public enum MachineMeterState
  18. {
  19. Normal,
  20. TimeOut,
  21. ValueDoesNotChange,
  22. MesApiGetNull,
  23. OValue
  24. }
  25. public int MachineState { get; set; }
  26. public MachineMeterState State { get; private set; } = MachineMeterState.Normal;
  27. public string Message { get; private set; } = string.Empty;
  28. public int TimeOutValue { get; private set; } = 10;
  29. public int Index { get; private set; } = 0;
  30. public int LastIndex { get; private set; } = 0;
  31. public List<ElectricEnergyMeterDataDto>[] ElectricEnergyMeterDtos { get; private set; }
  32. = new List<ElectricEnergyMeterDataDto>[10];
  33. public void GetElectricEnergyMeterDtos()
  34. {
  35. if (Index >= 10)
  36. Index = 0;
  37. var dtos = MesApiService.GetElectricEnergyMeters(Topic).ToList();
  38. ElectricEnergyMeterDtos[Index] = dtos;
  39. Index++;
  40. }
  41. public void Check()
  42. {
  43. DateTime time = DateTime.Now;
  44. if (Meters == null || Meters.Count() == 0)
  45. {
  46. State = MachineMeterState.MesApiGetNull;
  47. Message = $"待导入!";
  48. return;
  49. }
  50. if (ElectricEnergyMeterDtos[Index - 1] == null || ElectricEnergyMeterDtos[Index - 1].Count() == 0)
  51. {
  52. State = MachineMeterState.MesApiGetNull;
  53. Message = $"MesApi 未获取到数据!";
  54. return;
  55. }
  56. foreach (var electricEnergyMeterDto in ElectricEnergyMeterDtos[Index - 1])
  57. {
  58. var meter = Meters.FirstOrDefault(x => x.SerialNumber == electricEnergyMeterDto.SerialNumber);
  59. if (meter == null)
  60. continue;
  61. if (!meter.Enable)
  62. continue;
  63. double min = (time - electricEnergyMeterDto.DataTime).TotalMinutes;
  64. if (min > TimeOutValue)
  65. {
  66. State = MachineMeterState.TimeOut;
  67. Message = $"编号 {electricEnergyMeterDto.SerialNumber} 超过 {(int)min} 分钟未推送!";
  68. return;
  69. }
  70. if (electricEnergyMeterDto.ElectricEnergy == 0)
  71. {
  72. State = MachineMeterState.OValue;
  73. Message = $"编号 {electricEnergyMeterDto.SerialNumber} 能耗为 0!";
  74. return;
  75. }
  76. if (Index - 2 < 0)
  77. LastIndex = 9;
  78. else
  79. LastIndex = Index - 2;
  80. var last = ElectricEnergyMeterDtos[LastIndex];
  81. if (last == null)
  82. continue;
  83. var lastMeter = last.FirstOrDefault(x => x.SerialNumber == meter.SerialNumber);
  84. if (lastMeter == null)
  85. continue;
  86. if (electricEnergyMeterDto.ElectricEnergy < lastMeter.ElectricEnergy)
  87. {
  88. State = MachineMeterState.ValueDoesNotChange;
  89. Message = $"编号 {electricEnergyMeterDto.SerialNumber} 能耗在负增长!";
  90. return;
  91. }
  92. if (Index == 10)
  93. LastIndex = 0;
  94. else
  95. LastIndex = Index;
  96. last = ElectricEnergyMeterDtos[LastIndex];
  97. if (last == null)
  98. continue;
  99. lastMeter = last.FirstOrDefault(x => x.SerialNumber == meter.SerialNumber);
  100. if (lastMeter == null)
  101. continue;
  102. if (electricEnergyMeterDto.ElectricEnergy == lastMeter.ElectricEnergy
  103. && electricEnergyMeterDto.Uab == lastMeter.Uab
  104. && electricEnergyMeterDto.Ubc == lastMeter.Ubc
  105. && electricEnergyMeterDto.Uca == lastMeter.Uca
  106. && electricEnergyMeterDto.Ua == lastMeter.Ua
  107. && electricEnergyMeterDto.Ub == lastMeter.Ub
  108. && electricEnergyMeterDto.Uc == lastMeter.Uc
  109. && electricEnergyMeterDto.Ia == lastMeter.Ia
  110. && electricEnergyMeterDto.Ib == lastMeter.Ib
  111. && electricEnergyMeterDto.Ic == lastMeter.Ic)
  112. {
  113. double min1 = (electricEnergyMeterDto.DataTime - lastMeter.DataTime).TotalMinutes;
  114. State = MachineMeterState.ValueDoesNotChange;
  115. Message = $"编号 {electricEnergyMeterDto.SerialNumber} {(int)min1}分钟 值无变化!";
  116. return;
  117. }
  118. }
  119. State = MachineMeterState.Normal;
  120. Message = "正常";
  121. }
  122. }
  123. }