1
HuBingJie
5 天以前 7ac6ad15e5af1024f4d21d33bb3af98ffce43874
´úÂë¹ÜÀí/WCS/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/SpeStackerCraneJob/SpeStackerCraneJob.cs
@@ -56,9 +56,6 @@
            _agvStationService = agvStationService;
        }
        public string ziche001 = "RGV104";
        public string ziche002 = "RGV107";
        public Task Execute(IJobExecutionContext context)
        {
@@ -110,280 +107,219 @@
            return Task.CompletedTask;
        }
        /// <summary>
        /// èŽ·å–ä»»åŠ¡ï¼ˆæš‚æ—¶æ–¹æ³•if判断)
        /// </summary>
        /// <param name="DeviceCode"></param>
        /// <param name="RGVCurrentlocation"></param>
        /// <returns></returns>
        private Dt_Task? GetTask(string DeviceCode)
        {
            Dt_Task task;
            task = _taskService.QueryStackerCraneTask(DeviceCode);
            if (task != null)
            Dt_Task task = _taskService.QueryStackerCraneTask(DeviceCode);
            if (task == null) return null;
            return task.TaskType switch
            {
                //判断入库任务
                if(task.TaskType== (int)TaskInboundTypeEnum.Inbound)
                {
                    if(task.TaskState== (int)TaskInStatusEnum.RGV_InNew)
                    {
                        return task;
                    }else if(task.TaskState== (int)TaskInStatusEnum.RGV_InPickupFinish) //取货完成后,进行放货的流程
                    {
                        //判断该HCJ是否可放货
                        bool Currebool = QueryCurrentAdder(task); //判断HCJ是否可以放货
                        if (Currebool)
                        {
                            return task;
                        }
                        else { return null; }
                    }else if (task.TaskState == (int)TaskInStatusEnum.RGV_InReleaseFinish) //放货完成后,进行子车取货流程
                    {
                        //默认子车在取货的地方
                        return task;
                    }
                    else if (task.TaskState == (int)TaskInStatusEnum.RGV_InZicheFinish) //子车取货完成后,进行放货流程
                    {
                        //获取需要放货的站台
                        AGVStation aGVStation = _agvStationService.GetAllStationByDeviceCode(int.Parse(task.CurrentAddress));   //获取到过道站台放货信息
                        if (aGVStation.Station_Area == 1)   //表示在外测
                        {
                            bool muchebool=MotherCarMoves(task, aGVStation);
                            if (muchebool)
                            {
                                return task;
                            }else { return null; }
                        }
                        else if(aGVStation.Station_Area == 2)   //表示在内侧
                        {
                            bool muchebool = MotherCarMovesinside(task, aGVStation);
                            if (muchebool)
                            {
                                return task;
                            }
                            else { return null; }
                        }else if (aGVStation.Station_Area == 3)     //放在过道HCJ
                        {
                            bool muchebool = HCJMotherCarMovesinside(task, aGVStation);
                            if (muchebool)
                            {
                                return task;
                            }
                            else { return null; }
                        }
                    }else if (task.TaskState == (int)TaskInStatusEnum.RGV_InZicheReleaseFinish) //子车放货完成
                    {
                        AGVStation aGVStation = _agvStationService.GetAllStationByDeviceCode(int.Parse(task.CurrentAddress));
                        if(aGVStation.Station_Area == 1)
                        {
                            return task;
                            /*RgvCraneTaskCommand standardCommands = ConvertMotherCarTaskCommand(task, "1021");
                            bool sendFlag = SendCommand2(standardCommands, ziche001);
                            if (sendFlag)
                            {
                                //记录下发过母车移动任务
                                _taskService.UpdateTaskStatusToNext(task);
                            }*/
                        }
                        else if (aGVStation.Station_Area == 2)
                        {
                            RgvCraneTaskCommand standardCommands = ConvertMotherCarTaskCommand(task, "1021");   //注意::::该子车需要待定去向
                            bool sendFlag = SendCommand2(standardCommands, ziche002);
                            if (sendFlag)
                            {
                                //记录下发过母车移动任务
                                _taskService.UpdateTaskStatusToNext(task);
                            }
                        }
                        //进行移动子车
                    }
                    else if(task.TaskState == (int)TaskInStatusEnum.RGV_InZichemoveFinish) //子车移动完成后,进行
                    {
                        //获取需要放货的站台
                        AGVStation aGVStation = _agvStationService.GetAllStationByDeviceCode(int.Parse(task.CurrentAddress));   //获取入库站台信息
                        if (aGVStation.Station_Area == 1)   //入库口区域的1表示写入固定的1号子车104
                        {
                            //判断子车在哪个位置
                            int zicheadder = GetZichelocation(task, ziche001);
                            if(zicheadder!= int.Parse(aGVStation.ZicheMotherinlaw))
                            {
                                return task;
                            }
                        }
                        else if(aGVStation.Station_Area == 2)      //入库口区域的1表示写入固定的1号子车107
                        {
                            int zicheadder = GetZichelocation(task, ziche002);
                            if (zicheadder != int.Parse(aGVStation.ZicheMotherinlaw))
                            {
                                return task;
                            }
                        }
                    }
                }
                else if(task.TaskType == (int)TaskOutboundTypeEnum.Outbound)
                {
                    if (task.TaskState == (int)TaskOutStatusEnum.OutNew)
                    {
                        return task;
                    }else if (task.TaskState == (int)TaskOutStatusEnum.RGV_OutMothercarFinish)
                    {
                        return task;
                    }
                    else if (task.TaskState == (int)TaskOutStatusEnum.RGV_OutZicheFinish)
                    {
                        return task;
                    }else if(task.TaskState == (int)TaskOutStatusEnum.RGV_OutZicheReleaseFinish)
                    {
                        return task;
                    }else if(task.TaskState == (int)TaskOutStatusEnum.RGV_OutPickupFinish && task.CurrentAddress != "")
                    {
                        return task;
                    }
                }
            }
            return null;
                (int)TaskInboundTypeEnum.Inbound => HandleInboundTask(task),
                (int)TaskOutboundTypeEnum.Outbound => HandleOutboundTask(task),
                _ => null
            };
        }
        //判断外侧母车位置
        private bool MotherCarMoves(Dt_Task _Task, AGVStation aGVStation)
        private Dt_Task? HandleInboundTask(Dt_Task task)
        {
            int mcadder = GetMothercarlocation(_Task, aGVStation.Motherinlaw);
            if(mcadder != 0)
            return task.TaskState switch
            {
                if (mcadder == int.Parse(_Task.CurrentAddress))       //判断子车要去的位置,母车是否在该位置
                {
                    return true;
                }
                else
                {
                    //下发母车移动出来接货任务
                    RgvCraneTaskCommand standardCommands = ConvertMotherCarTaskCommand(_Task, _Task.CurrentAddress);
                    bool sendFlag = SendCommand2(standardCommands, aGVStation.ChildPosiDeviceCode);
                    if (sendFlag)
                    {
                        //记录下发过母车移动任务
                        _taskService.UpdateTaskStatusToNext(_Task);
                    }
                }
            }
            return false;
                (int)TaskInStatusEnum.RGV_InNew => task,
                (int)TaskInStatusEnum.RGV_InPickupFinish => HandlePickupFinish(task),
                (int)TaskInStatusEnum.RGV_InReleaseFinish => task,  //需要判断子车是内侧区还是外侧取货(待完成)
                (int)TaskInStatusEnum.RGV_InZicheFinish => HandleZicheFinish(task),
                (int)TaskInStatusEnum.RGV_InZicheReleaseFinish => HandleZicheReleaseFinish(task),
                (int)TaskInStatusEnum.RGV_InZichemoveFinish => HandleZicheMoveFinish(task),
                _ => null
            };
        }
        //判断内侧母车位置
        private bool MotherCarMovesinside(Dt_Task _Task, AGVStation aGVStation)
        private Dt_Task? HandleOutboundTask(Dt_Task task)
        {
            //获取外侧母车位置
            int mcadder = GetMothercarlocation(_Task, aGVStation.MotherCarDeviceCode);
            //获取内侧母车位置
            int ncadder = GetMothercarlocation(_Task, _Task.CurrentAddress);    //传入过道的地址
            if (mcadder != int.Parse(aGVStation.MotherCarDeviceCode) && ncadder == aGVStation.Station_code)       //判断外侧母车位置
            // å‡ºåº“任务的状态判断相对简单,可以直接返回
            int[] validStates =
            {
                return true;
            }
            else
            {
                if (mcadder == int.Parse(aGVStation.MotherCarDeviceCode))    //外侧母车需要移动
                {
                    //下发母车往里面走的任务
                    AGVStation aGVSta = _agvStationService.GetMothercarCode(aGVStation.MotherCarDeviceCode);    //堆垛机入库口
                (int)TaskOutStatusEnum.OutNew,
                (int)TaskOutStatusEnum.RGV_OutMothercarFinish,
                (int)TaskOutStatusEnum.RGV_OutZicheFinish,
                (int)TaskOutStatusEnum.RGV_OutZicheReleaseFinish,
                (int)TaskOutStatusEnum.RGV_OutPickupFinish
            };
                    RgvCraneTaskCommand standardCommands = ConvertMotherCarTaskCommand(_Task, aGVSta.Station_code.ToString()); //下发母车移动至入库站台位
                    bool sendFlag = SendCommand2(standardCommands, aGVSta.ChildPosiDeviceCode);
                    if (sendFlag)
                    {
                        //记录下发过母车移动任务
                        _taskService.UpdateTaskStatusToNext(_Task);
                    }
                }
                if (ncadder != aGVStation.Station_code)    //下发母车
                {
                    RgvCraneTaskCommand standardCommands = ConvertMotherCarTaskCommand(_Task, aGVStation.Station_code.ToString());
                    bool sendFlag = SendCommand2(standardCommands, aGVStation.ChildPosiDeviceCode);
                    if (sendFlag)
                    {
                        //记录下发过母车移动任务
                        _taskService.UpdateTaskStatusToNext(_Task);
                    }
                }
            }
            return false;
        }
        private bool HCJMotherCarMovesinside(Dt_Task _Task, AGVStation aGVStation)  //传进来的站台为HCJ
        {
            //获取外侧母车位置
            int mcadder = GetMothercarlocation(_Task, aGVStation.MotherCarDeviceCode);
            //获取内侧母车位置
            int ncadder = GetMothercarlocation(_Task, aGVStation.MotherCardeputy);
            if (mcadder != int.Parse(aGVStation.MotherCarDeviceCode) && ncadder != int.Parse(aGVStation.MotherCardeputy))       //判断外侧母车位置
            {
                return true;
            }
            else
            {
                if (mcadder == int.Parse(aGVStation.MotherCarDeviceCode))    //外侧母车需要移动
                {
                    //下发母车往里面走的任务
                    AGVStation aGVSta = _agvStationService.GetMothercarCode(aGVStation.MotherCarDeviceCode);    //堆垛机入库口
                    RgvCraneTaskCommand standardCommands = ConvertMotherCarTaskCommand(_Task, aGVSta.Station_code.ToString()); //下发母车移动至入库站台位
                    bool sendFlag = SendCommand2(standardCommands, aGVSta.ChildPosiDeviceCode);
                    if (sendFlag)
                    {
                        //记录下发过母车移动任务
                        _taskService.UpdateTaskStatusToNext(_Task);
                    }
                }
                if (ncadder == int.Parse(aGVStation.MotherCardeputy))    //下发母车
                {
                    //下发母车往里面走的任务
                    AGVStation aGVSta = _agvStationService.GetMothercarCode(aGVStation.MotherCardeputy);    //堆垛机入库口
                    RgvCraneTaskCommand standardCommands = ConvertMotherCarTaskCommand(_Task, aGVSta.Station_code.ToString()); //下发母车移动至入库站台位
                    bool sendFlag = SendCommand2(standardCommands, aGVSta.ChildPosiDeviceCode);
                    if (sendFlag)
                    {
                        //记录下发过母车移动任务
                        _taskService.UpdateTaskStatusToNext(_Task);
                    }
                }
            }
            return false;
            return validStates.Contains(task.TaskState) ? task : null;
        }
        public bool QueryCurrentAdder(Dt_Task task)
        #region å…¥åº“方法
        private Dt_Task? HandlePickupFinish(Dt_Task task)
        {
            try
            {
                //获取需要区分是否去入库与去不合格载货台任务
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "HCJ2000");
                CommonStackerCrane Commonstacker = (CommonStackerCrane)device;
                DeviceProDTO? deviceProDTO = GetDeviceProDTO(Commonstacker, task.CurrentAddress, "HCJ_GoodsStatus");
                DeviceProDTO? deviceProDTO = GetDeviceProDTO(Commonstacker, task.NextAddress, "HCJ_GoodsStatus");
                int HCJGStatus = GetLine(Commonstacker, deviceProDTO.DeviceProAddress);
                if (HCJGStatus == 0)    //为无货
                {
                    return true;
                    return task;
                }
            }
            catch (Exception ex)
            {
                //记录异常情况
                _taskService.UpdateTaskExceptionMessage(task.TaskNum,$"判断HCJ站台是否有货信息出错,出错信息:{ex.Message}");
                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"判断HCJ站台是否有货信息出错,出错信息:{ex.Message}");
                throw;
            }
            return null;
        }
        private Dt_Task? HandleZicheFinish(Dt_Task task)
        {
            //获取放货地址
            AGVStation aGVStation = _agvStationService.GetAllStationByDeviceCode(int.Parse(task.NextAddress));
            //分3区域进行放货
            return aGVStation.Station_Area switch
            {
                1 => CheckMotherCarMove(task, aGVStation, MotherCarMoves),  //外侧放货
                2 => CheckMotherCarMove(task, aGVStation, MotherCarMovesinside),    //内侧放货
                3 => CheckMotherCarMove(task, aGVStation, HCJMotherCarMovesinside),     //交接点放货
                _ => null
            };
        }
        private Dt_Task? CheckMotherCarMove(Dt_Task task, AGVStation station, Func<Dt_Task, AGVStation, bool> moveFunc)
        {
            return moveFunc(task, station) ? task : null;
        }
        private Dt_Task? HandleZicheReleaseFinish(Dt_Task task)
        {
            AGVStation aGVStation = _agvStationService.GetAllStationByDeviceCode(int.Parse(task.CurrentAddress));   //当前地址,母车上,下一个地址堆垛机取货口
            if (aGVStation.Station_Area == 1 && aGVStation.Station_Area == 2)
            {
                return task;    //返回子车移动任务
            }
            else if (aGVStation.Station_Area == 3)  //当前地址为HCJ站台,且AGV任务为取货
            {
                return task;    //去取货,需要判断子车当前位子,(后续优化)
            }
            return null;
        }
        private Dt_Task? HandleZicheMoveFinish(Dt_Task task)
        {
            AGVStation aGVStation = _agvStationService.GetAllStationByDeviceCode(int.Parse(task.CurrentAddress));
            int zicheAddress = GetZichelocation(task, task.CurrentAddress);
            //如果子车在母车上,则需要移库母车(待优化)
            return zicheAddress != int.Parse(aGVStation.ZicheMotherinlaw) ? task : null;
        }
        #endregion
        //区域一判断母车是否到位,可立即放货
        private bool MotherCarMoves(Dt_Task _Task, AGVStation aGVStation)
        {
            int mcadder = GetMothercarlocation(_Task.TaskNum, aGVStation.Motherinlaw);      //获取到母车外侧位置
            if(mcadder != 0)
            {
                if (mcadder == int.Parse(_Task.CurrentAddress))
                {
                    return true;
                }
                 RGVMovetask(_Task.CurrentAddress, aGVStation.ChildPosiDeviceCode,_Task.TaskId);
            }
            return false;
        }
        /// <summary>
        /// å­æ¯è½¦ç§»åŠ¨ä»»åŠ¡
        /// </summary>
        /// <param name="RGVAdders">目标地址</param>
        /// <param name="ChildPosiDeviceCode">设备编号</param>
        /// <returns></returns>
        private bool RGVMovetask(string RGVAdders,string ChildPosiDeviceCode,int taskid)
        {
            RgvCraneTaskCommand standardCommands = ConvertMotherCarTaskCommand(RGVAdders);
            bool sendFlag = SendCommand2(standardCommands, ChildPosiDeviceCode);
            _taskExecuteDetailService.AddTaskExecuteDetail(taskid, $"系统自动流程,执行母车移动任务,母车编号:{ChildPosiDeviceCode},移动地址:{RGVAdders},是否下发成功:{sendFlag}");
            return sendFlag;
        }
        /// <summary>
        /// åˆ¤æ–­å†…侧母车位置
        /// </summary>
        /// <param name="_Task"></param>
        /// <param name="aGVStation"></param>
        /// <returns></returns>
        private bool MotherCarMovesinside(Dt_Task _Task, AGVStation aGVStation)
        {
            //获取外侧母车位置
            int mcadder = GetMothercarlocation(_Task.TaskNum, aGVStation.MotherCarDeviceCode);
            //获取内侧母车位置
            int ncadder = GetMothercarlocation(_Task.TaskNum, _Task.NextAddress);
            if (mcadder != int.Parse(aGVStation.MotherCarDeviceCode) && ncadder == aGVStation.Station_code)
            {
                return true;
            }
            else
            {
                if (mcadder == int.Parse(aGVStation.MotherCarDeviceCode))
                {
                    AGVStation aGVSta = _agvStationService.GetMothercarCode(aGVStation.MotherCarDeviceCode);
                    RGVMovetask(aGVSta.ChildPosiDeviceCode, aGVSta.Station_code.ToString(), _Task.TaskId);
                }
                if (ncadder != aGVStation.Station_code)
                {
                    RGVMovetask(aGVStation.ChildPosiDeviceCode, aGVStation.Station_code.ToString(), _Task.TaskId);
                }
            }
            return false;
        }
        private bool HCJMotherCarMovesinside(Dt_Task _Task, AGVStation aGVStation)
        {
            //获取外侧母车位置
            int mcadder = GetMothercarlocation(_Task.TaskNum, aGVStation.MotherCarDeviceCode);
            //获取内侧母车位置
            int ncadder = GetMothercarlocation(_Task.TaskNum, aGVStation.MotherCardeputy);
            if (mcadder != int.Parse(aGVStation.MotherCarDeviceCode) && ncadder != int.Parse(aGVStation.MotherCardeputy))
            {
                return true;
            }
            else
            {
                if (mcadder == int.Parse(aGVStation.MotherCarDeviceCode))
                {
                    AGVStation aGVSta = _agvStationService.GetMothercarCode(aGVStation.MotherCarDeviceCode);
                    RGVMovetask(aGVSta.ChildPosiDeviceCode, aGVSta.Station_code.ToString(), _Task.TaskId);
                }
                if (ncadder == int.Parse(aGVStation.MotherCardeputy))
                {
                    AGVStation aGVSta = _agvStationService.GetMothercarCode(aGVStation.MotherCardeputy);
                    RGVMovetask(aGVSta.ChildPosiDeviceCode, aGVSta.Station_code.ToString(), _Task.TaskId);
                }
            }
            return false;
        }
        //判断是否
        public bool GetMotherCarCurrentAdder(Dt_Task task)
