From da5d613a85d97ecd826e343eae6d901806120a05 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期三, 18 三月 2026 14:18:36 +0800
Subject: [PATCH] fix: 修复输送线目标地址选择器并完善机械手任务地址映射

---
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/TaskService.cs                                |  277 ++++++++++++++++++++-------------------------
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineNewJob/ConveyorLineTargetAddressSelector.cs |   25 ++-
 2 files changed, 138 insertions(+), 164 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/TaskService.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/TaskService.cs
index 64f3c61..adc48a7 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/TaskService.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/TaskService.cs
@@ -19,7 +19,9 @@
 
 using MapsterMapper;
 using Microsoft.Extensions.Configuration;
+using Microsoft.IdentityModel.Tokens;
 using Newtonsoft.Json;
+using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
 using SqlSugar;
 using System.Diagnostics.CodeAnalysis;
 using WIDESEA_Core;
@@ -36,6 +38,8 @@
 using WIDESEAWCS_ITaskInfoService;
 using WIDESEAWCS_Model.Models;
 using WIDESEAWCS_QuartzJob;
+using WIDESEAWCS_QuartzJob.DeviceBase;
+using WIDESEAWCS_QuartzJob.DTO;
 using WIDESEAWCS_QuartzJob.Models;
 using WIDESEAWCS_QuartzJob.Service;
 
@@ -48,6 +52,7 @@
         private readonly ITaskExecuteDetailRepository _taskExecuteDetailRepository;
         private readonly IMapper _mapper;
         private readonly HttpClientHelper _httpClientHelper;
+        private readonly IRobotTaskService _robotTaskService;
 
         private Dictionary<string, OrderByType> _taskOrderBy = new()
             {
@@ -66,13 +71,14 @@
 
         public List<int> TaskRobotTypes => typeof(TaskOtherTypeEnum).GetEnumIndexList();
 
-        public TaskService(ITaskRepository BaseDal, IRouterService routerService, ITaskExecuteDetailService taskExecuteDetailService, ITaskExecuteDetailRepository taskExecuteDetailRepository, IMapper mapper, HttpClientHelper httpClientHelper) : base(BaseDal)
+        public TaskService(ITaskRepository BaseDal, IRouterService routerService, ITaskExecuteDetailService taskExecuteDetailService, ITaskExecuteDetailRepository taskExecuteDetailRepository, IMapper mapper, HttpClientHelper httpClientHelper, IRobotTaskService robotTaskService) : base(BaseDal)
         {
             _routerService = routerService;
             _taskExecuteDetailService = taskExecuteDetailService;
             _taskExecuteDetailRepository = taskExecuteDetailRepository;
             _mapper = mapper;
             _httpClientHelper = httpClientHelper;
+            _robotTaskService = robotTaskService;
         }
 
         /// <summary>
@@ -96,7 +102,13 @@
                     task.Creater = "WMS";
                     if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                     {
-                        var taskType = 100;
+                        int taskType = 0;
+                        if (task.TaskType == (int)TaskOutboundTypeEnum.OutEmpty)
+                        {
+                            taskType = 100;
+                        }
+                        else
+                            taskType = task.TaskType;
                         Dt_Router router = _routerService.QueryNextRoute(item.Roadway, item.TargetAddress, taskType);
                         //鏆備笉鑰冭檻澶氳矾寰�
                         if (router != null)
@@ -370,7 +382,7 @@
             // 鏇存柊浠诲姟鏁版嵁
             task.ModifyDate = DateTime.Now;
             task.Modifier = "System";
-            if(task.TaskStatus == (int)TaskOutStatusEnum.Line_OutFinish)
+            if (task.TaskStatus == (int)TaskOutStatusEnum.Line_OutFinish)
             {
                 BaseDal.DeleteAndMoveIntoHty(task, OperateTypeEnum.鑷姩鍒犻櫎);
             }
@@ -408,12 +420,14 @@
             }
             else if (task.TaskStatus == (int)TaskOutStatusEnum.Line_OutFinish && task.TaskType == (int)TaskOutboundTypeEnum.OutEmpty)
             {
-                return GetWMSRobotTask(task);
+                return _robotTaskService.GetWMSRobotTask(task);
             }
             else
             {
-                // return UpdateWMSTaskStatus(task);
-                return WebResponseContent.Instance.OK();
+                if (task.TaskStatus == (int)TaskOutStatusEnum.Line_OutExecuting)
+                    return WebResponseContent.Instance.OK();
+                else
+                    return UpdateWMSTaskStatus(task);
             }
         }
 
