FullShelfJob.cs 8.8 KB
using System;
using Model;

namespace BLL
{
    public class FullShelfJob : IJob
    {
        private AgvInfo _info;
        private MissionJob move;
        private string mission;
        private DateTime getTime;
        private string key;
        //private bool recycleFirst;
        private JobStep<FullShelfStep> fullShelfStep;

        public FullShelfJob()
        {
            IsEnd = false;
            fullShelfStep = new JobStep<FullShelfStep>(FullShelfStep.None);
            Common.log.Debug("加载FullShelfJob");
        }

        public bool IsEnd { get; private set; }

        public IJob Execute(AgvInfo info)
        {
            _info = info;
            if (fullShelfStep.Equals(FullShelfStep.None))
            {
                if (ManageWork.PauseFull)
                    EnterElevatorSideShelf();
                else
                    FindLine();
            }
            else if (fullShelfStep.Equals(FullShelfStep.MoveLine))
            {
                MoveLine();
            }
            else if (fullShelfStep.Equals(FullShelfStep.MoveElevator))
            {
                MoveElevator();
            }
            else if (fullShelfStep.Equals(FullShelfStep.SendUseAsk))
            {
                ManageWork.SendElevatorUseAsk();
                fullShelfStep.NextStep(FullShelfStep.UseAnswer);
                fullShelfStep.Msg = string.Format("{0} 发送电梯使用请求", _info.Name);
            }
            else if (fullShelfStep.Equals(FullShelfStep.UseAnswer))
            {
                if (ManageWork.ElevatorUseAnswer())
                {
                    ManageWork.PauseFull = false;
                    fullShelfStep.NextStep(FullShelfStep.CallElevator);
                    fullShelfStep.Msg = string.Format("{0} 电梯可以使用", _info.Name);
                }
                else if (ManageWork.FindElevatorWork())
                {
                    //送满架子时,同时要回收空架子
                    ManageWork.PauseFull = true;
                    mission = Common.MISSION_LEAVE_SHELF;
                    move = new MissionJob(mission);
                    move.Execute(_info);
                    fullShelfStep.NextStep(FullShelfStep.LeaveShelf);
                    fullShelfStep.Msg = string.Format("{0} 暂停送满货架,优先拉空货架", _info.Name);
                }
                else
                {
                    Common.log.Debug(string.Format("{0} 电梯还未应答使用请求", _info.Name));
                }
            }
            else if (fullShelfStep.Equals(FullShelfStep.CallElevator))
            {
                ManageWork.ElevatorCall(true);
                getTime = DateTime.Now;
                fullShelfStep.NextStep(FullShelfStep.CancelCallElevator);
                fullShelfStep.Msg = string.Format("{0} 发送电梯呼叫", _info.Name);
            }
            else if (fullShelfStep.Equals(FullShelfStep.CancelCallElevator))
            {
                TimeSpan ts = DateTime.Now - getTime;
                if (ts.Seconds >= 3)
                {
                    //呼叫信号不能长时间保持,3s-5s即可。
                    ManageWork.ElevatorCall(false);
                    fullShelfStep.Msg = string.Format("{0} 满3s以上取消呼叫保持", _info.Name);
                    fullShelfStep.NextStep(FullShelfStep.OpenDoorAnswer);
                }
            }
            else if (fullShelfStep.Equals(FullShelfStep.OpenDoorAnswer))
            {
                if (ManageWork.ElevatorReady())
                {
                    fullShelfStep.Msg = string.Format("{0} 电梯已开门", _info.Name);
                    fullShelfStep.NextStep(FullShelfStep.EnterInto);
                }
                else
                {
                    Common.log.Debug(string.Format("{0} 电梯还未开门", _info.Name));
                }
            }
            else if (fullShelfStep.Equals(FullShelfStep.EnterInto))
            {
                _info.Place = "送满架进电梯";
                EnterInto();
            }
            else if (fullShelfStep.Equals(FullShelfStep.Leave))
            {
                move.Execute(_info);
                if (move.IsEnd)
                {
                    _info.Place = "电梯门外";
                    ManageWork.ElevatorFullShelf();
                    getTime = DateTime.Now;
                    fullShelfStep.Msg = string.Format("{0} 发送满料信号", _info.Name);
                    fullShelfStep.NextStep(FullShelfStep.EndTask);
                }
            }
            else if (fullShelfStep.Equals(FullShelfStep.EndTask))
            {
                TimeSpan ts = DateTime.Now - getTime;
                if (ts.Seconds >= 3)
                {
                    //离开信号保持几秒后,取消所有信号
                    ManageWork.ElevatorEnd();
                    ManageWork.FirstFloorAdd();

                    fullShelfStep.Msg = string.Format("{0} 送满料任务结束,当前一楼货架数量{1}个", _info.Name, Common.FirstFloorCurr);
                    fullShelfStep.NextStep(FullShelfStep.End);
                }
            }
            else if (fullShelfStep.Equals(FullShelfStep.LeaveShelf))
            {
                move.Execute(_info);
                if (move.IsEnd)
                {
                    fullShelfStep.NextStep(FullShelfStep.End);
                    fullShelfStep.Msg = string.Format("{0} 退出货架,[{1}]", _info.Name, mission);
                }
            }
            else if (fullShelfStep.Equals(FullShelfStep.End))
            {
                IsEnd = true;
                if (ManageWork.PauseFull)
                {
                    return new RecycleJob();
                }
                else
                {
                    IJob job = ManageWork.GetJob();
                    if (job == null)
                        return new StandbyJob();
                    else
                        return job;
                }
            }

            return this;
        }

