using AutoMapper; using HslCommunication; using Newtonsoft.Json; using OfficeOpenXml.Drawing.Chart; using Quartz; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEAWCS_Common.ShuttleCarEnum; using WIDESEAWCS_Common.TaskEnum; using WIDESEAWCS_Core; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_DTO.WMSInfo; using WIDESEAWCS_IShuttleCar; using WIDESEAWCS_ITaskInfoService; using WIDESEAWCS_Model.Models; using WIDESEAWCS_QuartzJob; using WIDESEAWCS_QuartzJob.DTO; using WIDESEAWCS_QuartzJob.Service; using WIDESEAWCS_QuartzJob.StackerCrane.Enum; using WIDESEAWCS_TaskInfoService; using WIDESEAWCS_Tasks.ConveyorLineJob; using WIDESEAWCS_Tasks.ShuttleCarJob; using WIDESEAWCS_Tasks.StackerCraneJob; namespace WIDESEAWCS_Tasks { [DisallowConcurrentExecution] partial class CommonShuttleCarJob : JobBase, IJob { private readonly ITaskService _taskService; private readonly ITaskHtyService _taskHtyService; private readonly IShuttleCarService _shuttleCarService; private readonly ITaskExecuteDetailService _taskExecuteDetailService; private readonly IRouterService _routerService; private readonly IMapper _mapper; public CommonShuttleCarJob(ITaskService taskService, ITaskHtyService taskHtyService, IShuttleCarService shuttleCarService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper) { _taskService = taskService; _taskHtyService = taskHtyService; _shuttleCarService = shuttleCarService; _taskExecuteDetailService = taskExecuteDetailService; _routerService = routerService; _mapper = mapper; } public Task Execute(IJobExecutionContext context) { try { ShuttleCar shuttleCar = (ShuttleCar)context.JobDetail.JobDataMap.Get("JobParams"); if (shuttleCar != null) { ShuttleCarTaskCommandR command = ReadCustomer(shuttleCar, shuttleCar.DeviceCode);//读取穿梭车信息 if (command != null && command.JoinStatus == 1) { #region 穿梭车任务完成 if (command.TaskTypeComplete > (short)ShuttleCarTaskComplete.Standby) { var Status = _taskService.ShuttleCarTaskCompleted(command.number, command.TaskTypeComplete).Status; shuttleCar.SetValue(ShuttleCarDBName.ConfirmComplete, Status, shuttleCar.DeviceCode); } #endregion #region 创建移车充电任务 if (command.Err_Status == (short)ShuttleCarErr.LowBattery || command.ElectricQuantity == (short)ShuttleCarErr.LowBattery) { //生成堆垛机移车任务&穿梭车充电任务 var ShuttleCar = QueryCode(shuttleCar.DeviceCode);//充电终点待确认!!!!!!!!!!!!!!!!!!!!! _taskService.AddRelocationCarTask(ShuttleCar.ShuttleCarPosition, "", ShuttleCar.ShuttleCarCode, ShuttleCarTaskType.Charging.ToString()); } #endregion #region 获取堆垛机移车完成任务 Dt_Task dt_Task = GetTask((int)TaskCarStatusEnum.SC_CarFinish, (int)TaskOtherTypeEnum.RelocationCar); if (dt_Task != null) { var ShuttleCar = QueryCode(dt_Task.ShuttleCarCode); ShuttleCar.ShuttleCarPosition = dt_Task.TargetAddress; _shuttleCarService.UpdateData(ShuttleCar); if (string.IsNullOrEmpty(dt_Task.Remark))//完成移库任务 _taskService.AddTask_Hty(dt_Task, TaskOperateTypeEnum.AutoComplete.ToJson()); else//下发充电任务 SendTask(dt_Task); } #endregion #region 获取移车任务 dt_Task = GetTask((int)TaskCarStatusEnum.CarNew, (int)TaskOtherTypeEnum.RelocationCar);//获取新建移车任务 if (dt_Task != null) { #region 判断是否存在空闲穿梭车,如都在充电(选择电量高的并超过50%电量的穿梭车退出充电) #endregion if (string.IsNullOrEmpty(dt_Task.SourceAddress)) { if (command.Status == (short)ShuttleCarStatus.Standby && command.TaskTypeComplete == (short)ShuttleCarTaskComplete.Standby && command.Err_Status == (short)ShuttleCarErr.Normal) { dt_Task.SourceAddress = QueryCode(shuttleCar._deviceCode).ShuttleCarPosition; dt_Task.CurrentAddress = dt_Task.SourceAddress; dt_Task.ShuttleCarCode = shuttleCar._deviceCode; _taskService.UpdateData(dt_Task); } } else if (string.IsNullOrEmpty(dt_Task.TargetAddress)) { #region 向WMS申请 WebResponseContent content = new WebResponseContent(); var ResultData = HttpHelper.PostAsync(WMSInterfaceAddress.GetPosition + $"?position={dt_Task.SourceAddress}", "", headers: new Dictionary()); if (ResultData.Result != null) { content = JsonConvert.DeserializeObject(ResultData.Result); if (content != null && content.Status) { dt_Task.TargetAddress = content.Message; dt_Task.NextAddress = dt_Task.TargetAddress; _taskService.UpdateData(dt_Task); } } #endregion } //穿梭车移库任务下发 if (!string.IsNullOrEmpty(dt_Task.SourceAddress) && !string.IsNullOrEmpty(dt_Task.TargetAddress) && !string.IsNullOrEmpty(dt_Task.ShuttleCarCode)) { SendTask(dt_Task); } } #endregion #region 穿梭车出入库任务 else { var task = _taskService.QueryShuttleCarTask(shuttleCar.DeviceCode); if (task != null) { if (task.TaskType == (int)TaskOutboundTypeEnum.Outbound && !task.IsPickPlace) { var ShuttleCar = _shuttleCarService.QueryShuttleCar(task.SourceAddress);//出库任务判断是否存在穿梭车 if (ShuttleCar != null) SendTask(task); else _taskService.AddRelocationCarTask("", task.SourceAddress); } } } #endregion } } } catch (Exception ex) { Console.Out.WriteLine(nameof(CommonShuttleCarJob) + ":" + ex.ToString()); } finally { } return Task.CompletedTask; } private void SendTask(Dt_Task task) { ShuttleCar shuttleCar = Storage.Devices.FirstOrDefault(x => x.DeviceCode == task.ShuttleCarCode) as ShuttleCar; if (shuttleCar != null) { ShuttleCarTaskCommandR command = ReadCustomer(shuttleCar, shuttleCar.DeviceCode);//读取穿梭车信息 if (command != null && command.Status == (short)ShuttleCarStatus.Standby && command.TaskTypeComplete == (short)ShuttleCarTaskComplete.Standby && command.Err_Status == (short)ShuttleCarErr.Normal) { //任务转换 ShuttleCarTaskCommandW shuttleCarTaskCommand = new ShuttleCarTaskCommandW(); shuttleCarTaskCommand.Direction = command.Position; shuttleCarTaskCommand.TaskNum = task.TaskNum; if (task.TaskType == (int)TaskOtherTypeEnum.RelocationCar)//移车 { if (task.TaskState == (int)TaskCarStatusEnum.CarNew) { if (task.Remark == ShuttleCarTaskType.ExitCharge.ToString()) shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.Remark); else shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction); } else if (task.TaskState == (int)TaskCarStatusEnum.SC_CarFinish) { if (task.Remark == ShuttleCarTaskType.Charging.ToString()) shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.Remark); else shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction); } } else shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction); //shuttleCarTaskCommand.TaskType = string.IsNullOrEmpty(task.Remark) ? (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction) : (short)GetCarTaskType(task.Remark); //任务下发 if (shuttleCarTaskCommand.TaskType != null)//0 { if (shuttleCar.SendCommand(shuttleCarTaskCommand, shuttleCar.DeviceCode)) { if (!string.IsNullOrEmpty(task.Remark)) task.Remark = task.Remark + "已下发"; task.TaskState = GetTaskState(task.TaskType); _taskService.UpdateData(task); } } } } } ///下发退出充电任务并生成移库任务 //if (command.ElectricQuantity > 50) //{ //} } }