wanshenmean
2 天以前 0b2869539598059704e1d208e2bcb18603b0fe0f
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineNewJob/CommonConveyorLineNewJob.cs
@@ -1,16 +1,11 @@
using MapsterMapper;
using Masuit.Tools;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Quartz;
using Serilog;
using SqlSugar;
using WIDESEA_Core;
using WIDESEAWCS_Common.HttpEnum;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
@@ -88,7 +83,7 @@
        /// </summary>
        /// <remarks>
        /// </remarks>
        private static List<string> AddressToDeviceType = new List<string> { "11020", "11028", "2125" };
        private static List<string> AddressToDeviceType = new List<string> { "11020", "11028", "2125", "11062" };
        /// <summary>
        /// 托盘检查位置的最近执行时间(用于30秒间隔限制)
@@ -168,57 +163,57 @@
                            // ========== 检查特定位置是否有托盘 ==========
                            // 从配置中读取需要检查托盘的位置列表
                            var checkPalletPositions = App.Configuration.GetSection("CheckPalletPositions")
                                .Get<List<CheckPalletPosition>>() ?? new List<CheckPalletPosition>();
                            //var checkPalletPositions = App.Configuration.GetSection("CheckPalletPositions")
                            //    .Get<List<CheckPalletPosition>>() ?? new List<CheckPalletPosition>();
                            // 如果当前设备在检查列表中
                            if (checkPalletPositions.Any(x => x.Code == childDeviceCode))
                            {
                                // 30秒间隔限制
                                if (_lastPalletCheckTime.TryGetValue(childDeviceCode, out var lastTime) &&
                                    (DateTime.Now - lastTime).TotalSeconds < 30)
                                {
                                    continue;
                                }
                            //// 如果当前设备在检查列表中
                            //if (checkPalletPositions.Any(x => x.Code == childDeviceCode))
                            //{
                            //    // 30秒间隔限制
                            //    if (_lastPalletCheckTime.TryGetValue(childDeviceCode, out var lastTime) &&
                            //        (DateTime.Now - lastTime).TotalSeconds < 30)
                            //    {
                            //        continue;
                            //    }
                                // 检查输送线状态(是否有托盘)
                                if (command.CV_State == 2)
                                {
                                    // 检查该位置是否已有任务
                                    var existingTask = _taskService.Db.Queryable<Dt_Task>().First(x => x.TargetAddress == childDeviceCode);
                                    if (existingTask.IsNullOrEmpty())
                                    {
                                        // 没有任务,向 WMS 请求出库托盘任务
                                        var position = checkPalletPositions.FirstOrDefault(x => x.Code == childDeviceCode);
                            //    // 检查输送线状态(是否有托盘)
                            //    if (command.CV_State == 2)
                            //    {
                            //        // 检查该位置是否已有任务
                            //        var existingTask = _taskService.Db.Queryable<Dt_Task>().Count(x => x.TargetAddress == childDeviceCode);
                            //        if (existingTask < 5)
                            //        {
                            //            // 没有任务,向 WMS 请求出库托盘任务
                            //            var position = checkPalletPositions.FirstOrDefault(x => x.Code == childDeviceCode);
                                        string configKey = "GetOutBoundTrayTaskAsync";
                                        string requestParam = new CreateTaskDto()
                                        {
                                            WarehouseId = position.WarehouseId,
                                            TargetAddress = childDeviceCode
                                        }.Serialize();
                                        DateTime startTime = DateTime.Now;
                            //            string configKey = "GetOutBoundTrayTaskAsync";
                            //            string requestParam = new CreateTaskDto()
                            //            {
                            //                WarehouseId = position.WarehouseId,
                            //                TargetAddress = childDeviceCode
                            //            }.Serialize();
                            //            DateTime startTime = DateTime.Now;
                                        var responseResult = _httpClientHelper.Post<WebResponseContent>(configKey, requestParam);
                            //            var responseResult = _httpClientHelper.Post<WebResponseContent>(configKey, requestParam);
                                        _lastPalletCheckTime[childDeviceCode] = DateTime.Now;
                            //            _lastPalletCheckTime[childDeviceCode] = DateTime.Now;
                                        // 如果请求成功,接收 WMS 返回的任务
                                        if (responseResult.IsSuccess && responseResult.Data.Status)
                                        {
                                            QuartzLogHelper.LogInfo(_logger, $"调用WMS接口成功,接口:【{configKey}】,请求参数:【{requestParam}】,响应数据:【{responseResult.Data?.Data}】,耗时:{(DateTime.Now - startTime).TotalMilliseconds}ms", conveyorLine.DeviceCode);
                                            var wmsTask = JsonConvert.DeserializeObject<WMSTaskDTO>(responseResult.Data.Data.ToString());
                                            List<WMSTaskDTO> taskDTOs = new List<WMSTaskDTO> { wmsTask };
                                            if (wmsTask != null)
                                                _taskService.ReceiveWMSTask(taskDTOs);
                                        }
                                        else
                                        {
                                            QuartzLogHelper.LogError(_logger, $"调用WMS接口失败,接口:【{configKey}】,请求参数:【{requestParam}】,错误信息:【{responseResult.Data?.Message}】", conveyorLine.DeviceCode);
                                        }
                                    }
                                }
                            }
                            //            // 如果请求成功,接收 WMS 返回的任务
                            //            if (responseResult.IsSuccess && responseResult.Data.Status)
                            //            {
                            //                QuartzLogHelper.LogInfo(_logger, $"调用WMS接口成功,接口:【{configKey}】,请求参数:【{requestParam}】,响应数据:【{responseResult.Data?.Data}】,耗时:{(DateTime.Now - startTime).TotalMilliseconds}ms", conveyorLine.DeviceCode);
                            //                var wmsTask = JsonConvert.DeserializeObject<WMSTaskDTO>(responseResult.Data.Data.ToString());
                            //                List<WMSTaskDTO> taskDTOs = new List<WMSTaskDTO> { wmsTask };
                            //                if (wmsTask != null)
                            //                    _taskService.ReceiveWMSTask(taskDTOs);
                            //            }
                            //            else
                            //            {
                            //                QuartzLogHelper.LogError(_logger, $"调用WMS接口失败,接口:【{configKey}】,请求参数:【{requestParam}】,错误信息:【{responseResult.Data?.Message}】", conveyorLine.DeviceCode);
                            //            }
                            //        }
                            //    }
                            //}
                            #endregion 检测是否需要空托盘
