52 lines
1.7 KiB
C#
52 lines
1.7 KiB
C#
using System.Net;
|
|
using System.Net.Sockets;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Device.Entities;
|
|
using Kurisu.AspNetCore.CustomClass;
|
|
using Kurisu.AspNetCore.Utils;
|
|
using Kurisu.Scheduled.Attributes;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Dlhis.ScheduledTask.Jobs;
|
|
|
|
/// <summary>
|
|
/// 设备心跳
|
|
/// </summary>
|
|
[RecurringJob(nameof(HeartbeatJob), "0/10 * * * * ?", Name = "设备心跳")]
|
|
public class HeartbeatJob : BaseJob
|
|
{
|
|
private readonly RedisCache _redisCache;
|
|
|
|
public HeartbeatJob(IServiceProvider serviceProvider, RedisCache redisCache) : base(serviceProvider)
|
|
{
|
|
_redisCache = redisCache;
|
|
}
|
|
|
|
public override async Task RunAsync(IServiceProvider serviceProvider)
|
|
{
|
|
const string key = "heartbeat-devices";
|
|
await _redisCache.KeyDeleteAsync(key);
|
|
|
|
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(cancellationTokenSource.Token);
|
|
|
|
await _redisCache.ListLeftPushAsync(key, device.UniqueNo);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
}
|
|
}
|
|
}
|