using HslCommunication; using HslCommunication.WebSocket; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OfficeOpenXml.FormulaParsing.Excel.Functions.Information; using Quartz; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEA_Common.Log; using WIDESEAWCS_Common; using WIDESEAWCS_Common.APIEnum; using WIDESEAWCS_Common.TaskEnum; using WIDESEAWCS_Core; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_DTO.Agv; using WIDESEAWCS_DTO.TaskInfo; using WIDESEAWCS_IBasicInfoRepository; using WIDESEAWCS_ISystemServices; using WIDESEAWCS_ITaskInfoRepository; using WIDESEAWCS_ITaskInfoService; using WIDESEAWCS_Model.Models; using WIDESEAWCS_QuartzJob; namespace WIDESEAWCS_Tasks { [DisallowConcurrentExecution] public class CommonAGVJob : JobBase, IJob { private readonly ITaskRepository _taskRepository; private readonly ITaskService _taskService; private readonly ISys_ConfigService _sys_ConfigService; private readonly IDt_StationManagerRepository _stationManagerRepository; public CommonAGVJob(ITaskRepository taskRepository,ITaskService taskService,ISys_ConfigService configService,IDt_StationManagerRepository stationManagerRepository) { _taskRepository = taskRepository; _taskService = taskService; _sys_ConfigService = configService; _stationManagerRepository = stationManagerRepository; } public Task Execute(IJobExecutionContext context) { var newTasks = _taskService.Db.Queryable().Where(x => (x.TaskState == TaskAGVCarryStatusEnum.AGV_CarryNew.ObjToInt())).ToList().OrderBy(x => x.Grade).ThenBy(x => x.TaskNum).ToList(); #region 任务下发 if (newTasks.Count > 0) { WriteLog.Write_Log("AGV任务下发", "任务下发接口", "添加任务", $"任务:{newTasks.ToJson()}"); try { AgvTaskSendDTO agvTaskSend = new AgvTaskSendDTO() { MissionData = new List() }; string taskGroupId = Guid.NewGuid().ToString().Replace("-", ""); foreach (var task in newTasks) { //获取目标点货位 Dt_StationManager stationManagerStart = _stationManagerRepository.QueryFirst(x => x.stationLocation == task.CurrentAddress); //获取拣选出库站台 Dt_StationManager stationManagerEnd = _stationManagerRepository.QueryFirst(x => x.stationLocation == task.NextAddress); if (stationManagerStart == null || stationManagerStart == null) throw new Exception($"未找到任务号${task.TaskNum}起始点{task.CurrentAddress}或目标点{task.NextAddress}位置信息"); agvTaskSend.RequestId = taskGroupId; agvTaskSend.MissionCode = task.TaskNum.ToString(); agvTaskSend.ViewBoardType = "W01"; //料箱子搬运任务 MissionDataItem missionDataItem = new MissionDataItem() { Sequence = task.TaskNum, BinCode = task.PalletCode, StartPosition = stationManagerStart.stationLocation, EndPosition = stationManagerEnd.stationLocation, TakeActionConfirm = false, TakeActionInform = false, PutActionConfirm = true, PutActionInform = true, }; agvTaskSend.MissionData.Add(missionDataItem); } if (newTasks.OrderByDescending(x => x.Grade).FirstOrDefault()?.Grade == 0) { agvTaskSend.Priority = 99; } else { agvTaskSend.Priority = 99 - newTasks.OrderByDescending(x => x.Grade).FirstOrDefault().Grade; } //发送AGV任务 WebResponseContent content = _taskService.AgvSendTask(agvTaskSend, APIEnum.AgvSendTask); if (!content.Status) throw new Exception(content.Message); newTasks.ForEach(x => { x.Dispatchertime = DateTime.Now; x.TaskState = TaskAGVCarryStatusEnum.AGV_CarryExecuting.ObjToInt(); }); _taskService.UpdateData(newTasks); Thread.Sleep(500); } catch (Exception ex) { newTasks.ForEach(x => { x.TaskState = TaskAGVCarryStatusEnum.AGV_CarryException.ObjToInt(); x.ExceptionMessage = ex.Message; }); _taskService.UpdateData(newTasks); } } #endregion return Task.CompletedTask; } } }