wanshenmean
2026-03-02 bfd4fd8e4a05a681ec10a47992294cf752a764c4
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
@@ -65,6 +68,9 @@
            // 更新设备信息
            state.RobotCrane = robotCrane;
            try
            {
            // 检查是否有该客户端连接
            var clientIds = _TcpSocket.GetClientIds();
@@ -149,6 +155,17 @@
                }
            }
        }
            catch (Exception)
            {
            }
            finally
            {
                // 可选:在这里处理任何需要在任务完成后执行的清理工作
                // 更新缓存中的状态
                _cache.AddOrUpdate(ipAddress, state);
            }
        }
        //临时测试用
        private static string GenerateTrayBarcode(RobotSocketState state, string prefix = "")
@@ -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);
        }