From 17e5dbd7bd0364e27a33f1a7dab91cf33d5dcabc Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期三, 04 三月 2026 11:52:12 +0800
Subject: [PATCH] 增强Redis缓存服务与设备通信优化
---
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs | 205 ++++++++++++++++++++++++++++----------------------
1 files changed, 114 insertions(+), 91 deletions(-)
diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
index 4bf89ae..994ed38 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
@@ -11,6 +11,8 @@
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
{
@@ -21,6 +23,7 @@
{
private readonly IDeviceProtocolRepository _deviceProtocolRepository;
private readonly IDeviceInfoRepository _deviceInfoRepository;
+ private readonly ICacheService _cacheService;
/// <summary>
/// 璺敱閰嶇疆涓氬姟灞�
@@ -28,10 +31,11 @@
/// <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>
@@ -47,10 +51,10 @@
{
// 涓�娆℃�ф煡璇㈡墍鏈夎矾鐢辨暟鎹埌鍐呭瓨
List<Dt_Router> allRouters = BaseDal.QueryData(x => true);
-
+
// 鍦ㄥ唴瀛樹腑杩涜璺緞鎼滅储
routers = FindRoutesInMemory(startPosi, endPosi, allRouters, null);
-
+
if (routers.Count == 0)
{
throw new Exception($"璇ヨ矾寰勬湭閰嶇疆鎴栭厤缃敊璇�,璇锋鏌ヨ澶囪矾鐢变俊鎭�,璧风偣:銆恵startPosi}銆�,缁堢偣:銆恵endPosi}銆�");
@@ -77,10 +81,10 @@
{
// 涓�娆℃�ф煡璇㈡寚瀹氱被鍨嬬殑鎵�鏈夎矾鐢辨暟鎹埌鍐呭瓨
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}銆�");
@@ -110,7 +114,7 @@
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());
@@ -127,7 +131,7 @@
// 浣跨敤闃熷垪杩涜骞垮害浼樺厛鎼滅储
Queue<(Dt_Router router, List<Dt_Router> path)> queue = new Queue<(Dt_Router, List<Dt_Router>)>();
-
+
// 灏嗘墍鏈夌粓鐐硅矾鐢卞姞鍏ラ槦鍒�
foreach (var endRouter in endRouters)
{
@@ -186,9 +190,9 @@
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();
}
@@ -210,9 +214,9 @@
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();
}
@@ -235,9 +239,9 @@
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;
@@ -278,7 +282,7 @@
List<Dt_Router> path = new List<Dt_Router>();
string currentPosi = startPosi;
HashSet<string> visitedPositions = new HashSet<string>();
-
+
try
{
while (currentPosi != endPosi)
@@ -290,21 +294,21 @@
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>();
@@ -314,7 +318,7 @@
{
return new List<Dt_Router>();
}
-
+
return path;
}
@@ -327,24 +331,34 @@
{
// 鍒涘缓涓�涓瓧绗︿覆鍒楄〃锛岀敤浜庡瓨鍌ㄦ墍鏈変綅缃�
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;
}
@@ -363,34 +377,24 @@
// 閬嶅巻鎵�鏈夌粨鏉熺殑璺敱
foreach (var item in dt_Routers)
{
- // 鑾峰彇褰撳墠璺敱鐨勫瓙璺敱
- string routes = $"{item.NextPosi},";
- // 鑾峰彇褰撳墠璺敱鐨勭埗璺敱
- string str = GetPreviousRoutes(item.StartPosi, allRouters, item.InOutType);
- // 濡傛灉鐖惰矾鐢变笉涓虹┖
- if (!string.IsNullOrEmpty(str))
+ // 鑾峰彇鎵�鏈夊彲鑳界殑瀹屾暣璺緞
+ List<List<string>> allPaths = GetAllPaths(item.StartPosi, item.NextPosi, allRouters, item.InOutType);
+
+ // 涓烘瘡鏉$嫭绔嬭矾寰勭敓鎴愮粨鏋�
+ foreach (var path in allPaths)
{
- // 鍘绘帀鏈�鍚庝竴涓�楀彿
- if (str.EndsWith(","))
- str = str.Substring(0, str.Length - 1);
- // 灏嗙埗璺敱娣诲姞鍒板瓙璺敱涓�
- routes += str;
- }
- // 濡傛灉褰撳墠璺敱鏄叆鍙�
- if (item.InOutType == RouterInOutType.In.ObjToInt())
- {
- // 灏嗗瓙璺敱鍙嶈浆骞舵坊鍔犲埌data涓�
- List<string> itemRouters = routes.Split(",").Reverse().ToList();
- object obj = new { type = RouterInOutType.In, routes = itemRouters };
- data.Add(obj);
- }
- // 濡傛灉褰撳墠璺敱鏄嚭鍙�
- else
- {
- // 灏嗗瓙璺敱鍙嶈浆骞舵坊鍔犲埌data涓�
- List<string> itemRouters = routes.Split(",").Reverse().ToList();
- object obj = new { type = RouterInOutType.Out, routes = itemRouters };
- data.Add(obj);
+ // 濡傛灉褰撳墠璺敱鏄叆鍙�
+ if (item.InOutType == RouterInOutType.In.ObjToInt())
+ {
+ object obj = new { type = RouterInOutType.In, routes = path };
+ data.Add(obj);
+ }
+ // 濡傛灉褰撳墠璺敱鏄嚭鍙�
+ else
+ {
+ object obj = new { type = RouterInOutType.Out, routes = path };
+ data.Add(obj);
+ }
}
}
@@ -398,42 +402,61 @@
return data;
}
- private string GetPreviousRoutes(string startPosi, List<Dt_Router> allRouters, int routerType)
+ /// <summary>
+ /// 鑾峰彇浠庤捣鐐瑰埌缁堢偣鐨勬墍鏈夊畬鏁磋矾寰�
+ /// </summary>
+ /// <param name="startPosi">褰撳墠璧峰浣嶇疆</param>
+ /// <param name="endPosi">缁堢偣浣嶇疆</param>
+ /// <param name="allRouters">鎵�鏈夎矾鐢辨暟鎹�</param>
+ /// <param name="routerType">璺敱绫诲瀷</param>
+ /// <returns>鎵�鏈夊畬鏁磋矾寰勫垪琛紝姣忔潯璺緞鏄粠璧风偣鍒扮粓鐐圭殑浣嶇疆鍒楄〃</returns>
+ private List<List<string>> GetAllPaths(string startPosi, string endPosi, List<Dt_Router> allRouters, int routerType)
{
- // 瀹氫箟涓�涓┖瀛楃涓瞨outers
- string routers = string.Empty;
- // 鍒ゆ柇startPosi鏄惁涓虹┖
- if (!string.IsNullOrEmpty(startPosi))
+ List<List<string>> result = new List<List<string>>();
+
+ // 浠庣粓鐐瑰紑濮嬪弽鍚戞煡鎵炬墍鏈夎矾寰�
+ List<List<string>> reversePaths = new List<List<string>>();
+ BuildReversePaths(startPosi, new List<string> { endPosi, startPosi }, allRouters, routerType, reversePaths);
+
+ // 灏嗗弽鍚戣矾寰勮浆涓烘鍚戯紙浠庤捣鐐瑰埌缁堢偣锛�
+ foreach (var reversePath in reversePaths)
{
- // 鍒ゆ柇routers鏄惁浠ラ�楀彿缁撳熬
- if (!routers.EndsWith(","))
- // 濡傛灉涓嶆槸锛屽垯灏唖tartPosi娣诲姞鍒皉outers涓紝骞跺湪鍚庨潰鍔犱笂閫楀彿
- routers += $"{startPosi},";
- else
- // 濡傛灉鏄紝鍒欏皢startPosi娣诲姞鍒皉outers涓�
- routers += $"{startPosi}";
+ result.Add(reversePath.AsEnumerable().Reverse().ToList());
}
- // 浠巃llRouters涓瓫閫夊嚭NextPosi绛変簬startPosi涓擨nOutType绛変簬routerType鐨勫厓绱狅紝骞惰浆鎹负List
- List<Dt_Router> preRouters = allRouters.Where(x => x.NextPosi == startPosi && x.InOutType == routerType).ToList();
- // 閬嶅巻preRouters涓殑姣忎釜鍏冪礌
- foreach (var item in preRouters)
+
+ return result;
+ }
+
+ /// <summary>
+ /// 閫掑綊鏋勫缓鍙嶅悜璺緞锛堜粠缁堢偣鍚戣捣鐐规煡鎵撅級
+ /// </summary>
+ /// <param name="currentStartPosi">褰撳墠鑺傜偣鐨勮捣濮嬩綅缃�</param>
+ /// <param name="currentPath">褰撳墠宸叉瀯寤虹殑璺緞锛堝弽鍚戯紝浠庣粓鐐瑰紑濮嬶級</param>
+ /// <param name="allRouters">鎵�鏈夎矾鐢辨暟鎹�</param>
+ /// <param name="routerType">璺敱绫诲瀷</param>
+ /// <param name="result">鏀堕泦鎵�鏈夊畬鏁磋矾寰�</param>
+ private void BuildReversePaths(string currentStartPosi, List<string> currentPath, List<Dt_Router> allRouters, int routerType, List<List<string>> result)
+ {
+ // 鏌ユ壘褰撳墠鑺傜偣鐨勫墠缃妭鐐癸紙NextPosi绛変簬褰撳墠StartPosi鐨勮矾鐢憋級
+ List<Dt_Router> preRouters = allRouters.Where(x => x.NextPosi == currentStartPosi && x.InOutType == routerType).ToList();
+
+ // 濡傛灉娌℃湁鍓嶇疆鑺傜偣锛岃鏄庡凡缁忓埌杈捐矾寰勮捣鐐癸紝淇濆瓨褰撳墠璺緞
+ if (preRouters.Count == 0)
{
- // 璋冪敤GetPreviousRoutes鏂规硶锛屼紶鍏tem.StartPosi銆乤llRouters鍜宺outerType锛屽苟灏嗚繑鍥炲�艰祴缁檚tr
- string str = GetPreviousRoutes(item.StartPosi, allRouters, routerType);
- // 鍒ゆ柇str鏄惁涓虹┖
- if (!string.IsNullOrEmpty(str))
- {
- // 鍒ゆ柇routers鏄惁浠ラ�楀彿缁撳熬
- if (routers.EndsWith(","))
- // 濡傛灉鏄紝鍒欏皢str娣诲姞鍒皉outers涓�
- routers += $"{str}";
- else
- // 濡傛灉涓嶆槸锛屽垯灏唖tr娣诲姞鍒皉outers涓紝骞跺湪鍚庨潰鍔犱笂閫楀彿
- routers += $"{str},";
- }
+ result.Add(new List<string>(currentPath));
+ return;
}
- // 杩斿洖routers
- return routers;
+
+ // 瀵规瘡涓墠缃妭鐐癸紝鍒涘缓鏂扮殑璺緞鍒嗘敮
+ foreach (var preRouter in preRouters)
+ {
+ // 鍒涘缓鏂扮殑璺緞鍓湰
+ List<string> newPath = new List<string>(currentPath);
+ newPath.Add(preRouter.StartPosi);
+
+ // 閫掑綊鏌ユ壘鍓嶇疆鑺傜偣
+ BuildReversePaths(preRouter.StartPosi, newPath, allRouters, routerType, result);
+ }
}
/// <summary>
--
Gitblit v1.9.3