| | |
| | | using WIDESEAWCS_DTO.BasicInfo; |
| | | using WIDESEAWCS_QuartzJob.Models; |
| | | using WIDESEAWCS_QuartzJob.Repository; |
| | | using WIDESEAWCS_Common; |
| | | using ICacheService = WIDESEAWCS_Core.Caches.ICacheService; |
| | | |
| | | namespace WIDESEAWCS_QuartzJob.Service |
| | | { |
| | |
| | | { |
| | | private readonly IDeviceProtocolRepository _deviceProtocolRepository; |
| | | private readonly IDeviceInfoRepository _deviceInfoRepository; |
| | | private readonly ICacheService _cacheService; |
| | | |
| | | /// <summary> |
| | | /// 路由配置业务层 |
| | |
| | | /// <param name="BaseDal"></param> |
| | | /// <param name="deviceProtocolRepository"></param> |
| | | /// <param name="deviceInfoRepository"></param> |
| | | public RouterService(IRouterRepository BaseDal, IDeviceProtocolRepository deviceProtocolRepository, IDeviceInfoRepository deviceInfoRepository) : base(BaseDal) |
| | | public RouterService(IRouterRepository BaseDal, IDeviceProtocolRepository deviceProtocolRepository, IDeviceInfoRepository deviceInfoRepository, ICacheService cacheService) : base(BaseDal) |
| | | { |
| | | _deviceProtocolRepository = deviceProtocolRepository; |
| | | _deviceInfoRepository = deviceInfoRepository; |
| | | _cacheService = cacheService; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | { |
| | | // 一次性查询所有路由数据到内存 |
| | | List<Dt_Router> allRouters = BaseDal.QueryData(x => true); |
| | | |
| | | |
| | | // 在内存中进行路径搜索 |
| | | routers = FindRoutesInMemory(startPosi, endPosi, allRouters, null); |
| | | |
| | | |
| | | if (routers.Count == 0) |
| | | { |
| | | throw new Exception($"该路径未配置或配置错误,请检查设备路由信息,起点:【{startPosi}】,终点:【{endPosi}】"); |
| | |
| | | { |
| | | // 一次性查询指定类型的所有路由数据到内存 |
| | | List<Dt_Router> allRouters = BaseDal.QueryData(x => x.InOutType == routeType); |
| | | |
| | | |
| | | // 在内存中进行路径搜索 |
| | | routers = FindRoutesInMemory(startPosi, endPosi, allRouters, routeType); |
| | | |
| | | |
| | | if (routers.Count == 0) |
| | | { |
| | | throw new Exception($"该路径未配置或配置错误,请检查设备路由信息,起点:【{startPosi}】,终点:【{endPosi}】"); |
| | |
| | | var routersByNext = allRouters |
| | | .GroupBy(r => r.NextPosi) |
| | | .ToDictionary(g => g.Key, g => g.ToList()); |
| | | |
| | | |
| | | var routersByChild = allRouters |
| | | .GroupBy(r => r.ChildPosi) |
| | | .ToDictionary(g => g.Key, g => g.ToList()); |
| | |
| | | |
| | | // 使用队列进行广度优先搜索 |
| | | Queue<(Dt_Router router, List<Dt_Router> path)> queue = new Queue<(Dt_Router, List<Dt_Router>)>(); |
| | | |
| | | |
| | | // 将所有终点路由加入队列 |
| | | foreach (var endRouter in endRouters) |
| | | { |
| | |
| | | try |
| | | { |
| | | // 查询从起点出发的所有路由 |
| | | List<Dt_Router> routes = BaseDal.QueryData(x => x.StartPosi == startPosi, |
| | | List<Dt_Router> routes = BaseDal.QueryData(x => x.StartPosi == startPosi, |
| | | new Dictionary<string, OrderByType> { { nameof(Dt_Router.IsEnd), OrderByType.Desc } }); |
| | | |
| | | |
| | | // 返回第一个路由 |
| | | return routes.FirstOrDefault(); |
| | | } |
| | |
| | | try |
| | | { |
| | | // 查询从起点出发的指定类型路由 |
| | | List<Dt_Router> routes = BaseDal.QueryData(x => x.StartPosi == startPosi && x.InOutType == routeType, |
| | | List<Dt_Router> routes = BaseDal.QueryData(x => x.StartPosi == startPosi && x.InOutType == routeType, |
| | | new Dictionary<string, OrderByType> { { nameof(Dt_Router.IsEnd), OrderByType.Desc } }); |
| | | |
| | | |
| | | // 返回第一个路由 |
| | | return routes.FirstOrDefault(); |
| | | } |
| | |
| | | try |
| | | { |
| | | // 查询从起点出发的指定类型路由 |
| | | List<Dt_Router> routes = BaseDal.QueryData(x => x.StartPosi == startPosi && x.InOutType == routeType, |
| | | List<Dt_Router> routes = BaseDal.QueryData(x => x.StartPosi == startPosi && x.InOutType == routeType, |
| | | new Dictionary<string, OrderByType> { { nameof(Dt_Router.IsEnd), OrderByType.Desc } }); |
| | | |
| | | |
| | | if (routes.Count == 0) |
| | | return null; |
| | | |
| | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据起点、终点方向和路由类型获取下一个单个子节点路由(智能选择朝向终点的路由) |
| | | /// </summary> |
| | | /// <param name="startPosi">起点/当前位置</param> |
| | | /// <param name="endPosi">终点位置(用于方向判断)</param> |
| | | /// <returns>返回下一个路由节点,优先返回朝向终点的路由,如果没有则返回null</returns> |
| | | public Dt_Router QueryNextRoute(string startPosi, string endPosi) |
| | | { |
| | | try |
| | | { |
| | | // 查询从起点出发的指定类型路由 |
| | | List<Dt_Router> routes = BaseDal.QueryData(x => x.StartPosi == startPosi, |
| | | new Dictionary<string, OrderByType> { { nameof(Dt_Router.IsEnd), OrderByType.Desc } }); |
| | | |
| | | if (routes.Count == 0) |
| | | return null; |
| | | |
| | | // 优先选择直接指向终点的路由 |
| | | Dt_Router directRoute = routes.FirstOrDefault(x => x.NextPosi == endPosi || x.ChildPosi == endPosi); |
| | | if (directRoute != null) |
| | | return directRoute; |
| | | |
| | | //// 如果没有直接路由,使用查找算法找到朝向终点的路由 |
| | | //List<Dt_Router> allRouters = BaseDal.QueryData(x => x.InOutType == routeType); |
| | | //foreach (var route in routes) |
| | | //{ |
| | | // // 检查从这个路由的下一个位置是否能到达终点 |
| | | // var pathToEnd = FindRoutesInMemory(route.NextPosi, endPosi, allRouters, routeType); |
| | | // if (pathToEnd.Count > 0) |
| | | // return route; |
| | | //} |
| | | |
| | | // 如果都不能到达终点,返回第一个路由 |
| | | return routes.FirstOrDefault(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | // 记录错误信息 |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取从起点到终点的完整路径(按顺序返回每个子节点路由) |
| | | /// </summary> |
| | | /// <param name="startPosi">起点位置</param> |
| | |
| | | List<Dt_Router> path = new List<Dt_Router>(); |
| | | string currentPosi = startPosi; |
| | | HashSet<string> visitedPositions = new HashSet<string>(); |
| | | |
| | | |
| | | try |
| | | { |
| | | while (currentPosi != endPosi) |
| | |
| | | visitedPositions.Add(currentPosi); |
| | | |
| | | Dt_Router nextRoute = QueryNextRoute(currentPosi, endPosi, routeType); |
| | | |
| | | |
| | | if (nextRoute == null) |
| | | { |
| | | break; |
| | | } |
| | | |
| | | |
| | | path.Add(nextRoute); |
| | | currentPosi = nextRoute.NextPosi; |
| | | |
| | | |
| | | if (path.Count > 1000) |
| | | { |
| | | break; |
| | | } |
| | | } |
| | | |
| | | |
| | | if (currentPosi != endPosi) |
| | | { |
| | | return new List<Dt_Router>(); |
| | |
| | | { |
| | | return new List<Dt_Router>(); |
| | | } |
| | | |
| | | |
| | | return path; |
| | | } |
| | | |
| | |
| | | { |
| | | // 创建一个字符串列表,用于存储所有位置 |
| | | List<string> positions = new List<string>(); |
| | | try |
| | | var device = _cacheService.Get<List<string>>($"{RedisPrefix.System}:{RedisName.DevicePositions}:{deviceCode}"); |
| | | if (device.IsNullOrEmpty()) |
| | | { |
| | | // 查询所有进入路由器的位置 |
| | | List<string> inRouterPositions = BaseDal.QueryData(x => x.ChildPosiDeviceCode == deviceCode && x.InOutType == RouterInOutType.In.ObjToInt()).GroupBy(x => x.StartPosi).Select(x => x.Key).ToList(); |
| | | try |
| | | { |
| | | // 查询所有进入路由器的位置 |
| | | List<string> inRouterPositions = BaseDal.QueryData(x => x.ChildPosiDeviceCode == deviceCode && x.InOutType == RouterInOutType.In.ObjToInt()).GroupBy(x => x.StartPosi).Select(x => x.Key).ToList(); |
| | | |
| | | // 查询所有离开路由器的位置 |
| | | List<string> outRouterPositions = BaseDal.QueryData(x => x.ChildPosiDeviceCode == deviceCode && x.InOutType == RouterInOutType.Out.ObjToInt()).GroupBy(x => x.ChildPosi).Select(x => x.Key).ToList(); |
| | | // 查询所有离开路由器的位置 |
| | | List<string> outRouterPositions = BaseDal.QueryData(x => x.ChildPosiDeviceCode == deviceCode && x.InOutType == RouterInOutType.Out.ObjToInt()).GroupBy(x => x.ChildPosi).Select(x => x.Key).ToList(); |
| | | |
| | | // 将进入和离开路由器的位置添加到列表中 |
| | | positions.AddRange(inRouterPositions); |
| | | positions.AddRange(outRouterPositions); |
| | | // 返回去重后的位置列表 |
| | | return positions.GroupBy(x => x).Select(x => x.Key).ToList(); |
| | | // 将进入和离开路由器的位置添加到列表中 |
| | | positions.AddRange(inRouterPositions); |
| | | positions.AddRange(outRouterPositions); |
| | | // 返回去重后的位置列表 |
| | | return positions.GroupBy(x => x).Select(x => x.Key).ToList(); |
| | | } |
| | | catch |
| | | { |
| | | |
| | | } |
| | | finally |
| | | { |
| | | _cacheService.TryAdd($"{RedisPrefix.System}:{RedisName.DevicePositions}:{deviceCode}", positions); |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | |
| | | } |
| | | else |
| | | positions = device; |
| | | // 返回位置列表 |
| | | return positions; |
| | | } |