using Masuit.Tools; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System.Net.Sockets; using System.Net; using System.Text; using WIDESEA_Cache; using WIDESEA_Core.Const; using WIDESEA_DTO.WMS; using WIDESEA_IServices; using WIDESEAWCS_BasicInfoRepository; using OfficeOpenXml.FormulaParsing.Excel.Functions.Numeric; using WIDESEA_StorageSocketServices; using WIDESEA_IStorageSocketServices; using Microsoft.Extensions.Configuration; using WIDESEA_Model.Models; using static WIDESEAWCS_BasicInfoService.Dt_StationManagerService; using WIDESEA_DTO.AGV; using System.Threading.Tasks; namespace WIDESEA_StorageTaskServices { public class ReissueToHostMessage : IHostedService, IDisposable { private readonly ILogger _logger; public SocketClientService _Socket { get; set; } private Timer _timer; private readonly IDt_TaskRepository _taskRepository; private readonly IDt_StationManagerRepository _stationManagerRepository; private readonly IDt_HostLogRepository _hostLogRepository; public ReissueToHostMessage(ILogger logger, SocketClientService socketClientService, IDt_TaskRepository taskRepository, IDt_StationManagerRepository stationManagerRepository, IDt_HostLogRepository hostLogRepository) { _logger = logger; _Socket = socketClientService; _taskRepository = taskRepository; _stationManagerRepository = stationManagerRepository; _hostLogRepository = hostLogRepository; } public Task StartAsync(CancellationToken cancellationToken = default) { //TimeSpan.FromMinutes(5) _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(1)); return Task.CompletedTask; } private void DoWork(object state) { DateTime threshold = DateTime.Now.AddSeconds(-30); var hostLog = _hostLogRepository.QueryData(x => x.CreateDate > threshold || x.ModifyDate > threshold).ToList(); foreach ( var item in hostLog) { _Socket.clientSend(_Socket.MakeStringToByteMsg(item.Messgae)); Thread.Sleep(1000); } } public Task StopAsync(CancellationToken cancellationToken) { _logger.LogInformation("ReissueToHostMessage is stopping."); _timer?.Change(Timeout.Infinite, 0); return Task.CompletedTask; } public void Dispose() { _timer?.Dispose(); } } }