| | |
| | | using WIDESEA_IStorageSocketServices; |
| | | using Microsoft.Extensions.Configuration; |
| | | using WIDESEA_Repository; |
| | | using Microsoft.Extensions.DependencyInjection; |
| | | |
| | | namespace WIDESEA_StorageTaskServices |
| | | { |
| | | public class MyBackgroundService : IHostedService, IDisposable |
| | | public class MyBackgroundService : BackgroundService, IDisposable |
| | | { |
| | | private readonly ILogger<MyBackgroundService> _logger; |
| | | Connection connection = AppSettings.Configuration.GetSection("Connection").Get<Connection>(); |
| | | public SocketClientService _socket { get; set; } |
| | | |
| | | private readonly IDt_HostLogRepository _hostLogRepository; |
| | | private Timer _timer; |
| | | |
| | | public MyBackgroundService(ILogger<MyBackgroundService> logger, SocketClientService socketClientService, IDt_HostLogRepository hostLogRepository) |
| | | |
| | | private readonly IServiceProvider _serviceProvider; |
| | | |
| | | public MyBackgroundService(ILogger<MyBackgroundService> logger, SocketClientService socketClientService, IServiceProvider serviceProvider) |
| | | { |
| | | _logger = logger; |
| | | _socket = socketClientService; |
| | | _hostLogRepository = hostLogRepository; |
| | | _serviceProvider = serviceProvider; |
| | | } |
| | | |
| | | protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
| | | { |
| | | while (!stoppingToken.IsCancellationRequested) |
| | | { |
| | | // ä¸ºæ¯æ¬¡ä»»å¡å建æ°ä½ç¨å |
| | | using (var scope = _serviceProvider.CreateScope()) |
| | | { |
| | | DateTime timeHandLe = DateTime.Now.AddMinutes(-15); |
| | | if (!_socket.Socketonline()) |
| | | { |
| | | if (_socket.Time < timeHandLe) |
| | | { |
| | | _socket.Time = DateTime.Now; |
| | | _socket.AddErrorMessage("2000", "Host", ""); |
| | | } |
| | | _socket.ConnectServer(connection.IP, connection.Port); |
| | | } |
| | | |
| | | var hostAll = await SqlSugarHelper.DbWMS.Queryable<Dt_HostLog>().Where(x => true).ToListAsync(); |
| | | |
| | | if (hostAll.Count != 0) |
| | | { |
| | | DateTime threshold = DateTime.Now.AddSeconds(-30); |
| | | DateTime time = DateTime.Now.AddMinutes(-5); |
| | | DateTime Y = DateTime.Now.AddMinutes(-3); |
| | | |
| | | var Reissue = hostAll.Where(x => x.CreateDate < threshold || x.ModifyDate < threshold).ToList(); |
| | | |
| | | var hostLogtime = hostAll.Where(x => x.CreateDate < time || x.ModifyDate < time).ToList(); |
| | | |
| | | var Heart = hostAll.Where(x => x.CreateDate < Y || x.ModifyDate < Y && x.CommandID == 910).FirstOrDefault(); |
| | | |
| | | if (Heart != null) |
| | | { |
| | | Heart.CreateDate = DateTime.Now; |
| | | await SqlSugarHelper.DbWMS.Updateable(Heart).ExecuteCommandAsync(); |
| | | |
| | | _socket.AddErrorMessage("2000", "Host", ""); |
| | | _socket.HandleDisconnection(); |
| | | } |
| | | if (hostLogtime.Count > 0) |
| | | { |
| | | await SqlSugarHelper.DbWMS.Updateable(hostLogtime).ExecuteCommandAsync(); |
| | | } |
| | | foreach (var item in Reissue) |
| | | { |
| | | _socket.clientSend(_socket.MakeStringToByteMsg(item.Messgae)); |
| | | Thread.Sleep(1000); |
| | | } |
| | | } |
| | | } |
| | | |
| | | await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken); |
| | | } |
| | | } |
| | | |
| | | public Task StartAsync(CancellationToken cancellationToken = default) |
| | | { |
| | | //TimeSpan.FromMinutes(5) |
| | | _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(1)); |
| | | //_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(1)); |
| | | |
| | | return Task.CompletedTask; |
| | | } |
| | |
| | | DateTime Y = DateTime.Now.AddMinutes(-3); |
| | | |
| | | |
| | | var hostLog = _hostLogRepository.QueryData(x => x.CreateDate < threshold || x.ModifyDate < threshold).ToList(); |
| | | var hostLogtime = _hostLogRepository.QueryData(x => x.CreateDate < time || x.ModifyDate < time).ToList(); |
| | | var Heart = _hostLogRepository.QueryFirst(x => x.CreateDate < Y || x.ModifyDate < Y && x.CommandID == 910); |
| | | //var hostLog = _hostLogRepository.QueryData(x => x.CreateDate < threshold || x.ModifyDate < threshold).ToList(); |
| | | var hostLog = SqlSugarHelper.DbWMS.Queryable<Dt_HostLog>().Where(x => x.CreateDate < threshold || x.ModifyDate < threshold).ToList(); |
| | | var hostLogtime = SqlSugarHelper.DbWMS.Queryable<Dt_HostLog>().Where(x => x.CreateDate < time || x.ModifyDate < time).ToList(); |
| | | //var hostLogtime = _hostLogRepository.QueryData(x => x.CreateDate < time || x.ModifyDate < time).ToList(); |
| | | |
| | | var Heart = SqlSugarHelper.DbWMS.Queryable<Dt_HostLog>().Where(x => x.CreateDate < Y || x.ModifyDate < Y && x.CommandID == 910).First(); |
| | | |
| | | //var Heart = _hostLogRepository.QueryFirst(x => x.CreateDate < Y || x.ModifyDate < Y && x.CommandID == 910); |
| | | if (Heart != null) |
| | | { |
| | | Heart.CreateDate = DateTime.Now; |
| | |
| | | } |
| | | if (hostLogtime.Count > 0) |
| | | { |
| | | _hostLogRepository.DeleteData(hostLogtime); |
| | | SqlSugarHelper.DbWMS.Updateable(hostLogtime).ExecuteCommand(); |
| | | //_hostLogRepository.DeleteData(hostLogtime); |
| | | } |
| | | foreach (var item in hostLog) |
| | | { |