@@ -506,8 +520,8 @@
         private WebResponseContent GetWMSOutboundTrayTask(Dt_Task task)
         {
             var targetAddress = task.TargetAddress;
-            var warehouseId = MapWarehouseIdConfigKey(task.TargetAddress);
-            string sourceLineNo = ResolveRobotRuleValue(targetAddress, "AddressSourceLineNoMap", task.TargetAddress);
+            var warehouseId = _robotTaskService.MapWarehouseIdConfigKey(task.TargetAddress);
+            string sourceLineNo = _robotTaskService.ResolveRobotRuleValue(targetAddress, "AddressSourceLineNoMap", task.TargetAddress);
 
             var result = _httpClientHelper.Post<WebResponseContent>(
                 nameof(ConfigKey.GetOutBoundTrayTaskAsync),
@@ -516,153 +530,17 @@
             if (!result.IsSuccess || !result.Data.Status)
                 return WebResponseContent.Instance.Error($"鑾峰彇WMS绯荤粺绌烘墭鐩樺嚭搴撲换鍔″け璐�,浠诲姟鍙�:銆恵task.TaskNum}銆�,鎵樼洏鍙�:銆恵task.PalletCode}銆�,閿欒淇℃伅:銆恵result.Data?.Message}銆�");
 
-            var wMSTask = JsonConvert.DeserializeObject<WMSTaskDTO>(result.Data.Data.ToString());
+            var wMSTask = JsonConvert.DeserializeObject<WMSTaskDTO>(result.Data.Data?.ToString() ?? string.Empty);
 
-            var tasks = new List<WMSTaskDTO>();
-            tasks.Add(wMSTask);
-
+            var tasks = new List<WMSTaskDTO>
+            {
+                wMSTask
+            };
 
             return ReceiveWMSTask(tasks);
         }
 
-        /// <summary>
-        /// 鑾峰彇WMS绯荤粺鏈烘鎵嬩换鍔�
-        /// </summary>
-        /// <param name="task"></param>
-        /// <returns></returns>
-        private WebResponseContent GetWMSRobotTask(Dt_Task task)
-        {
-            string configKey = ResolveRobotTaskConfigKey(task.TargetAddress);
-            StockDTO stock = BuildRobotTaskStock(task, configKey);
-
-            var result = _httpClientHelper.Post<WebResponseContent>(configKey, stock.ToJson());
-
-            if (!result.IsSuccess || !result.Data.Status)
-                return WebResponseContent.Instance.Error($"鑾峰彇WMS绯荤粺鏈烘鎵嬩换鍔″け璐�,浠诲姟鍙�:銆恵task.TaskNum}銆�,鎵樼洏鍙�:銆恵task.PalletCode}銆�,鐩爣鍦板潃:銆恵task.TargetAddress}銆�,鎺ュ彛:銆恵configKey}銆�,閿欒淇℃伅:銆恵result.Data?.Message}銆�");
-
-            var tasks = JsonConvert.DeserializeObject<List<WMSTaskDTO>>(result.Data.Data?.ToString() ?? string.Empty);
-            if (tasks == null || tasks.Count == 0)
-                return WebResponseContent.Instance.Error($"鑾峰彇WMS绯荤粺鏈烘鎵嬩换鍔″け璐�,浠诲姟鍙�:銆恵task.TaskNum}銆�,鎵樼洏鍙�:銆恵task.PalletCode}銆�,閿欒淇℃伅:銆怶MS鏈繑鍥炴湁鏁堜换鍔℃暟鎹��");
-
-            return ReceiveWMSTask(tasks);
-        }
-
-        /// <summary>
-        /// 鏍规嵁杈撻�佺嚎鐩爣鍦板潃瑙f瀽鏈烘鎵嬩换鍔℃帴鍙c��
-        /// 瑙勫垯锛�
-        /// 1. 浠庨厤缃鍙栫簿纭湴鍧�鏄犲皠锛圓ddressMap锛�
-        /// 2. 鏈懡涓椂榛樿鎹㈢洏
-        /// </summary>
-        private string ResolveRobotTaskConfigKey(string? targetAddress)
-        {
-            string address = (targetAddress ?? string.Empty).Trim();
-            if (string.IsNullOrWhiteSpace(address))
-                return nameof(ConfigKey.CreateRobotChangePalletTask);
-
-            var section = App.Configuration.GetSection("RobotTaskAddressRules");
-            var addressMap = ReadRobotRuleMap(section.GetSection("AddressMap"));
-            if (addressMap.TryGetValue(address, out string? exactTaskType))
-                return MapRobotTaskTypeToConfigKey(exactTaskType);
-
-            return nameof(ConfigKey.CreateRobotChangePalletTask);
-        }
-
-        private int MapWarehouseIdConfigKey(string? targetAddress)
-        {
-            return targetAddress switch
-            {
-                "11068" => 1,
-                "11001" => 3,
-                "11010" => 3,
-                "10010" => 1,
-                "10030" => 1,
-                _ => 1
-            };
-        }
-
-        /// <summary>
-        /// 灏嗛厤缃换鍔$被鍨嬭浆鎹负鎺ュ彛閰嶇疆閿��
-        /// 鏀寔鍊硷細Split/Group/Change锛堝ぇ灏忓啓涓嶆晱鎰燂級
-        /// </summary>
-        private string MapRobotTaskTypeToConfigKey(string? taskType)
-        {
-            string type = (taskType ?? string.Empty).Trim().ToLowerInvariant();
-            return type switch
-            {
-                "split" => nameof(ConfigKey.CreateRobotSplitPalletTask),
-                "group" => nameof(ConfigKey.CreateRobotGroupPalletTask),
-                _ => nameof(ConfigKey.CreateRobotChangePalletTask)
-            };
-        }
-
-        /// <summary>
-        /// 鏍规嵁鎺ュ彛绫诲瀷鏋勫缓鏈烘鎵嬩换鍔″叆鍙傘��
-        /// </summary>
-        private StockDTO BuildRobotTaskStock(Dt_Task task, string configKey)
-        {
-            string targetAddress = task.TargetAddress ?? string.Empty;
-            string roadway = ResolveRobotRuleValue(targetAddress, "AddressRoadwayMap", task.Roadway);
-            string sourceLineNo = ResolveRobotRuleValue(targetAddress, "AddressSourceLineNoMap", task.SourceAddress);
-
-            var stock = new StockDTO
-            {
-                Roadway = roadway,
-                SourceLineNo = sourceLineNo,
-                TargetLineNo = task.TargetAddress,
-                SourcePalletNo = task.PalletCode,
-                TargetPalletNo = task.PalletCode
-            };
-
-            if (configKey == nameof(ConfigKey.CreateRobotSplitPalletTask))
-            {
-                stock.TargetPalletNo = string.Empty;
-            }
-            else if (configKey == nameof(ConfigKey.CreateRobotGroupPalletTask))
-            {
-                stock.SourcePalletNo = string.Empty;
-            }
-            else if (configKey == nameof(ConfigKey.CreateRobotChangePalletTask))
-            {
-                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceProDTOs.Any(d => d.DeviceChildCode == sourceLineNo));
-                if (device != null)
-                {
-                    CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
-                    var barcode = conveyorLine.GetValue<ConveyorLineDBNameNew, string>(ConveyorLineDBNameNew.Barcode, sourceLineNo);
-                    stock.SourcePalletNo = string.IsNullOrEmpty(barcode) ? string.Empty : barcode;
-                }
-            }
-
-            return stock;
-        }
-
-        /// <summary>
-        /// 鏍规嵁鐩爣鍦板潃鎸夈�岀簿纭� > 鍥為��鍊笺�嶈В鏋愯鍒欏�笺��
-        /// </summary>
-        private string ResolveRobotRuleValue(string? targetAddress, string addressSectionName, string? fallback)
-        {
-            string address = (targetAddress ?? string.Empty).Trim();
-            string defaultValue = fallback ?? string.Empty;
-            if (string.IsNullOrWhiteSpace(address))
-                return defaultValue;
-
-            var section = App.Configuration.GetSection("RobotTaskAddressRules");
-            var addressMap = ReadRobotRuleMap(section.GetSection(addressSectionName));
-            if (addressMap.TryGetValue(address, out string? value))
-                return value;
-
-            return defaultValue;
-        }
-
-        /// <summary>
-        /// 璇诲彇瑙勫垯鏄犲皠娈点��
-        /// </summary>
-        private Dictionary<string, string> ReadRobotRuleMap(IConfigurationSection section)
-        {
-            return section
-                .GetChildren()
-                .Where(x => !string.IsNullOrWhiteSpace(x.Key) && !string.IsNullOrWhiteSpace(x.Value))
-                .ToDictionary(x => x.Key.Trim(), x => x.Value!.Trim());
-        }
+       
 
         /// <summary>
         /// 浠嶹MS绯荤粺鑾峰彇鍏ュ簱鐩爣鍦板潃
