redis 阵地区域

This commit is contained in:
yanghongwei 2025-03-26 10:47:56 +08:00
parent 86e73f4ef6
commit 6d9acac89d
4 changed files with 58 additions and 25 deletions

View File

@ -39,5 +39,9 @@
{ {
return $"device_token_{id}"; return $"device_token_{id}";
} }
public static string PositioinRegion(long id)
{
return $"position_region_{id}";
}
} }
} }

View File

@ -150,4 +150,15 @@ namespace LY.App.Model
/// </summary> /// </summary>
public long Id { get; set; } public long Id { get; set; }
} }
/// <summary>
/// 阵地下所有区域
/// </summary>
public class GeoRegion
{
/// <summary>
/// 识别区
/// </summary>
public string Region { get; set; }
}
} }

View File

@ -5,8 +5,12 @@ using LY.App.Extensions.DI;
using LY.App.Model; using LY.App.Model;
using Mapster; using Mapster;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO;
using SqlSugar; using SqlSugar;
using StackExchange.Redis; using StackExchange.Redis;
using System.Collections.Concurrent;
using System.Reflection;
namespace LY.App.Service namespace LY.App.Service
{ {
@ -58,6 +62,7 @@ namespace LY.App.Service
item.PostionName = deviceinfo.PositionName; item.PostionName = deviceinfo.PositionName;
item.Time = input.time; 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(); await _db.CopyNew().Insertable(entity).SplitTable().ExecuteReturnSnowflakeIdListAsync();
//推送报警信息 //推送报警信息
@ -65,6 +70,34 @@ namespace LY.App.Service
} }
return new ApiResult(); return new ApiResult();
} }
/// <summary>
/// 计算入侵级别
/// </summary>
/// <param name="positionId"></param>
/// <param name="lon"></param>
/// <param name="lat"></param>
/// <returns></returns>
private async Task<int> 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<string>(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;
}
/// <summary> /// <summary>
/// 推送消息 /// 推送消息
/// </summary> /// </summary>

View File

@ -160,32 +160,17 @@ namespace LY.App.Service
foreach (var item in devices) foreach (var item in devices)
{ {
var key = RedisKeyList.DeviceInfo(item.DeviceSN); 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); await AddDevice2Manager(item);
} }
//deviceManager.AddDevice(new Device.Device() // 加载阵地信息
//{ var position = await _db.Queryable<PositionInfo>()
// Id = 1111, .Where(s=>s.IsDeleted==false).ToListAsync();
// Port = 1883, foreach (var item in position)
// GraphQlEndpoint = "http://localhost:5000/graphql", {
// Topic = "F010/uav_dynamic_data/beijing", var key = RedisKeyList.PositioinRegion(item.Id);
// IpAddress = "110.42.53.32", await _redisService.SetAsync(key, item.Region, TimeSpan.FromDays(100));
// Protocol = ProtocolType.MQTT, }
// username = "yangzi",
// password = "64B7qvztS38l",
// DataReceived = async (data) =>
// {
// try
// {
// var input = JsonConvert.DeserializeObject<RevData>(data);
// await _alarmService.AddAlarm(input);
// }
// catch (Exception ex)
// {
// Console.WriteLine($"error:{ex.Message},data:{data}");
// }
// }
//});
} }
} }
} }