@@ -417,12 +353,12 @@
        /// <param name="task"></param>
        /// <param name="MotherCarDeviceCode">过道地址</param>
        /// <returns></returns>
        public int GetMothercarlocation(Dt_Task task,string MotherCarDeviceCode)
        public int GetMothercarlocation(int TaskNum, string MotherCarDeviceCode)
        {
            try
            {
                //利用入库站台地址获取母车plc
                AGVStation aGVStation =_agvStationService.GetMothercarCode(MotherCarDeviceCode);
                AGVStation aGVStation =_agvStationService.GetMothercarCode(MotherCarDeviceCode);
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == aGVStation.ChildPosiDeviceCode);
                CommonStackerCrane Commonstacker = (CommonStackerCrane)device;
                DeviceProDTO? deviceProDTO = GetDeviceProDTO(Commonstacker, aGVStation.ChildPosiDeviceCode, "RGVCurrentlocation");
@@ -432,7 +368,7 @@
            catch (Exception ex)
            {
                //记录异常情况
                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"判断母车位置信息出错,出错信息:{ex.Message}");
                _taskService.UpdateTaskExceptionMessage(TaskNum, $"判断母车位置信息出错,出错信息:{ex.Message}");
                throw;
            }
        }
@@ -474,10 +410,19 @@
        public RgvCraneTaskCommand? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task)
        {
            RgvCraneTaskCommand stackerCraneTaskCommand = new RgvCraneTaskCommand();
            stackerCraneTaskCommand.RGV_RGVTasklocation = short.Parse(task.CurrentAddress);
            stackerCraneTaskCommand.RGV_RGVtasktype = 2;
            stackerCraneTaskCommand.RGV_Rgvtaskid = 1;          //rgv任务号
            stackerCraneTaskCommand.RGV_Lanjiantaskid = 0;      //兰剑任务id
            short locaticurr = task.RGVTaskType switch
            {
                _ when task.RGVTaskType == (short)RGVTaskTypeEnum.PickingUp ||
                       task.RGVTaskType == (short)RGVTaskTypeEnum.TravelingOnly
                    => short.Parse(task.CurrentAddress),
                _ when task.RGVTaskType == (short)RGVTaskTypeEnum.Placing
                    => short.Parse(task.NextAddress),
                _ => (short)0
            };
            stackerCraneTaskCommand.RGV_RGVTasklocation = locaticurr;
            stackerCraneTaskCommand.RGV_RGVtasktype = (short)task.RGVTaskType;
            stackerCraneTaskCommand.RGV_Rgvtaskid = (short)task.TaskNum;
            stackerCraneTaskCommand.RGV_Lanjiantaskid = (short)task.TaskNum;
            return stackerCraneTaskCommand;
        }
@@ -488,10 +433,10 @@
        /// <param name="task">任务实体</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public RgvCraneTaskCommand? ConvertMotherCarTaskCommand([NotNull] Dt_Task task,string CurrentAddress)
        public RgvCraneTaskCommand? ConvertMotherCarTaskCommand(string RGVAddress)
        {
            RgvCraneTaskCommand stackerCraneTaskCommand = new RgvCraneTaskCommand();
            stackerCraneTaskCommand.RGV_RGVTasklocation = short.Parse(CurrentAddress);
            stackerCraneTaskCommand.RGV_RGVTasklocation = short.Parse(RGVAddress);
            stackerCraneTaskCommand.RGV_RGVtasktype = 3;
            stackerCraneTaskCommand.RGV_Rgvtaskid = 999;          //rgv任务号
            stackerCraneTaskCommand.RGV_Lanjiantaskid = 999;      //兰剑任务id