@@ -744,7 +622,8 @@
                 string oldCurrentPos = task.CurrentAddress;
                 string oldNextPos = task.NextAddress;
 
-                Dt_Router routers = _routerService.QueryNextRoute(task.CurrentAddress);
+
+                Dt_Router routers = _routerService.QueryNextRoute(oldNextPos, task.TargetAddress);
                 if (routers == null) throw new Exception($"鏈壘鍒拌澶囪矾鐢变俊鎭�");
 
                 task.CurrentAddress = task.NextAddress;
@@ -779,13 +658,20 @@
 
                 if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup && task.TaskStatus == (int)TaskOutStatusEnum.SC_OutExecuting)
                 {
-                    List<Dt_Router> routers = _routerService.QueryNextRoutes(task.NextAddress, task.TargetAddress);
-                    if (!routers.Any()) return WebResponseContent.Instance.Error($"鏈壘鍒拌澶囪矾鐢变俊鎭�");
+                    int taskType = 0;
+                    if (task.TaskType == (int)TaskOutboundTypeEnum.OutEmpty)
+                    {
+                        taskType = 100;
+                    }
+                    else
+                        taskType = task.TaskType;
+                    Dt_Router router = _routerService.QueryNextRoute(task.NextAddress, task.TargetAddress, taskType);
+                    if (router == null) return WebResponseContent.Instance.Error($"鏈壘鍒拌澶囪矾鐢变俊鎭�");
 
                     int nextStatus = task.TaskStatus.GetNextNotCompletedStatus<TaskOutStatusEnum>();
                     task.TaskStatus = nextStatus;
                     task.CurrentAddress = task.NextAddress;
-                    task.NextAddress = routers.FirstOrDefault().ChildPosi;
+                    task.NextAddress = router.ChildPosi;
                     task.ModifyDate = DateTime.Now;
                     task.Modifier = "System";
 
@@ -1005,4 +891,87 @@
     {
         Barcode
     }
+    public class ConveyorLineTaskCommandNew : DeviceCommand
+    {
+        /// <summary>
+        /// 浠诲姟鍙�
+        /// </summary>
+        public short TaskNo { get; set; }
+
+        /// <summary>
+        /// 婧愪綅缃� 寮�濮嬪湴鍧�
+        /// </summary>
+        public short Source { get; set; }
+
+        /// <summary>
+        /// 鐩爣浣嶇疆
+        /// </summary>
+        public short Target { get; set; }
+
+        /// <summary>
+        /// 绠卞瀷
+        /// </summary>
+        public byte BoxType { get; set; }
+
+        /// <summary>
+        /// 杈撻�佺嚎鐘舵�� 璁惧绌洪棽鐘舵��
+        /// </summary>
+        public byte CV_State { get; set; }
+
+        /// <summary>
+        /// 杈撻�佺嚎閿欒浠g爜
+        /// </summary>
+        public byte CV_ERRCode { get; set; }
+
+        /// <summary>
+        /// WCS灏辩华鏍囧織 WCS涓嬪彂瀹屾垚鏃讹紝瑙﹀彂涓�1
+        /// </summary>
+        public byte WCS_STB { get; set; }
+
+        /// <summary>
+        /// WCS搴旂瓟鏍囧織 WCS鏀跺埌瀹屾垚鏃讹紝瑙﹀彂涓�1
+        /// </summary>
+        public byte WCS_ACK { get; set; }
+
+        /// <summary>
+        /// PLC灏辩华鏍囧織 瀹屾垚浠诲姟鏃讹紝瑙﹀彂涓�1
+        /// </summary>
+        public byte PLC_STB { get; set; }
+
+        /// <summary>
+        /// PLC搴旂瓟鏍囧織 鏀跺埌浠诲姟鏃讹紝瑙﹀彂涓�1
+        /// </summary>
+        public byte PLC_ACK { get; set; }
+
+        /// <summary>
+        /// PLC璇锋眰鏍囧織 鍏ュ簱绔欏彴锛屽埌浣嶅啓1
+        /// </summary>
+        public byte PLC_REQ { get; set; }
+
+        /// <summary>
+        /// WCS閿欒浠g爜
+        /// </summary>
+        public byte WCS_ERRCode { get; set; }
+
+        /// <summary>
+        /// WCS鐗规畩鏍囧織 (鏃嬭浆鏍囪瘑銆佸己鍒舵斁琛屻�佸惊鐜�佺壒娈婄敵璇枫�佹槸鍚﹀彔鐩樸�佹槸鍚﹀牭濉�)
+        /// </summary>
+        public byte WCS_Special { get; set; }
+
+        /// <summary>
+        /// 璁惧鑷姩妯″紡 鎵嬪姩1锛岃嚜鍔�2
+        /// </summary>
+        public byte Equ_Auto { get; set; }
+
+        /// <summary>
+        /// 灏炬澘鏍囧織
+        /// </summary>
+        public byte Last_pallet { get; set; }
+
+        /// <summary>
+        /// 鏉$爜锛�22涓瓧绗︼級
+        /// </summary>
+        [DataLength(22)]
+        public string Barcode { get; set; }
+    }
 }
diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineNewJob/ConveyorLineTargetAddressSelector.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineNewJob/ConveyorLineTargetAddressSelector.cs
index 2983836..16f7735 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineNewJob/ConveyorLineTargetAddressSelector.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineNewJob/ConveyorLineTargetAddressSelector.cs
@@ -9,6 +9,11 @@
         private const string ConstraintMachineName = "鎷樻潫鏈�";
         private const string PinMachineName = "鎻掓嫈閽夋満";
 
+        // 鎷樻潫鏈虹偣浣�
+        private static readonly List<string> ConstraintMachineCodes = new List<string> { "10180", "20090" };
+        // 鎻掓嫈閽夋満鐐逛綅
+        private static readonly List<string> PinMachineCodes = new List<string> { "10190", "20100" };
+
         public void HandleInboundNextAddress(CommonConveyorLine conveyorLine, string nextAddress, string childDeviceCode)
         {
             HandleDeviceRequest(conveyorLine, nextAddress, childDeviceCode, isUpper: true);
@@ -23,7 +28,7 @@
         {
             var devices = Storage.Devices;
 
-            if (string.Equals(nextAddress, ConstraintMachineName, StringComparison.Ordinal))
+            if (ConstraintMachineCodes.Contains(nextAddress))
             {
                 ConstraintMachine? constraint = devices.OfType<ConstraintMachine>().FirstOrDefault(d => d.DeviceName == ConstraintMachineName);
                 if (constraint == null)
@@ -35,11 +40,11 @@
                     conveyorLine,
                     childDeviceCode,
                     getMaterialRequest: () => isUpper
-                        ? constraint.GetValue<ConstraintMachineDBName, bool>(ConstraintMachineDBName.MaterialRequestUpper)
-                        : constraint.GetValue<ConstraintMachineDBName, bool>(ConstraintMachineDBName.MaterialRequestLower),
+                        ? constraint.GetValue<ConstraintMachineDBName, short>(ConstraintMachineDBName.MaterialRequestUpper) != 0
+                        : constraint.GetValue<ConstraintMachineDBName, short>(ConstraintMachineDBName.MaterialRequestLower) != 0,
                     getOutputRequest: () => isUpper
-                        ? constraint.GetValue<ConstraintMachineDBName, bool>(ConstraintMachineDBName.OutputRequestUpper)
-                        : constraint.GetValue<ConstraintMachineDBName, bool>(ConstraintMachineDBName.OutputRequestLower),
+                        ? constraint.GetValue<ConstraintMachineDBName, short>(ConstraintMachineDBName.OutputRequestUpper) != 0
+                        : constraint.GetValue<ConstraintMachineDBName, short>(ConstraintMachineDBName.OutputRequestLower) != 0,
                     setOutputReady: outputReq =>
                     {
                         if (isUpper)
@@ -52,7 +57,7 @@
                         }
                     });
             }
