wanshenmean
2025-03-22 af9c3ed5d036ad68faf7e18f566679b3da92d244
CodeManagement/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/Task/RequestInbound.cs
@@ -1,6 +1,5 @@
//using HslCommunication;
using HslCommunication;
using Mapster;
using Masuit.Tools;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
@@ -283,18 +282,8 @@
        private async Task RequestWmsTask(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, int ProtocalDetailValue, Dt_StationManager stationManager)
        {
            string isTrue = string.Empty;
            ExecuteConveyorLineTask(conveyorLine, command, ProtocalDetailValue, childDeviceCode);
            ResultTrayCellsStatus result = GetResultTrayCellsStatus(command, stationManager);
            if(result.SerialNos.Count == 0)
            {
                var Traycontent = await _taskService.RequestWMSTask(command.Barcode, childDeviceCode);
                if (Traycontent.Status)
                {
                    ConsoleHelper.WriteSuccessLine("请求回流静置");
                    ExecuteConveyorLineTask(conveyorLine, command, ProtocalDetailValue, childDeviceCode);
                }
            }
            List<string> strings = stationManager.Roadway.Split(",").ToList();
            foreach (string item in strings)
@@ -515,161 +504,61 @@
                }
            }
        }
        /// <summary>
        /// 执行输送线任务
        /// </summary>
        /// <param name="conveyorLine">输送线对象</param>
        /// <param name="command">输送线任务命令</param>
        /// <param name="ProtocalDetailValue">协议明细值</param>
        /// <param name="childDeviceCode">子设备编号</param>
        //public void ExecuteConveyorLineTask(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, int ProtocalDetailValue, string childDeviceCode)
        //{
        //    var task = _taskService.QueryBarCodeConveyorLineTask(command.Barcode, childDeviceCode);
        //    if (task != null)
        //    {
        //        var value = _sys_ConfigService.GetByConfigKey(CateGoryConst.CONFIG_SYS_InStation, SysConfigKeyConst.JZNGInBoundStation).ConfigValue;
        //        var valueList = value.Split(',').ToList();
        //        if (valueList.Contains(task.SourceAddress))
        //        {
        //            conveyorLine.SetValue(ConveyorLineDBName.WriteConveyorLineTargetAddress, "1000", childDeviceCode);
        //            var log = $"【{conveyorLine._deviceName}】任务号:【{task.TaskNum}】,托盘条码:【{task.PalletCode}】已到达【{childDeviceCode}】请求扫码入库(实盘),下一目标地址【{1000}】";
        //            ConsoleHelper.WriteWarningLine(log);
        //            _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
        //            WriteInfo(conveyorLine.DeviceName, log);
        //            ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
        //        }
        //        else
        //        {
        //            if (task.Roadway.Contains("JZ"))
        //            {
        //                var outJZTask = _taskRepository.QueryData(x => x.Roadway== task.Roadway && x.TaskState == (int)TaskOutStatusEnum.Line_OutExecuting || x.TaskState == (int)TaskOutStatusEnum.SC_OutExecuting || x.TaskState == (int)TaskOutStatusEnum.SC_OutFinish);
        //                if (outJZTask.IsNullOrEmpty())
        //                {
        //                    var next = task.NextAddress;
        //                    var taskCommand = MapTaskCommand(task, command);
        //                    task.NextAddress = next;
        //                    var log = $"【{conveyorLine._deviceName}】任务号:【{command.TaskNum}】,托盘条码:【{command.Barcode}】已到达【{childDeviceCode}】请求扫码入库(实盘),下一目标地址【{taskCommand.TargetAddress}】";
        //                    ConsoleHelper.WriteWarningLine(log);
        //                    _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
        //                    WriteInfo(conveyorLine.DeviceName, log);
        //                    conveyorLine.SendCommand(taskCommand, childDeviceCode);
        //                    ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
        //                    _taskService.UpdateTaskStatusToNext(task);
        //                }
        //                else
        //                {
        //                    ConsoleHelper.WriteWarningLine("已存在静置出库任务,静置入库任务无法下发线体");
        //                }
        //            }
        //            else
        //            {
        //                var next = task.NextAddress;
        //                var taskCommand = MapTaskCommand(task, command);
        //                task.NextAddress = next;
        //                var log = $"【{conveyorLine._deviceName}】任务号:【{command.TaskNum}】,托盘条码:【{command.Barcode}】已到达【{childDeviceCode}】请求扫码入库(实盘),下一目标地址【{taskCommand.TargetAddress}】";
        //                ConsoleHelper.WriteWarningLine(log);
        //                _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
        //                WriteInfo(conveyorLine.DeviceName, log);
        //                conveyorLine.SendCommand(taskCommand, childDeviceCode);
        //                ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
        //                _taskService.UpdateTaskStatusToNext(task);
        //            }
        //        }
        //    }
        //}
        /// <summary>
        /// 执行输送线任务
        /// </summary>
        /// <param name="conveyorLine">输送线对象</param>
        /// <param name="command">输送线任务命令</param>
        /// <param name="ProtocalDetailValue">协议明细值</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void ExecuteConveyorLineTask(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, int ProtocalDetailValue, string childDeviceCode)
        public void ExecuteConveyorLineTask(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, int ProtocalDetailValue ,string childDeviceCode)
        {
            // 查询任务信息
            var task = _taskService.QueryBarCodeConveyorLineTask(command.Barcode, childDeviceCode);
            if (task == null) return;
            // 获取配置值并转换为列表
            var value = _sys_ConfigService.GetByConfigKey(CateGoryConst.CONFIG_SYS_InStation, SysConfigKeyConst.JZNGInBoundStation).ConfigValue;
            var valueList = value.Split(',').ToList();
            // 日志模板
            var logMessage = $"【{conveyorLine._deviceName}】任务号:【{task.TaskNum}】,托盘条码:【{task.PalletCode}】已到达【{childDeviceCode}】请求扫码入库(实盘),下一目标地址【{{0}}】";
            // 判断任务的起始地址是否在配置列表中
            if (valueList.Contains(task.SourceAddress))
            if (task != null)
            {
                // 设置目标地址为 "1000"
                conveyorLine.SetValue(ConveyorLineDBName.WriteConveyorLineTargetAddress, "1000", childDeviceCode);
                // 记录日志并发送完成信号
                LogAndSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, logMessage, "1000");
            }
            else if (task.Roadway.Contains("JZ"))
            {
                // 查询是否存在静置出库任务
                var outJZTask = _taskRepository.QueryData(x => x.Roadway == task.Roadway &&
                    (x.TaskState == (int)TaskOutStatusEnum.Line_OutExecuting ||
                     x.TaskState == (int)TaskOutStatusEnum.SC_OutExecuting ||
                     x.TaskState == (int)TaskOutStatusEnum.SC_OutFinish));
                if (outJZTask.IsNullOrEmpty())
                var value = _sys_ConfigService.GetByConfigKey(CateGoryConst.CONFIG_SYS_InStation, SysConfigKeyConst.JZNGInBoundStation).ConfigValue;
                var valueList = value.Split(',').ToList();
                if (valueList.Contains(task.SourceAddress))
                {
                    // 映射任务命令
                    conveyorLine.SetValue(ConveyorLineDBName.WriteConveyorLineTargetAddress, "1000", childDeviceCode);
                    var log = $"【{conveyorLine._deviceName}】任务号:【{task.TaskNum}】,托盘条码:【{task.PalletCode}】已到达【{childDeviceCode}】请求扫码入库(实盘),下一目标地址【{1000}】";
                    ConsoleHelper.WriteWarningLine(log);
                    _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
                    WriteInfo(conveyorLine.DeviceName, log);
                    ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
                }
                else if (task.TargetAddress == "1020-1")
                {
                    var next = task.NextAddress;
                    var taskCommand = MapTaskCommand(task, command);
                    // 记录日志并发送完成信号
                    LogAndSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, logMessage, taskCommand.TargetAddress.ToString());
                    // 发送任务命令
                    task.NextAddress = next;
                    var log = $"【{conveyorLine._deviceName}】任务号:【{command.TaskNum}】,托盘条码:【{command.Barcode}】已到达【{childDeviceCode}】请求扫码入库(实盘),下一目标地址【{taskCommand.TargetAddress}】";
                    ConsoleHelper.WriteWarningLine(log);
                    _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
                    WriteInfo(conveyorLine.DeviceName, log);
                    conveyorLine.SendCommand(taskCommand, childDeviceCode);
                    // 更新任务状态
                    _taskService.UpdateTaskStatusToNext(task);
                    ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
                    var taskHty = task.Adapt<Dt_Task_Hty>();
                    _task_HtyRepository.AddData(taskHty);
                    _taskService.DeleteData(task);
                }
                else
                {
                    ConsoleHelper.WriteWarningLine("已存在静置出库任务,静置入库任务无法下发至线体");
                    var next = task.NextAddress;
                    var taskCommand = MapTaskCommand(task, command);
                    task.NextAddress = next;
                    var log = $"【{conveyorLine._deviceName}】任务号:【{command.TaskNum}】,托盘条码:【{command.Barcode}】已到达【{childDeviceCode}】请求扫码入库(实盘),下一目标地址【{taskCommand.TargetAddress}】";
                    ConsoleHelper.WriteWarningLine(log);
                    _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
                    WriteInfo(conveyorLine.DeviceName, log);
                    conveyorLine.SendCommand(taskCommand, childDeviceCode);
                    ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
                    _taskService.UpdateTaskStatusToNext(task);
                }
            }
            else
            {
                // 映射任务命令
                var taskCommand = MapTaskCommand(task, command);
                // 记录日志并发送完成信号
                LogAndSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, logMessage, taskCommand.TargetAddress.ToString());
                // 发送任务命令
                conveyorLine.SendCommand(taskCommand, childDeviceCode);
                // 更新任务状态
                _taskService.UpdateTaskStatusToNext(task);
            }
        }
        /// <summary>
        /// 记录日志并发送完成信号
        /// </summary>
        /// <param name="conveyorLine">输送线对象</param>
        /// <param name="childDeviceCode">子设备编号</param>
        /// <param name="ProtocalDetailValue">协议明细值</param>
        /// <param name="logMessage">日志消息模板</param>
        /// <param name="targetAddress">目标地址</param>
        private void LogAndSendFinish(CommonConveyorLine conveyorLine, string childDeviceCode, int ProtocalDetailValue, string logMessage, string targetAddress)
        {
            // 格式化日志消息
            var log = string.Format(logMessage, targetAddress);
            // 输出警告日志
            ConsoleHelper.WriteWarningLine(log);
            // 记录日志
            _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log, time = DateTime.Now.ToString("G"), color = "red" });
            WriteInfo(conveyorLine.DeviceName, log);
            // 发送完成信号
            ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
        }
    }
}