@@ -228,7 +223,11 @@
                            {
                                // 如果 WCS_ACK 为 1,先清除(表示处理过上一次请求)
                                if (command.WCS_ACK == 1)
                                    conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_ACK, (short)0, childDeviceCode);
                                {
                                    conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_ACK, (byte)0, childDeviceCode);
                                    //Thread.Sleep(300);
                                    //conveyorLine.SetValue(ConveyorLineDBNameNew.Target, (short)0, childDeviceCode);
                                }
                                // 处理手动入库任务(起点为线体点位的任务)
                                try
@@ -247,17 +246,36 @@
                                }
                                continue;
                            }
                            else
                            {
                                if (childDeviceCode == "2103" || childDeviceCode == "2101")
                                {
                                    try
                                    {
                                        var task = _taskService.QueryManualInboundTask(childDeviceCode);
                                        if (task != null)
                                        {
                                            QuartzLogHelper.LogInfo(_logger, $"获取到输送线开始任务,任务号:{task.TaskNum},状态: {task.TaskStatus},当前地址:{conveyorLine.DeviceCode}", conveyorLine.DeviceCode);
                                            var handler = new ManualInboundTaskHandler(_taskService, _logger);
                                            handler.WriteTaskToPlc(conveyorLine, childDeviceCode, task);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        QuartzLogHelper.LogError(_logger, ex, "处理手动入库任务异常", $"处理手动入库任务异常: {ex.Message}", "CommonConveyorLineNewJob");
                                    }
                                }
                            }
                            // ========== 处理无托盘条码的情况 ==========
                            // 无托盘条码时,请求出库任务
                            if (command.Barcode.IsNullOrEmpty() || command.Barcode.Replace("\0", "") == "")
                            // 如果 PLC_STB 为 1,但没有任务号,可能是新任务的开始,先请求出库任务(适用于无条码的情况)
                            if (command.TaskNo == 1000)
                            {
                                _conveyorLineDispatch.RequestOutbound(conveyorLine, command, childDeviceCode);
                                continue;
                            }
                            // ========== 处理已有任务号的情况 ==========
                            if (command.TaskNo > 0 && !command.Barcode.IsNullOrEmpty())
                            if (command.TaskNo > 0)
                            {
                                // 查询正在执行的任务
                                Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNo, childDeviceCode);
@@ -268,6 +286,7 @@
                                    ProcessTaskState(conveyorLine, command, task, childDeviceCode);
                                    continue;
                                }
                                // Todo: 下面的逻辑主要处理没有查询到任务但有条码的情况,可能是需要生成机械手任务或者调用 WMS 创建入库任务 (临时使用)
                                else if (!command.Barcode.IsNullOrEmpty() && childDeviceCode == "11068")
                                {
                                    var isWcsTask = _taskService.Db.Queryable<Dt_Task>().Any(x => x.PalletCode == command.Barcode && (x.TaskStatus == (int)TaskOutStatusEnum.OutNew || x.TaskStatus == (int)TaskInStatusEnum.InNew));
@@ -296,48 +315,6 @@
                                    {
                                        QuartzLogHelper.LogInfo(_logger, $"生产机械手任务成功, {robotTask.RobotTaskNum},状态: {robotTask.RobotTaskState},当前地址:{conveyorLine.DeviceCode}", conveyorLine.DeviceCode);
                                        conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_ACK, (short)1, childDeviceCode);
                                    }
                                }
                                else if (!command.Barcode.IsNullOrEmpty() && (childDeviceCode == "11001" || childDeviceCode == "11010"))
                                {
                                    var isWcsTask = _taskService.Db.Queryable<Dt_Task>().Any(x => x.PalletCode == command.Barcode && (x.TaskStatus == (int)TaskOutStatusEnum.OutNew || x.TaskStatus == (int)TaskInStatusEnum.InNew));
                                    var isRobotTask = _robotTaskService.Db.Queryable<Dt_RobotTask>().Any(x => x.RobotTargetAddressPalletCode == command.Barcode);
                                    if (isWcsTask || isRobotTask)
                                    {
                                        continue;
                                    }
                                    // 调用 WMS 创建空托盘入库任务
                                    string configKey = nameof(ConfigKey.CreateTaskInboundAsync);
                                    string requestParam = new CreateTaskDto()
                                    {
                                        PalletCode = command.Barcode,
                                        SourceAddress = childDeviceCode,
                                        TargetAddress = "GWSC1",  // 目标地址
                                        Roadway = "GWSC1",             // 巷道
                                        WarehouseId = 1,                   // 仓库 ID
                                        PalletType = 1,                             // 托盘类型(默认为1)
                                        TaskType = TaskTypeEnum.InEmpty.GetHashCode()                         // 任务类型(入库/空托盘入库)
                                    }.Serialize();
                                    DateTime startTime = DateTime.Now;
                                    var responseResult = _httpClientHelper.Post<WebResponseContent>(configKey, requestParam);
                                    if (responseResult.IsSuccess && responseResult.Data.Status)
                                    {
                                        QuartzLogHelper.LogInfo(_logger, $"调用WMS接口成功,接口:【{configKey}】,请求参数:【{requestParam}】,响应数据:【{responseResult.Data?.Data}】,耗时:{(DateTime.Now - startTime).TotalMilliseconds}ms", conveyorLine.DeviceCode);
                                        var wmsTask = JsonConvert.DeserializeObject<WMSTaskDTO>(responseResult?.Data?.Data?.ToString());
                                        List<WMSTaskDTO> taskDTOs = new List<WMSTaskDTO> { wmsTask };
                                        if (wmsTask == null) continue;
                                        if (_taskService.ReceiveWMSTask(taskDTOs).Status)
                                        {
                                            conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_ACK, (short)1, childDeviceCode);
                                        }
                                    }
                                    else
                                    {
                                        QuartzLogHelper.LogError(_logger, $"调用WMS接口失败,接口:【{configKey}】,请求参数:【{requestParam}】,错误信息:【{responseResult.Data?.Message}】", conveyorLine.DeviceCode);
                                    }
                                }
                            }