|  |  |  | 
|---|
|  |  |  | using WIDESEAWCS_TaskInfoService; | 
|---|
|  |  |  | using WIDESEAWCS_Tasks.ConveyorLineJob; | 
|---|
|  |  |  | using WIDESEAWCS_QuartzJob.Repository; | 
|---|
|  |  |  | using Autofac.Core; | 
|---|
|  |  |  | using WIDESEAWCS_DTO.TaskInfo; | 
|---|
|  |  |  | using AutoMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | namespace WIDESEAWCS_Tasks | 
|---|
|  |  |  | { | 
|---|
|  |  |  | [DisallowConcurrentExecution] | 
|---|
|  |  |  | public class StackerCraneJob_BC : JobBase, IJob | 
|---|
|  |  |  | { | 
|---|
|  |  |  | private readonly IMapper _mapper; | 
|---|
|  |  |  | private readonly ICacheService _cacheService; | 
|---|
|  |  |  | private readonly ITaskService _taskService; | 
|---|
|  |  |  | private readonly ITaskExecuteDetailService _taskExecuteDetailService; | 
|---|
|  |  |  | 
|---|
|  |  |  | private readonly IStationMangerRepository _stationMangerRepository; | 
|---|
|  |  |  | private readonly IRouterRepository _routerRepository; | 
|---|
|  |  |  | private List<Dt_ApiInfo> apiInfos; | 
|---|
|  |  |  | private readonly List<Dt_WarehouseDevice> warehouseDevices; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public StackerCraneJob_BC(ITaskService taskService, ICacheService cacheService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository) | 
|---|
|  |  |  | public StackerCraneJob_BC(IMapper mapper, ITaskService taskService, ICacheService cacheService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _mapper = mapper; | 
|---|
|  |  |  | _taskService = taskService; | 
|---|
|  |  |  | _taskExecuteDetailService = taskExecuteDetailService; | 
|---|
|  |  |  | _taskRepository = taskRepository; | 
|---|
|  |  |  | 
|---|
|  |  |  | apiInfos = infos; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | string? warehouseDevicesStr = _cacheService.Get<string>(nameof(Dt_WarehouseDevice)); | 
|---|
|  |  |  | if (!string.IsNullOrEmpty(warehouseDevicesStr)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | warehouseDevices = JsonConvert.DeserializeObject<List<Dt_WarehouseDevice>>(warehouseDevicesStr) ?? new List<Dt_WarehouseDevice>(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | warehouseDevices = new List<Dt_WarehouseDevice>(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Task Execute(IJobExecutionContext context) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | try | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //Console.WriteLine(DateTime.Now); | 
|---|
|  |  |  | CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams"); | 
|---|
|  |  |  | if (commonStackerCrane != null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | Dt_WarehouseDevice? warehouseDevice = warehouseDevices.FirstOrDefault(x => x.DeviceCode == commonStackerCrane.DeviceCode); | 
|---|
|  |  |  | if (warehouseDevice == null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | WriteError(commonStackerCrane.DeviceName, $"请é
ç½®ä»åºè®¾å¤ä¿¡æ¯"); | 
|---|
|  |  |  | return Task.CompletedTask; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!commonStackerCrane.IsEventSubscribed) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | commonStackerCrane.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler;//订é
ä»»å¡å®æäºä»¶ | 
|---|
|  |  |  | 
|---|
|  |  |  | commonStackerCrane.CheckStackerCraneTaskCompleted();//鲿¢ä»»å¡å®æäºä»¶çæµè¶
æ¶ï¼åæå¨è§¦å䏿¬¡ | 
|---|
|  |  |  | if (commonStackerCrane.StackerCraneAutoStatusValue == StackerCraneAutoStatus.Automatic && commonStackerCrane.StackerCraneStatusValue == StackerCraneStatus.Normal && commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | Thread.Sleep(1000); | 
|---|
|  |  |  | Dt_Task? task = GetTask(commonStackerCrane); | 
|---|
|  |  |  | if (task != null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | 
|---|
|  |  |  | task.Dispatchertime = DateTime.Now; | 
|---|
|  |  |  | task.ExceptionMessage = ""; | 
|---|
|  |  |  | _taskService.UpdateTask(task, TaskStatusEnum.SC_Executing); | 
|---|
|  |  |  | //å»¶æ¶1s | 
|---|
|  |  |  | Thread.Sleep(1000); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | /// <param name="e"></param> | 
|---|
|  |  |  | private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, WIDESEAWCS_QuartzJob.StackerCrane.StackerCraneTaskCompletedEventArgs e) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | CommonStackerCrane? commonStackerCrane = sender as CommonStackerCrane; | 
|---|
|  |  |  | if (commonStackerCrane != null) | 
|---|
|  |  |  | try | 
|---|
|  |  |  | { | 
|---|
|  |  |  | if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5) | 
|---|
|  |  |  | CommonStackerCrane? commonStackerCrane = sender as CommonStackerCrane; | 
|---|
|  |  |  | if (commonStackerCrane != null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | StackerCraneTaskCompleted(e.TaskNum, commonStackerCrane.DeviceCode); | 
|---|
|  |  |  | commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5); | 
|---|
|  |  |  | //if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | if (e.TaskNum != 0) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | WriteDebug(commonStackerCrane.DeviceCode, $"读åå°ä»»å¡å®æ,ä»»å¡å·:{e.TaskNum}"); | 
|---|
|  |  |  | StackerCraneTaskCompleted(e.TaskNum, commonStackerCrane.DeviceCode); | 
|---|
|  |  |  | commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | catch (Exception ex) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | WriteError(nameof(StackerCraneJob_BC), "ä»»å¡å®æå¤±è´¥", ex); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.StackerCraneStationCode == task.NextAddress); | 
|---|
|  |  |  | if (stationManger == null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _taskExecuteDetailService.AddTaskExecuteDetail(taskNum, $"è¾é线åºåºç«ç¹æªé
ç½®,{task.NextAddress}"); | 
|---|
|  |  |  | //_taskExecuteDetailService.AddTaskExecuteDetail(taskNum, $"è¾é线åºåºç«ç¹æªé
ç½®,{task.NextAddress}"); | 
|---|
|  |  |  | _taskService.UpdateTaskExceptionMessage(taskNum, $"è¾é线åºåºç«ç¹æªé
ç½®,{task.NextAddress}"); | 
|---|
|  |  |  | WriteError(deviceCode, $"è¾é线åºåºç«ç¹æªé
ç½®,{task.NextAddress}"); | 
|---|
|  |  |  | return WebResponseContent.Instance.Error($"è¾é线åºåºç«ç¹æªé
ç½®,{task.NextAddress}"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Dt_Router router = _routerRepository.QueryFirst(x => x.InOutType == task.TaskType && x.StartPosi == stationManger.StationCode); | 
|---|
|  |  |  | if (router == null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _taskExecuteDetailService.AddTaskExecuteDetail(taskNum, $"æªæ¾å°è·¯ç±ä¿¡æ¯,{task.NextAddress}"); | 
|---|
|  |  |  | _taskService.UpdateTaskExceptionMessage(taskNum, $"æªæ¾å°è·¯ç±ä¿¡æ¯,{task.NextAddress}"); | 
|---|
|  |  |  | return WebResponseContent.Instance.Error($"æªæ¾å°è·¯ç±ä¿¡æ¯,{task.NextAddress}"); | 
|---|
|  |  |  | router = _routerRepository.QueryFirst(x => x.ChildPosi == deviceCode && x.ChildPosiDeviceCode == stationManger.StationDeviceCode && x.NextPosi == stationManger.StationCode && x.InOutType == task.TaskType && x.IsEnd); | 
|---|
|  |  |  | if (router != null && router.IsEnd) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _taskService.TaskCompleted(taskNum); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _taskService.UpdateTaskExceptionMessage(taskNum, $"æªæ¾å°è·¯ç±ä¿¡æ¯,{task.NextAddress}"); | 
|---|
|  |  |  | WriteError(deviceCode, $"æªæ¾å°è·¯ç±ä¿¡æ¯,{task.NextAddress}"); | 
|---|
|  |  |  | return WebResponseContent.Instance.Error($"æªæ¾å°è·¯ç±ä¿¡æ¯,{task.NextAddress}"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | _taskService.UpdateTask(task, TaskStatusEnum.Line_Execute, deviceCode: stationManger.StationDeviceCode, currentAddress: stationManger.StationCode, nextAddress: router.NextPosi, targetAddress: router.NextPosi); | 
|---|
|  |  |  | if (task.TargetAddress.Contains("SCUTL-001")) //èªå¨ä¸çº¿ | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _taskService.UpdateTask(task, TaskStatusEnum.Line_Execute, deviceCode: stationManger.StationDeviceCode, currentAddress: stationManger.StationCode, nextAddress: router.NextPosi, targetAddress: router.NextPosi); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else if (task.TargetAddress.Contains("SCUTL-002")) //èªå¨äºçº¿ | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _taskService.UpdateTask(task, TaskStatusEnum.Line_Execute, deviceCode: stationManger.StationDeviceCode, currentAddress: stationManger.StationCode, nextAddress: "1103", targetAddress: "1103"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else //æå¨çº¿ | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _taskService.UpdateTask(task, TaskStatusEnum.Line_Execute, deviceCode: stationManger.StationDeviceCode, currentAddress: stationManger.StationCode, nextAddress: "1102", targetAddress: "1102"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup || task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | 
|---|
|  |  |  | /// <returns></returns> | 
|---|
|  |  |  | private Dt_Task? GetTask(CommonStackerCrane commonStackerCrane) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | try | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //Dt_Task oldTask = _taskRepository.QueryFirst(x => x.DeviceCode == commonStackerCrane.DeviceCode && x.TaskState == TaskStatusEnum.SC_Executing.ObjToInt()); | 
|---|
|  |  |  | //if (oldTask != null) | 
|---|
|  |  |  | //{ | 
|---|
|  |  |  | //    StackerCraneTaskCompleted(oldTask.TaskNum, commonStackerCrane.DeviceCode); | 
|---|
|  |  |  | //} | 
|---|
|  |  |  | } | 
|---|
|  |  |  | catch { } | 
|---|
|  |  |  | if (_taskRepository.QueryFirst(x => x.DeviceCode == commonStackerCrane.DeviceCode && x.TaskState==TaskStatusEnum.SC_Executing.ObjToInt())!=null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | WriteDebug(commonStackerCrane.DeviceCode, $"读åå°å½åä»»å¡å·:{commonStackerCrane.CurrentTaskNum}"); | 
|---|
|  |  |  | Dt_Task task; | 
|---|
|  |  |  | if (commonStackerCrane.LastTaskType == null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | 
|---|
|  |  |  | if (OutTaskStationIsOccupied(task) == null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | bool flag = false; | 
|---|
|  |  |  | List<string> otherOutStaionCodes = _routerService.QueryNextRoutes(commonStackerCrane.DeviceCode, task.NextAddress).Select(x => x.ChildPosi).ToList(); | 
|---|
|  |  |  | List<string> otherOutStaionCodes = _routerService.QueryNextRoutes(commonStackerCrane.DeviceCode, task.NextAddress,task.TaskType).Select(x => x.ChildPosi).ToList(); | 
|---|
|  |  |  | List<Dt_Task> tasks = _taskService.QueryStackerCraneOutTasks(commonStackerCrane.DeviceCode, otherOutStaionCodes); | 
|---|
|  |  |  | foreach (var item in tasks) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | string? url = apiInfos.FirstOrDefault(x => x.ApiCode == APIEnum.FeedBackWMSTaskCompleted.ToString())?.ApiAddress; | 
|---|
|  |  |  | string? url = apiInfos.FirstOrDefault(x => x.ApiCode == APIEnum.WMSIsReloaction.ToString())?.ApiAddress; | 
|---|
|  |  |  | if (string.IsNullOrEmpty(url)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskNum, $"æªæ¾å°WMSç§»åºå¤ææ¥å£"); | 
|---|
|  |  |  | 
|---|
|  |  |  | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"æªæ¾å°WMSç§»åºå¤ææ¥å£"); | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | HttpHelper.Post($"{url}?taskNum={task.TaskNum}&locationCode={task.NextAddress}", "");//todo è°ç¨WMSä»»å¡å®ææ¹æ³ | 
|---|
|  |  |  | string response = HttpHelper.Post($"{url}?taskNum={task.TaskNum}&locationCode={task.CurrentAddress}", "");//todo è°ç¨WMSä»»å¡å®ææ¹æ³ | 
|---|
|  |  |  | if (string.IsNullOrEmpty(response)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskNum, $"ç§»åºæ¥å£è°ç¨é误"); | 
|---|
|  |  |  | WriteError(commonStackerCrane.DeviceCode, $"ç§»åºæ¥å£è°ç¨é误"); | 
|---|
|  |  |  | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"ç§»åºæ¥å£è°ç¨é误"); | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | WebResponseContent? responseContent = JsonConvert.DeserializeObject<WebResponseContent>(response); | 
|---|
|  |  |  | if (responseContent == null || !responseContent.Status) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskNum, $"ç§»åºæ¥å£è°ç¨é误"); | 
|---|
|  |  |  | WriteError(commonStackerCrane.DeviceCode, $"ç§»åºæ¥å£è°ç¨é误"); | 
|---|
|  |  |  | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"ç§»åºæ¥å£è°ç¨é误"); | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | WMSTaskDTO? taskDTO =  JsonConvert.DeserializeObject<WMSTaskDTO>(responseContent.Data.Serialize()); | 
|---|
|  |  |  | if (taskDTO == null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskNum, $"ç§»åºæ¥å£è°ç¨é误"); | 
|---|
|  |  |  | WriteError(commonStackerCrane.DeviceCode, $"ç§»åºæ¥å£è°ç¨é误"); | 
|---|
|  |  |  | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"ç§»åºæ¥å£è°ç¨é误"); | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (task.TaskNum == taskDTO.TaskNum) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return task; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | Dt_Task reloTask = _mapper.Map<Dt_Task>(taskDTO); | 
|---|
|  |  |  | //å¤æç§»åºè´§ä½ä»»å¡æ¯å¦å·²åå¨ï¼å¦åå¨å
æ§è¡ | 
|---|
|  |  |  | Dt_Task existTask = _taskService.QueryStackerExistTask(reloTask.PalletCode, reloTask.SourceAddress); | 
|---|
|  |  |  | if (existTask != null && existTask.TaskState == (int)TaskStatusEnum.SC_Execute) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return existTask; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else if (existTask != null && existTask.TaskState != (int)TaskStatusEnum.SC_Execute) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | reloTask.TaskState = TaskStatusEnum.SC_Execute.ObjToInt(); | 
|---|
|  |  |  | reloTask.CurrentAddress = taskDTO.SourceAddress; | 
|---|
|  |  |  | reloTask.NextAddress = taskDTO.TargetAddress; | 
|---|
|  |  |  | reloTask.DeviceCode = task.DeviceCode; | 
|---|
|  |  |  | reloTask.TaskType = TaskTypeEnum.Relocation.ObjToInt(); | 
|---|
|  |  |  | int taskId = _taskRepository.AddData(reloTask); | 
|---|
|  |  |  | reloTask.TaskId = taskId; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return reloTask; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return task; | 
|---|
|  |  |  | 
|---|
|  |  |  | /// <returns>妿æªè¢«å ç¨ï¼è¿åä¼ å
¥çä»»å¡ä¿¡æ¯ï¼å¦åï¼è¿ånull</returns> | 
|---|
|  |  |  | private Dt_Task? OutTaskStationIsOccupied([NotNull] Dt_Task task) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | Dt_StationManger? stationManger = _stationMangerRepository.QueryFirst(x => x.StationCode == task.NextAddress && x.StackerCraneCode == task.DeviceCode); | 
|---|
|  |  |  | Dt_StationManger? stationManger = _stationMangerRepository.QueryFirst(x => (x.StationCode == task.NextAddress || x.StackerCraneStationCode == task.NextAddress) && x.StackerCraneCode == task.DeviceCode); | 
|---|
|  |  |  | if (stationManger != null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode); | 
|---|
|  |  |  | 
|---|
|  |  |  | stackerCraneTaskCommand.Barcode = task.PalletCode; | 
|---|
|  |  |  | stackerCraneTaskCommand.TaskNum = task.TaskNum; | 
|---|
|  |  |  | stackerCraneTaskCommand.WorkType = 1; | 
|---|
|  |  |  | stackerCraneTaskCommand.TrayType = (Int16)task.PalletType; | 
|---|
|  |  |  | stackerCraneTaskCommand.TrayType = (short)task.PalletType; | 
|---|
|  |  |  | if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)//夿æ¯å¦æ¯å
¥åºä»»å¡ | 
|---|
|  |  |  | { | 
|---|
|  |  |  | string[] startCodes = task.CurrentAddress.Split("-"); | 
|---|