1
刘磊
2026-03-05 f45293a0c3c9838567d6ff01eae53157a9ed5284
ÏîÄ¿´úÂë/WCS/WCSServer/WIDESEAWCS_Tasks/SC/SCJob.cs
@@ -1,11 +1,14 @@
using Mapster;
using Microsoft.AspNetCore.Components.Routing;
using Newtonsoft.Json;
using Quartz;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_BasicInfoRepository;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.HttpContextUser;
@@ -67,14 +70,20 @@
                    Parallel.For(0, childCodes.Count, (i, state) =>
                    {
                        ConsoleHelper.WriteColorLine($"【{childCodes[i]}】时间【{DateTime.Now}】】【{Thread.CurrentThread.ManagedThreadId}】", ConsoleColor.Magenta);
                        //ConsoleHelper.WriteColorLine($"【{childCodes[i]}】时间【{DateTime.Now}】】【{Thread.CurrentThread.ManagedThreadId}】", ConsoleColor.Magenta);
                        if (commonStackerCrane.GetValue<StackerCraneDBName, byte>(StackerCraneDBName.TaskState, childCodes[i]) == 2)
                        if (commonStackerCrane.GetValue<StackerCraneDBName, Int16>(StackerCraneDBName.TaskState, childCodes[i]) == 2)
                        {
                            int taskNum = commonStackerCrane.GetValue<StackerCraneDBName, int>(StackerCraneDBName.CurrentTaskNum, childCodes[i]);
                            byte workStatus = commonStackerCrane.GetValue<StackerCraneDBName, byte>(StackerCraneDBName.TaskState, childCodes[i]);
                            int taskNum = commonStackerCrane.GetValue<StackerCraneDBName, int>(StackerCraneDBName.TaskNum, childCodes[i]);
                            int workStatus = commonStackerCrane.GetValue<StackerCraneDBName, Int16>(StackerCraneDBName.TaskState, childCodes[i]);
                            bool RfidError = commonStackerCrane.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.ForkTemp1, childCodes[i]);
                            ConsoleHelper.WriteColorLine($"【{childCodes[i]}】堆垛机作业状态:【{workStatus}】时间【{DateTime.Now}", ConsoleColor.Magenta);
                            if (RfidError)
                            {
                                ConsoleHelper.WriteColorLine($"【{childCodes[i]}】堆垛机作业异常:RFID异常;时间【{DateTime.Now}", ConsoleColor.Magenta);
                            }
                            string str = $"【{childCodes[i]}】任务完成,任务号:【{taskNum}】时间【{DateTime.Now}】";
                            WriteInfo(childCodes[i], str);
@@ -84,12 +93,21 @@
                            if (task == null)
                            {
                                commonStackerCrane.SetValue(StackerCraneDBName.CMD, 2, childCodes[i]);
                                if (commonStackerCrane.GetValue<StackerCraneDBName, Int16>(StackerCraneDBName.TaskState, childCodes[i]) == 0)
                                {
                                    commonStackerCrane.SetValue(StackerCraneDBName.CMD, 0, childCodes[i]);
                                }
                            }
                            else
                            {
                                var content = _taskService.StackCraneTaskCompleted(taskNum);
                                var isWorkType = commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 2, childCodes[i]);
                                var isWorkType = commonStackerCrane.SetValue(StackerCraneDBName.CMD, 2, childCodes[i]);
                                if (commonStackerCrane.GetValue<StackerCraneDBName, Int16>(StackerCraneDBName.TaskState, childCodes[i]) == 0)
                                {
                                    Thread.Sleep(500);
                                    commonStackerCrane.SetValue(StackerCraneDBName.CMD, 0, childCodes[i]);
                                }
                                str = $"【{childCodes[i]}】WMS|WCS任务完成:【{content.Status}】,堆垛机完成信号写入:【{isWorkType}】,任务号:【{taskNum}】时间【{DateTime.Now}】";
                                WriteInfo(childCodes[i], str);
                                ConsoleHelper.WriteColorLine(str, ConsoleColor.Blue);
@@ -99,29 +117,37 @@
                        if (commonStackerCrane.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.Online, childCodes[i])        //在线
                               && commonStackerCrane.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.Free, childCodes[i])     //空闲
                               && !commonStackerCrane.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.Alarm, childCodes[i])     //无报警
                               && !commonStackerCrane.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.Goods, childCodes[i])    //载货台无货
                               && commonStackerCrane.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.Flag, childCodes[i])      //货叉启用
                               && !commonStackerCrane.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.ForkGoods, childCodes[i])    //载货台无货
                                                                                                                                         //&& commonStackerCrane.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.ForkFlag, childCodes[i])      //货叉启用
                            )
                        {
                            Console.WriteLine($"当前堆垛机{childCodes[i]}");
                            Dt_Task? task = GetTask(commonStackerCrane, childCodes[i]);
                            if (task != null)
                            {
                                StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
                                if (stackerCraneTaskCommand != null)
                                //StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
                                //if (stackerCraneTaskCommand != null)
                                //{
                                var taskNum = commonStackerCrane.GetValue<StackerCraneDBName, int>(StackerCraneDBName.TaskNo, childCodes[i]);
                                //var taskBarCode = commonStackerCrane.GetValue<StackerCraneDBName, string>(StackerCraneDBName.Barcode);
                                ConsoleHelper.WriteColorLine($"【{childCodes[i]}】堆垛机任务号:【{taskNum}】任务任务号:【{task.TaskNum}】", ConsoleColor.DarkBlue);
                                if (taskNum != task.TaskNum)
                                {
                                    var taskNum = commonStackerCrane.GetValue<StackerCraneDBName, int>(StackerCraneDBName.CurrentTaskNum, childCodes[i]);
                                    //var taskBarCode = commonStackerCrane.GetValue<StackerCraneDBName, string>(StackerCraneDBName.Barcode);
                                    ConsoleHelper.WriteColorLine($"【{childCodes[i]}】堆垛机任务号:【{taskNum}】任务任务号:【{task.TaskNum}】", ConsoleColor.DarkBlue);
                                    if (taskNum == 0)
                                    ConsoleHelper.WriteColorLine($"【{childCodes[i]}】任务号为【{0}】,任务号不一致可以下发任务", ConsoleColor.DarkBlue);
                                    Thread.Sleep(500);
                                    WebResponseContent responseContent = SendStackerTask(commonStackerCrane, task, childCodes[i]);
                                    if(responseContent.Status)
                                    {
                                        ConsoleHelper.WriteColorLine($"【{childCodes[i]}】任务号为【{0}】,任务号不一致可以下发任务", ConsoleColor.DarkBlue);
                                        Thread.Sleep(500);
                                        bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand, childCodes[i]);
                                        _taskService.UpdateTaskStatusToNext(task.TaskNum);
                                    }
                                    //StackerExecutor.SendStackerTask(commonStackerCrane, task, childCodes[i]);
                                    //bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand, childCodes[i]);
                                    //commonStackerCrane.LastTaskType = task.TaskType;
                                }
                                //}
                            }
                        }
                    });
