设备重连
This commit is contained in:
		
							parent
							
								
									95bad4cdfa
								
							
						
					
					
						commit
						c3ee874294
					
				| 
						 | 
				
			
			@ -52,8 +52,11 @@ namespace LY.App.Device
 | 
			
		|||
        private static DeviceManager _instance;
 | 
			
		||||
        private readonly RedisService _redis = ServiceLocator.Instance.GetService<RedisService>();
 | 
			
		||||
        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
 | 
			
		||||
        {
 | 
			
		||||
            get
 | 
			
		||||
| 
						 | 
				
			
			@ -330,9 +333,10 @@ namespace LY.App.Device
 | 
			
		|||
                device.IsConnected = false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private async Task HandleDeviceConnection(Device device)
 | 
			
		||||
        {
 | 
			
		||||
            int retryDelay = 1000; // 初始重连间隔(毫秒)
 | 
			
		||||
            int retryDelay = 1000; // 初始重连间隔(1秒)
 | 
			
		||||
            int maxDelay = 30000;  // 最大重连间隔(30秒)
 | 
			
		||||
 | 
			
		||||
            while (!device.IsConnected)
 | 
			
		||||
| 
						 | 
				
			
			@ -354,21 +358,35 @@ namespace LY.App.Device
 | 
			
		|||
                            await HandleMqttDevice(device);
 | 
			
		||||
                            break;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    if (device.IsConnected)
 | 
			
		||||
                    {
 | 
			
		||||
                        // 连接成功,重置重连间隔
 | 
			
		||||
                        retryDelay = 1000;
 | 
			
		||||
                        retryDelay = 1000; // 连接成功后重置重连间隔
 | 
			
		||||
                        Console.WriteLine($"设备 {device.Id} 重新连接成功");
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                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);
 | 
			
		||||
                // 指数退避算法,避免频繁重试
 | 
			
		||||
                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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -94,7 +94,7 @@ SnowFlakeSingle.WorkId = Convert.ToInt32(builder.Configuration.GetSection("SnowF
 | 
			
		|||
var app = builder.Build();
 | 
			
		||||
ServiceLocator.Instance = app.Services;
 | 
			
		||||
var device = app.Services.GetService<DeviceService>();
 | 
			
		||||
device?.Init();
 | 
			
		||||
 await device?.Init();
 | 
			
		||||
app.UseStaticFiles(new StaticFileOptions()
 | 
			
		||||
{
 | 
			
		||||
    FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "Img")),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue