From b690250002ee04f4309e6a90fd16fbfd9bd959e2 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期五, 01 五月 2026 23:11:23 +0800
Subject: [PATCH] feat(router): 添加托盘操作页面路由
---
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotMessageHandler.cs | 139 +++++++++++++++++++---------------------------
1 files changed, 58 insertions(+), 81 deletions(-)
diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotMessageHandler.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotMessageHandler.cs
index 081bfd4..7a78510 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotMessageHandler.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotMessageHandler.cs
@@ -1,8 +1,6 @@
-using Microsoft.Extensions.Logging;
+using Serilog;
using System.Net.Sockets;
using WIDESEAWCS_Common;
-using WIDESEAWCS_Core.Caches;
-using WIDESEAWCS_Core.LogHelper;
using WIDESEAWCS_Tasks.Workflow.Abstractions;
namespace WIDESEAWCS_Tasks
@@ -12,7 +10,7 @@
/// </summary>
/// <remarks>
/// 鏍稿績鑱岃矗锛�
- /// 1. 缂撳瓨鐘舵�佽鍙栵細浠� Redis 涓幏鍙栨満鍣ㄤ汉鏈�鏂扮殑鐘舵��
+ /// 1. 鐘舵�佹帴鏀讹細浠庤皟鐢ㄦ柟鑾峰彇鏈哄櫒浜烘渶鏂扮殑鐘舵��
/// 2. 鍛戒护鍒嗗彂锛氭牴鎹秷鎭被鍨嬪垎鍙戠粰涓嶅悓鐨勫鐞嗗櫒
/// - 绠�鍗曞懡浠わ紙濡� homing銆乺unning锛夛細鐢� <see cref="IRobotSimpleCommandHandler"/> 澶勭悊
/// - 鍓嶇紑鍛戒护锛堝 pickfinished銆乸utfinished锛夛細鐢� <see cref="IRobotPrefixCommandHandler"/> 澶勭悊
@@ -25,68 +23,45 @@
/// <summary>
/// Socket 瀹㈡埛绔綉鍏虫帴鍙�
/// </summary>
- /// <remarks>
- /// 鐢ㄤ簬鍚戝鎴风鍙戦�佸搷搴旀秷鎭��
- /// </remarks>
private readonly ISocketClientGateway _socketClientGateway;
/// <summary>
/// 鏈烘鎵嬬姸鎬佺鐞嗗櫒
/// </summary>
- /// <remarks>
- /// 鐢ㄤ簬璇诲彇鍜屾洿鏂版満鍣ㄤ汉鐨勭姸鎬併��
- /// </remarks>
private readonly RobotStateManager _stateManager;
-
- /// <summary>
- /// 缂撳瓨鏈嶅姟
- /// </summary>
- /// <remarks>
- /// 鐩存帴浣跨敤缂撳瓨鏈嶅姟妫�鏌ョ姸鎬佹槸鍚﹀瓨鍦ㄣ��
- /// </remarks>
- private readonly ICacheService _cache;
/// <summary>
/// 绠�鍗曞懡浠ゅ鐞嗗櫒
/// </summary>
- /// <remarks>
- /// 澶勭悊绠�鍗曠殑鐘舵�佹洿鏂板懡浠わ紝濡傝繍琛岀姸鎬併�佹ā寮忓垏鎹㈢瓑銆�
- /// </remarks>
private readonly IRobotSimpleCommandHandler _simpleCommandHandler;
/// <summary>
/// 鍓嶇紑鍛戒护澶勭悊鍣�
/// </summary>
- /// <remarks>
- /// 澶勭悊甯﹀弬鏁扮殑鍓嶇紑鍛戒护锛屽 pickfinished锛堝彇璐у畬鎴愶級銆乸utfinished锛堟斁璐у畬鎴愶級銆�
- /// </remarks>
private readonly IRobotPrefixCommandHandler _prefixCommandHandler;
/// <summary>
/// 鏃ュ織璁板綍鍣�
/// </summary>
- private readonly ILogger<RobotJob> _logger;
+ private readonly ILogger _logger;
/// <summary>
/// 鏋勯�犲嚱鏁�
/// </summary>
/// <param name="socketClientGateway">Socket 缃戝叧</param>
/// <param name="stateManager">鐘舵�佺鐞嗗櫒</param>
- /// <param name="cache">缂撳瓨鏈嶅姟</param>
/// <param name="simpleCommandHandler">绠�鍗曞懡浠ゅ鐞嗗櫒</param>
/// <param name="prefixCommandHandler">鍓嶇紑鍛戒护澶勭悊鍣�</param>
/// <param name="logger">鏃ュ織璁板綍鍣�</param>
public RobotMessageHandler(
ISocketClientGateway socketClientGateway,
RobotStateManager stateManager,
- ICacheService cache,
IRobotSimpleCommandHandler simpleCommandHandler,
IRobotPrefixCommandHandler prefixCommandHandler,
- ILogger<RobotJob> logger)
+ ILogger logger)
{
_socketClientGateway = socketClientGateway;
_stateManager = stateManager;
- _cache = cache;
_simpleCommandHandler = simpleCommandHandler;
_prefixCommandHandler = prefixCommandHandler;
_logger = logger;
@@ -98,12 +73,11 @@
/// <remarks>
/// 澶勭悊娴佺▼锛�
/// 1. 璁板綍鏃ュ織锛堣褰曞師濮嬫秷鎭唴瀹癸級
- /// 2. 楠岃瘉缂撳瓨涓槸鍚﹀瓨鍦ㄨ璁惧鐨勭姸鎬�
- /// 3. 灏濊瘯鐢ㄧ畝鍗曞懡浠ゅ鐞嗗櫒澶勭悊锛堢姸鎬佹洿鏂扮被鍛戒护锛�
+ /// 2. 灏濊瘯鐢ㄧ畝鍗曞懡浠ゅ鐞嗗櫒澶勭悊锛堢姸鎬佹洿鏂扮被鍛戒护锛�
/// - 濡傛灉澶勭悊鎴愬姛锛屽洖鍐欏師娑堟伅骞舵洿鏂扮姸鎬�
- /// 4. 濡傛灉涓嶆槸绠�鍗曞懡浠わ紝妫�鏌ユ槸鍚︽槸鍓嶇紑鍛戒护锛坧ickfinished/putfinished锛�
+ /// 3. 濡傛灉涓嶆槸绠�鍗曞懡浠わ紝妫�鏌ユ槸鍚︽槸鍓嶇紑鍛戒护锛坧ickfinished/putfinished锛�
/// - 濡傛灉鏄紝璋冪敤鍓嶇紑鍛戒护澶勭悊鍣ㄥ鐞�
- /// 5. 淇濇寔鍘熸湁琛屼负锛氱畝鍗曞懡浠ゅ拰鍓嶇紑鍛戒护閮藉洖鍐欏師娑堟伅
+ /// 4. 淇濇寔鍘熸湁琛屼负锛氱畝鍗曞懡浠ゅ拰鍓嶇紑鍛戒护閮藉洖鍐欏師娑堟伅
///
/// 娉ㄦ剰锛氭鏂规硶鍙兘鍦� TCP 娑堟伅鎺ユ敹鐨勪笂涓嬫枃涓棰戠箒璋冪敤锛岄渶娉ㄦ剰鎬ц兘銆�
/// </remarks>
@@ -112,62 +86,65 @@
/// <param name="client">TCP 瀹㈡埛绔繛鎺�</param>
/// <param name="state">鏈哄櫒浜哄綋鍓嶇姸鎬�</param>
/// <returns>鍝嶅簲娑堟伅锛屽鏋滄棤闇�鍥炲鍒欒繑鍥� null</returns>
- public async Task<string?> HandleMessageReceivedAsync(string message, bool isJson, TcpClient client, RobotSocketState state)
+ public async Task<string?> HandleMessageReceivedAsync(string message, bool isJson, TcpClient client)
{
- // 璁板綍鎺ユ敹鍒扮殑娑堟伅鏃ュ織
- _logger.LogInformation($"鎺ユ敹鍒板鎴风銆恵state.RobotCrane.DeviceName}銆戝彂閫佹秷鎭�恵message}銆�");
- QuartzLogger.Info($"鎺ユ敹鍒板鎴风娑堟伅銆恵message}銆�", state.RobotCrane.DeviceName);
+ var state = _stateManager.GetState(client.Client.RemoteEndPoint.ToString());
- // 妫�鏌ヤ换鍔℃�绘暟鏄惁鏈揪鍒颁笂闄�
- if (state.RobotTaskTotalNum > RobotConst.MaxTaskTotalNum)
+ // 璁惧鐘舵�佷笉瀛樺湪鏃剁洿鎺ヨ繑鍥烇紝閬垮厤鍚庣画绌哄紩鐢�
+ if (state == null)
+ {
+ return null;
+ }
+
+ // 鑾峰彇璇ヨ澶囩殑浜掓枼閿侊紝纭繚娑堟伅澶勭悊涓� Job 鎵ц浜掓枼
+ // Job 鍦ㄥ鐞嗕换鍔℃椂浼氱瓑寰呮閿侀噴鏀惧悗鎵嶇户缁墽琛�
+ var robotLock = _stateManager.GetOrCreateLock(state.IPAddress);
+ await robotLock.WaitAsync();
+ try
{
// 璁板綍鎺ユ敹鍒扮殑娑堟伅鏃ュ織
- _logger.LogInformation($"鎺ユ敹鍒板鎴风銆恵state.RobotCrane.DeviceName}銆戝彂閫佹秷鎭�恵message}銆�");
- QuartzLogger.Info($"鎺ユ敹鍒板鎴风娑堟伅銆恵message}銆�", state.RobotCrane.DeviceName);
- // 澶勭悊鎴愬姛鍚庯紝灏嗗師娑堟伅鍥炲啓鍒板鎴风锛堜繚鎸佸師鏈夎涓猴級
- await _socketClientGateway.SendMessageAsync(client, message);
- return null;
+ QuartzLogHelper.LogInfo(_logger, $"鎺ユ敹鍒板鎴风銆恵state.RobotCrane?.DeviceName}銆戝彂閫佹秷鎭�恵message}銆�", state.RobotCrane?.DeviceName);
+
+ // 妫�鏌ヤ换鍔℃�绘暟鏄惁鏈揪鍒颁笂闄�
+ if (state.RobotTaskTotalNum > RobotConst.MaxTaskTotalNum)
+ {
+ QuartzLogHelper.LogInfo(_logger, $"鎺ユ敹鍒板鎴风銆恵state.RobotCrane?.DeviceName}銆戝彂閫佹秷鎭�恵message}銆�", state.RobotCrane?.DeviceName);
+ await _socketClientGateway.SendMessageAsync(client, message);
+ return null;
+ }
+
+ // 灏嗘秷鎭浆鎹负灏忓啓锛堢敤浜庣畝鍗曞懡浠ゅ尮閰嶏級
+ string messageLower = message.ToLowerInvariant();
+
+ // 灏濊瘯鐢ㄧ畝鍗曞懡浠ゅ鐞嗗櫒澶勭悊
+ // 绠�鍗曞懡浠ゅ寘鎷細homing銆乭omed銆乺unning銆乸ausing銆乺unmode銆乧ontrolmode 绛�
+ if (await _simpleCommandHandler.HandleAsync(messageLower, state))
+ {
+ //if (messageLower != "batteryarrived")
+ //{
+ // 澶勭悊鎴愬姛鍚庯紝灏嗗師娑堟伅鍥炲啓鍒板鎴风锛堜繚鎸佸師鏈夎涓猴級
+ //await _socketClientGateway.SendMessageAsync(client, message);
+ QuartzLogHelper.LogInfo(_logger, $"鎺ユ敹鍒版秷鎭秷鎭�恵message}銆戯紝绾﹀畾涓嶈繑鍥炲彂閫佹秷鎭細銆恵message}銆�", state.RobotCrane?.DeviceName);
+ //}
+
+ //// 瀹夊叏鏇存柊鐘舵�佸埌鏁版嵁搴�
+ //_stateManager.TryUpdateStateSafely(state.IPAddress, state);
+ return null;
+ }
+
+ // 濡傛灉涓嶆槸绠�鍗曞懡浠わ紝妫�鏌ユ槸鍚︽槸鍓嶇紑鍛戒护
+ // 鍓嶇紑鍛戒护鍖呮嫭锛歱ickfinished銆乸utfinished锛堝悗闈㈣窡閫楀彿鍒嗛殧鐨勪綅缃弬鏁帮級
+ if (_prefixCommandHandler.IsPrefixCommand(messageLower))
+ {
+ // 璋冪敤鍓嶇紑鍛戒护澶勭悊鍣�
+ await _prefixCommandHandler.HandleAsync(message, state, client);
+ }
}
-
- // 鏋勫缓缂撳瓨閿紝妫�鏌� Redis 涓槸鍚﹀瓨鍦ㄨ璁惧鐨勭姸鎬�
- var cacheKey = $"{RedisPrefix.Code}:{RedisName.SocketDevices}:{client.Client.RemoteEndPoint}";
-
- // 濡傛灉缂撳瓨涓笉瀛樺湪鎴栫姸鎬佷负 null锛屽拷鐣ユ娑堟伅
- if (!_cache.TryGetValue(cacheKey, out RobotSocketState? cachedState) || cachedState == null)
+ finally
{
- return null;
+ robotLock.Release();
}
- // 浣跨敤缂撳瓨涓幏鍙栫殑鐘舵��
- var activeState = cachedState;
-
- // 灏嗘秷鎭浆鎹负灏忓啓锛堢敤浜庣畝鍗曞懡浠ゅ尮閰嶏級
- string messageLower = message.ToLowerInvariant();
-
- // 灏濊瘯鐢ㄧ畝鍗曞懡浠ゅ鐞嗗櫒澶勭悊
- // 绠�鍗曞懡浠ゅ寘鎷細homing銆乭omed銆乺unning銆乸ausing銆乺unmode銆乧ontrolmode 绛�
- if (await _simpleCommandHandler.HandleAsync(messageLower, activeState))
- {
- // 澶勭悊鎴愬姛鍚庯紝灏嗗師娑堟伅鍥炲啓鍒板鎴风锛堜繚鎸佸師鏈夎涓猴級
- await _socketClientGateway.SendMessageAsync(client, message);
- _logger.LogInformation($"鍙戦�佹秷鎭�恵message}銆�");
- QuartzLogger.Info($"鍙戦�佹秷鎭細銆恵message}銆�", state.RobotCrane.DeviceName);
-
- // 瀹夊叏鏇存柊鐘舵�佸埌 Redis
- _stateManager.TryUpdateStateSafely(activeState.IPAddress, activeState);
- return null;
- }
-
- // 濡傛灉涓嶆槸绠�鍗曞懡浠わ紝妫�鏌ユ槸鍚︽槸鍓嶇紑鍛戒护
- // 鍓嶇紑鍛戒护鍖呮嫭锛歱ickfinished銆乸utfinished锛堝悗闈㈣窡閫楀彿鍒嗛殧鐨勪綅缃弬鏁帮級
- if (_prefixCommandHandler.IsPrefixCommand(messageLower))
- {
- // 璋冪敤鍓嶇紑鍛戒护澶勭悊鍣�
- // 鍓嶇紑鍛戒护澶勭悊鍣ㄤ細瑙f瀽浣嶇疆鍙傛暟骞舵洿鏂扮姸鎬�
- await _prefixCommandHandler.HandleAsync(message, activeState, client);
- }
-
- // 榛樿杩斿洖 null锛屼笉浜х敓鍝嶅簲娑堟伅
return null;
}
}
--
Gitblit v1.9.3