| | |
| | | using WIDESEA_StorageSocketServices; |
| | | using WIDESEA_IStorageSocketServices; |
| | | using Microsoft.Extensions.Configuration; |
| | | using WIDESEA_Repository; |
| | | |
| | | namespace WIDESEA_StorageTaskServices |
| | | { |
| | |
| | | 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) |
| | | |
| | | public MyBackgroundService(ILogger<MyBackgroundService> logger, SocketClientService socketClientService, IDt_HostLogRepository hostLogRepository) |
| | | { |
| | | _logger = logger; |
| | | _socket = socketClientService; |
| | | _hostLogRepository = hostLogRepository; |
| | | } |
| | | |
| | | public Task StartAsync(CancellationToken cancellationToken = default) |
| | | { |
| | | //TimeSpan.FromMinutes(5) |
| | | _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(5)); |
| | | _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(1)); |
| | | |
| | | return Task.CompletedTask; |
| | | } |
| | |
| | | |
| | | private void DoWork(object state) |
| | | { |
| | | _socket.ConnectServer(connection.IP, connection.Port); |
| | | 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); |
| | | } |
| | | DateTime threshold = DateTime.Now.AddSeconds(-30); |
| | | DateTime time = DateTime.Now.AddMinutes(-2); |
| | | 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); |
| | | if (Heart != null) |
| | | { |
| | | Heart.CreateDate = DateTime.Now; |
| | | SqlSugarHelper.DbWMS.Updateable(Heart).ExecuteCommand(); |
| | | |
| | | _socket.AddErrorMessage("2000", "Host", ""); |
| | | _socket.HandleDisconnection(); |
| | | return; |
| | | } |
| | | if (hostLogtime.Count > 0) |
| | | { |
| | | _hostLogRepository.DeleteData(hostLogtime); |
| | | } |
| | | foreach (var item in hostLog) |
| | | { |
| | | _socket.clientSend(_socket.MakeStringToByteMsg(item.Messgae)); |
| | | Thread.Sleep(1000); |
| | | } |
| | | } |
| | | |
| | | public Task StopAsync(CancellationToken cancellationToken) |