ChargeManage.cs 15.8 KB
using XingYe_ACS.BaseStruct;
using XingYe_ACS.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XingYe_ACS.Business
{
    public class ChargeManager
    {
        public static void AgvCharge(int taskNum)
        {
            try
            {
                foreach (Agv agv in App.AgvList)
                {
                    //记录日志

                    App.ExFile.MessageLog("AgvCharge", "进入充电判断");
                    if (agv.agvTask != null || !agv.isEnable || !agv.isOnline || agv.heightState != EnumMsg.HeightState.低位 || agv.isCarry == true) continue;
                    //待关机车辆禁止车辆
                    if (App.offAgvNo.Contains(agv.strAgvNo)) continue;
                    //当前AGV所在群组能够执行的所有任务群组
                    List<TaskAgvGroup> taskGroupList = App.TaskAgvGroupList.FindAll(b => b.agvGroup == agv.agvGroup);
                    //判断当前agv所在AGV群组对应的任务的群组里是否有未提交的任务
                    List<FTask> noSubmitList = App.TaskList.Where(a => !a.isTaskSubmit && a.strTaskAgv == ""
                                && taskGroupList.Exists(b => b.taskGroup == a.strTaskGroup)).ToList();
                    //当前点不在地图中,禁止充电
                    Point startPoint = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                    if (startPoint == null) continue;
                    if (agv.agvState == EnumMsg.State.自动空闲)
                    {
                        //判断提升机有货 不做回家任务
                        //Hoister hoister = App.HoisterList.First();
                        //if (hoister.read_isFull) continue;
                        // if (agv.currentCharge > App.DangerCharge) continue;
                        //空闲AGV是否为其他车辆子任务终点
                        var agvPoint = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                        var sonTasks = App.TaskList.SelectMany(a => a.sonTaskList).ToList();
                        if (sonTasks.Count != 0)
                        {
                            var result = sonTasks.Exists(a => a.endPoint == agvPoint && a.sonTaskState != EnumMsg.SonTaskState.初始化);
                            if (result)
                            {
                                Point endPoint = null;
                                if (agvPoint.pointType == EnumMsg.PointType.充电点)
                                {
                                    endPoint = App.PointList.Find(a => a.pointType == EnumMsg.PointType.避让点);
                                }
                                else
                                {
                                    endPoint = App.PointList.Find(a => a.pointType == EnumMsg.PointType.充电点);
                                }
                                TaskManage.TaskBuild("回家", "", "", 1, agv.strAgvNo, "", startPoint, endPoint, "");
                                continue;
                            }
                        }
                        #region 测试电量
                        //Point point = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                        //if (point.pointType != EnumMsg.PointType.充电点) continue;
                        //int intChargeTime = 0;
                        //if (agv.chargeTime != DateTime.MinValue)
                        //{
                        //    TimeSpan ts1 = new TimeSpan(agv.chargeTime.Ticks);
                        //    TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
                        //    TimeSpan ts3 = ts2.Subtract(ts1).Duration();
                        //    intChargeTime = ts3.Minutes * 60 + ts3.Seconds;
                        //}
                        ////20181006 充电延时
                        //if (intChargeTime < 120) continue;
                        #endregion
                        int flag = 0;
                        if (agv.currentCharge < App.DangerCharge)
                        {
                            NeedCharge(agv, ref flag);
                        }
                        //else if (agv.currentCharge < App.FullCharge && agv.currentCharge > App.DangerCharge)
                        //{
                        //    if (noSubmitList.Count > 0) { flag = 0; }
                        //    //查找是否有比当前车辆更低电量的车
                        //    else if (App.AgvList.Where(a => a.isEnable && a.isOnline && a.agvTask == null
                        //        && a.agvState == EnumMsg.State.自动空闲 && a.currentCharge < agv.currentCharge && a.agvGroup == agv.agvGroup).ToList().Count == 0)
                        //    {
                        //        NeedCharge(agv, ref flag);
                        //    }
                        //}
                        //未找到充电桩,回家待命等待充电桩
                        if (flag == 99 || flag == 0)
                        //if(agv.agvState==EnumMsg.State.自动空闲 && agv.currentCharge >App.DangerCharge)
                        {
                            
                            Station endStation = null;
                            //工位优先补车
                            Point minHomePoint = null;
                            //minHomePoint = ComnMethod.GetMinHomePoint(agv, 0);
                            minHomePoint = App.PointList.Find(a => a.intX == 10 && a.intY == 988);
                            if (agv.strBarcode == minHomePoint.strBarcode) continue;
                            App.ExFile.MessageLog("AgvCharge", "进入回家判断");
                            if (minHomePoint != null)
                            {
                                //endStation = App.StationList.Find(a => a.strBarcode == minHomePoint.strBarcode);
                                bool isResult = false;
                                isResult = TaskManage.TaskBuild("回家", "", "", 1, agv.strAgvNo,"", startPoint, minHomePoint, "");
                                if (isResult)
                                {
                                    //agv.strChooseStation = endStation.strStationNo;
                                    //endStation.isLocked = true;
                                    //bool updateAgvResult = UpdateManage.UpdateAgvMsg(agv);
                                    //bool updateAtationResult = UpdateManage.UpdateStation(endStation);
                                }
                            }
                        }
                        continue;
                    }
                    else if (agv.agvState == EnumMsg.State.充电中)
                    {

                        //异常或者满电结束充电
                        if (agv.strAgvError != "" || agv.currentCharge == 100)
                        {
                            TaskManage.TaskBuild("取消充电", "", "", 1, agv.strAgvNo, "", startPoint, startPoint, "");
                            taskNum--;
                            continue;
                        }
                        var hoister1 = App.HoisterList.First();
                        if (agv.currentCharge > App.DangerCharge && hoister1.read_isFull)
                        {
                            TaskManage.TaskBuild("取消充电", "", "", 1, agv.strAgvNo, "", startPoint, startPoint, "");
                            taskNum--;
                            continue;
                        }
                        #region 充电延时注释
                        int intChargeTime = 0;
                        if (agv.chargeTime != DateTime.MinValue)
                        {
                            TimeSpan ts1 = new TimeSpan(agv.chargeTime.Ticks);
                            TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
                            TimeSpan ts3 = ts2.Subtract(ts1).Duration();
                            intChargeTime = ts3.Minutes * 60 + ts3.Seconds;
                        }
                        //20181006 充电延时
                        if (intChargeTime < 3600) continue;
                        #endregion

                       

                        //当前电量小于安全电量,继续充电
                        if (agv.currentCharge < App.DangerCharge) continue;
                        //充电量小于最小充电量,继续充电
                        //if (agv.currentCharge - agv.startCharges <= App.MinCharges) continue;
                        //当前AGV所属任务群组有未提交的任务
                        if (noSubmitList.Count > 0)
                        {
                            //获取所有能执行当前任务群组的AGV组
                            List<TaskAgvGroup> agvGroups =
                                        App.TaskAgvGroupList.Where(a => a.taskGroup == noSubmitList[0].strTaskGroup).ToList();
                            //判断当前任务群组小车是否有取消充电任务
                            if (App.AgvList.Where(a => ((a.agvTask != null && a.agvTask.strTaskID == "取消充电")
                            || (a.isEnable && a.isOnline && a.agvTask == null && a.agvState != EnumMsg.State.充电中))
                                && agvGroups.Exists(b => b.agvGroup == a.agvGroup)).ToList().Count > 0) continue;
                            TaskManage.TaskBuild("取消充电", "", "", 1, agv.strAgvNo, "", startPoint, startPoint, "");
                        }
                       // int tt = App.AgvList.Where(a => a.agvTask != null && a.agvTask.strTaskID == "取消充电").ToList().Count;
                        //int ss = App.AgvList.Where(a => a.isEnable && a.isOnline && a.agvTask == null && a.agvState == EnumMsg.State.自动空闲).ToList().Count;
                        //if (taskNum <= (tt + ss) && agv.currentCharge < App.FullCharge) continue;
                        taskNum--;
                    }
                }
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("AgvCharge", Ex.StackTrace);
            }
        }

        public static void NeedCharge(Agv agv, ref int flag)
        {
            try
            {
                #region
                //int intMin = 0;
                //Station minCs = null;
                ////获取所有充电点
                //List<Point> listPoint = App.PointList.Where(a => a.pointType == EnumMsg.PointType.充电点).ToList();
                //foreach (Point point in listPoint)
                //{
                //    Station Cs = App.StationList.FirstOrDefault(a => a.isEnable && !a.isLocked && a.strBarcode == point.strBarcode);
                //    if (Cs == null) { continue; }
                //    else
                //    {
                //        Point agvPoint = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                //        if (point == null) { continue; }

                //        int csMinNum = Common.ComnMethod.GetMinPoint(agvPoint.intX, agvPoint.intY, point.intX, point.intY);
                //        if (intMin == 0) intMin = csMinNum;
                //        if (csMinNum <= intMin)
                //        {
                //            minCs = Cs;
                //            intMin = csMinNum;
                //        }
                //    }
                //}
                #endregion
                Station minCs = null;
                Point startPoint = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                //未安装充电桩
                if (startPoint.pointType == EnumMsg.PointType.充电点)
                {
                    minCs = App.StationList.Find(a => a.strBarcode == startPoint.strBarcode);
                }
                else
                {
                    minCs = ComnMethod.GetMinChargeStation(agv);
                    if (minCs == null)
                    {
                        agv.strStopReason = ComnMethod.GetStopReason(agv.strStopReason, "未找到充电桩;");
                        //未找到充电桩,判断充电中的车是否有满足条件可取消充电的车
                        //当前AGV所在群组能够执行的所有任务群组
                        List<TaskAgvGroup> taskGroupList = App.TaskAgvGroupList.FindAll(b => b.agvGroup == agv.agvGroup);
                        //判断是否有取消充电任务
                        FTask cancleTask = App.TaskList.FirstOrDefault(a => a.strTaskID == EnumMsg.TaskType.取消充电.ToString());
                        if (cancleTask == null && agv.currentCharge <= App.SafeCharge)
                        {
                            List<Agv> safeAgv = new List<Agv>();
                            //执行相同任务群组的AGV是否有可以取消充电的任务
                            safeAgv = App.AgvList.Where(a => a.currentCharge > App.SafeCharge
                                                     && agv.currentCharge - agv.startCharges < App.MinCharges
                                                     && a.agvState == EnumMsg.State.充电中
                                                     && agv.agvTask == null && a.isEnable && a.isOnline
                                                     && taskGroupList.Exists(b => b.agvGroup == a.agvGroup)).OrderByDescending(a => a.currentCharge).ToList();
                            if (safeAgv.Count == 0)
                            {
                                //按照电量从高到底排序
                                safeAgv = App.AgvList.Where(a => a.currentCharge > App.SafeCharge
                                                       && agv.currentCharge - agv.startCharges < App.MinCharges
                                                       && a.agvState == EnumMsg.State.充电中
                                                       && agv.agvTask == null && a.isEnable && a.isOnline).OrderByDescending(a => a.currentCharge).ToList();
                            }
                            if (safeAgv.Count != 0)
                            {
                                Agv sAgv = safeAgv[0];
                                Point point = App.PointList.Find(a => a.strBarcode == sAgv.strBarcode);
                                TaskManage.TaskBuild("取消充电", "", "", 1, sAgv.strAgvNo, "", point, point, "");
                                flag = 2;
                                return;
                            }
                        }
                        flag = 99;
                    }
                }
                if (minCs != null)
                {
                    Point endPoint = App.PointList.Find(a => a.strBarcode == minCs.strBarcode);
                    bool result = false;
                    if (startPoint.AreaType != endPoint.AreaType && agv.agvAreaType == "2")
                    {
                        result = TaskManage.TaskBuild("充电-1", "", "", 1, agv.strAgvNo, minCs.strStationNo, startPoint, endPoint, "");
                    }
                    else
                    {
                        result = TaskManage.TaskBuild("充电", "", "", 1, agv.strAgvNo, minCs.strStationNo, startPoint, endPoint, "");
                    }
                    if (result)
                    {
                        Station endCs = App.StationList.Find(a => a.strBarcode == endPoint.strBarcode);
                        endCs.isLocked = true;
                        bool isResult = UpdateManage.UpdateStation(endCs);
                        agv.strChooseStation = endCs.strStationNo;
                        bool isResultCs = UpdateManage.UpdateAgvMsg(agv);
                        flag = 1;
                    }
                }
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("NeedCharge", Ex.Message.Replace(Environment.NewLine, ""));
            }
        }
    }
}