FullShelfJob.cs 11.6 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 DateTime dtUseAsk;
        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 string Msg
        {
            get
            {
                return fullShelfStep.Msg;
            }
        }

        public IJob Execute(AgvInfo info)
        {
            _info = info;
            if (fullShelfStep.Equals(FullShelfStep.None))
            {
                if (ManageWork.PauseFull)
                {
                    _info.LogJson.SetMissionStart("执行暂停的满架任务", "电梯");
                    _info.LogJson.SetMissionStep("开始执行任务", FullShelfStep.None.ToString());
                    EnterElevatorSideShelf();
                }
                else
                {
                    _info.LogJson.SetMissionStart("送满架任务", "产线");
                    _info.LogJson.SetMissionStep("开始执行任务", FullShelfStep.None.ToString());
                    FindLine();
                }
            }
            else if (fullShelfStep.Equals(FullShelfStep.MoveLine))
            {
                MoveLine();
            }
            else if (fullShelfStep.Equals(FullShelfStep.MoveElevator))
            {
                MoveElevator();
            }
            else if (fullShelfStep.Equals(FullShelfStep.SendUseAsk))
            {
                ManageWork.SendElevatorUseAsk();
                dtUseAsk = DateTime.Now;
                fullShelfStep.NextStep(FullShelfStep.UseAnswer);
                fullShelfStep.Msg = string.Format("{0} 发送电梯使用请求", _info.Name);
                _info.LogJson.SetMissionStep("发送电梯使用请求", FullShelfStep.SendUseAsk.ToString());
            }
            else if (fullShelfStep.Equals(FullShelfStep.UseAnswer))
            {
                if (ManageWork.ElevatorUseAnswer())
                {
                    ManageWork.PauseFull = false;
                    fullShelfStep.NextStep(FullShelfStep.CallElevator);
                    fullShelfStep.Msg = string.Format("{0} 电梯可以使用", _info.Name);
                    _info.LogJson.SetMissionStep("电梯可以使用", FullShelfStep.UseAnswer.ToString());
                    _info.LogJson.SetErrorOver();
                }
                else if (ManageWork.FindElevatorWork())
                {
                    //送满架子时,同时要回收空架子
                    ManageWork.PauseFull = true;
                    mission = Common.MISSION_LEAVE_SHELF;
                    _info.ExistShelf = 0;
                    move = new MissionJob(mission);
                    move.Execute(_info);
                    fullShelfStep.NextStep(FullShelfStep.LeaveShelf);
                    fullShelfStep.Msg = string.Format("{0} 暂停送满货架,优先拉空货架", _info.Name);
                    _info.LogJson.SetMissionStep("暂停送满货架,优先拉空货架", FullShelfStep.LeaveShelf.ToString());
                    _info.LogJson.SetErrorOver();
                }
                else
                {
                    TimeSpan ts = DateTime.Now - dtUseAsk;
                    if (ts.TotalMinutes > 3)
                    {
                        string value = string.Format("在电梯等待区等待电梯应答超时{0}分钟", Convert.ToInt32(ts.TotalMinutes));
                        DisplayBoard.Add("12号车", "lineAgv.12号车.StandTimeOut", value, 0);
                        _info.LogJson.SetErrorStart(_info.TaskName, value);
                    }
                    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);
                _info.LogJson.SetMissionStep("发送电梯呼叫", FullShelfStep.CallElevator.ToString());
            }
            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);
                    _info.LogJson.SetMissionStep("电梯已开门", FullShelfStep.OpenDoorAnswer.ToString());
                }
                else
                {
                    Common.log.Debug(string.Format("{0} 电梯还未开门", _info.Name));
                }
            }
            else if (fullShelfStep.Equals(FullShelfStep.EnterInto))
            {
                _info.Place = "送满架进电梯";
                EnterInto();
                _info.LogJson.SetMissionStep("送满架进电梯", FullShelfStep.EnterInto.ToString());
            }
            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);
                    _info.LogJson.SetMissionStep("发送满料信号", FullShelfStep.EndTask.ToString());
                }
            }
            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);
                    _info.LogJson.SetMissionStep("送满料任务结束", FullShelfStep.End.ToString());
                }
            }
            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);
                    _info.LogJson.SetMissionStep("退出货架", FullShelfStep.LeaveShelf.ToString());
                }
            }
            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;
                _info.ExistShelf = 1;
                move = new MissionJob(mission);
                move.Execute(_info);
                fullShelfStep.NextStep(FullShelfStep.MoveLine);
                fullShelfStep.Msg = string.Format("{0} 送满货架任务,去{1},[{2}]", _info.Name, name, mission);
                _info.LogJson.SetMissionStep("去产线" + name, FullShelfStep.MoveLine.ToString());
            }
            else
            {
                fullShelfStep.NextStep(FullShelfStep.End);
                fullShelfStep.Msg = string.Format("{0} 没有找到产线呼叫,或产线没有货架", _info.Name);
                _info.LogJson.SetMissionStep("没有找到产线呼叫", FullShelfStep.End.ToString());
            }
        }

        private void MoveLine()
        {
            move.Execute(_info);
            if (move.IsEnd)
            {
                ManageWork.LineTakeAway(key);
                _info.Place = "去电梯";
                _info.ExistShelf = -1;
                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);
                _info.LogJson.SetMissionStep("去电梯", FullShelfStep.MoveElevator.ToString());
            }
        }

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

        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);
                _info.LogJson.SetMissionStep("到达电梯等待位", FullShelfStep.SendUseAsk.ToString());
            }
        }

        private void EnterInto()
        {
            mission = Common.MISSION_ENTER_ELEVATOR_FULL;
            _info.ExistShelf = 0;
            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
        }
    }
}