huangxiaoqiang
2025-06-03 73a077d76b715aee6b9f384b087a6792a84989d8
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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_Repository;
 
namespace WIDESEA_StorageTaskServices
{
    public class MyBackgroundService : IHostedService, 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)
        {
            _logger = logger;
            _socket = socketClientService;
            _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 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)
        {
            _logger.LogInformation("MyBackgroundService is stopping.");
            _timer?.Change(Timeout.Infinite, 0);
            return Task.CompletedTask;
        }
 
        public void Dispose()
        {
            _timer?.Dispose();
        }
    }
}