diff --git a/Device/DeviceManager.cs b/Device/DeviceManager.cs index 39235c8..ce75e19 100644 --- a/Device/DeviceManager.cs +++ b/Device/DeviceManager.cs @@ -16,6 +16,7 @@ using Newtonsoft.Json; using StackExchange.Redis; using LY.App.Common.HttpUtil; using LY.App.Device.Command; +using LY.App.Service; namespace LY.App.Device { @@ -53,7 +54,9 @@ namespace LY.App.Device private readonly RedisService _redis = ServiceLocator.Instance.GetService(); private static readonly object _lock = new object(); private readonly CancellationTokenSource _monitorCancellationTokenSource = new(); - private DeviceManager() { + private readonly LogService _log = ServiceLocator.Instance.GetService(); + private DeviceManager() + { Task.Run(() => MonitorDevices(_monitorCancellationTokenSource.Token)); } @@ -124,6 +127,13 @@ namespace LY.App.Device return _devices; } + /// + /// 下发命令 + /// + /// + /// + /// + /// public async Task SendCommand(long deviceId, string command) { if (_devices.TryGetValue(deviceId, out var device)) @@ -295,7 +305,6 @@ namespace LY.App.Device { Console.WriteLine($"设备 {device.Id} 掉线,尝试重连..."); device.IsConnected = false; - await HandleDeviceConnection(device); }; mqttClient.ApplicationMessageReceivedAsync += e => @@ -312,20 +321,6 @@ namespace LY.App.Device _mqttClients[device.Id] = mqttClient; // 保存 MQTT 客户端实例 device.IsConnected = true; } - - - //while (device.isconnected) - //{ - // await task.delay(1000); - //} - - // 循环结束后断开连接 - //if (!mqttClient.IsConnected) - //{ - // await mqttClient.DisconnectAsync(); - // mqttClient.Dispose(); - //} - } catch (Exception ex) { @@ -338,7 +333,7 @@ namespace LY.App.Device { int retryDelay = 1000; // 初始重连间隔(1秒) int maxDelay = 30000; // 最大重连间隔(30秒) - + await _log.AddLog(new AddLog { Message = $"设备 {device.Id} 掉线,重新连接中...", Parameters = "", StackTrace = "", url = "" }); while (!device.IsConnected) { try @@ -363,6 +358,7 @@ namespace LY.App.Device { retryDelay = 1000; // 连接成功后重置重连间隔 Console.WriteLine($"设备 {device.Id} 重新连接成功"); + await _log.AddLog(new AddLog { Message = $"设备 {device.Id} 重新连接成功", Parameters = "", StackTrace = "", url = "" }); } } catch (Exception ex) @@ -374,6 +370,11 @@ namespace LY.App.Device retryDelay = Math.Min(retryDelay * 2, maxDelay); // 指数退避算法,避免频繁重试 } } + /// + /// 监控设备状态 + /// + /// + /// private async Task MonitorDevices(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) @@ -389,6 +390,11 @@ namespace LY.App.Device await Task.Delay(10000, cancellationToken); // 每 10 秒检查一次设备状态 } } + /// + /// 测试设备连接 + /// + /// + /// public async Task TestConnection(long deviceId) { if (_devices.TryGetValue(deviceId, out var device)) diff --git a/Service/AlarmService.cs b/Service/AlarmService.cs index 095c61c..b57f737 100644 --- a/Service/AlarmService.cs +++ b/Service/AlarmService.cs @@ -91,7 +91,7 @@ namespace LY.App.Service .Where(s => s.Id == positionId) .Select(s => s.Region) .FirstAsync(); - await _redisService.SetAsync(key, geodata); + await _redisService.SetAsync(key, geodata); } WKTReader reader = new WKTReader(); Geometry point = reader.Read($"POINT ({lon} {lat})"); @@ -124,7 +124,7 @@ namespace LY.App.Service //更新 设备缓存 var key = RedisKeyList.DeviceInfo(input.product_ad_id); var deviceinfo = await _redisService.GetAsync(key); - if (deviceinfo == null) + if (deviceinfo != null) { deviceinfo.Lat = input.product_lat; deviceinfo.Lon = input.product_lon; diff --git a/Service/DeviceService.cs b/Service/DeviceService.cs index f0a1b8b..094927c 100644 --- a/Service/DeviceService.cs +++ b/Service/DeviceService.cs @@ -137,7 +137,7 @@ namespace LY.App.Service try { var input = JsonConvert.DeserializeObject(data); - Console.WriteLine($"rev data:{data}"); + // Console.WriteLine($"rev data:{data}"); await _alarmService.AddAlarm(input); } catch (Exception ex)