using HslCommunication; 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_Model.Models; using WIDESEAWCS_QuartzJob.DTO; using WIDESEAWCS_QuartzJob; using WIDESEAWCS_Core; using Newtonsoft.Json; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_DTO.WMSInfo; using WIDESEAWCS_Tasks.ShuttleCarJob; using System.Reflection.Metadata; namespace WIDESEAWCS_Tasks { partial class CommonShuttleCarJob { private readonly object _lockObject = new object(); #region 堆垛机移车完成 /// /// 获取堆垛机完成穿梭车待执行任务 /// public void GetSC_CarFinish() { #region 移车任务 Dt_Task dt_Task = GetTask(TaskCarStatusEnum.SC_CarFinish.ObjToInt(), TaskOtherTypeEnum.RelocationCar.ObjToInt()); if (dt_Task != null) { UpdateShuttleCar(dt_Task.ShuttleCarCode, dt_Task.CurrentAddress); dt_Task.TaskState = TaskCarStatusEnum.RelocationCarFinish.ObjToInt(); _taskService.AddTask_Hty(dt_Task, TaskOperateTypeEnum.AutoComplete); } #endregion #region 移车充电任务 dt_Task = GetTask(CarChargingTaskEnum.CarChargingSC_CarFinish.ObjToInt(), TaskOtherTypeEnum.RelocationCarCharging.ObjToInt()); if (dt_Task != null) { UpdateShuttleCar(dt_Task.ShuttleCarCode, dt_Task.CurrentAddress); SendTask(dt_Task);//下发充电任务 } #endregion #region 移库任务 dt_Task = GetTask(TaskRelocationStatusEnum.SC_RelocationFinish.ObjToInt(), TaskRelocationTypeEnum.Relocation.ObjToInt()); if (dt_Task != null) SendTask(dt_Task); #endregion #region 入库任务 dt_Task = _taskRepository.QueryFirst(x => _taskService.TaskInboundTypes.Contains(x.TaskType) && x.TaskState == (int)TaskInStatusEnum.SC_InFinish); if (dt_Task != null) SendTask(dt_Task); #endregion } #endregion /// /// 修改穿梭车位置信号 /// /// 穿梭车号 /// 位置 public void UpdateShuttleCar(string ShuttleCarCode, string Address) { var ShuttleCar = QueryCode(ShuttleCarCode); ShuttleCar.ShuttleCarPosition = Address; ShuttleCar.ShuttleCarPosition1 = Address; _shuttleCarService.UpdateData(ShuttleCar); } public void GetShuttleCarTask(ShuttleCar shuttleCar, ShuttleCarTaskCommandR command) { #region 新建移车充电任务 Dt_Task CarCharging_Task = GetTask((int)CarChargingTaskEnum.CarChargingNew, (int)TaskOtherTypeEnum.RelocationCarCharging); if (CarCharging_Task != null) { Dt_Task? _Task = _taskRepository.QueryFirst(x => x.ShuttleCarCode == CarCharging_Task.ShuttleCarCode && x.TaskType != TaskOtherTypeEnum.RelocationCarCharging.ObjToInt()); if (_Task == null) SendTask(CarCharging_Task); else if (_taskService.TaskOutboundTypes.Contains(_Task.TaskType) && _Task.TaskState >= TaskOutStatusEnum.SC_OutFinish.ObjToInt()) SendTask(CarCharging_Task); } #endregion #region 新建移车任务起点为空 Dt_Task? RelocationCar_Task = null; lock (_lockObject) { RelocationCar_Task = GetTask((int)TaskCarStatusEnum.CarNew, (int)TaskOtherTypeEnum.RelocationCar); if (RelocationCar_Task != null) { if (string.IsNullOrEmpty(RelocationCar_Task.SourceAddress)) { (bool, Dt_ShuttleCar) result = GetSourceTask(shuttleCar, command); if (result.Item1) UpdateSource(ref RelocationCar_Task, result.Item2); else GetShuttleCar(ref RelocationCar_Task, shuttleCar);//获取另外一个穿梭车信息 } } Thread.Sleep(1000); } #endregion #region 堆垛机移库完成前任务/堆垛机出库完成前任务 //判断是否存在满足堆垛机入库的任务 if (_taskRepository.QueryFirst(x => _taskService.TaskInboundTypes.Contains(x.TaskType) && x.TaskState == (int)TaskInStatusEnum.Line_InFinish && !string.IsNullOrEmpty(x.NextAddress), _taskService.TaskOrderBy) == null) { Dt_Task dt_Task = _taskRepository.QueryFirst(x => _taskService.TaskRelocationTypes.Contains(x.TaskType) && x.TaskState < (int)TaskRelocationStatusEnum.SC_RelocationFinish, _taskService.TaskOrderBy) ?? _taskRepository.QueryFirst(x => _taskService.TaskOutboundTypes.Contains(x.TaskType) && x.TaskState < (int)TaskOutStatusEnum.SC_OutFinish, _taskService.TaskOrderBy); if (dt_Task != null) { if (dt_Task.TaskState == (int)TaskRelocationStatusEnum.RelocationNew || dt_Task.TaskState == (int)TaskOutStatusEnum.OutNew) { var ShuttleCar = _shuttleCarService.QueryShuttleCar(dt_Task.SourceAddress);//判断起点是否有穿梭车 if (ShuttleCar == null) { if (RelocationCar_Task != null && string.IsNullOrEmpty(RelocationCar_Task.TargetAddress)) { RelocationCar_Task.TargetAddress = dt_Task.SourceAddress; RelocationCar_Task.NextAddress = dt_Task.SourceAddress; _taskService.UpdateData(RelocationCar_Task); _taskExecuteDetailService.AddTaskExecuteDetail(RelocationCar_Task.TaskNum, "更新移车任务目标地址"); } else _taskService.AddRelocationCarTask("", dt_Task.SourceAddress); } else { dt_Task.ShuttleCarCode = ShuttleCar.ShuttleCarCode; SendTask(dt_Task); } } } } #endregion #region 新建移车任务终点为空 if (RelocationCar_Task != null) { if (string.IsNullOrEmpty(RelocationCar_Task.TargetAddress)) { #region 向WMS申请 var ShuttleCar = _shuttleCarService.QueryNoCode(RelocationCar_Task.ShuttleCarCode); WebResponseContent content = new WebResponseContent(); var ResultData = HttpHelper.PostAsync(WMSInterfaceAddress.GetPosition + $"?position={RelocationCar_Task.SourceAddress}&&position1={ShuttleCar.ShuttleCarPosition}", "", headers: new Dictionary()); if (ResultData.Result != null) { content = JsonConvert.DeserializeObject(ResultData.Result); if (content != null && content.Status) { RelocationCar_Task.TargetAddress = content.Message; RelocationCar_Task.NextAddress = RelocationCar_Task.TargetAddress; _taskService.UpdateData(RelocationCar_Task); _taskExecuteDetailService.AddTaskExecuteDetail(RelocationCar_Task.TaskNum, "更新移车任务目标地址"); } } #endregion } if (!string.IsNullOrEmpty(RelocationCar_Task.SourceAddress) && !string.IsNullOrEmpty(RelocationCar_Task.TargetAddress) && !string.IsNullOrEmpty(RelocationCar_Task.ShuttleCarCode)) { Dt_Task? _Task = _taskRepository.QueryFirst(x => x.ShuttleCarCode == RelocationCar_Task.ShuttleCarCode && x.TaskType != TaskOtherTypeEnum.RelocationCar.ObjToInt()); if (_Task == null) SendTask(RelocationCar_Task); else if (_taskService.TaskOutboundTypes.Contains(_Task.TaskType) && _Task.TaskState >= TaskOutStatusEnum.SC_OutFinish.ObjToInt()) SendTask(RelocationCar_Task); } } #endregion } private static readonly SemaphoreSlim _CarTaskObject = new SemaphoreSlim(1, 1); #region 获取移车任务 /// /// 获取移车任务/出入库任务 /// public async Task Get_CarTask(ShuttleCar shuttleCar, ShuttleCarTaskCommandR command) { await _CarTaskObject.WaitAsync(); try { #region 获取新建移车充电任务 Dt_Task dt_Task = GetTask((int)CarChargingTaskEnum.CarChargingNew, (int)TaskOtherTypeEnum.RelocationCarCharging); if (dt_Task != null && dt_Task.ShuttleCarCode == shuttleCar.DeviceCode) { if (!string.IsNullOrEmpty(dt_Task.SourceAddress) && !string.IsNullOrEmpty(dt_Task.TargetAddress) && !string.IsNullOrEmpty(dt_Task.ShuttleCarCode)) { Dt_Task? _Task = _taskRepository.QueryData(x => x.ShuttleCarCode == dt_Task.ShuttleCarCode && x.TaskType != TaskOtherTypeEnum.RelocationCarCharging.ObjToInt()).OrderByDescending(x => x.CreateDate).FirstOrDefault(); if (_Task != null && _taskService.TaskOutboundTypes.Contains(_Task.TaskType)) { if (_Task.TaskState >= TaskOutStatusEnum.SC_OutFinish.ObjToInt()) SendTask(dt_Task); } else SendTask(dt_Task); } } #endregion #region 获取新建移车任务 dt_Task = GetTask((int)TaskCarStatusEnum.CarNew, (int)TaskOtherTypeEnum.RelocationCar); if (dt_Task != null) { if (string.IsNullOrEmpty(dt_Task.SourceAddress)) { (bool, Dt_ShuttleCar) result = GetSourceTask(shuttleCar, command); if (result.Item1) UpdateSource(ref dt_Task, result.Item2); else GetShuttleCar(ref dt_Task, shuttleCar);//获取另外一个穿梭车信息 } else if (string.IsNullOrEmpty(dt_Task.TargetAddress)) { #region 向WMS申请 var ShuttleCar = _shuttleCarService.QueryNoCode(dt_Task.ShuttleCarCode); WebResponseContent content = new WebResponseContent(); var ResultData = HttpHelper.PostAsync(WMSInterfaceAddress.GetPosition + $"?position={dt_Task.SourceAddress}&&position1={ShuttleCar.ShuttleCarPosition}", "", 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); _taskExecuteDetailService.AddTaskExecuteDetail(dt_Task.TaskNum, "更新移车任务目标地址"); } } #endregion } //穿梭车移车任务下发 if (!string.IsNullOrEmpty(dt_Task.SourceAddress) && !string.IsNullOrEmpty(dt_Task.TargetAddress) && !string.IsNullOrEmpty(dt_Task.ShuttleCarCode) && dt_Task.ShuttleCarCode == shuttleCar.DeviceCode) { Dt_Task? _Task = _taskRepository.QueryData(x => x.ShuttleCarCode == dt_Task.ShuttleCarCode && x.TaskType != TaskOtherTypeEnum.RelocationCar.ObjToInt()).OrderByDescending(x => x.CreateDate).FirstOrDefault(); if (_Task != null && _taskService.TaskOutboundTypes.Contains(_Task.TaskType)) { if (_Task.TaskState >= TaskOutStatusEnum.SC_OutFinish.ObjToInt()) SendTask(dt_Task); } else SendTask(dt_Task); } } #endregion #region 穿梭车任务 var task = _taskService.ShuttleCarRelocationTask(shuttleCar.DeviceCode);//获取穿梭车移库任务 if (task != null) { if (!task.SourceIsPickPlace && task.ShuttleCarCode == shuttleCar.DeviceCode) { SendTask(task); } } else { task = _taskService.QueryShuttleCarTask(shuttleCar.DeviceCode); if (task != null) { if (_taskService.TaskOutboundTypes.Contains(task.TaskType)) { if (!task.SourceIsPickPlace && task.TaskState == (int)TaskOutStatusEnum.OutNew) { var ShuttleCar = _shuttleCarService.QueryShuttleCar(task.SourceAddress);//出库任务判断起点是否有穿梭车 if (ShuttleCar == null) _taskService.AddRelocationCarTask("", task.SourceAddress); else { task.ShuttleCarCode = ShuttleCar.ShuttleCarCode; SendTask(task); } } } else SendTask(task); } } #endregion } catch (Exception) { throw; } finally { _CarTaskObject.Release(); } } #endregion #region 更新移车任务起始地址 /// /// 更新移车任务起始地址 /// /// /// /// public void UpdateSource(ref Dt_Task dt_Task, Dt_ShuttleCar _ShuttleCar) { dt_Task.SourceAddress = _ShuttleCar.ShuttleCarPosition; dt_Task.CurrentAddress = dt_Task.SourceAddress; dt_Task.ShuttleCarCode = _ShuttleCar.ShuttleCarCode; _taskService.UpdateData(dt_Task); _taskExecuteDetailService.AddTaskExecuteDetail(dt_Task.TaskNum, "更新移车任务起始地址"); } #endregion /// /// 判断当前穿梭车是否可以执行移库 /// /// /// /// public (bool, Dt_ShuttleCar) GetSourceTask(ShuttleCar shuttleCar, ShuttleCarTaskCommandR command) { Dt_ShuttleCar _ShuttleCar = QueryCode(shuttleCar.DeviceCode); bool ok = false; if (command.Status == (short)ShuttleCarStatus.Standby && command.TaskTypeComplete == (short)ShuttleCarTaskComplete.Standby && command.Err_Status == (short)ShuttleCarErr.Normal) { if (_taskRepository.QueryFirst(x => x.SourceAddress == _ShuttleCar.ShuttleCarPosition && _taskService.TaskRelocationTypes.Contains(x.TaskType) && x.TaskState == TaskRelocationStatusEnum.SC_RelocationExecuting.ObjToInt()) != null) return (ok, _ShuttleCar); Dt_Task? _Task = _taskRepository.QueryFirst(x => x.ShuttleCarCode == _ShuttleCar.ShuttleCarCode); if (_Task != null) ok = _taskService.TaskOutboundTypes.Contains(_Task.TaskType) && _Task.TaskState >= TaskOutStatusEnum.SC_OutFinish.ObjToInt(); ok = _Task == null; #region 判断当前穿梭车是否存在任务 //Dt_Task? _Task = _taskRepository.QueryFirst(x => x.SourceAddress == _ShuttleCar.ShuttleCarPosition); //if (_Task != null) //{ // if (_Task.TaskState <= TaskOutStatusEnum.Car_OutFinish.ObjToInt() || // _Task.TaskState <= TaskRelocationStatusEnum.CarSource_Finish.ObjToInt() || // _Task.TaskState <= TaskCarStatusEnum.ShuttleCar_Finish.ObjToInt() || // _Task.TaskState <= CarChargingTaskEnum.CarCharging_Finish.ObjToInt()) // { // //_Task.ShuttleCarCode = shuttleCar.DeviceCode; // //SendTask(_Task); // return (ok, _ShuttleCar); // } //} //_Task = _taskRepository.QueryFirst(x => x.TargetAddress == _ShuttleCar.ShuttleCarPosition); //if (_Task != null) //{ // if (_Task.TaskState >= TaskInStatusEnum.Line_InFinish.ObjToInt() || // _Task.TaskState >= TaskRelocationStatusEnum.CarSource_Finish.ObjToInt() || // _Task.TaskState >= CarChargingTaskEnum.CarCharging_Finish.ObjToInt()) // { // //_Task.ShuttleCarCode = shuttleCar.DeviceCode; // //SendTask(_Task); // return (ok, _ShuttleCar); // } //} //_Task = _taskRepository.QueryData(x => x.SourceAddress == _ShuttleCar.ShuttleCarPosition && //x.TaskState < TaskRelocationStatusEnum.CarSource_Finish.ObjToInt() && //x.TaskType == TaskRelocationTypeEnum.Relocation.ObjToInt()).OrderByDescending(x => x.CreateDate).FirstOrDefault(); //if (_Task != null) return (ok, _ShuttleCar); //_Task = _taskRepository.QueryData(x => x.ShuttleCarCode == _ShuttleCar.ShuttleCarCode && _taskService.TaskOutboundTypes.Contains(x.TaskType)).OrderByDescending(x => x.CreateDate).FirstOrDefault(); //if (_Task != null) //{ // if (_Task.TaskState >= TaskOutStatusEnum.SC_OutFinish.ObjToInt()) // _Task = null; //} //ok = _Task == null; #endregion } if (!ok) { if (command.Status == (short)ShuttleCarStatus.Charging && command.ElectricQuantity >= 50)//退出充电 { ok = shuttleCar.SetValue(ShuttleCarDBName.TaskType, (short)ShuttleCarStatus.QuitCharging); } } return (ok, _ShuttleCar); } /// /// 获取另外一个穿梭车信息 /// public void GetShuttleCar(ref Dt_Task dt_Task, ShuttleCar shuttleCar) { Dt_ShuttleCar _ShuttleCar = _shuttleCarService.QueryNoCode(shuttleCar.DeviceCode); ShuttleCar shuttleCar1 = Storage.Devices.Where(x => x.DeviceCode.Equals(_ShuttleCar.ShuttleCarCode)).FirstOrDefault() as ShuttleCar; ShuttleCarTaskCommandR command = ReadCustomer(shuttleCar1, shuttleCar1.DeviceCode);//读取穿梭车信息 (bool, Dt_ShuttleCar) result = GetSourceTask(shuttleCar1, command); if (result.Item1) UpdateSource(ref dt_Task, result.Item2); } #region 下发穿梭车任务 /// /// 下发穿梭车任务 /// /// 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 || command.Err_Status == (short)ShuttleCarErr.LowBattery)) { #region 判断小车位置无AB面不下发任务 Dt_ShuttleCar _ShuttleCar = QueryCode(task.ShuttleCarCode); var Row = Convert.ToInt32(_ShuttleCar.ShuttleCarPosition.Split("-")[0]); short Position = Convert.ToInt16(Row); #endregion //任务转换 shuttleCar.SetValue(ShuttleCarDBName.Direction, Position);//写入车体方向 ShuttleCarTaskCommandW shuttleCarTaskCommand = new ShuttleCarTaskCommandW(); shuttleCarTaskCommand.Direction = Position; shuttleCarTaskCommand.TaskNum = task.TaskNum; if (task.TaskType == (int)TaskOtherTypeEnum.RelocationCar)//移车 { if (task.TaskState == (int)TaskCarStatusEnum.CarNew) { 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 if (task.TaskType == (int)TaskOtherTypeEnum.RelocationCarCharging) { shuttleCarTaskCommand.TaskType = task.TaskState == CarChargingTaskEnum.CarChargingNew.ObjToInt() ? (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction) : (short)GetCarTaskType(ShuttleCarTaskType.Charging.ToString()); } else if (task.TaskType == (int)TaskRelocationTypeEnum.Relocation) { shuttleCarTaskCommand.TaskType = task.TaskState == TaskRelocationStatusEnum.RelocationNew.ObjToInt() ? (short)ShuttleCarTaskType.Out : (short)ShuttleCarTaskType.In; } else shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction); //任务下发 if (shuttleCarTaskCommand.TaskType != null)//0 { if (shuttleCar.SendCommand(shuttleCarTaskCommand, shuttleCar.DeviceCode)) { if (!string.IsNullOrEmpty(task.Remark) && task.TaskState == CarChargingTaskEnum.CarChargingNew.ObjToInt()) task.Remark = task.Remark + "已下发"; task.TaskState = GetTaskState(task.TaskType, task.TaskState); _taskService.UpdateData(task); } } } } } #endregion /// /// 根据任务状态、类型获取任务 /// /// /// /// private Dt_Task GetTask(int TaskState, int TaskType) { return _taskService.GetTaskState(TaskState, TaskType); } /// /// 根据穿梭车编号获取穿梭车信息 /// /// /// public Dt_ShuttleCar QueryCode(string ShuttleCarCode) { return _shuttleCarService.QueryCode(ShuttleCarCode); } ShuttleCarTaskType GetCarTaskType(string remark) => remark switch { "Charging" => ShuttleCarTaskType.Charging, "ExitCharge" => ShuttleCarTaskType.ExitCharge, }; ShuttleCarTaskType GetCarTaskType(int TaskType, short Direction) { ShuttleCarTaskType taskType = new ShuttleCarTaskType(); switch (TaskType) { case (int)TaskInboundTypeEnum.Inbound: case (int)TaskInboundTypeEnum.PalletInbound: taskType = ShuttleCarTaskType.In; break; case (int)TaskOutboundTypeEnum.Outbound: case (int)TaskOutboundTypeEnum.PalletOutbound: taskType = ShuttleCarTaskType.Out; break; case (int)TaskOtherTypeEnum.RelocationCar: case (int)TaskOtherTypeEnum.RelocationCarCharging: taskType = Direction == 1 ? ShuttleCarTaskType.ZeroA : ShuttleCarTaskType.ZeroB; break; default: break; } return taskType; } /// /// 获取任务状态 /// /// /// int GetTaskState(int TaskType, int state) { switch (TaskType) { case (int)TaskInboundTypeEnum.Inbound: case (int)TaskInboundTypeEnum.PalletInbound: state = (int)TaskInStatusEnum.Car_InExecuting; break; case (int)TaskOutboundTypeEnum.Outbound: case (int)TaskOutboundTypeEnum.PalletOutbound: state = (int)TaskOutStatusEnum.Car_OutExecuting; break; case (int)TaskRelocationTypeEnum.Relocation: state = state < (int)TaskRelocationStatusEnum.SC_RelocationFinish.ObjToInt() ? (int)TaskRelocationStatusEnum.CarSource_Executing : (int)TaskRelocationStatusEnum.CarTarget_Executing; break; case (int)TaskOtherTypeEnum.RelocationCar: state = (int)TaskCarStatusEnum.ShuttleCar_Executing; break; case (int)TaskOtherTypeEnum.RelocationCarCharging: state = state < (int)CarChargingTaskEnum.CarChargingSC_CarFinish ? (int)CarChargingTaskEnum.CarCharging_Executing : (int)CarChargingTaskEnum.CarCharging_Charging; break; default: break; } return state; } /// /// 读取穿梭车信息 /// /// /// /// /// /// public T ReadCustomer(ShuttleCar shuttleCar, string deviceChildCode) where T : IDataTransfer, new() { string deviceChildCode2 = deviceChildCode; if (!shuttleCar.IsConnected) { throw new Exception("通讯连接错误,请检查网络"); } DeviceProDTO? deviceProDTO = (from x in shuttleCar.DeviceProDTOs where x.DeviceProParamType == "ReadDeviceCommand" && x.DeviceChildCode == deviceChildCode2 orderby x.DeviceProOffset select x).FirstOrDefault(); if (deviceProDTO == null) { throw new Exception("未找到协议信息"); } return shuttleCar.Communicator.ReadCustomer(deviceProDTO.DeviceProAddress); } } }