ElectricEnergyMeterCheckService.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using Microsoft.Extensions.DependencyInjection;
  2. using Microsoft.Extensions.Hosting;
  3. using ProductionLineMonitor.Application.Services.EnergyConsumptionService.Dtos;
  4. using ProductionLineMonitor.Core.Models;
  5. using ProductionLineMonitor.EntityFramework;
  6. using ProductionLineMonitor.EntityFramework.Repositories;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace ProductionLineMonitor.Web.HostedServices
  13. {
  14. /// <summary>
  15. /// 设备电能表运行状况检测服务
  16. /// </summary>
  17. public class ElectricEnergyMeterCheckService : BackgroundService
  18. {
  19. private readonly IServiceScopeFactory _scopeFactory;
  20. public ElectricEnergyMeterCheckService(
  21. IServiceScopeFactory scopeFactory)
  22. {
  23. _scopeFactory = scopeFactory;
  24. }
  25. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  26. {
  27. while (!stoppingToken.IsCancellationRequested)
  28. {
  29. try
  30. {
  31. await Task.Delay(CheckPeriod, stoppingToken);
  32. lock (Machines)
  33. {
  34. GetMachines();
  35. foreach (var machine in Machines)
  36. {
  37. machine.GetElectricEnergyMeterDtos();
  38. machine.Check();
  39. }
  40. CheckOfflineMachine();
  41. AddElectricEnergyMeterLog();
  42. }
  43. }
  44. catch (Exception ex)
  45. {
  46. Console.WriteLine(ex.ToString());
  47. }
  48. }
  49. }
  50. public int CheckPeriod { get; private set; } = 1000 * 60 * 5;
  51. public static IList<MachineElectricEnergyMeterCheckDto> Machines { get; set; }
  52. = new List<MachineElectricEnergyMeterCheckDto>();
  53. public void GetMachines()
  54. {
  55. using IServiceScope scope = _scopeFactory.CreateScope();
  56. ProductionLineContext dbContext = scope.ServiceProvider.GetRequiredService<ProductionLineContext>();
  57. UnitOfWork unitOfWork = new UnitOfWork(dbContext);
  58. var machines = unitOfWork.MachineRepository.GetList().OrderBy(o => o.Type);
  59. foreach (var machine in machines)
  60. {
  61. MachineElectricEnergyDto dto = new MachineElectricEnergyDto()
  62. {
  63. Id = machine.Id,
  64. Name = machine.Name,
  65. Topic = machine.Topic,
  66. Type = machine.Type,
  67. IsInclusionLineStatistics = machine.IsInclusionLineStatistics,
  68. ProductionLineId = machine.ProductionLineId,
  69. ProductionLineOrder = machine.ProductionLineOrder,
  70. FaultTopic = machine.FaultTopic
  71. };
  72. var line = unitOfWork.ProductionLineRepository
  73. .FirstOrDefault(x => x.Id == machine.ProductionLineId);
  74. if (line != null)
  75. {
  76. dto.Floor = line.Floor;
  77. dto.Line = line.Line;
  78. }
  79. var meters = unitOfWork.ElectricEnergyMeterRepository
  80. .GetList(x => x.MachineId == machine.Id)
  81. .OrderBy(o => o.Order)
  82. .ToList();
  83. if (meters != null)
  84. dto.Meters = meters;
  85. if (!Machines.Any(x => x.Id == dto.Id))
  86. {
  87. var m = new MachineElectricEnergyMeterCheckDto()
  88. {
  89. Id = dto.Id,
  90. Name = dto.Name,
  91. Topic = dto.Topic,
  92. ProductionLineOrder = dto.ProductionLineOrder,
  93. Type = dto.Type,
  94. FaultTopic = dto.FaultTopic,
  95. ProductionLineId = dto.ProductionLineId,
  96. IsInclusionLineStatistics = dto.IsInclusionLineStatistics,
  97. Meters = dto.Meters
  98. };
  99. Machines.Add(m);
  100. }
  101. }
  102. }
  103. public void AddElectricEnergyMeterLog()
  104. {
  105. var abnormalMachines = Machines.Where(x =>
  106. x.MachineState != 1 &&
  107. x.State != MachineElectricEnergyMeterCheckDto.MachineMeterState.Normal &&
  108. x.State != MachineElectricEnergyMeterCheckDto.MachineMeterState.MesApiGetNull);
  109. if (abnormalMachines != null && abnormalMachines.Count() > 0)
  110. {
  111. using IServiceScope scope = _scopeFactory.CreateScope();
  112. ProductionLineContext dbContext = scope.ServiceProvider.GetRequiredService<ProductionLineContext>();
  113. UnitOfWork unitOfWork = new UnitOfWork(dbContext);
  114. foreach (var abnormalMachine in abnormalMachines)
  115. {
  116. unitOfWork.ElectricEnergyMeterLogRepository.Create(new Core.Models.ElectricEnergyMeterLog()
  117. {
  118. Id = Guid.NewGuid().ToString().ToUpper(),
  119. CreateTime = DateTime.Now,
  120. MachineId = abnormalMachine.Id,
  121. Message = abnormalMachine.Message,
  122. MachineName = abnormalMachine.Name
  123. });
  124. }
  125. unitOfWork.SaveChanges();
  126. }
  127. }
  128. private void CheckOfflineMachine()
  129. {
  130. foreach (var machine in Machines)
  131. {
  132. machine.MachineState = 0;
  133. }
  134. var abnormalMachines = Machines.Where(x =>
  135. x.State != MachineElectricEnergyMeterCheckDto.MachineMeterState.Normal &&
  136. x.State != MachineElectricEnergyMeterCheckDto.MachineMeterState.MesApiGetNull);
  137. if (abnormalMachines != null && abnormalMachines.Count() > 0)
  138. {
  139. using IServiceScope scope = _scopeFactory.CreateScope();
  140. ProductionLineContext dbContext = scope.ServiceProvider.GetRequiredService<ProductionLineContext>();
  141. UnitOfWork unitOfWork = new UnitOfWork(dbContext);
  142. List<Cim> cims = unitOfWork.CimRepository.GetList().ToList();
  143. DateTime dateTime = DateTime.Now;
  144. foreach (var abnormalMachine in abnormalMachines)
  145. {
  146. var cim = cims.FirstOrDefault(x => x.Topic == abnormalMachine.Topic);
  147. if (cim == null)
  148. {
  149. continue;
  150. }
  151. int min = (dateTime - cim.UpdateTime).Value.Minutes;
  152. if (min > 5)
  153. {
  154. abnormalMachine.MachineState = 1;
  155. }
  156. }
  157. }
  158. }
  159. public static IList<MachineElectricEnergyMeterCheckDto> GetCheckMachines()
  160. {
  161. return Machines;
  162. }
  163. public static void ChearMachines()
  164. {
  165. lock (Machines)
  166. {
  167. Machines = new List<MachineElectricEnergyMeterCheckDto>();
  168. }
  169. }
  170. }
  171. }