@@ -146,15 +172,15 @@
            task = _taskService.QueryExcutingTask(childCode);
            if (task != null)
            {
                ConsoleHelper.WriteErrorLine($"堆垛机存在执行中的任务,请检查");
                ConsoleHelper.WriteErrorLine($"{childCode}堆垛机存在执行中的任务,请检查");
                return null;
            }
            task = _taskService.QueryRelocationTask(childCode);
            if (task != null)
            {
                return task;
            }
            //task = _taskService.QueryRelocationTask(childCode);
            //if (task != null)
            //{
            //    return task;
            //}
            if (commonStackerCrane.LastTaskType == null)
            {
@@ -198,7 +224,7 @@
                    _noticeService.Logs(userTokenIds, new { commonStackerCrane.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
                    WriteInfo(commonStackerCrane.DeviceName, log);
                    task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
                    task = _taskService.QueryStackerCraneInTask(childCode);
                }
                else
                {
@@ -207,21 +233,20 @@
            }
            else if (task == null)
            {
                task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
                task = _taskService.QueryStackerCraneInTask(childCode);
            }
            if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)
            {
                var relocation = _taskService.InBeforeCheck(task.TaskNum);
            //if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)
            //{
            //    var relocation = _taskService.InBeforeCheck(task.TaskNum);
                if (relocation != null)
                {
                    return relocation;
                }
            }
            //    if (relocation != null)
            //    {
            //        return relocation;
            //    }
            //}
            return task;
        }
        /// <summary>
        /// å‡ºåº“任务判断出库站台是否被占用
