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; /// /// 定时设备同步任务。遍历所有在线且启用的网关节点,触发全量设备同步。 /// Cron 建议: 每 5 分钟 ("0 */5 * * * ?") /// 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(); var client = sp.GetService(); 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}"); } } } }