From 88724eca51a5863f20ee0a552af741aeebf4e8f2 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期三, 11 二月 2026 17:31:45 +0800
Subject: [PATCH] 添加堆垛机配置并重构任务

---
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs |  155 ++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 120 insertions(+), 35 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs
index 37cefb6..147c902 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs
@@ -30,6 +30,7 @@
         private readonly ITaskExecuteDetailService _taskExecuteDetailService;
         private readonly ITaskRepository _taskRepository;
         private readonly IRouterService _routerService;
+        private readonly StackerCraneCommandConfig _config;
 
         public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService)
         {
@@ -37,6 +38,28 @@
             _taskExecuteDetailService = taskExecuteDetailService;
             _taskRepository = taskRepository;
             _routerService = routerService;
+            _config = LoadConfig();
+        }
+
+        /// <summary>
+        /// 鍔犺浇閰嶇疆锛堜紭鍏堢骇锛氶厤缃枃浠� > 榛樿閰嶇疆锛�
+        /// </summary>
+        private static StackerCraneCommandConfig LoadConfig()
+        {
+            try
+            {
+                string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StackerCraneJob", "stackercrane-command-config.json");
+                if (File.Exists(configPath))
+                {
+                    string json = File.ReadAllText(configPath);
+                    return System.Text.Json.JsonSerializer.Deserialize<StackerCraneCommandConfig>(json) ?? new StackerCraneCommandConfig();
+                }
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine($"閰嶇疆鍔犺浇澶辫触: {ex.Message}锛屼娇鐢ㄩ粯璁ら厤缃�");
+            }
+            return new StackerCraneCommandConfig();
         }
 
         public Task Execute(IJobExecutionContext context)
@@ -63,7 +86,7 @@
                     Dt_Task? task = GetTask(commonStackerCrane);
                     if (task != null)
                     {
-                        StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
+                        var stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
                         if (stackerCraneTaskCommand != null)
                         {
                             bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand);
@@ -178,32 +201,85 @@
         /// <summary>
         /// 浠诲姟瀹炰綋杞崲鎴愬懡浠odel
         /// </summary>
-        /// <param name="task">浠诲姟瀹炰綋</param>
-        /// <returns></returns>
-        public StackerCraneTaskCommand? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task)
+        public dynamic? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task)
         {
-            StackerCraneTaskCommand stackerCraneTaskCommand = new StackerCraneTaskCommand
+            // 鏍规嵁閰嶇疆鍒ゆ柇鍛戒护绫诲瀷
+            string commandType = GetCommandType(task.Roadway);
+
+            // 鍒涘缓骞舵瀯寤哄懡浠�
+            return commandType switch
+            {
+                "Formation" => BuildCommand(task, CreateFormationCommand(task)),
+                _ => BuildCommand(task, CreateStandardCommand(task))
+            };
+        }
+
+        /// <summary>
+        /// 鏍规嵁 Roadway 鑾峰彇鍛戒护绫诲瀷
+        /// </summary>
+        private string GetCommandType(string roadway)
+        {
+            foreach (var mapping in _config.RoadwayCommandMapping)
+            {
+                if (roadway.Contains(mapping.Key))
+                {
+                    return mapping.Value;
+                }
+            }
+            return _config.DefaultCommandType;
+        }
+
+        /// <summary>
+        /// 鍒涘缓鏍囧噯鍫嗗灈鏈哄懡浠�
+        /// </summary>
+        private static StackerCraneTaskCommand CreateStandardCommand(Dt_Task task)
+        {
+            return new StackerCraneTaskCommand
             {
                 Barcode = task.PalletCode,
                 TaskNum = task.TaskNum,
                 WorkType = 1,
                 TrayType = 0
             };
+        }
 
-            TaskTypeGroup taskTypeGroup = task.TaskType.GetTaskTypeGroup();
-            return taskTypeGroup switch
+        /// <summary>
+        /// 鍒涘缓缁勭洏鍫嗗灈鏈哄懡浠�
+        /// </summary>
+        private static FormationStackerCraneTaskCommand CreateFormationCommand(Dt_Task task)
+        {
+            return new FormationStackerCraneTaskCommand
             {
-                TaskTypeGroup.InboundGroup => BuildInboundCommand(task, stackerCraneTaskCommand),
-                TaskTypeGroup.OutbondGroup => BuildOutboundCommand(task, stackerCraneTaskCommand),
-                TaskTypeGroup.RelocationGroup => BuildRelocationCommand(task, stackerCraneTaskCommand),
-                _ => stackerCraneTaskCommand
+                Barcode = task.PalletCode,
+                TaskNum = task.TaskNum,
+                WorkType = 1,
+                WorkAction = 1,
+                FireAlarm = 0,
+                HeartBeat = 0,
+                FieldName = string.Empty
             };
         }
 
         /// <summary>
-        /// 鏋勫缓鍏ュ簱鍛戒护
+        /// 閫氱敤鍛戒护鏋勫缓鏂规硶
         /// </summary>
