ChargeJob.cs 3.3 KB
using System;
using Model;

namespace BLL
{
    public class ChargeJob : IJob
    {
        private AgvInfo _info;
        private MoveJob move;
        private string mission;
        private JobStep<ChargeStep> chargeStep;
        private int currBattery;
        //private const int CHARGE_PLACE_PLC = 20;

        public ChargeJob()
        {
            chargeStep = new JobStep<ChargeStep>(ChargeStep.None);
            Common.log.Debug("加载ChargeJob");
        }

        public bool IsEnd { get; private set; }

        public IJob Execute(AgvInfo info)
        {
            _info = info;
            if (chargeStep.Equals(ChargeStep.None))
            {
                currBattery = _info.Battery;
                if (_info.IsWorkspace())
                    MoveCharge();
                else
                    PassDoor();
            }
            else if (chargeStep.Equals(ChargeStep.PassDoor))
            {
                move.Execute(_info);
                if(move.IsEnd)
                    MoveCharge();
            }
            else if (chargeStep.Equals(ChargeStep.MoveStation))
            {
                MoveStation();
            }
            else if (chargeStep.Equals(ChargeStep.Charging))
            {
                Common.log.Debug(_info.Name + "当前电量" + _info.Battery);

                if (_info.Battery == _info.BatteryMax)
                {
                    chargeStep.Msg = _info.Name + "电量达到" + _info.BatteryMax;
                    chargeStep.NextStep(ChargeStep.End);
                }
                else if (_info.Battery > _info.BatteryMin)
                {
                    IJob job = SteelManage.GetSteelJob(_info);
                    if (job != null)
                    {
                        Common.mir.Del_Mission(_info.IP, _info.Authorization);
                        return job;
                    }
                }
            }
            else if (chargeStep.Equals(ChargeStep.End))
            {
                Common.mir.Del_Mission(_info.IP, _info.Authorization);
                chargeStep.Msg = _info.Name + "充电工作结束,回待机位";
                return new StandbyJob();
            }

            return this;
        }

        private void MoveCharge()
        {
            mission = Common.MISSION_CHARGE + _info.Workshop;
            _info.Place = mission;
            move = new MoveJob(mission);
            move.Execute(_info);
            chargeStep.NextStep(ChargeStep.MoveStation);
            chargeStep.Msg = _info.Name + "发送任务" + mission;
        }

        private void PassDoor()
        {
            mission = Common.MISSION_PASS_DOOR_INTO + _info.Workshop;
            _info.Place = mission;
            move = new MoveJob(mission);
            move.Execute(_info);
            chargeStep.NextStep(ChargeStep.PassDoor);
            chargeStep.Msg = _info.Name + "发送任务" + mission;
        }

        private void MoveStation()
        {
            if (_info.Battery > currBattery)
            {
                chargeStep.Msg = _info.Name + "到达充电位,准备充电";
                chargeStep.NextStep(ChargeStep.Charging);
            }
        }

        private enum ChargeStep
        {
            None,
            End,
            PassDoor,
            MoveStation,
            Charging
        }
    }

}