fix 设备任务获取结合心跳

This commit is contained in:
2024-04-04 15:15:18 +08:00
parent 82ef9e8cd6
commit 53e796ff53
3 changed files with 21 additions and 3 deletions

View File

@@ -2,6 +2,7 @@
using System.Net.Sockets;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Kurisu.AspNetCore.Utils;
using Kurisu.Core.CustomClass;
using Kurisu.Scheduled;
using Kurisu.Scheduled.Attributes;
@@ -15,12 +16,18 @@ namespace Dlhis.ScheduledTask.Jobs;
[RecurringJob(nameof(HeartbeatJob), "0/10 * * * * ?", Name = "设备心跳")]
public class HeartbeatJob : BaseJob
{
public HeartbeatJob(IServiceProvider serviceProvider) : base(serviceProvider)
private readonly RedisCache _redisCache;
public HeartbeatJob(IServiceProvider serviceProvider, RedisCache redisCache) : base(serviceProvider)
{
_redisCache = redisCache;
}
public override async Task RunAsync(IServiceProvider serviceProvider)
{
var key = "heardbeat-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)
{
@@ -34,6 +41,8 @@ public class HeartbeatJob : BaseJob
await DbContext.Updateable<DeviceRegistryEntity>().SetColumns(x => x.LastHeartbeatTime == DateTime.Now)
.Where(x => x.DeviceUniqueNo == device.UniqueNo).ExecuteCommandAsync();
await _redisCache.ListLeftPushAsync(key, device.UniqueNo);
}
catch (Exception)
{

View File

@@ -1,4 +1,5 @@
using Kurisu.AspNetCore.Startup;
using Kurisu.AspNetCore.Utils;
using Kurisu.AspNetCore.Utils.Extensions;
using Kurisu.Startup;
@@ -25,6 +26,6 @@ public class Startup : DefaultStartup
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
base.Configure(app, env);
app.UseSnowFlakeDistributedInitialize();
SnowFlakeHelper.Initialize(2, 1);
}
}

View File

@@ -14,6 +14,7 @@ using Kurisu.SqlSugar.Extensions;
using Newtonsoft.Json.Linq;
using Kurisu.Core.CustomClass;
using Kurisu.AspNetCore;
using Kurisu.AspNetCore.Utils;
namespace Dlhis.ScheduledTask.TaskAssignment.Services.Implements;
@@ -21,15 +22,21 @@ public class AssignmentService : IAssignmentService
{
private readonly IDbContext _dbContext;
private readonly ILogger<AssignmentService> _logger;
private readonly RedisCache _redisCache;
public AssignmentService(IDbContext dbContext, ILogger<AssignmentService> logger)
public AssignmentService(IDbContext dbContext, ILogger<AssignmentService> logger, RedisCache redisCache)
{
_dbContext = dbContext;
_logger = logger;
_redisCache = redisCache;
}
public async Task<List<DeviceTaskToDeviceByDeviceResponse>> DeviceGetTasksAsync(DeviceTaskGetRequest request)
{
//获取心跳设备
var key = "heardbeat-devices";
var heartbeatDeviceNos = (await _redisCache.ListRangeAsync(key))?.Select(x => x.ToString()).ToList();
var data = await ContinueUpdateAsync(async () =>
{
var page = await _dbContext.Queryable<DeviceTaskEntity>()
@@ -42,6 +49,7 @@ public class AssignmentService : IAssignmentService
.WhereIF(request.Types.IsPresent(), deviceTask => request.Types.Contains(deviceTask.Type))
.WhereIF(!request.IsProcess, deviceTask => deviceTask.Status == DeviceTaskStatusEnumType.Wait)
.WhereIF(request.IsProcess, deviceTask => deviceTask.Status == DeviceTaskStatusEnumType.Process)
.Where(deviceTask => heartbeatDeviceNos.Contains(deviceTask.DeviceUniqueNo))
.OrderBy(deviceTask => deviceTask.CreateTime)
.Select((deviceTask, deivce, deivceTamplate, ga, department) => new
{