From 6d9acac89dd7ee44157a0a3a237b3c83b0181534 Mon Sep 17 00:00:00 2001 From: yanghongwei Date: Wed, 26 Mar 2025 10:47:56 +0800 Subject: [PATCH] =?UTF-8?q?redis=20=E9=98=B5=E5=9C=B0=E5=8C=BA=E5=9F=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/Redis/RedisKeyList.cs | 4 ++++ Model/PositionInfo.cs | 11 +++++++++++ Service/AlarmService.cs | 35 ++++++++++++++++++++++++++++++++++- Service/DeviceService.cs | 33 +++++++++------------------------ 4 files changed, 58 insertions(+), 25 deletions(-) diff --git a/Common/Redis/RedisKeyList.cs b/Common/Redis/RedisKeyList.cs index de5bbad..6c03f11 100644 --- a/Common/Redis/RedisKeyList.cs +++ b/Common/Redis/RedisKeyList.cs @@ -39,5 +39,9 @@ { return $"device_token_{id}"; } + public static string PositioinRegion(long id) + { + return $"position_region_{id}"; + } } } diff --git a/Model/PositionInfo.cs b/Model/PositionInfo.cs index 1990ae3..5a50e9b 100644 --- a/Model/PositionInfo.cs +++ b/Model/PositionInfo.cs @@ -150,4 +150,15 @@ namespace LY.App.Model /// public long Id { get; set; } } + /// + /// 阵地下所有区域 + /// + public class GeoRegion + { + /// + /// 识别区 + /// + public string Region { get; set; } + + } } diff --git a/Service/AlarmService.cs b/Service/AlarmService.cs index 424e771..659376f 100644 --- a/Service/AlarmService.cs +++ b/Service/AlarmService.cs @@ -5,8 +5,12 @@ using LY.App.Extensions.DI; using LY.App.Model; using Mapster; using Microsoft.AspNetCore.SignalR; +using NetTopologySuite.Geometries; +using NetTopologySuite.IO; using SqlSugar; using StackExchange.Redis; +using System.Collections.Concurrent; +using System.Reflection; namespace LY.App.Service { @@ -57,7 +61,8 @@ namespace LY.App.Service item.positionId = deviceinfo.PositionId; item.PostionName = deviceinfo.PositionName; item.Time = input.time; - item.distance = GisHelper.HaversineDistance(item.drone_lat, item.drone_lon, item.app_lat,item.app_lon); + item.distance = GisHelper.HaversineDistance(item.drone_lat, item.drone_lon, item.app_lat, item.app_lon); + item.alarmLevel = await GetAlarmLevel(deviceinfo.PositionId, item.drone_lon, item.drone_lat); } await _db.CopyNew().Insertable(entity).SplitTable().ExecuteReturnSnowflakeIdListAsync(); //推送报警信息 @@ -65,6 +70,34 @@ namespace LY.App.Service } return new ApiResult(); } + + /// + /// 计算入侵级别 + /// + /// + /// + /// + /// + private async Task GetAlarmLevel(long positionId, double lon, double lat) + { + int result = 0; + if (positionId > 0 && lon > 0 && lat > 0) + { + var key = RedisKeyList.PositioinRegion(positionId); + var geodata = await _redisService.GetAsync(key); + WKTReader reader = new WKTReader(); + Geometry point = reader.Read($"POINT ({lon} {lat})"); + if (!string.IsNullOrEmpty(geodata)) + { + Geometry multipolygon = reader.Read(geodata); + if (multipolygon.Contains(point)) + { + return 1; + } + } + } + return result; + } /// /// 推送消息 /// diff --git a/Service/DeviceService.cs b/Service/DeviceService.cs index 8ef8b3b..8cbae90 100644 --- a/Service/DeviceService.cs +++ b/Service/DeviceService.cs @@ -160,32 +160,17 @@ namespace LY.App.Service foreach (var item in devices) { var key = RedisKeyList.DeviceInfo(item.DeviceSN); - await _redisService.SetAsync(key, item, TimeSpan.FromDays(10)); + await _redisService.SetAsync(key, item, TimeSpan.FromDays(100)); await AddDevice2Manager(item); } - //deviceManager.AddDevice(new Device.Device() - //{ - // Id = 1111, - // Port = 1883, - // GraphQlEndpoint = "http://localhost:5000/graphql", - // Topic = "F010/uav_dynamic_data/beijing", - // IpAddress = "110.42.53.32", - // Protocol = ProtocolType.MQTT, - // username = "yangzi", - // password = "64B7qvztS38l", - // DataReceived = async (data) => - // { - // try - // { - // var input = JsonConvert.DeserializeObject(data); - // await _alarmService.AddAlarm(input); - // } - // catch (Exception ex) - // { - // Console.WriteLine($"error:{ex.Message},data:{data}"); - // } - // } - //}); + // 加载阵地信息 + var position = await _db.Queryable() + .Where(s=>s.IsDeleted==false).ToListAsync(); + foreach (var item in position) + { + var key = RedisKeyList.PositioinRegion(item.Id); + await _redisService.SetAsync(key, item.Region, TimeSpan.FromDays(100)); + } } } }