        private void FindLine()
        {
            bool rtn = ManageWork.FindLineWork(out string key, out string name);
            if (rtn)
            {
                _info.Place = name;
                this.key = key;
                mission = Common.MISSION_CARRY_FULL + key;
                move = new MissionJob(mission);
                move.Execute(_info);
                fullShelfStep.NextStep(FullShelfStep.MoveLine);
                fullShelfStep.Msg = string.Format("{0} 送满货架任务,去{1},[{2}]", _info.Name, name, mission);              
            }
            else
            {
                fullShelfStep.NextStep(FullShelfStep.End);
                fullShelfStep.Msg = string.Format("{0} 没有找到产线呼叫,或产线没有货架", _info.Name);
            }
        }

        private void MoveLine()
        {
            move.Execute(_info);
            if (move.IsEnd)
            {
                ManageWork.LineTakeAway(key);
                _info.Place = "去电梯";
                mission = Common.MISSION_MOVE_ELEVATOR;
                move = new MissionJob(mission);
                move.Execute(_info);
                fullShelfStep.NextStep(FullShelfStep.MoveElevator);
                fullShelfStep.Msg = string.Format("{0} 送满货架任务,去电梯,[{1}]", _info.Name, mission);
            }
        }

        private void EnterElevatorSideShelf()
        {
            _info.Place = "进入电梯旁货架";
            mission = Common.MISSION_CARRY_ELEVATOR;
            move = new MissionJob(mission);
            move.Execute(_info);
            fullShelfStep.NextStep(FullShelfStep.MoveElevator);
            fullShelfStep.Msg = string.Format("{0} 送满货架任务,去电梯,[{1}]", _info.Name, mission);
        }

        private void MoveElevator()
        {
            move.Execute(_info);
            if (move.IsEnd)
            {
                ManageWork.PauseFull = false;
                _info.Place = "电梯等待位";
                fullShelfStep.NextStep(FullShelfStep.SendUseAsk);
                fullShelfStep.Msg = string.Format("{0} 到达电梯等待位", _info.Name);
            }
        }

        private void EnterInto()
        {
            mission = Common.MISSION_ENTER_ELEVATOR_FULL;
            move = new MissionJob(mission);
            move.Execute(_info);
            fullShelfStep.Msg = string.Format("{0} 送满货架进电梯,[{1}]", _info.Name, mission);
            fullShelfStep.NextStep(FullShelfStep.Leave);
        }

        private enum FullShelfStep
        {
            None,
            End,
            MoveLine,
            MoveElevator,
            SendUseAsk,
            UseAnswer,
            CallElevator,
            CancelCallElevator,
            OpenDoorAnswer,
            EnterInto,
            Leave,
            LeaveShelf,
            EndTask
        }
    }
}