From 3b3f39b55ba6d04a95536bebd6233b2a6e464290 Mon Sep 17 00:00:00 2001 From: xxyy <cathay_xy@163.com> Date: 星期二, 18 二月 2025 10:20:02 +0800 Subject: [PATCH] 更新命名空间、异常处理和任务逻辑 --- Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerStationJob/CommonStackerStationCraneJob.cs | 195 ++++++++++++++++++++++++++++++------------------ 1 files changed, 121 insertions(+), 74 deletions(-) diff --git a/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerStationJob/CommonStackerStationCraneJob.cs b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerStationJob/CommonStackerStationCraneJob.cs index f65a3c1..73152f9 100644 --- a/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerStationJob/CommonStackerStationCraneJob.cs +++ b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerStationJob/CommonStackerStationCraneJob.cs @@ -2,9 +2,13 @@ using Newtonsoft.Json; using Quartz; using System.Diagnostics.CodeAnalysis; +using System.Text; +using System.Threading.Tasks; using WIDESEAWCS_BasicInfoRepository; using WIDESEAWCS_Common.TaskEnum; +using WIDESEAWCS_Core.Caches; using WIDESEAWCS_Core.Helper; +using WIDESEAWCS_Core.HttpContextUser; using WIDESEAWCS_DTO.MOM; using WIDESEAWCS_IProcessRepository; using WIDESEAWCS_ITaskInfoRepository; @@ -16,7 +20,9 @@ using WIDESEAWCS_QuartzJob.Models; using WIDESEAWCS_QuartzJob.Service; using WIDESEAWCS_QuartzJob.StackerCrane.Enum; +using WIDESEAWCS_SignalR; using WIDESEAWCS_Tasks.StackerCraneJob; +using WIDESEAWCS_Common; namespace WIDESEAWCS_Tasks { @@ -28,24 +34,24 @@ private readonly ITaskRepository _taskRepository; private readonly IProcessRepository _processRepository; private readonly IDt_StationManagerRepository _stationManagerRepository; + private readonly ICacheService _cacheService; + private readonly INoticeService _noticeService; - public CommonStackerStationCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IProcessRepository processRepository, IDt_StationManagerRepository stationManagerRepository) + public CommonStackerStationCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IProcessRepository processRepository, IDt_StationManagerRepository stationManagerRepository, ICacheService cacheService, INoticeService noticeService) { _taskService = taskService; _taskExecuteDetailService = taskExecuteDetailService; _taskRepository = taskRepository; _processRepository = processRepository; _stationManagerRepository = stationManagerRepository; + _cacheService = cacheService; + _noticeService = noticeService; } public Task Execute(IJobExecutionContext context) { try { - // speStackerCrane.GetStackerCraneStatus<StackerCraneAutoStatus>(); - // speStackerCrane.GetStackerCraneStatus<StackerCraneWorkStatus>(); - // speStackerCrane.GetStackerCraneStatus<StackerCraneStatus>(); - CommonStackerStationCrane commonStackerCrane = (CommonStackerStationCrane)context.JobDetail.JobDataMap.Get("JobParams"); if (commonStackerCrane != null) { @@ -62,15 +68,30 @@ if (commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby) { - Dt_Task? task = GetTask(commonStackerCrane); + Dt_Task? task = null; + if (StaticVariable.isLineRun) + { + StaticVariable.isStackerRun = false; + task = GetTask(commonStackerCrane); + } if (task != null) { StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task); if (stackerCraneTaskCommand != null) { + Thread.Sleep(1000); bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand); if (sendFlag) { + StringBuilder builder = new StringBuilder(); + builder.AppendLine(); + builder.AppendLine($"銆恵commonStackerCrane.DeviceName}銆戝爢鍨涙満鐘舵�侊細銆恵commonStackerCrane.StackerCraneStatusDes}銆�,鏃堕棿锛氥�恵DateTime.Now}銆�"); + builder.AppendLine($"銆恵commonStackerCrane.DeviceName}銆戞墜鑷姩鐘舵�侊細銆恵commonStackerCrane.StackerCraneAutoStatusDes}銆�,鏃堕棿锛氥�恵DateTime.Now}銆�"); + builder.AppendLine($"銆恵commonStackerCrane.DeviceName}銆戜綔涓氱姸鎬侊細銆恵commonStackerCrane.StackerCraneWorkStatusDes}銆�,鏃堕棿锛氥�恵DateTime.Now}銆�"); + builder.AppendLine($"銆恵commonStackerCrane.DeviceName}銆戜笅鍙戜换鍔℃垚鍔�,銆恵JsonConvert.SerializeObject(stackerCraneTaskCommand, Formatting.Indented)}銆�"); + builder.AppendLine($"鏃堕棿锛氥�恵DateTime.Now}銆�"); + builder.AppendLine(); + ConsoleHelper.WriteColorLine(builder, ConsoleColor.Blue); commonStackerCrane.LastTaskType = task.TaskType; _taskService.UpdateTaskStatusToNext(task.TaskNum); } @@ -78,12 +99,41 @@ } } } + + #region 璋冪敤浜嬩欢鎬荤嚎閫氱煡鍓嶇 + + var tokenInfos = _cacheService.Get<List<UserInfo>>("Cache_UserToken"); + if (tokenInfos == null || !tokenInfos.Any()) + { + //throw new Exception(commonStackerCrane.DeviceName + "缂撳瓨涓湭鎵惧埌Token缂撳瓨"); + return Task.CompletedTask; + } + var userTokenIds = tokenInfos?.Select(x => x.Token_ID).ToList(); + var userIds = tokenInfos?.Select(x => x.UserId).ToList(); + + object obj = new + { + commonStackerCrane.StackerCraneStatusDes, + commonStackerCrane.StackerCraneAutoStatusDes, + commonStackerCrane.StackerCraneWorkStatusDes, + commonStackerCrane.DeviceCode, + commonStackerCrane.DeviceName, + commonStackerCrane.CurrentTaskNum, + commonStackerCrane.LastTaskNum, + }; + _noticeService.StackerData(userIds?.FirstOrDefault(), userTokenIds, new { commonStackerCrane.DeviceName, data = obj }); + + #endregion 璋冪敤浜嬩欢鎬荤嚎閫氱煡鍓嶇 } } catch (Exception ex) { WriteError("CommonStackerStationCraneJob", "test", ex); //Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString()); + } + finally + { + StaticVariable.isStackerRun = true; } //WriteDebug("CommonStackerStationCraneJob", "test"); return Task.CompletedTask; @@ -101,9 +151,19 @@ { if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5) { - Console.Out.WriteLine("TaskCompleted" + e.TaskNum); - _taskService.StackCraneTaskCompletedByStation(e.TaskNum); + ConsoleHelper.WriteColorLine($"銆恵commonStackerCrane.DeviceName}銆戝爢鍨涙満浣滀笟鐘舵�侊細銆恵(int)commonStackerCrane.StackerCraneWorkStatusValue}銆戞椂闂淬�恵DateTime.Now}銆�", ConsoleColor.Magenta); + + string str = $"銆恵commonStackerCrane.DeviceName}銆戜换鍔″畬鎴�,浠诲姟鍙凤細銆恵e.TaskNum}銆戞椂闂淬�恵DateTime.Now}銆�"; + WriteInfo(commonStackerCrane.DeviceName, str); + ConsoleHelper.WriteColorLine(str, ConsoleColor.Blue); + + var content = _taskService.StackCraneTaskCompletedByStation(e.TaskNum); commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5); + + var isWorkType = commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5); + str = $"{commonStackerCrane.DeviceName}銆慦MS|WCS浠诲姟瀹屾垚锛氥�恵content.Status}銆�,鍫嗗灈鏈哄畬鎴愪俊鍙峰啓鍏ワ細銆恵isWorkType}銆�,浠诲姟鍙凤細銆恵e.TaskNum}銆戞椂闂淬�恵DateTime.Now}銆�"; + WriteInfo(commonStackerCrane.DeviceName, str); + ConsoleHelper.WriteColorLine(str, ConsoleColor.Blue); } } } @@ -116,6 +176,13 @@ private Dt_Task? GetTask(CommonStackerStationCrane commonStackerCrane) { Dt_Task task; + + task = _taskService.QueryRelocationTask(commonStackerCrane.DeviceCode); + if (task != null) + { + return task; + } + if (commonStackerCrane.LastTaskType == null) { task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode); @@ -138,21 +205,21 @@ if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) { - if (OutTaskStationIsOccupied(task) != null) + // 妫�鏌ュ綋鍓嶅嚭搴撲换鍔$珯鍙版槸鍚﹀厑璁告斁璐� + var occupiedStation = OutTaskStationIsOccupied(task); + if (occupiedStation == null) { - return task; + // 濡傛灉褰撳墠鍑哄簱浠诲姟绔欏彴涓嶅厑璁告斁璐э紝鎺掗櫎褰撳墠浠诲姟锛屾煡鎵惧叾浠栧嚭搴撲换鍔� + ConsoleHelper.WriteErrorLine($"浠诲姟鍙凤細銆恵task.TaskNum}銆戝嚭搴撳湴鍧�锛氥�恵task.NextAddress}銆戜笉鍏佽鏀捐揣"); + task = FindAnotherOutboundTask(commonStackerCrane.DeviceCode, task); } else { - //List<string> otherOutStaionCodes = _routerService.QueryNextRoutes(commonStackerCrane.DeviceCode, task.NextAddress).Select(x => x.ChildPosi).ToList(); - //List<Dt_Task> tasks = _taskService.QueryStackerCraneOutTasks(commonStackerCrane.DeviceCode, otherOutStaionCodes); - //foreach (var item in tasks) - //{ - // if (OutTaskStationIsOccupied(task) != null) - // { - // return task; - // } - //} + return task; + } + + if (task == null) + { task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode); } } @@ -189,6 +256,40 @@ } /// <summary> + /// 鏌ユ壘鍏朵粬鍑哄簱浠诲姟鐨勮緟鍔╂柟娉曪紙鎺掗櫎涓嶅彲鍑虹殑鍑哄簱鍙d换鍔★級 + /// </summary> + /// <param name="deviceCode">璁惧浠g爜</param> + /// <param name="excludedTaskId">瑕佹帓闄ょ殑浠诲姟ID</param> + /// <returns></returns> + private Dt_Task? FindAnotherOutboundTask(string deviceCode, Dt_Task task) + { + // 鍏堣幏鍙栨墍鏈夌鍚堟潯浠讹紙鎺掗櫎涓嶅彲鍑虹殑锛夌殑鍑哄簱浠诲姟鍒楄〃 + var allOutboundTasks = _taskService.QueryAllOutboundTasks(deviceCode); + Console.WriteLine(allOutboundTasks.Count); + var availableTasks = allOutboundTasks?.Where(t => t.TargetAddress != task.TargetAddress && t.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup).ToList(); + + Console.WriteLine("鍏朵粬鍑哄簱鍙d换鍔★細" + availableTasks?.Count); + + if (availableTasks == null || availableTasks.Count == 0) + { + return null; + } + + // 閬嶅巻鍙敤浠诲姟鍒楄〃锛屾鏌ヤ换鍔$珯鍙版槸鍚﹀厑璁告斁璐э紝鎵惧埌绗竴涓厑璁告斁璐х殑浠诲姟灏辫繑鍥� + foreach (var candidateTask in availableTasks) + { + var occupiedStation = OutTaskStationIsOccupied(candidateTask); + if (occupiedStation != null) + { + return candidateTask; + } + ConsoleHelper.WriteErrorLine($"浠诲姟鍙凤細銆恵candidateTask.TaskNum}銆戝嚭搴撳湴鍧�锛氥�恵candidateTask.NextAddress}銆戜笉鍏佽鏀捐揣"); + } + + return null; + } + + /// <summary> /// 浠诲姟瀹炰綋杞崲鎴愬懡浠odel /// </summary> /// <param name="task">浠诲姟瀹炰綋</param> @@ -207,7 +308,7 @@ string[] sourceCodes = task.SourceAddress.Split("-"); if (sourceCodes.Length == 3) { - stackerCraneTaskCommand.StartRow = (short)(Convert.ToInt16(sourceCodes[0]) % 2 == 0 ? 2:1); + stackerCraneTaskCommand.StartRow = (short)(Convert.ToInt16(sourceCodes[0]) % 2 == 0 ? 2 : 1); stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[1]); stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[2]); } @@ -220,27 +321,6 @@ string[] targetCodes = task.TargetAddress.Split("-"); if (targetCodes.Length == 3) { - //if (task.Roadway.Contains("FR")) - //{ - // if(task.Roadway.EndsWith("03")|| task.Roadway.EndsWith("01")) - // { - - // stackerCraneTaskCommand.EndRow = (short)(Convert.ToInt16(targetCodes[0]) % 2 == 0 ? 1 : 2); - // stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[1]); - // stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[2]); - // } - // else - // { - - // stackerCraneTaskCommand.EndRow = (short)(Convert.ToInt16(targetCodes[0]) % 2 == 0 ? 2 : 1); - // stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[1]); - // stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[2]); - // } - //} - //else - //{ - - //} stackerCraneTaskCommand.EndRow = (short)(Convert.ToInt16(targetCodes[0]) % 2 == 0 ? 2 : 1); stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[1]); stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[2]); @@ -254,38 +334,5 @@ return stackerCraneTaskCommand; } - - - /// <summary> - /// 璁惧蹇冭烦 - /// </summary> - /// <param name="commonStackerCrane"></param> - public async void EqptAlive(CommonStackerCrane commonStackerCrane) - { - if (commonStackerCrane.StackerOnline) - { - BasicDto dto = new BasicDto - { - EmployeeNo = "T00001", - EquipmentCode = commonStackerCrane.DeviceCode, - RequestTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"), - SessionId = Guid.NewGuid().ToString(), - Software = commonStackerCrane.DeviceName, - }; - var respone = await HttpHelper.PostAsync("http://ts-momapp01:12020/api/MachineIntegration/EqptAlive", dto.ToJsonString()); - if (respone != null) - { - var result = JsonConvert.DeserializeObject<ResponseEqptAliveDto>(respone); - if (result != null && result.Success) - { - if (result.KeyFlag == "99") - commonStackerCrane.StackerOnline = true; - else if (result.KeyFlag == "98") - commonStackerCrane.StackerOnline = false; - } - } - } - } - } } \ No newline at end of file -- Gitblit v1.9.3