using AutoMapper; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using ProductionLineMonitor.Application.Services; using ProductionLineMonitor.Application.Services.FaultService; using ProductionLineMonitor.Application.Services.OEEService; using ProductionLineMonitor.Core.Profiles; using ProductionLineMonitor.Core.Services; using ProductionLineMonitor.Core.Services.Fault; using ProductionLineMonitor.Core.Utils; using ProductionLineMonitor.EntityFramework; using ProductionLineMonitor.EntityFramework.Repositories; using ProductionLineMonitor.Web.Filters; using System; using System.Threading; using System.Threading.Tasks; using FaultService = ProductionLineMonitor.Application.Services.FaultService.FaultService; using IFaultService = ProductionLineMonitor.Application.Services.FaultService.IFaultService; namespace ProductionLineMonitor.Web.HostedServices { public class DispatchService : BackgroundService { private readonly IServiceScopeFactory _scopeFactory; private readonly ILogger _logger; public DispatchService( IServiceScopeFactory scopeFactory, ILogger logger) { _scopeFactory = scopeFactory; _logger = logger; } private readonly int Interval = 60; protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { try { DateTime dateTime = DateTime.Now; if (dateTime.Minute == 5 && (dateTime.Hour == 8 || dateTime.Hour == 20)) { using IServiceScope scope = _scopeFactory.CreateScope(); ProductionLineContext dbContext = scope.ServiceProvider.GetRequiredService(); UnitOfWork unitOfWork = new UnitOfWork(dbContext); IFaultService faultService = new FaultService(unitOfWork, null); IOEEService oEEService = new OEEService(unitOfWork); IExcelService excelService = new ExcelService(unitOfWork, oEEService, faultService); bool rev = excelService.EquipmentOperationReport(dateTime); if (rev == false) { excelService.EquipmentOperationReport(dateTime); } } } catch (Exception ex) { _logger.LogError(ex.Message); } await Task.Delay(Interval * 1000, stoppingToken); } } //protected override async Task ExecuteAsync(CancellationToken stoppingToken) //{ // while (!stoppingToken.IsCancellationRequested) // { // try // { // DateTime dateTime = DateTime.Now; // if (dateTime.Hour == 8 && dateTime.Minute == 10) // { // using IServiceScope scope = _scopeFactory.CreateScope(); // ProductionLineContext dbContext = scope.ServiceProvider.GetRequiredService(); // UnitOfWork unitOfWork = new UnitOfWork(dbContext); // ReportFormService reportFormService = new ReportFormService(unitOfWork); // reportFormService.CalculationAllOEE(); // } // } // catch (Exception ex) // { // _logger.LogError(ex.Message); // } // await Task.Delay(Interval * 1000, stoppingToken); // } //} } }