-            else if (string.Equals(nextAddress, PinMachineName, StringComparison.Ordinal))
+            else if (PinMachineCodes.Contains(nextAddress))
             {
                 PinMachine? pinMachine = devices.OfType<PinMachine>().FirstOrDefault(d => d.DeviceName == PinMachineName);
                 if (pinMachine == null)
@@ -64,11 +69,11 @@
                     conveyorLine,
                     childDeviceCode,
                     getMaterialRequest: () => isUpper
-                        ? pinMachine.GetValue<PinMachineDBName, bool>(PinMachineDBName.MaterialRequestUpper)
-                        : pinMachine.GetValue<PinMachineDBName, bool>(PinMachineDBName.MaterialRequestLower),
+                        ? pinMachine.GetValue<PinMachineDBName, short>(PinMachineDBName.MaterialRequestUpper) != 0
+                        : pinMachine.GetValue<PinMachineDBName, short>(PinMachineDBName.MaterialRequestLower) != 0,
                     getOutputRequest: () => isUpper
-                        ? pinMachine.GetValue<PinMachineDBName, bool>(PinMachineDBName.OutputRequestUpper)
-                        : pinMachine.GetValue<PinMachineDBName, bool>(PinMachineDBName.OutputRequestLower),
+                        ? pinMachine.GetValue<PinMachineDBName, short>(PinMachineDBName.OutputRequestUpper) != 0
+                        : pinMachine.GetValue<PinMachineDBName, short>(PinMachineDBName.OutputRequestLower) != 0,
                     setOutputReady: outputReq =>
                     {
                         if (isUpper)

--
Gitblit v1.9.3