DispatchService.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using AutoMapper;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using Microsoft.Extensions.Hosting;
  4. using Microsoft.Extensions.Logging;
  5. using ProductionLineMonitor.Application.Services;
  6. using ProductionLineMonitor.Application.Services.FaultService;
  7. using ProductionLineMonitor.Application.Services.OEEService;
  8. using ProductionLineMonitor.Core.Profiles;
  9. using ProductionLineMonitor.Core.Services;
  10. using ProductionLineMonitor.Core.Services.Fault;
  11. using ProductionLineMonitor.Core.Utils;
  12. using ProductionLineMonitor.EntityFramework;
  13. using ProductionLineMonitor.EntityFramework.Repositories;
  14. using ProductionLineMonitor.Web.Filters;
  15. using System;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using FaultService = ProductionLineMonitor.Application.Services.FaultService.FaultService;
  19. using IFaultService = ProductionLineMonitor.Application.Services.FaultService.IFaultService;
  20. namespace ProductionLineMonitor.Web.HostedServices
  21. {
  22. public class DispatchService : BackgroundService
  23. {
  24. private readonly IServiceScopeFactory _scopeFactory;
  25. private readonly ILogger<GlobalExceptionAttribute> _logger;
  26. public DispatchService(
  27. IServiceScopeFactory scopeFactory, ILogger<GlobalExceptionAttribute> logger)
  28. {
  29. _scopeFactory = scopeFactory;
  30. _logger = logger;
  31. }
  32. private readonly int Interval = 60;
  33. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  34. {
  35. while (!stoppingToken.IsCancellationRequested)
  36. {
  37. try
  38. {
  39. DateTime dateTime = DateTime.Now;
  40. if (dateTime.Minute == 5 && (dateTime.Hour == 8 || dateTime.Hour == 20))
  41. {
  42. using IServiceScope scope = _scopeFactory.CreateScope();
  43. ProductionLineContext dbContext = scope.ServiceProvider.GetRequiredService<ProductionLineContext>();
  44. UnitOfWork unitOfWork = new UnitOfWork(dbContext);
  45. IFaultService faultService = new FaultService(unitOfWork, null);
  46. IOEEService oEEService = new OEEService(unitOfWork);
  47. IExcelService excelService = new ExcelService(unitOfWork, oEEService, faultService);
  48. bool rev = excelService.EquipmentOperationReport(dateTime);
  49. if (rev == false)
  50. {
  51. excelService.EquipmentOperationReport(dateTime);
  52. }
  53. }
  54. }
  55. catch (Exception ex)
  56. {
  57. _logger.LogError(ex.Message);
  58. }
  59. await Task.Delay(Interval * 1000, stoppingToken);
  60. }
  61. }
  62. //protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  63. //{
  64. // while (!stoppingToken.IsCancellationRequested)
  65. // {
  66. // try
  67. // {
  68. // DateTime dateTime = DateTime.Now;
  69. // if (dateTime.Hour == 8 && dateTime.Minute == 10)
  70. // {
  71. // using IServiceScope scope = _scopeFactory.CreateScope();
  72. // ProductionLineContext dbContext = scope.ServiceProvider.GetRequiredService<ProductionLineContext>();
  73. // UnitOfWork unitOfWork = new UnitOfWork(dbContext);
  74. // ReportFormService reportFormService = new ReportFormService(unitOfWork);
  75. // reportFormService.CalculationAllOEE();
  76. // }
  77. // }
  78. // catch (Exception ex)
  79. // {
  80. // _logger.LogError(ex.Message);
  81. // }
  82. // await Task.Delay(Interval * 1000, stoppingToken);
  83. // }
  84. //}
  85. }
  86. }