设备重连

This commit is contained in:
yanghongwei 2025-03-26 14:41:32 +08:00
parent 95bad4cdfa
commit c3ee874294
2 changed files with 27 additions and 9 deletions

View File

@ -52,8 +52,11 @@ namespace LY.App.Device
private static DeviceManager _instance; private static DeviceManager _instance;
private readonly RedisService _redis = ServiceLocator.Instance.GetService<RedisService>(); private readonly RedisService _redis = ServiceLocator.Instance.GetService<RedisService>();
private static readonly object _lock = new object(); private static readonly object _lock = new object();
private readonly CancellationTokenSource _monitorCancellationTokenSource = new();
private DeviceManager() {
private DeviceManager() { } Task.Run(() => MonitorDevices(_monitorCancellationTokenSource.Token));
}
public static DeviceManager Instance public static DeviceManager Instance
{ {
get get
@ -330,9 +333,10 @@ namespace LY.App.Device
device.IsConnected = false; device.IsConnected = false;
} }
} }
private async Task HandleDeviceConnection(Device device) private async Task HandleDeviceConnection(Device device)
{ {
int retryDelay = 1000; // 初始重连间隔(秒) int retryDelay = 1000; // 初始重连间隔(1秒)
int maxDelay = 30000; // 最大重连间隔30秒 int maxDelay = 30000; // 最大重连间隔30秒
while (!device.IsConnected) while (!device.IsConnected)
@ -354,21 +358,35 @@ namespace LY.App.Device
await HandleMqttDevice(device); await HandleMqttDevice(device);
break; break;
} }
if (device.IsConnected) if (device.IsConnected)
{ {
// 连接成功,重置重连间隔 retryDelay = 1000; // 连接成功后重置重连间隔
retryDelay = 1000; Console.WriteLine($"设备 {device.Id} 重新连接成功");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"设备 {device.Id} 连接失败,{ex.Message}{retryDelay}ms 后重试..."); Console.WriteLine($"设备 {device.Id} 连接失败,{retryDelay}ms 后重试... 错误: {ex.Message}");
} }
Console.WriteLine($"设备 {device.Id} 连接失败,{retryDelay}ms 后重试...");
await Task.Delay(retryDelay); await Task.Delay(retryDelay);
// 指数退避算法,避免频繁重试 retryDelay = Math.Min(retryDelay * 2, maxDelay); // 指数退避算法,避免频繁重试
retryDelay = Math.Min(retryDelay * 2, maxDelay); }
}
private async Task MonitorDevices(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
foreach (var device in _devices.Values)
{
if (!device.IsConnected)
{
Console.WriteLine($"设备 {device.Id} 掉线,尝试重新连接...");
_ = Task.Run(() => HandleDeviceConnection(device)); // 异步执行设备重连
}
}
await Task.Delay(10000, cancellationToken); // 每 10 秒检查一次设备状态
} }
} }
public async Task<bool> TestConnection(long deviceId) public async Task<bool> TestConnection(long deviceId)

View File

@ -94,7 +94,7 @@ SnowFlakeSingle.WorkId = Convert.ToInt32(builder.Configuration.GetSection("SnowF
var app = builder.Build(); var app = builder.Build();
ServiceLocator.Instance = app.Services; ServiceLocator.Instance = app.Services;
var device = app.Services.GetService<DeviceService>(); var device = app.Services.GetService<DeviceService>();
device?.Init(); await device?.Init();
app.UseStaticFiles(new StaticFileOptions() app.UseStaticFiles(new StaticFileOptions()
{ {
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "Img")), FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "Img")),