GoFullShelfStationJob.cs 17.3 KB

using AGVControl;
using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AGVControl
{
    /// <summary>
    /// 检查AGV是否有负载,去A6入料口并装车
    /// </summary>
    public class GoFullShelfStationJob : Job
    {
        /// <summary>
        /// 去A6入料口任务并检查AGV是否有负载
        /// </summary>
        /// <param name="agvCurPlace">接到任务时,AGV的位置(空表示待机位)</param>
        /// <param name="palce"></param>
        public GoFullShelfStationJob(string agvCurPlace = "", string palce = "A6")
        {
            FullShelfStationPlace = palce;
            agvPlace = agvCurPlace;
        }

        /// <summary>
        /// 出满料位置点
        /// </summary>
        public string FullShelfStationPlace { get; set; }

        /// <summary>
        /// 接到任务时,AGV的位置
        /// </summary>
        public string agvPlace { get; set; }
        private string runInfo = "";
        /// <summary>
        /// 运行信息
        /// </summary>
        public override string RunInfo
        {
            get { return runInfo; }
        }

        /// <summary>
        /// AGV上是否有负载
        /// </summary>
        public bool IsLoadOnAGV { get; private set; } = false;

        public AGVManager.BoxDestInfo FullShelfDestInfo = null;

        private JobStep<GO_FULL_SHELF_STATION_STEP> GoFullShelfStationStep = new JobStep<GO_FULL_SHELF_STATION_STEP>(GO_FULL_SHELF_STATION_STEP.NONE);

        public override Job Execute(Agv_Info agv)
        {
            string msg = agv.Name + " ";
            bool rtn = false;
            agv.Msg = "去云仓接满料任务:"+runInfo;
            //runInfo = "去云仓接满料任务:";
            //if (!CurTaskID.Equals(-1) && Common.mir.Get_Task_State(CurTaskID, out string st))
            //    CurTaskState = st;
            if (GoFullShelfStationStep.IsStep(GO_FULL_SHELF_STATION_STEP.NONE))
            {
                GoFullShelfStationStep.ToNextStep(GO_FULL_SHELF_STATION_STEP.WAIT_CHECK_RESULT);
                runInfo = "检查AGV负载情况";
                msg += runInfo;
                GoFullShelfStationStep.Msg = msg;
                Common.CheckLoad(agv);
                UpdateJobTaskInfo(agv.CurTaskName, agv.CurTaskID);
            }
            else if (GoFullShelfStationStep.IsStep(GO_FULL_SHELF_STATION_STEP.WAIT_CHECK_RESULT))
            {
                //Common.log.Debug("WAIT_CHECK_RESULT " + Common.agvMission["CheckShelf"].Equals(agv.CurTaskGUID).ToString() + " " + agv.CurTaskGUID + " " + Common.agvMission["CheckShelf"]);
                CurTaskState = Common.GetTakJobState(CurTaskID);
                if (Common.agvMission["CheckShelf"].Equals(agv.CurTaskGUID) && agv.CurTaskState.Equals(SettingString.Done))
                {
                    rtn = Common.mir.Get_IO_Status(agv, out bool[] input, out bool[] output);
                    if (rtn)
                    {
                        Common.log.Debug("WAIT_CHECK_RESULT: " + agv.Name + " 获取IO状态成功:满载信号=" + input[3].ToString());
                        if (input != null && input[3])
                        {
                            GoFullShelfStationStep.ToNextStep(GO_FULL_SHELF_STATION_STEP.END);
                            runInfo = "车上有料架,无法去入料口出料";
                            msg += runInfo;
                            GoFullShelfStationStep.Msg = msg;
                            agv.IsExistShelf = true;
                            IsLoadOnAGV = true;
                        }
                        else if (input != null && !input[3])
                        {
                            if (agvPlace.Equals("") || agvPlace.Equals(SettingString.Standby) || agvPlace.StartsWith(SettingString.AutoCharge))//待机位/充电位接到任务
                            {
                                GoFullShelfStationStep.ToNextStep(GO_FULL_SHELF_STATION_STEP.WAIT_AGV_REACH_A6);
                                runInfo = "无负载,准备运动到入料口" + FullShelfStationPlace;
                                msg += runInfo;
                                GoFullShelfStationStep.Msg = msg;
                                Common.MoveToNode(agv, FullShelfStationPlace);
                                UpdateJobTaskInfo(agv.CurTaskName, agv.CurTaskID);
                            }
                            else if (Common.CheckIsInAirDoor(agvPlace))
                            {
                                GoFullShelfStationStep.ToNextStep(GO_FULL_SHELF_STATION_STEP.WAIT_AGV_REACH_AIR_DOOR);
                                runInfo = "在4C风淋门内,先过风淋门";
                                msg += runInfo;
                                GoFullShelfStationStep.Msg = msg;
                                Common.DoorMission(agv, SettingString.DoorAirOut);
                                UpdateJobTaskInfo(agv.CurTaskName, agv.CurTaskID);
                            }
                            else if (agvPlace.StartsWith(SettingString.C4_Name_Prefix))//4C车间
                            {
                                GoFullShelfStationStep.ToNextStep(GO_FULL_SHELF_STATION_STEP.WAIT_AGV_REACH_DOOR);
                                runInfo = "在4C车间,向4D门运行,再到双层线入料口";
                                msg += runInfo;
                                GoFullShelfStationStep.Msg = msg;
                                Common.DoorMission(agv, SettingString.DoorCToD);
                                UpdateJobTaskInfo(agv.CurTaskName, agv.CurTaskID);
                            }
                            else
                            {
                                GoFullShelfStationStep.ToNextStep(GO_FULL_SHELF_STATION_STEP.WAIT_AGV_REACH_A6);
                                runInfo = "无负载,准备运动到入料口" + FullShelfStationPlace;
                                msg += runInfo;
                                GoFullShelfStationStep.Msg = msg;
                                Common.MoveToNode(agv, FullShelfStationPlace);
                                UpdateJobTaskInfo(agv.CurTaskName, agv.CurTaskID);
                            }
                        }
                    }
                    else
                    {
                        Common.LogInfo("WAIT_CHECK_RESULT: 获取IO状态失败,使用小车IO信号");
                        if (!agv.IsExistShelf)
                        {
                            if (agvPlace.Equals(""))//待机位接到任务
                            {
                                GoFullShelfStationStep.ToNextStep(GO_FULL_SHELF_STATION_STEP.WAIT_AGV_REACH_A6);
                                runInfo = "无负载,准备运动到入料口" + FullShelfStationPlace;
                                msg = runInfo;
                                GoFullShelfStationStep.Msg = msg;
                                Common.MoveToNode(agv, FullShelfStationPlace);
                                UpdateJobTaskInfo(agv.CurTaskName, agv.CurTaskID);
                            }
                            else if(Common.CheckIsInAirDoor(agvPlace))
                            {
                                GoFullShelfStationStep.ToNextStep(GO_FULL_SHELF_STATION_STEP.WAIT_AGV_REACH_AIR_DOOR);
                                runInfo = "在4C风淋门内,先过风淋门";
                                msg += runInfo;
                                GoFullShelfStationStep.Msg = msg;
                                Common.DoorMission(agv, SettingString.DoorAirOut);
                                UpdateJobTaskInfo(agv.CurTaskName, agv.CurTaskID);
                            }
                            else if (agvPlace.StartsWith(SettingString.C4_Name_Prefix))//4C车间
                            {
                                GoFullShelfStationStep.ToNextStep(GO_FULL_SHELF_STATION_STEP.WAIT_AGV_REACH_DOOR);
                                runInfo = "在4C车间,向4D门运行,再到双层线入料口";
                                msg += runInfo;
                                GoFullShelfStationStep.Msg = msg;
                                Common.DoorMission(agv, SettingString.DoorCToD);
                                UpdateJobTaskInfo(agv.CurTaskName, agv.CurTaskID);
                            }
                        }

                    }
                }

            }
            else if (GoFullShelfStationStep.IsStep(GO_FULL_SHELF_STATION_STEP.WAIT_AGV_REACH_AIR_DOOR))
            {
                CurTaskState = Common.GetTakJobState(CurTaskID);
                if (Common.CheckTaskFinished(agv, SettingString.DoorAirOut, CurTaskState))
                {
                    GoFullShelfStationStep.ToNextStep(GO_FULL_SHELF_STATION_STEP.WAIT_AGV_REACH_DOOR);
                    runInfo = "过风淋门,向4D门运行,再到双层线入料口" + FullShelfStationPlace;
                    msg += runInfo;
                    GoFullShelfStationStep.Msg = msg;
                    Common.MoveToNode(agv, SettingString.DoorCToD);
                    UpdateJobTaskInfo(agv.CurTaskName, agv.CurTaskID);
                }

            }
            else if (GoFullShelfStationStep.IsStep(GO_FULL_SHELF_STATION_STEP.WAIT_AGV_REACH_DOOR))
            {
                CurTaskState = Common.GetTakJobState(CurTaskID);
                if (Common.CheckTaskFinished(agv, SettingString.DoorCToD, CurTaskState))
                {
                    GoFullShelfStationStep.ToNextStep(GO_FULL_SHELF_STATION_STEP.WAIT_AGV_REACH_A6);
                    runInfo = "到达4D门,准备运动到双层线入料口" + FullShelfStationPlace;
                    msg += runInfo;
                    GoFullShelfStationStep.Msg = msg;
                    Common.MoveToNode(agv, FullShelfStationPlace);
                    UpdateJobTaskInfo(agv.CurTaskName, agv.CurTaskID);
                }

            }
            else if (GoFullShelfStationStep.IsStep(GO_FULL_SHELF_STATION_STEP.WAIT_AGV_REACH_A6))
            {
                CurTaskState = Common.GetTakJobState(CurTaskID);
                if (Common.CheckTaskFinished(agv, FullShelfStationPlace, CurTaskState))
                {
                    ClientNode node = Common.nodeInfo.Find(s => s.Name.Equals(SettingString.A6)
                    && (s.StateEquals(eNodeStatus.NeedLeave) || s.StateEquals(eNodeStatus.NeedEnterLeave)) && !s.RFID.Equals("00"));
                    if (node != null && node.Name.Equals(SettingString.A6))
                    {
                        if (AGVManager.FindFullShelfTarget(node.RFID, out FullShelfDestInfo))
                        {                           
                            GoFullShelfStationStep.ToNextStep(GO_FULL_SHELF_STATION_STEP.WAIT_DOUBLE_LINE_RESPONSE);
                            if (FullShelfDestInfo!=null)
                            {
                                Common.GetLineNameByNodeName(FullShelfDestInfo.location,out string line);
                                agv.BoxDestInfo = FullShelfDestInfo.ShowInfo(line);
                            }
                            runInfo = "AGV到达 " + FullShelfStationPlace + ",并发送出料架请求[ReadyLeave]";
                            msg += runInfo;
                            GoFullShelfStationStep.Msg = msg;
                            Common.server.ReadyLeave(FullShelfStationPlace);
                        }
                        else
                        {
                            if (FullShelfDestInfo !=null)
                            {
                                Common.GetLineNameByNodeName(FullShelfDestInfo.location, out string line);
                                runInfo = "AGV到达 " + FullShelfStationPlace + ",查询满料架目的地:" + FullShelfDestInfo.ShowInfo(line);
                                msg += runInfo; ;
                                GoFullShelfStationStep.Msg = msg;
                            }
                            else
                            {
                                //[{"msg":"0料车已解绑或未发新料"}]
                                GoFullShelfStationStep.ToNextStep(GO_FULL_SHELF_STATION_STEP.WAIT_REACH_STANDBY);
                                runInfo = "从产线" + agvPlace + "回到待机位";
                                msg += runInfo;
                                GoFullShelfStationStep.Msg = msg;
                                Common.MoveToNode(agv, SettingString.Standby);
                                UpdateJobTaskInfo(agv.CurTaskName, agv.CurTaskID);
                            }
                        }
                    }


                }
                //else if (GoFullShelfStationStep.IsTimeOut(30000, out double timeOutValue) && !agv.TaskSend.Equals(SettingString.Move + SettingString.A6) 
                //    && !agv.CurTaskName.Equals(SettingString.Move+SettingString.A6))
                //{
                //    Common.MoveToNode(agv, FullShelfStationPlace);
                //}
            }
            else if (GoFullShelfStationStep.IsStep(GO_FULL_SHELF_STATION_STEP.WAIT_REACH_STANDBY))
            {
                CurTaskState = Common.GetTakJobState(CurTaskID);
                if (Common.CheckTaskFinished(agv, SettingString.Standby, CurTaskState))
                {
                    GoFullShelfStationStep.ToNextStep(GO_FULL_SHELF_STATION_STEP.END);
                    runInfo = "到达待机位";
                    msg += runInfo;
                    GoFullShelfStationStep.Msg = msg;
                }
            }
            else if (GoFullShelfStationStep.IsStep(GO_FULL_SHELF_STATION_STEP.WAIT_DOUBLE_LINE_RESPONSE))
            {
                int id = Common.FindNode(FullShelfStationPlace);
                if (id == -1)
                {
                    runInfo = "未找到节点:" + FullShelfStationPlace;
                    msg += runInfo;
                    GoFullShelfStationStep.Msg = msg;
                    return this;
                }
                ClientNode node = Common.nodeInfo[id];
                if (node.StateEquals(eNodeStatus.MayLeave))
                {
                    if (!agv.CurTaskName.Equals("Enter"))
                    {
                        GoFullShelfStationStep.ToNextStep(GO_FULL_SHELF_STATION_STEP.WAIT__FULL_SHELF_IN_AGV);
                        runInfo = "收到双层线出料架请求[ReadyLeave]的响应 " + FullShelfStationPlace + "出料架,小车链条运行";
                        msg += runInfo;
                        GoFullShelfStationStep.Msg = msg;
                        rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Enter"]);
                        UpdateJobTaskInfo(agv.CurTaskName, agv.CurTaskID);
                    }

                }
                else if (GoFullShelfStationStep.IsTimeOut(15000, out double timeOutValue))
                {
                    GoFullShelfStationStep.ToNextStep(GO_FULL_SHELF_STATION_STEP.WAIT_AGV_REACH_A6);
                    //runInfo = "AGV到达 " + FullShelfStationPlace + ",15秒后重新向双层线发送出料架请求[ReadyLeave]";
                    //msg += runInfo;
                    //GoFullShelfStationStep.Msg = msg;
                }


            }
            else if (GoFullShelfStationStep.IsStep(GO_FULL_SHELF_STATION_STEP.WAIT__FULL_SHELF_IN_AGV))
            {
                CurTaskState = Common.GetTakJobState(CurTaskID);
                if (Common.CheckEnterOrLeaveFinished(agv, "Enter", CurTaskState))
                {

                    //GoFullShelfStationStep.ToNextStep(GO_FULL_SHELF_STATION_STEP.END);
                    runInfo = FullShelfStationPlace + "满料架进入小车完成";
                    msg += runInfo;
                    GoFullShelfStationStep.Msg = msg;
                    return new SendFullShelfToLineJob(FullShelfDestInfo);
                }
                else if (GoFullShelfStationStep.IsTimeOut(60000, out double timeOutValue))
                {
                    //链条停止
                    runInfo = "满料架在[" + FullShelfStationPlace + "]进入小车超时[" + timeOutValue.ToString("f1") + "秒],请检查料架进入小车的情况";
                    //msg +=runInfo;
                    //GoFullShelfStationStep.Msg = msg;
                }

            }

            return this;
        }

    }
    /// <summary>
    /// 出满料步骤
    /// </summary>
    public enum GO_FULL_SHELF_STATION_STEP
    {
        /// <summary>
        /// 负载检查
        /// </summary>
        NONE,
        /// <summary>
        /// 负载检查结果
        /// </summary>
        WAIT_CHECK_RESULT,
        /// <summary>
        /// 等待到达4C风淋门
        /// </summary>
        WAIT_AGV_REACH_AIR_DOOR,
        /// <summary>
        /// 等待AGV到达门
        /// </summary>
        WAIT_AGV_REACH_DOOR,
        /// <summary>
        /// 等待到达A6
        /// </summary>
        WAIT_AGV_REACH_A6,
        /// <summary>
        /// 等待双层线回应
        /// </summary>
        WAIT_DOUBLE_LINE_RESPONSE,
        /// <summary>
        /// 等待料架进入小车
        /// </summary>
        WAIT__FULL_SHELF_IN_AGV,
        /// <summary>
        /// 等待AGV到达待机位
        /// </summary>
        WAIT_REACH_STANDBY,
        /// <summary>
        /// 送上双层线
        /// </summary>
        END
    }
}