From ba9c1994b95624b88ef606ec00394990d8f2009f Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期三, 15 四月 2026 19:41:27 +0800
Subject: [PATCH] refactor(TaskService): 重构任务服务代码结构,拆分文件并优化逻辑
---
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineNewJob/CommonConveyorLineNewJob.cs | 105 ++++++++++++++++++++++++++++++++--------------------
1 files changed, 64 insertions(+), 41 deletions(-)
diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineNewJob/CommonConveyorLineNewJob.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineNewJob/CommonConveyorLineNewJob.cs
index cad24d5..edbdda4 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineNewJob/CommonConveyorLineNewJob.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineNewJob/CommonConveyorLineNewJob.cs
@@ -1,9 +1,9 @@
using MapsterMapper;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
+using Newtonsoft.Json;
using Quartz;
using SqlSugar;
-using System.Text.Json;
using WIDESEA_Core;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
@@ -14,6 +14,7 @@
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.Service;
+using ManualInboundTaskHandler = WIDESEAWCS_Tasks.ConveyorLineNewJob.ManualInbound.ManualInboundTaskHandler;
namespace WIDESEAWCS_Tasks
{
@@ -77,6 +78,13 @@
private readonly ILogger<CommonConveyorLineNewJob> _logger;
/// <summary>
+ /// 鐩爣鍦板潃鍒拌澶囩被鍨嬬殑鏄犲皠
+ /// </summary>
+ /// <remarks>
+ /// </remarks>
+ private static List<string> AddressToDeviceType = new List<string> { "11020", "11028" };
+
+ /// <summary>
/// 鏋勯�犲嚱鏁�
/// </summary>
/// <param name="taskService">浠诲姟鏈嶅姟</param>
@@ -125,25 +133,25 @@
if (childDeviceCodes == null || childDeviceCodes.Count == 0)
{
// 娌℃湁瀛愯澶囷紝鐩存帴杩斿洖
- _logger.LogInformation("杈撻�佺嚎 {DeviceCode} 娌℃湁瀛愯澶�", conveyorLine.DeviceCode);
- QuartzLogger.Info($"杈撻�佺嚎 {conveyorLine.DeviceCode} 娌℃湁瀛愯澶�", conveyorLine.DeviceCode);
+ QuartzLogHelper.LogInfo(_logger, "杈撻�佺嚎 {DeviceCode} 娌℃湁瀛愯澶�", $"杈撻�佺嚎 {conveyorLine.DeviceCode} 娌℃湁瀛愯澶�", conveyorLine.DeviceCode, conveyorLine.DeviceCode);
return Task.CompletedTask;
}
// 鍒涘缓骞惰閫夐」锛岄檺鍒舵渶澶у苟鍙戞暟
- var parallelOptions = new ParallelOptions
- {
- // 闄愬埗骞跺彂鏁帮細瀛愯澶囨暟閲忓拰 CPU 鏍稿績鏁�*2 鐨勮緝灏忓��
- MaxDegreeOfParallelism = Math.Min(childDeviceCodes.Count, Environment.ProcessorCount * 2),
- };
+ //var parallelOptions = new ParallelOptions
+ //{
+ // // 闄愬埗骞跺彂鏁帮細瀛愯澶囨暟閲忓拰 CPU 鏍稿績鏁�*2 鐨勮緝灏忓��
+ // MaxDegreeOfParallelism = Math.Min(childDeviceCodes.Count, Environment.ProcessorCount * 2),
+ //};
- _logger.LogDebug("Execute锛氬紑濮嬪苟琛屽鐞嗚緭閫佺嚎 {DeviceCode}锛屽瓙璁惧鏁伴噺: {Count}", conveyorLine.DeviceCode, childDeviceCodes.Count);
- QuartzLogger.Debug($"寮�濮嬪苟琛屽鐞嗚緭閫佺嚎锛屽瓙璁惧鏁伴噺: {childDeviceCodes.Count}", conveyorLine.DeviceCode);
+ //_logger.LogDebug("Execute锛氬紑濮嬪苟琛屽鐞嗚緭閫佺嚎 {DeviceCode}锛屽瓙璁惧鏁伴噺: {Count}", conveyorLine.DeviceCode, childDeviceCodes.Count);
+ //QuartzLogger.Debug($"寮�濮嬪苟琛屽鐞嗚緭閫佺嚎锛屽瓙璁惧鏁伴噺: {childDeviceCodes.Count}", conveyorLine.DeviceCode);
// 骞惰澶勭悊姣忎釜瀛愯澶�
- Parallel.For(0, childDeviceCodes.Count, parallelOptions, i =>
+ //Parallel.For(0, childDeviceCodes.Count, parallelOptions, i =>
+ foreach (var childDeviceCode in childDeviceCodes)
{
- string childDeviceCode = childDeviceCodes[i];
+ //string childDeviceCode = childDeviceCodes[i];
var correlationId = Guid.NewGuid().ToString("N");
try
{
@@ -153,14 +161,10 @@
// 濡傛灉鍛戒护涓虹┖锛岃烦杩�
if (command == null)
{
- _logger.LogDebug("Execute锛氬瓙璁惧 {ChildDeviceCode} 鍛戒护涓虹┖锛岃烦杩�", childDeviceCode);
- QuartzLogger.Debug($"瀛愯澶� {childDeviceCode} 鍛戒护涓虹┖锛岃烦杩�", conveyorLine.DeviceCode);
- return;
+ continue;
}
- // 濡傛灉 WCS_ACK 涓� 1锛屽厛娓呴櫎锛堣〃绀哄鐞嗚繃涓婁竴娆¤姹傦級
- if (command.WCS_ACK == 1)
- conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_ACK, 0, childDeviceCode);
+ #region 妫�娴嬫槸鍚﹂渶瑕佺┖鎵樼洏
// ========== 妫�鏌ョ壒瀹氫綅缃槸鍚︽湁鎵樼洏 ==========
// 浠庨厤缃腑璇诲彇闇�瑕佹鏌ユ墭鐩樼殑浣嶇疆鍒楄〃
@@ -171,16 +175,15 @@
if (checkPalletPositions.Any(x => x.Code == childDeviceCode))
{
// 妫�鏌ヨ緭閫佺嚎鐘舵�侊紙鏄惁鏈夋墭鐩橈級
- if (command.CV_State.ObjToBool())
+ if (command.CV_State == 2)
{
// 妫�鏌ヨ浣嶇疆鏄惁宸叉湁浠诲姟
- var existingTask = _taskService.Repository.QueryFirst(x => x.TargetAddress == childDeviceCode);
+ var existingTask = _taskService.Db.Queryable<Dt_Task>().First(x => x.TargetAddress == childDeviceCode);
if (existingTask.IsNullOrEmpty())
{
// 娌℃湁浠诲姟锛屽悜 WMS 璇锋眰鍑哄簱鎵樼洏浠诲姟
var position = checkPalletPositions.FirstOrDefault(x => x.Code == childDeviceCode);
- _logger.LogInformation("Execute锛氭鏌ユ墭鐩樹綅缃� {ChildDeviceCode}锛岃姹俉MS鍑哄簱鎵樼洏浠诲姟", childDeviceCode);
- QuartzLogger.Info($"妫�鏌ユ墭鐩樹綅缃� {childDeviceCode}锛岃姹俉MS鍑哄簱鎵樼洏浠诲姟", conveyorLine.DeviceCode);
+ QuartzLogHelper.LogInfo(_logger, "Execute锛氭鏌ユ墭鐩樹綅缃� {ChildDeviceCode}锛岃姹俉MS鍑哄簱鎵樼洏浠诲姟", $"妫�鏌ユ墭鐩樹綅缃� {childDeviceCode}锛岃姹俉MS鍑哄簱鎵樼洏浠诲姟", conveyorLine.DeviceCode, childDeviceCode);
var responseResult = _httpClientHelper.Post<WebResponseContent>("GetOutBoundTrayTaskAsync", new CreateTaskDto()
{
@@ -191,62 +194,76 @@
// 濡傛灉璇锋眰鎴愬姛锛屾帴鏀� WMS 杩斿洖鐨勪换鍔�
if (responseResult.IsSuccess && responseResult.Data.Status)
{
- var wmsTask = JsonSerializer.Deserialize<List<WMSTaskDTO>>(responseResult.Data.Data.Serialize());
+ var wmsTask = JsonConvert.DeserializeObject<WMSTaskDTO>(responseResult.Data.Data.ToString());
+ List<WMSTaskDTO> taskDTOs = new List<WMSTaskDTO> { wmsTask };
if (wmsTask != null)
- _taskService.ReceiveWMSTask(wmsTask);
+ _taskService.ReceiveWMSTask(taskDTOs);
}
}
}
}
+ #endregion
+
// ========== 妫�鏌� PLC_STB 鏍囧織 ==========
// 鍙湁褰� PLC_STB 涓� 1 鏃舵墠澶勭悊浠诲姟
if (command.PLC_STB != 1)
{
- _logger.LogDebug("Execute锛氬瓙璁惧 {ChildDeviceCode} PLC_STB 涓嶄负1锛岃烦杩�", childDeviceCode);
- QuartzLogger.Debug($"瀛愯澶� {childDeviceCode} PLC_STB 涓嶄负1锛岃烦杩�", conveyorLine.DeviceCode);
- return;
+ // 濡傛灉 WCS_ACK 涓� 1锛屽厛娓呴櫎锛堣〃绀哄鐞嗚繃涓婁竴娆¤姹傦級
+ if (command.WCS_ACK == 1)
+ conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_ACK, (short)0, childDeviceCode);
+
+ // 澶勭悊鎵嬪姩鍏ュ簱浠诲姟锛堣捣鐐逛负绾夸綋鐐逛綅鐨勪换鍔★級
+ try
+ {
+ var task = _taskService.QueryManualInboundTask(childDeviceCode);
+ if (task != null)
+ {
+ var handler = new ManualInboundTaskHandler(_taskService);
+ handler.WriteTaskToPlc(conveyorLine, childDeviceCode, task);
+ }
+ }
+ catch (Exception ex)
+ {
+ QuartzLogHelper.LogError(_logger, ex, "澶勭悊鎵嬪姩鍏ュ簱浠诲姟寮傚父", $"澶勭悊鎵嬪姩鍏ュ簱浠诲姟寮傚父: {ex.Message}", "CommonConveyorLineNewJob");
+ }
+ continue;
}
// ========== 澶勭悊鏃犳墭鐩樻潯鐮佺殑鎯呭喌 ==========
// 鏃犳墭鐩樻潯鐮佹椂锛岃姹傚嚭搴撲换鍔�
if (command.Barcode.IsNullOrEmpty() || command.Barcode.Replace("\0", "") == "")
{
- _logger.LogDebug("Execute锛氬瓙璁惧 {ChildDeviceCode} 鏃犳墭鐩樻潯鐮侊紝璇锋眰鍑哄簱浠诲姟", childDeviceCode);
- QuartzLogger.Debug($"瀛愯澶� {childDeviceCode} 鏃犳墭鐩樻潯鐮侊紝璇锋眰鍑哄簱浠诲姟", conveyorLine.DeviceCode);
_conveyorLineDispatch.RequestOutbound(conveyorLine, command, childDeviceCode);
- return;
+ continue;
}
// ========== 澶勭悊宸叉湁浠诲姟鍙风殑鎯呭喌 ==========
- if (command.TaskNo > 0)
+ if (command.TaskNo > 0 && !command.Barcode.IsNullOrEmpty())
{
// 鏌ヨ姝e湪鎵ц鐨勪换鍔�
Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNo, childDeviceCode);
if (!task.IsNullOrEmpty())
{
- _logger.LogInformation("Execute锛氬瓙璁惧 {ChildDeviceCode} 澶勭悊浠诲姟 {TaskNum}锛岀姸鎬�: {Status}", childDeviceCode, task.TaskNum, task.TaskStatus);
- QuartzLogger.Info($"澶勭悊浠诲姟 {task.TaskNum}锛岀姸鎬�: {task.TaskStatus}", conveyorLine.DeviceCode);
+ QuartzLogHelper.LogInfo(_logger, "Execute锛氬瓙璁惧 {ChildDeviceCode} 澶勭悊浠诲姟 {TaskNum}锛岀姸鎬�: {Status}", $"澶勭悊浠诲姟 {task.TaskNum}锛岀姸鎬�: {task.TaskStatus}", conveyorLine.DeviceCode, childDeviceCode, task.TaskNum, task.TaskStatus);
// 澶勭悊浠诲姟鐘舵�侊紙鏍规嵁鐘舵�佸垎鍙戝埌涓嶅悓鏂规硶锛�
ProcessTaskState(conveyorLine, command, task, childDeviceCode);
- return;
+ return Task.CompletedTask;
}
}
}
catch (Exception innerEx)
{
// 璁板綍寮傚父锛屼絾涓嶅奖鍝嶅叾浠栧瓙璁惧鐨勫鐞�
- _logger.LogError(innerEx, "Execute锛氬瓙璁惧 {ChildDeviceCode} 澶勭悊寮傚父锛孋orrelationId: {CorrelationId}", childDeviceCode, correlationId);
- QuartzLogger.Error($"瀛愯澶囧鐞嗗紓甯�: {innerEx.Message}", conveyorLine.DeviceCode, innerEx);
+ QuartzLogHelper.LogError(_logger, innerEx, "Execute锛氬瓙璁惧 {ChildDeviceCode} 澶勭悊寮傚父锛孋orrelationId: {CorrelationId}", $"瀛愯澶囧鐞嗗紓甯�: {innerEx.Message}", conveyorLine.DeviceCode, childDeviceCode, correlationId);
}
- });
+ }
}
}
catch (Exception ex)
{
// 璁板綍鏁翠綋寮傚父
- _logger.LogError(ex, "Execute锛氳緭閫佺嚎 {DeviceCode} 鎵ц寮傚父", ex.Message);
- QuartzLogger.Error($"杈撻�佺嚎鎵ц寮傚父: {ex.Message}", "CommonConveyorLineNewJob", ex);
+ QuartzLogHelper.LogError(_logger, ex, "Execute锛氳緭閫佺嚎 {DeviceCode} 鎵ц寮傚父", $"杈撻�佺嚎鎵ц寮傚父: {ex.Message}", "CommonConveyorLineNewJob", ex.Message);
}
return Task.CompletedTask;
}
@@ -284,7 +301,13 @@
{
case InExecuting:
// 鍏ュ簱鎵ц涓紝璋冪敤涓嬩竴鍦板潃澶勭悊
- _conveyorLineDispatch.RequestInNextAddress(conveyorLine, command, childDeviceCode);
+
+ if (AddressToDeviceType.Contains(childDeviceCode))
+ // 鍒拌揪鐩爣鍦板潃锛岃皟鐢ㄥ叆搴撳畬鎴�
+ _conveyorLineDispatch.ConveyorLineInFinish(conveyorLine, command, childDeviceCode);
+ else
+ // 鏈埌杈剧洰鏍囧湴鍧�锛岃皟鐢ㄥ叆搴撲笅涓�鍦板潃澶勭悊
+ _conveyorLineDispatch.RequestInNextAddress(conveyorLine, command, childDeviceCode);
break;
case OutExecuting:
@@ -309,4 +332,4 @@
}
}
}
-}
+}
\ No newline at end of file
--
Gitblit v1.9.3