55 lines
1.8 KiB
C#
55 lines
1.8 KiB
C#
using System.Net;
|
|
using System.Net.Sockets;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Device.Entities;
|
|
using Kurisu.Core.DataAccess.Entity;
|
|
using Kurisu.SqlSugar.Services;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Dlhis.ScheduledTask.CommonJobs;
|
|
|
|
public class HeartbeatJob
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly ILogger<HeartbeatJob> _logger;
|
|
|
|
public HeartbeatJob(IDbContext dbContext, ILogger<HeartbeatJob> logger)
|
|
{
|
|
_dbContext = dbContext;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task RunAsync()
|
|
{
|
|
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
|
|
try
|
|
{
|
|
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Enable).Where(x => x.HeartbeatInterval > 0).ToListAsync();
|
|
foreach (var device in devices)
|
|
{
|
|
try
|
|
{
|
|
var ipInfo = JsonConvert.DeserializeObject<IPPortDto>(device.ConnectionInfo);
|
|
|
|
//超时时间
|
|
var CancellationTokenSource = new CancellationTokenSource(300);
|
|
await new TcpClient().ConnectAsync(IPAddress.Parse(ipInfo.IP), ipInfo.Port, CancellationTokenSource.Token);
|
|
|
|
await _dbContext.Updateable<DeviceRegistryEntity>().SetColumns(x => x.LastHeartbeatTime == DateTime.Now)
|
|
.Where(x => x.DeviceUniqueNo == device.UniqueNo).ExecuteCommandAsync();
|
|
|
|
_logger.LogInformation("心跳设备成功:{no}", device.UniqueNo);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "心跳设备失败:{message}", ex.Message);
|
|
}
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
_dbContext.Client.QueryFilter.Restore();
|
|
}
|
|
}
|
|
}
|