wanshenmean
2026-02-06 f9d341542aaacdd8595154c2d0bcae89f4865fa7
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs
@@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using System.Net.Sockets;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.Stock;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
@@ -17,27 +18,24 @@
        private readonly TcpSocketServer _TcpSocket;
        private static readonly ConcurrentDictionary<string, RobotSocketState> _socketStates = new();
        private static int _eventSubscribedFlag;
        private readonly ITaskService _taskService;
        private readonly IRobotTaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskRepository _taskRepository;
        private readonly IRouterService _routerService;
        public RobotJob(TcpSocketServer TcpSocket, ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService)
        public RobotJob(TcpSocketServer TcpSocket, IRobotTaskService taskService)
        {
            _TcpSocket = TcpSocket;
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskRepository = taskRepository;
            _routerService = routerService;
        }
        public Task Execute(IJobExecutionContext context)
        public async Task Execute(IJobExecutionContext context)
        {
            bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
            RobotCraneDevice robotCrane = (RobotCraneDevice?)value ?? new RobotCraneDevice();
            if (!flag || robotCrane.IsNullOrEmpty())
            {
                return Task.CompletedTask;
                return;
            }
            string ipAddress = robotCrane.IPAddress;
@@ -56,7 +54,7 @@
            var clientIds = _TcpSocket.GetClientIds();
            if (!clientIds.Contains(ipAddress))
            {
                return Task.CompletedTask;
                return;
            }
            // 订阅一次 message 事件(全局一次)
@@ -74,13 +72,34 @@
            }
            // 获取任务并缓存到状态中
            Dt_Task? task = GetTask(robotCrane);
            Dt_RobotTask? task = GetTask(robotCrane);
            if (task != null)
            {
                state.CurrentTask = task;
                if (task.RobotTaskTotalNum != 48)
                {
                    // 处理正在执行的任务
                    if (state.RobotRunMode == 1 && state.RobotControlMode == 1)
                    {
                        await Task.Delay(1000);
                        if ((state.CurrentAction == "Homed" || state.CurrentAction == "PickFinished" || state.CurrentAction == "PutFinished") && state.OperStatus == "Running")
                        {
                            // TODO 读取线体电池条码,发送取电池指令
                            if (true)
                            {
                                // 模拟读取条码
                                state.CellBarcode = new string[] { "CellBarcode1", "CellBarcode2", "CellBarcode3", "CellBarcode4" };
                                string taskString = $"Pickbattery,{task.RobotSourceAddress}";
                                // 发送任务指令
                                bool result = await _TcpSocket.SendToClientAsync(ipAddress, taskString);
                            }
                        }
                    }
                }
            }
            return Task.CompletedTask;
            return;
        }
        /// <summary>
@@ -133,6 +152,24 @@
                        else if (cmd.StartsWith("putfinished"))
                        {
                            state.LastPutPositions = positions;
                            // 发送数据给WMS组盘/换盘
                            StockDTO stockDTO = new StockDTO
                            {
                                SourceLineNo = state.CurrentTask?.RobotSourceAddressLineCode,
                                SourcePalletNo = state.CurrentTask?.RobotSourceAddressPalletCode,
                                TargetPalletNo = state.CurrentTask?.RobotTargetAddressPalletCode,
                                TargetLineNo = state.CurrentTask?.RobotTargetAddressLineCode,
                                Details = positions
                                    .Where(x => x > 0)
                                    .OrderBy(x => x)
                                    .Select((x, idx) => new StockDetailDTO
                                    {
                                        Quantity = state.CurrentTask?.RobotTaskTotalNum ?? 1,
                                        Channel = x > 0 ? x : throw new ArgumentOutOfRangeException(nameof(x), "Channel must be positive"),
                                        CellBarcode = state.CellBarcode[idx]
                                    })
                                    .ToList()
                            };
                            state.CurrentAction = "PutFinished";
                        }
                    }
@@ -173,10 +210,18 @@
                case "allpickfinished":
                    state.CurrentAction = "AllPickFinished";
                    if(state.CurrentTask?.RobotTaskType == 2|| state.CurrentTask?.RobotTaskType == 3)
                    {
                        // TODO 机械手取货完成,判断是否换盘、拆盘任务,创建空托盘回库任务
                    }
                    return true;
                case "allputfinished":
                    state.CurrentAction = "AllPutFinished";
                    if (state.CurrentTask?.RobotTaskType == 1 )
                    {
                        // TODO 机械手取货完成,判断是否组盘任务,创建组盘入库任务
                    }
                    return true;
                case "running":
@@ -199,7 +244,7 @@
                    state.RobotRunMode = 1;
                    return true;
                case "runmodemode,2":
                case "runmode,2":
                    state.RobotRunMode = 2;
                    return true;
@@ -234,7 +279,7 @@
            return message.StartsWith("pickfinished") || message.StartsWith("putfinished");
        }
        private Dt_Task? GetTask(RobotCraneDevice robotCrane)
        private Dt_RobotTask? GetTask(RobotCraneDevice robotCrane)
        {
            return _taskService.QueryRobotCraneTask(robotCrane.DeviceCode);
        }
@@ -290,8 +335,13 @@
        public int[]? LastPutPositions { get; set; }
        /// <summary>
        /// 抓取位置条码
        /// </summary>
        public string[] CellBarcode { get; set; }
        /// <summary>
        /// 当前抓取任务
        /// </summary>
        public Dt_Task? CurrentTask { get; set; }
        public Dt_RobotTask? CurrentTask { get; set; }
    }
}