1
huangxiaoqiang
2025-05-26 41702c7ce4c88ad70f52d83d153dd4c596ff69d5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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<ReissueToHostMessage> _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<ReissueToHostMessage> 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();
        }
    }
}