-        private StackerCraneTaskCommand? BuildInboundCommand(Dt_Task task, StackerCraneTaskCommand command)
+        private T? BuildCommand<T>(Dt_Task task, T command) where T : class
+        {
+            TaskTypeGroup taskTypeGroup = task.TaskType.GetTaskTypeGroup();
+
+            return taskTypeGroup switch
+            {
+                TaskTypeGroup.InboundGroup => BuildInboundCommand(task, command),
+                TaskTypeGroup.OutbondGroup => BuildOutboundCommand(task, command),
+                TaskTypeGroup.RelocationGroup => BuildRelocationCommand(task, command),
+                _ => command
+            };
+        }
+
+        /// <summary>
+        /// 閫氱敤鍏ュ簱鍛戒护鏋勫缓
+        /// </summary>
+        private T? BuildInboundCommand<T>(Dt_Task task, T command) where T : class
         {
             Dt_Router? router = _routerService.QueryNextRoute(task.CurrentAddress, task.Roadway, task.TaskType);
             if (router == null)
@@ -212,9 +288,9 @@
                 return null;
             }
 
-            command.StartRow = Convert.ToInt16(router.SrmRow);
-            command.StartColumn = Convert.ToInt16(router.SrmColumn);
-            command.StartLayer = Convert.ToInt16(router.SrmLayer);
+            SetCommandProperty(command, "StartRow", Convert.ToInt16(router.SrmRow));
+            SetCommandProperty(command, "StartColumn", Convert.ToInt16(router.SrmColumn));
+            SetCommandProperty(command, "StartLayer", Convert.ToInt16(router.SrmLayer));
 
             if (!TryParseAddress(task.NextAddress, out short endRow, out short endColumn, out short endLayer))
             {
@@ -222,17 +298,17 @@
                 return null;
             }
 
-            command.EndRow = endRow;
-            command.EndColumn = endColumn;
-            command.EndLayer = endLayer;
+            SetCommandProperty(command, "EndRow", endRow);
+            SetCommandProperty(command, "EndColumn", endColumn);
+            SetCommandProperty(command, "EndLayer", endLayer);
 
             return command;
         }
 
         /// <summary>
-        /// 鏋勫缓鍑哄簱鍛戒护
+        /// 閫氱敤鍑哄簱鍛戒护鏋勫缓
         /// </summary>
-        private StackerCraneTaskCommand? BuildOutboundCommand(Dt_Task task, StackerCraneTaskCommand command)
+        private T? BuildOutboundCommand<T>(Dt_Task task, T command) where T : class
         {
             Dt_Router? router = _routerService.QueryNextRoute(task.Roadway, task.TargetAddress, task.TaskType);
             if (router == null)
@@ -241,9 +317,9 @@
                 return null;
             }
 
-            command.EndRow = Convert.ToInt16(router.SrmRow);
-            command.EndColumn = Convert.ToInt16(router.SrmColumn);
-            command.EndLayer = Convert.ToInt16(router.SrmLayer);
+            SetCommandProperty(command, "EndRow", Convert.ToInt16(router.SrmRow));
+            SetCommandProperty(command, "EndColumn", Convert.ToInt16(router.SrmColumn));
+            SetCommandProperty(command, "EndLayer", Convert.ToInt16(router.SrmLayer));
 
             if (!TryParseAddress(task.CurrentAddress, out short startRow, out short startColumn, out short startLayer))
             {
@@ -251,17 +327,17 @@
                 return null;
             }
 
-            command.StartRow = startRow;
-            command.StartColumn = startColumn;
-            command.StartLayer = startLayer;
+            SetCommandProperty(command, "StartRow", startRow);
+            SetCommandProperty(command, "StartColumn", startColumn);
+            SetCommandProperty(command, "StartLayer", startLayer);
 
             return command;
         }
 
         /// <summary>
-        /// 鏋勫缓绉诲簱鍛戒护
+        /// 閫氱敤绉诲簱鍛戒护鏋勫缓
         /// </summary>
-        private StackerCraneTaskCommand? BuildRelocationCommand(Dt_Task task, StackerCraneTaskCommand command)
+        private T? BuildRelocationCommand<T>(Dt_Task task, T command) where T : class
         {
             if (!TryParseAddress(task.NextAddress, out short endRow, out short endColumn, out short endLayer))
             {
@@ -269,9 +345,9 @@
                 return null;
             }
 
-            command.EndRow = endRow;
-            command.EndColumn = endColumn;
-            command.EndLayer = endLayer;
+            SetCommandProperty(command, "EndRow", endRow);
+            SetCommandProperty(command, "EndColumn", endColumn);
+            SetCommandProperty(command, "EndLayer", endLayer);
 
             if (!TryParseAddress(task.CurrentAddress, out short startRow, out short startColumn, out short startLayer))
             {
@@ -279,14 +355,23 @@
                 return null;
             }
 
-            command.StartRow = startRow;
-            command.StartColumn = startColumn;
-            command.StartLayer = startLayer;
+            SetCommandProperty(command, "StartRow", startRow);
+            SetCommandProperty(command, "StartColumn", startColumn);
+            SetCommandProperty(command, "StartLayer", startLayer);
 
             return command;
         }
 
         /// <summary>
+        /// 浣跨敤鍙嶅皠璁剧疆鍛戒护灞炴��
+        /// </summary>
+        private static void SetCommandProperty<T>(T command, string propertyName, object value) where T : class
+        {
+            var property = typeof(T).GetProperty(propertyName);
+            property?.SetValue(command, value);
+        }
+
+        /// <summary>
         /// 瑙f瀽鍦板潃瀛楃涓诧紙鏍煎紡锛氳-鍒�-灞傦級
         /// </summary>
         private bool TryParseAddress(string address, out short row, out short column, out short layer)

--
Gitblit v1.9.3