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/RobotStateManager.cs |   36 +++++++++++++++++++++++++++++-------
 1 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotStateManager.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotStateManager.cs
index 4aaae62..c4bac8a 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotStateManager.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotStateManager.cs
@@ -1,6 +1,6 @@
-using Microsoft.Extensions.Logging;
+using System.Collections.Concurrent;
 using Newtonsoft.Json;
-using WIDESEAWCS_Core.LogHelper;
+using Serilog;
 using WIDESEAWCS_ITaskInfoRepository;
 using WIDESEAWCS_Model.Models;
 
@@ -12,6 +12,7 @@
     /// <remarks>
     /// 鏍稿績鍔熻兘鏄�氳繃 IRobotStateRepository 绠$悊鏁版嵁搴撲腑鐨勬満姊版墜鐘舵�併��
     /// 鎻愪緵涔愯骞跺彂鎺у埗锛岄�氳繃 Version 瀛楁闃叉骞跺彂鏇存柊鏃剁殑鏁版嵁瑕嗙洊闂銆�
+    /// 鍚屾椂鎻愪緵鍩轰簬 SemaphoreSlim 鐨勪簰鏂ラ攣锛岀‘淇濇秷鎭鐞嗕笌 Job 鎵ц涓嶄細骞跺彂鎿嶄綔鍚屼竴璁惧鐘舵�併��
     /// </remarks>
     public class RobotStateManager
     {
@@ -26,6 +27,15 @@
         private readonly ILogger _logger;
 
         /// <summary>
+        /// 姣忎釜璁惧鐨勫紓姝ヤ簰鏂ラ攣瀛楀吀锛岀敤浜� Job 鎵ц涓庢秷鎭鐞嗕箣闂寸殑浜掓枼
+        /// </summary>
+        /// <remarks>
+        /// Key 涓鸿澶� IP 鍦板潃锛孷alue 涓鸿璁惧涓撳睘鐨� SemaphoreSlim(1,1)銆�
+        /// 纭繚鍚屼竴璁惧鐨� Job 杞鍜� TCP 娑堟伅澶勭悊涓嶄細鍚屾椂鎿嶄綔鐘舵�併��
+        /// </remarks>
+        private readonly ConcurrentDictionary<string, SemaphoreSlim> _robotLocks = new();
+
+        /// <summary>
         /// 鏋勯�犲嚱鏁�
         /// </summary>
         /// <param name="repository">浠撳偍鏈嶅姟瀹炰緥</param>
@@ -34,6 +44,20 @@
         {
             _repository = repository;
             _logger = logger;
+        }
+
+        /// <summary>
+        /// 鑾峰彇鎴栧垱寤烘寚瀹氳澶囩殑寮傛浜掓枼閿�
+        /// </summary>
+        /// <remarks>
+        /// 浣跨敤 ConcurrentDictionary 纭繚姣忎釜璁惧 IP 瀵瑰簲鍞竴鐨� SemaphoreSlim(1,1)銆�
+        /// 璇ラ攣鐢ㄤ簬 Job 鎵ц涓� TCP 娑堟伅澶勭悊涔嬮棿鐨勪簰鏂ワ紝闃叉骞跺彂鎿嶄綔鍚屼竴璁惧鐘舵�併��
+        /// </remarks>
+        /// <param name="ipAddress">璁惧 IP 鍦板潃</param>
+        /// <returns>璇ヨ澶囩殑淇″彿閲忓疄渚�</returns>
+        public SemaphoreSlim GetOrCreateLock(string ipAddress)
+        {
+            return _robotLocks.GetOrAdd(ipAddress, _ => new SemaphoreSlim(1, 1));
         }
 
         /// <summary>
@@ -94,8 +118,7 @@
             if (currentEntity == null)
             {
                 _repository.GetOrCreate(newState.IPAddress, newState.RobotCrane ?? new RobotCraneDevice());
-                _logger.LogDebug("TryUpdateStateSafely锛氬垱寤烘柊鐘舵�侊紝IP: {IpAddress}", ipAddress);
-                QuartzLogger.Debug($"鍒涘缓鏂扮姸鎬侊紝IP: {ipAddress}", ipAddress);
+                QuartzLogHelper.LogDebug(_logger, $"鍒涘缓鏂扮姸鎬侊紝IP: {ipAddress}", ipAddress);
                 return true;
             }
 
@@ -112,8 +135,7 @@
 
             if (!success)
             {
-                _logger.LogWarning("TryUpdateStateSafely锛氱増鏈啿绐侊紝鏇存柊澶辫触锛孖P: {IpAddress}锛屾湡鏈涚増鏈�: {ExpectedVersion}", ipAddress, expectedVersion);
-                QuartzLogger.Warn($"鐗堟湰鍐茬獊锛屾洿鏂板け璐ワ紝IP: {ipAddress}", ipAddress);
+                QuartzLogHelper.LogWarn(_logger, $"TryUpdateStateSafely锛氱増鏈啿绐侊紝鏇存柊澶辫触锛孖P: {ipAddress}锛屾湡鏈涚増鏈�: {expectedVersion}", ipAddress);
             }
 
             return success;
@@ -161,4 +183,4 @@
             return _repository.ToSocketState(entity);
         }
     }
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3