From bfd4fd8e4a05a681ec10a47992294cf752a764c4 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期一, 02 三月 2026 15:10:58 +0800
Subject: [PATCH] 添加Redis服务与缓存增强
---
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs | 149 +++++++++++++++++++++++++++----------------------
1 files changed, 83 insertions(+), 66 deletions(-)
diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs
index 7efce62..497a7ed 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs
@@ -7,6 +7,7 @@
using WIDESEAWCS_Common.HttpEnum;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
+using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.Http;
using WIDESEAWCS_DTO.Stock;
@@ -26,20 +27,22 @@
private const int MaxTaskTotalNum = 48;
private readonly TcpSocketServer _TcpSocket;
- private static readonly ConcurrentDictionary<string, RobotSocketState> _socketStates = new();
+ //private static readonly ConcurrentDictionary<string, RobotSocketState> _socketStates = new();
private static int _eventSubscribedFlag;
private readonly ITaskService _taskService;
private readonly IRobotTaskService _robotTaskService;
+ private readonly ICacheService _cache;
private static IRobotTaskService _latestRobotTaskService = null!;
private static ITaskService _latestTaskService = null!;
- public RobotJob(TcpSocketServer TcpSocket, IRobotTaskService RobottaskService, ITaskService TaskService)
+ public RobotJob(TcpSocketServer TcpSocket, IRobotTaskService RobottaskService, ITaskService TaskService, ICacheService cache)
{
_TcpSocket = TcpSocket;
_robotTaskService = RobottaskService;
_taskService = TaskService;
+ _cache = cache;
_latestRobotTaskService = RobottaskService;
_latestTaskService = TaskService;
@@ -57,7 +60,7 @@
string ipAddress = robotCrane.IPAddress;
// 鑾峰彇鎴栧垱寤虹姸鎬�
- RobotSocketState state = _socketStates.GetOrAdd(ipAddress, _ => new RobotSocketState
+ RobotSocketState state = _cache.GetOrAdd(ipAddress, _ => new RobotSocketState
{
IPAddress = ipAddress,
RobotCrane = robotCrane
@@ -66,87 +69,101 @@
// 鏇存柊璁惧淇℃伅
state.RobotCrane = robotCrane;
- // 妫�鏌ユ槸鍚︽湁璇ュ鎴风杩炴帴
- var clientIds = _TcpSocket.GetClientIds();
- if (!clientIds.Contains(ipAddress))
+ try
{
- return;
- }
- // 璁㈤槄涓�娆� message 浜嬩欢锛堝叏灞�涓�娆★級
- if (Interlocked.CompareExchange(ref _eventSubscribedFlag, 1, 0) == 0)
- {
- _TcpSocket.MessageReceived += _TcpSocket_MessageReceived;
- _TcpSocket.RobotReceived += _TcpSocket_RobotReceived;
- }
-
- if (!state.IsEventSubscribed)
- {
- if (_TcpSocket._clients.TryGetValue(ipAddress, out TcpClient client))
+ // 妫�鏌ユ槸鍚︽湁璇ュ鎴风杩炴帴
+ var clientIds = _TcpSocket.GetClientIds();
+ if (!clientIds.Contains(ipAddress))
{
- _ = _TcpSocket.HandleClientAsync(client, robotCrane.IPAddress, _TcpSocket._cts.Token, state)
- .ContinueWith(t =>
- {
- if (t.IsFaulted)
- Console.WriteLine($"HandleClientAsync error: {t.Exception?.GetBaseException().Message}");
- }, TaskContinuationOptions.OnlyOnFaulted);
- state.IsEventSubscribed = true;
+ return;
}
- }
- // 鑾峰彇浠诲姟骞剁紦瀛樺埌鐘舵�佷腑
- Dt_RobotTask? task = GetTask(robotCrane);
- if (task != null)
- {
- state.IsSplitPallet = task.RobotTaskType == RobotTaskTypeEnum.SplitPallet.GetHashCode();
- state.IsGroupPallet = task.RobotTaskType == RobotTaskTypeEnum.GroupPallet.GetHashCode() || task.RobotTaskType == RobotTaskTypeEnum.ChangePallet.GetHashCode();
- state.CurrentTask = task;
- if (task.RobotTaskTotalNum <= MaxTaskTotalNum)
+ // 璁㈤槄涓�娆� message 浜嬩欢锛堝叏灞�涓�娆★級
+ if (Interlocked.CompareExchange(ref _eventSubscribedFlag, 1, 0) == 0)
{
- // 澶勭悊姝e湪鎵ц鐨勪换鍔�
- if (state.RobotRunMode == 2 && state.RobotControlMode == 1 && state.OperStatus != "Running")
+ _TcpSocket.MessageReceived += _TcpSocket_MessageReceived;
+ _TcpSocket.RobotReceived += _TcpSocket_RobotReceived;
+ }
+
+ if (!state.IsEventSubscribed)
+ {
+ if (_TcpSocket._clients.TryGetValue(ipAddress, out TcpClient client))
{
- await Task.Delay(1000);
- if (state.CurrentAction == "PickFinished" && state.RobotArmObject == 1 && task.RobotTaskState != TaskRobotStatusEnum.RobotExecuting.GetHashCode())
+ _ = _TcpSocket.HandleClientAsync(client, robotCrane.IPAddress, _TcpSocket._cts.Token, state)
+ .ContinueWith(t =>
+ {
+ if (t.IsFaulted)
+ Console.WriteLine($"HandleClientAsync error: {t.Exception?.GetBaseException().Message}");
+ }, TaskContinuationOptions.OnlyOnFaulted);
+ state.IsEventSubscribed = true;
+ }
+ }
+
+ // 鑾峰彇浠诲姟骞剁紦瀛樺埌鐘舵�佷腑
+ Dt_RobotTask? task = GetTask(robotCrane);
+ if (task != null)
+ {
+ state.IsSplitPallet = task.RobotTaskType == RobotTaskTypeEnum.SplitPallet.GetHashCode();
+ state.IsGroupPallet = task.RobotTaskType == RobotTaskTypeEnum.GroupPallet.GetHashCode() || task.RobotTaskType == RobotTaskTypeEnum.ChangePallet.GetHashCode();
+ state.CurrentTask = task;
+ if (task.RobotTaskTotalNum <= MaxTaskTotalNum)
+ {
+ // 澶勭悊姝e湪鎵ц鐨勪换鍔�
+ if (state.RobotRunMode == 2 && state.RobotControlMode == 1 && state.OperStatus != "Running")
{
- string taskString = $"Putbattery,{task.RobotTargetAddress}";
- bool result = await _TcpSocket.SendToClientAsync(ipAddress, taskString);
- if (result)
+ await Task.Delay(1000);
+ if (state.CurrentAction == "PickFinished" && state.RobotArmObject == 1 && task.RobotTaskState != TaskRobotStatusEnum.RobotExecuting.GetHashCode())
+ {
+ string taskString = $"Putbattery,{task.RobotTargetAddress}";
+ bool result = await _TcpSocket.SendToClientAsync(ipAddress, taskString);
+ if (result)
+ {
+ task.RobotTaskState = TaskRobotStatusEnum.RobotExecuting.GetHashCode();
+ await _robotTaskService.UpdateRobotTaskAsync(task);
+ }
+ }
+ else if (state.CurrentAction == "PutFinished" && state.RobotArmObject == 0 && task.RobotTaskState != TaskRobotStatusEnum.RobotExecuting.GetHashCode())
{
task.RobotTaskState = TaskRobotStatusEnum.RobotExecuting.GetHashCode();
await _robotTaskService.UpdateRobotTaskAsync(task);
}
- }
- else if (state.CurrentAction == "PutFinished" && state.RobotArmObject == 0 && task.RobotTaskState != TaskRobotStatusEnum.RobotExecuting.GetHashCode())
- {
- task.RobotTaskState = TaskRobotStatusEnum.RobotExecuting.GetHashCode();
- await _robotTaskService.UpdateRobotTaskAsync(task);
- }
- else if (state.OperStatus == "Homed" && state.RobotArmObject == 0 && task.RobotTaskState != TaskRobotStatusEnum.RobotExecuting.GetHashCode())
- {
- // TODO 璇诲彇绾夸綋鐢垫睜鏉$爜锛屽彂閫佸彇鐢垫睜鎸囦护
- // 闅忔満鐢熸垚涓ゅぉ鎵樼洏鏉$爜瀛樻斁鍒颁袱涓彉閲忛噷闈�
- // 瀹氫箟鍓嶇紑锛堜緥濡傦細TRAY浠h〃鎵樼洏锛�
- string prefix = "TRAY";
-
- // 鐢熸垚涓や釜鎵樼洏鏉$爜
- string trayBarcode1 = GenerateTrayBarcode(state, prefix);
- string trayBarcode2 = GenerateTrayBarcode(state, prefix);
- if (!trayBarcode1.IsNullOrEmpty() && !trayBarcode2.IsNullOrEmpty())
+ else if (state.OperStatus == "Homed" && state.RobotArmObject == 0 && task.RobotTaskState != TaskRobotStatusEnum.RobotExecuting.GetHashCode())
{
- string taskString = $"Pickbattery,{task.RobotSourceAddress}";
- // 鍙戦�佷换鍔℃寚浠�
- bool result = await _TcpSocket.SendToClientAsync(ipAddress, taskString);
- if (result)
+ // TODO 璇诲彇绾夸綋鐢垫睜鏉$爜锛屽彂閫佸彇鐢垫睜鎸囦护
+ // 闅忔満鐢熸垚涓ゅぉ鎵樼洏鏉$爜瀛樻斁鍒颁袱涓彉閲忛噷闈�
+ // 瀹氫箟鍓嶇紑锛堜緥濡傦細TRAY浠h〃鎵樼洏锛�
+ string prefix = "TRAY";
+
+ // 鐢熸垚涓や釜鎵樼洏鏉$爜
+ string trayBarcode1 = GenerateTrayBarcode(state, prefix);
+ string trayBarcode2 = GenerateTrayBarcode(state, prefix);
+ if (!trayBarcode1.IsNullOrEmpty() && !trayBarcode2.IsNullOrEmpty())
{
- // TODO 澶勭悊鎴愬姛鍙戦�佷换鍔℃寚浠ゅ悗鐨勯�昏緫
- task.RobotTaskState = TaskRobotStatusEnum.RobotExecuting.GetHashCode();
- result = await _robotTaskService.UpdateRobotTaskAsync(task);
+ string taskString = $"Pickbattery,{task.RobotSourceAddress}";
+ // 鍙戦�佷换鍔℃寚浠�
+ bool result = await _TcpSocket.SendToClientAsync(ipAddress, taskString);
+ if (result)
+ {
+ // TODO 澶勭悊鎴愬姛鍙戦�佷换鍔℃寚浠ゅ悗鐨勯�昏緫
+ task.RobotTaskState = TaskRobotStatusEnum.RobotExecuting.GetHashCode();
+ result = await _robotTaskService.UpdateRobotTaskAsync(task);
+ }
}
}
}
}
}
+ }
+ catch (Exception)
+ {
+
+ }
+ finally
+ {
+ // 鍙�夛細鍦ㄨ繖閲屽鐞嗕换浣曢渶瑕佸湪浠诲姟瀹屾垚鍚庢墽琛岀殑娓呯悊宸ヤ綔
+ // 鏇存柊缂撳瓨涓殑鐘舵��
+ _cache.AddOrUpdate(ipAddress, state);
}
}
@@ -176,7 +193,7 @@
/// <returns></returns>
private Task<string?> _TcpSocket_RobotReceived(string clientId)
{
- _socketStates.TryRemove(clientId, out _);
+ _cache.TryRemove(clientId, out _);
return Task.FromResult<string?>(null);
}
--
Gitblit v1.9.3