@@ -230,15 +255,34 @@
        /// <returns>如果未被占用,返回传入的任务信息,否则,返回null</returns>
        private Dt_Task? OutTaskStationIsOccupied([NotNull] Dt_Task task)
        {
            Dt_Router? router = _routerService.QueryNextRoutes(task.Roadway, task.NextAddress).FirstOrDefault();
            //return task;
            //Dt_Router? router = _routerService.QueryNextRoutes(task.Roadway, task.NextAddress).FirstOrDefault();
            var stationList = _stationManagerRepository.QueryData(x => x.stationType == 2 && x.Roadway == task.Roadway);
            //foreach (var item in stationList)
            //{
            //    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == item.stationPLC);
            //    if (device != null)
            //    {
            //        CommonConveyorLineTZ conveyorLine = (CommonConveyorLineTZ)device;
            //        if (conveyorLine.IsOccupied(item.stationChildCode))//出库站台未被占用
            //        {
            //            return task;
            //        }
            //    }
            //    else
            //    {
            //        _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到出库站台【{item.stationChildCode}】对应的通讯对象,无法判断出库站台是否被占用");
            //    }
            //}
            Dt_Router? router = _routerService.QueryNextRoutes(task.Roadway, task.NextAddress).FirstOrDefault();
            if (router != null)
            {
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == router.ChildPosiDeviceCode);
                if (device != null)
                {
                    CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                    CommonConveyorLineTZ conveyorLine = (CommonConveyorLineTZ)device;
                    if (conveyorLine.IsOccupied(router.ChildPosi))//出库站台未被占用
                    {
                        return task;
@@ -256,109 +300,89 @@
            return null;
        }
        /// <summary>
        /// ä»»åŠ¡å®žä½“è½¬æ¢æˆå‘½ä»¤Model
        /// </summary>
        /// <param name="task">任务实体</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public StackerCraneTaskCommand? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task)
        public WebResponseContent SendStackerTask(STK commonStackerCrane, Dt_Task task, string scName)
        {
            StackerCraneTaskCommand stackerCraneTaskCommand = new StackerCraneTaskCommand();
            stackerCraneTaskCommand.TrayCode = "";
            stackerCraneTaskCommand.TaskNo = task.TaskNum;
            stackerCraneTaskCommand.TaskType = 1;
            stackerCraneTaskCommand.Goods_Type = 0;
            stackerCraneTaskCommand.CMD = 1;
            if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)//判断是否是入库任务
            WebResponseContent responseContent = new WebResponseContent();
            try
            {
                List<Dt_Router> routers = _routerService.QueryNextRoutes(task.CurrentAddress, task.Roadway);
                if (routers.Count > 0)
                if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)//判断是否是入库任务
                {
                    stackerCraneTaskCommand.S_Row = Convert.ToInt16(routers.FirstOrDefault().SrmRow);
                    stackerCraneTaskCommand.S_Bay = Convert.ToInt16(routers.FirstOrDefault().SrmColumn);
                    stackerCraneTaskCommand.S_level = Convert.ToInt16(routers.FirstOrDefault().SrmLayer);
                    string[] targetCodes = task.NextAddress.Split("-");
                    if (targetCodes.Length == 3)
                    List<Dt_Router> routers = _routerService.QueryNextRoutes(task.CurrentAddress, task.Roadway);
                    if (routers.Count > 0)
                    {
                        stackerCraneTaskCommand.E_Row = Convert.ToInt16(targetCodes[0]) /*% 2 != 0 ? (short)1 : (short)2*/;
                        stackerCraneTaskCommand.E_Bay = Convert.ToInt16(targetCodes[1]);
                        stackerCraneTaskCommand.E_level = Convert.ToInt16(targetCodes[2]);
                        commonStackerCrane.SetValue(StackerCraneDBName.S_Row, Convert.ToInt16(routers.FirstOrDefault().SrmRow), scName);//来源货位行
                        commonStackerCrane.SetValue(StackerCraneDBName.S_Bay, Convert.ToInt16(routers.FirstOrDefault().SrmColumn), scName);//来源货位列
                        commonStackerCrane.SetValue(StackerCraneDBName.S_level, Convert.ToInt16(routers.FirstOrDefault().SrmLayer), scName);//来源货位层
                        string[] targetCodes = task.NextAddress.Split("-");
                        if (targetCodes.Length == 3)
                        {
                            commonStackerCrane.SetValue(StackerCraneDBName.E_Row, Convert.ToInt16(targetCodes[0]), scName);//目的货位行
                            commonStackerCrane.SetValue(StackerCraneDBName.E_Bay, Convert.ToInt16(targetCodes[1]), scName);//目的货位列
                            commonStackerCrane.SetValue(StackerCraneDBName.E_level, Convert.ToInt16(targetCodes[2]), scName);//目的货位层
                        }
                        else
                        {
                            //数据配置错误
                            _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,起点:【{task.NextAddress}】");
                            return responseContent.Error();
                        }
                    }
                    else
                    {
                        //数据配置错误
                        _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,起点:【{task.NextAddress}】");
                        return null;
                        _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机取货站台信息");
                        return responseContent.Error();
                    }
                }
                else
                else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                {
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机取货站台信息");
                    return null;
                }
            }
            else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
            {
                List<Dt_Router> routers = _routerService.QueryNextRoutes(task.Roadway, task.TargetAddress);
                if (routers.Count > 0)
                {
                    stackerCraneTaskCommand.E_Row = Convert.ToInt16(routers.FirstOrDefault().SrmRow);
                    stackerCraneTaskCommand.E_Bay = Convert.ToInt16(routers.FirstOrDefault().SrmColumn);
                    stackerCraneTaskCommand.E_level = Convert.ToInt16(routers.FirstOrDefault().SrmLayer);
                    string[] sourceCodes = task.CurrentAddress.Split("-");
                    if (sourceCodes.Length == 3)
                    var OutStation = _stationManagerRepository.QueryFirst(x => x.stationChildCode == task.NextAddress);
                    //List<Dt_Router> routers = _routerService.QueryNextRoutes(task.Roadway, task.TargetAddress);
                    if (OutStation != null)
                    {
                        stackerCraneTaskCommand.S_Row = Convert.ToInt16(sourceCodes[0]) /*% 2 != 0 ? (short)1 : (short)2*/;
                        stackerCraneTaskCommand.S_Bay = Convert.ToInt16(sourceCodes[1]);
                        stackerCraneTaskCommand.S_level = Convert.ToInt16(sourceCodes[2]);
                        string[] targetCodes = OutStation.stationLocation.Split("-");
                        commonStackerCrane.SetValue(StackerCraneDBName.E_Row, Convert.ToInt16(targetCodes[0]), scName);//目的货位行
                        commonStackerCrane.SetValue(StackerCraneDBName.E_Bay, Convert.ToInt16(targetCodes[1]), scName);//目的货位列
                        commonStackerCrane.SetValue(StackerCraneDBName.E_level, Convert.ToInt16(targetCodes[2]), scName);//目的货位层
                        string[] sourceCodes = task.CurrentAddress.Split("-");
                        if (sourceCodes.Length == 3)
                        {
                            commonStackerCrane.SetValue(StackerCraneDBName.S_Row, Convert.ToInt16(sourceCodes[0]), scName);//来源货位行
                            commonStackerCrane.SetValue(StackerCraneDBName.S_Bay, Convert.ToInt16(sourceCodes[1]), scName);//来源货位列
                            commonStackerCrane.SetValue(StackerCraneDBName.S_level, Convert.ToInt16(sourceCodes[2]), scName);//来源货位层
                        }
                        else
                        {
                            //数据配置错误
                            _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务起点错误,起点:【{task.CurrentAddress}】");
                            return null;
                        }
                    }
                    else
                    {
                        //数据配置错误
                        _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务起点错误,起点:【{task.CurrentAddress}】");
                        _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机放货站台信息");
                        return null;
                    }
                }
                else
                {
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机放货站台信息");
                    return null;
                }
                commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 1, scName);//工作命令代码
                commonStackerCrane.SetValue(StackerCraneDBName.TaskNum, task.TaskNum, scName);//工作序号
                commonStackerCrane.SetValue(StackerCraneDBName.RFID, task.PalletCode.PadLeft(4, '0') + task.PVI, scName);//pvi
                //写入后 è¯»å–验证
                commonStackerCrane.SetValue(StackerCraneDBName.CMD, 1, scName);
                return responseContent.OK();
            }
            else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup)
            catch (Exception ex)
            {
                string[] targetCodes = task.NextAddress.Split("-");
                if (targetCodes.Length == 3)
                {
                    stackerCraneTaskCommand.E_Row = Convert.ToInt16(targetCodes[0]) /*% 2 != 0 ? (short)1 : (short)2*/;
                    stackerCraneTaskCommand.E_Bay = Convert.ToInt16(targetCodes[1]);
                    stackerCraneTaskCommand.E_level = Convert.ToInt16(targetCodes[2]);
                }
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务终点错误,起点:【{task.NextAddress}】");
                    return null;
                }
                string[] sourceCodes = task.CurrentAddress.Split("-");
                if (sourceCodes.Length == 3)
                {
                    stackerCraneTaskCommand.S_Row = Convert.ToInt16(sourceCodes[0])/* % 2 != 0 ? (short)1 : (short)2*/;
                    stackerCraneTaskCommand.S_Bay = Convert.ToInt16(sourceCodes[1]);
                    stackerCraneTaskCommand.S_level = Convert.ToInt16(sourceCodes[2]);
                }
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务起点错误,起点:【{task.CurrentAddress}】");
                    return null;
                }
                return responseContent.Error(ex.Message);
            }
            return stackerCraneTaskCommand;
        }
    }
}