47 lines
1.5 KiB
C#
47 lines
1.5 KiB
C#
using Quartz;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Warehouse.IServices;
|
|
using VolPro.Entity.DomainModels;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace VolPro.Warehouse.Services;
|
|
|
|
/// <summary>
|
|
/// 定时设备同步任务。遍历所有在线且启用的网关节点,触发全量设备同步。
|
|
/// Cron 建议: 每 5 分钟 ("0 */5 * * * ?")
|
|
/// </summary>
|
|
public class SyncDevicesJob : IJob
|
|
{
|
|
private readonly IServiceProvider _sp;
|
|
public SyncDevicesJob(IServiceProvider sp) { _sp = sp; }
|
|
|
|
public async Task Execute(IJobExecutionContext? context)
|
|
{
|
|
var sp = _sp;
|
|
var gwSvc = sp.GetService<Igateway_nodesService>();
|
|
var client = sp.GetService<GatewayClient>();
|
|
if (gwSvc == null || client == null) return;
|
|
|
|
// 遍历所有在线且启用的网关
|
|
var onlineNodes = await gwSvc.FindAsIQueryable(
|
|
x => x.IsOnline == "在线" && x.Enable == "启用" && x.BaseUrl != null)
|
|
.ToListAsync();
|
|
|
|
foreach (var node in onlineNodes)
|
|
{
|
|
try
|
|
{
|
|
// 触发网关全量同步
|
|
await client.TriggerFullSyncAsync(node.BaseUrl!, node.AdapterTypes ?? "");
|
|
Console.WriteLine($"[SyncDevicesJob] 网关 {node.NodeCode} 同步触发成功");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.Error.WriteLine($"[SyncDevicesJob] 网关 {node.NodeCode} 同步失败: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|