fix 任务根据设备心跳状态获取

This commit is contained in:
2024-06-09 02:40:08 +08:00
parent 0a7898cd73
commit 82a3994d4d
9 changed files with 90 additions and 67 deletions

View File

@@ -1,4 +1,5 @@
using Dlhis.Basic.Device.Request;
using Dlhis.Basic.Device.Dtos;
using Dlhis.Basic.Device.Request;
using Dlhis.Basic.Device.Response;
namespace Dlhis.Basic.Device.Services;
@@ -31,7 +32,7 @@ public interface IDeviceConnectService : IScopeDependency
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
Task HeartbeatAsync(DeviceHeartbeatRequest request);
Task<int> HeartbeatAsync(DeviceHeartbeatRequest request);
/// <summary>
/// 尝试连接
@@ -39,4 +40,10 @@ public interface IDeviceConnectService : IScopeDependency
/// <param name="request"></param>
/// <returns></returns>
Task<bool> TryConnectAsync(DeviceConnectionTryRequest request);
}
/// <summary>
/// 获取在线设备信息
/// </summary>
/// <returns></returns>
Task<List<DeviceUniqueWithConnectionInfoDto>> GetOnlineDeviceUniqueInfoAsync();
}

View File

@@ -12,8 +12,6 @@ public interface IDeviceService : IScopeDependency
Task<bool> CheckExistsAsync(string deviceUniqueNo);
Task<DeviceResponse> GetDeviceByNoAsync(DeviceByNoRequest request);
Task<List<DeviceResponse>> GetAttendanceDevicesAsync();
Task<DeviceNumberResponse> GetDeviceNumberAsync();

View File

@@ -1,5 +1,6 @@
using System.Net;
using System.Net.Sockets;
using Dlhis.Basic.Device.Dtos;
using Dlhis.Basic.Device.Request;
using Dlhis.Basic.Device.Response;
using Dlhis.Entity.Basic.Entity;
@@ -9,6 +10,7 @@ using Kurisu.AspNetCore.CustomClass;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
using Mapster;
using SqlSugar;
namespace Dlhis.Basic.Device.Services.Implements;
@@ -71,7 +73,7 @@ public class DeviceConnectService : IDeviceConnectService
if (request.BusinessPlaceCode.HasValue)
{
var businessPlace = await _dbContext.Queryable<BusinessPlaceEntity>().Where(x => x.Code == request.BusinessPlaceCode).FirstAsync();
businessPlace.ThrowIfNull("营业点不存在,无法设置到设备注册信息中");
businessPlace.ThrowIfNull("营业点不存在,无法设置到设备注册信息中");
}
//更新营业点信息
@@ -81,13 +83,12 @@ public class DeviceConnectService : IDeviceConnectService
}
public async Task HeartbeatAsync(DeviceHeartbeatRequest request)
public async Task<int> HeartbeatAsync(DeviceHeartbeatRequest request)
{
var registry = await _dbContext.Queryable<DeviceRegistryEntity>().Where(x => x.DeviceUniqueNo == request.UniqueNo).FirstAsync();
registry.ThrowIfNull($"注册信息不存在,无法发起心跳");
registry.LastHeartbeatTime = DateTime.Now;
await _dbContext.UpdateAsync(registry);
return await _dbContext.Updateable<DeviceRegistryEntity>()
.SetColumns(x => x.LastHeartbeatTime == DateTime.Now)
.Where(x => x.DeviceUniqueNo == request.UniqueNo)
.ExecuteCommandAsync();
}
@@ -110,6 +111,20 @@ public class DeviceConnectService : IDeviceConnectService
}
public async Task<List<DeviceUniqueWithConnectionInfoDto>> GetOnlineDeviceUniqueInfoAsync()
{
return await _dbContext.Queryable<DeviceEntity>()
.LeftJoin<DeviceRegistryEntity>((d, dr) => d.UniqueNo == dr.DeviceUniqueNo)
.Where((d, dr) => d.Enable && SqlFunc.DateAdd(dr.LastHeartbeatTime, SqlFunc.IIF(d.HeartbeatInterval == 0, 30, 30), DateType.Second) > DateTime.Now)
.Select((d, dr) => new DeviceUniqueWithConnectionInfoDto
{
DeviceUnique = d.UniqueNo,
ConnectionInfo = d.ConnectionInfo
})
.ToListAsync();
}
//******************************************************** help methods **********************************************************
/// <summary>
@@ -123,8 +138,8 @@ public class DeviceConnectService : IDeviceConnectService
private async Task<DeviceEntity> GetEnableDeviceAsync(string uniqueNo, bool checkExists = true, string deployPlace = "", string tenantId = "")
{
var device = await _dbContext.Queryable<DeviceEntity>()
.Where(x => x.UniqueNo == uniqueNo)
.FirstAsync();
.Where(x => x.UniqueNo == uniqueNo)
.FirstAsync();
if (checkExists)
{
@@ -165,4 +180,4 @@ public class DeviceConnectService : IDeviceConnectService
return device;
}
}
}

View File

@@ -274,25 +274,7 @@ public class DeviceService : IDeviceService
_dbContext.Client.QueryFilter.Restore();
}
}
/// <summary>
/// 获取设备
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<DeviceResponse> GetDeviceByNoAsync(DeviceByNoRequest request)
{
var device = await _dbContext.Queryable<DeviceEntity>()
.Where(x => x.UniqueNo == request.UniqueNo)
.Select(device => new DeviceResponse
{
FuncsStr = device.Funcs.ToString(),
ConnectionInfoStr = device.ConnectionInfo
}, true)
.SingleAsync();
return device;
}
public async Task<List<DeviceResponse>> GetAttendanceDevicesAsync()
{
@@ -347,7 +329,7 @@ public class DeviceService : IDeviceService
var onlineDevices = deviceRegistries.Where(x =>
{
if (x.HeartbeatInterval <= 0) return true;
return x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval * 3) >= now;
return x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval) >= now;
})
.Select(x => new
{