ChargeTask.cs 6.4 KB
using CtuDeviceLib;
using Mushiny;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceLibrary.CtuService
{
    /// <summary>
    /// 充电任务
    /// </summary>
    public class ChargeTask : CtuTaskBase
    {
        public ChargeTask(CTU ctu, string dst) : base(ctu, dst)
        {
            Name = "充电任务";
        }

        public override void Excute()
        {
            if (string.IsNullOrEmpty(DstName))
            {
                Msg.add($"【{ctu.Name}】无充电位置,请检查充电位置是否配置", MsgLevel.warning);
                return;
            }
            //当前位置不是目的地,就重新规划路线
            if (MoveInfo.MoveStep != RunStep.Wait && MoveInfo.MoveStep != RunStep.Charge_01_MoveToDst)
            {
                if (ctu.CurLandMark != destination)
                {
                    MoveInfo.NextMoveStep(RunStep.Wait);
                    MoveInfo.Info($"当前点位不在目标点【{DstName}】【{destination}】,重新进入充电流程");
                }
            }
            switch (MoveInfo.MoveStep)
            {
                case RunStep.Wait:
                    if (!CalcDst())
                    {
                        Msg.add($"规划去【{DstName}】失败,请检查", MsgLevel.warning);
                        return;
                    }

                    MoveInfo.NextMoveStep(RunStep.Charge_01_MoveToDst);
                    MoveInfo.Info($"去充电点【{DstName}】【{destination}】");

                    break;

                case RunStep.Charge_01_MoveToDst:
                    if (IsFinished)
                    {
                        MoveInfo.NextMoveStep(RunStep.Charge_02_AtDst);
                        MoveInfo.Info($"已到充电点【{DstName}】【{destination}】");
                    }
                    else
                    {
                        Move();
                    }
                    break;

                case RunStep.Charge_02_AtDst:
                    if (StartChargeTask())
                    {
                        MoveInfo.NextMoveStep(RunStep.Charge_03_StartCharge);
                        MoveInfo.Info($"对接充电桩【{DstName}】【{destination}】");
                    }
                    else
                    {
                        MoveInfo.Info($"创建对接充电桩异常【{DstName}】【{destination}】");
                        Msg.add($"【{ctu.Name}】创建对接充电桩异常", MsgLevel.warning);
                    }
                    break;
                case RunStep.Charge_03_StartCharge:
                    if (IsFinished)
                    {
                        MoveInfo.NextMoveStep(RunStep.Charge_04_Charging);
                        MoveInfo.Info($"对接充电桩成功【{DstName}】【{destination}】");
                    }
                    break;
                case RunStep.Charge_04_Charging:
                    var task = findTasks();
                    if (task != null || RobotManager.IsReadyUpdate)
                    {
                        if (CreatesStopChargeTask())
                        {
                            MoveInfo.NextMoveStep(RunStep.Charge_05_ExitCharging);
                            MoveInfo.Info($"退出充电桩【{DstName}】【{destination}】");
                        }
                        else
                        {
                            MoveInfo.Info($"创建退出充电桩异常【{DstName}】【{destination}】");
                            Msg.add($"【{ctu.Name}】创建退出充电桩异常", MsgLevel.warning);
                        }
                    }
                    else
                    {
                        MoveInfo.Info($"充电中【{ctu.SOC}%】");
                    }
                    break;
                case RunStep.Charge_05_ExitCharging:
                    if (IsFinished)
                    {
                        MoveInfo.NextMoveStep(RunStep.CtuTask_ProcessFinished);
                        MoveInfo.Info($"退出完成【{DstName}】【{destination}】【{ctu.SOC}%】,搜寻任务");
                    }
                    break;
                case RunStep.CtuTask_ProcessFinished:
                    var nextTask = findTasks();
                    if (nextTask != null)
                    {
                        ctu.CtuTask = nextTask;
                    }
                    break;
            }

        }
        bool StartChargeTask()
        {
            if (MushinyManager.GenStartChargePathPoints(ctu, dstPosInfo, out PathPoint pathPoint))
            {
                runInfo.Reset(CtuTaskRunInfo.StartCharge);
                return createPathPoint(new List<PathPoint>() { pathPoint }, CtuTaskRunInfo.StartCharge);
            }
            else
            {
                return false;
            }
        }
        bool CreatesStopChargeTask()
        {
            if (MushinyManager.GenStopChargePathPoints(ctu, dstPosInfo, out PathPoint pathPoint))
            {
                runInfo.Reset(CtuTaskRunInfo.StopCharge);
                return createPathPoint(new List<PathPoint>() { pathPoint }, CtuTaskRunInfo.StopCharge);
            }
            else
            {
                return false;
            }
        }
        CtuTaskBase findTasks()
        {
            var exitChargeSoc = ConfigAppSettings.GetValue(Setting_Init.Charge_ExitChargeSoc, 80);
            if (ctu.SOC >= exitChargeSoc)
            {
                CtuTaskBase task = null;
                {
                    task = new StandbyTask(ctu);
                }
                return task;
            }
            else
            {
                var forceChargeSoc = ConfigAppSettings.GetValue(Setting_Init.Charge_ForceChargeSoc, 80);
                if (ctu.SOC <= forceChargeSoc + 5)
                {
                    return null;
                }
                var othCtus = CTUManager.GetOhterEnabledCTUs(ctu);
                if (othCtus != null && othCtus.Count > 0)
                {
                    var find = othCtus.Find(s => (s.SOC < forceChargeSoc) || (s.SOC < ctu.SOC && s.CtuTask != null && s.CtuTask is StandbyTask));
                    if (find != null)
                    {
                        return new StandbyTask(ctu);
                    }
                }
            }
            return null;
        }
    }
}