ChargeManage.cs 7.76 KB
using RCS.Model.Comm;
using RCS.Model.Entity;

namespace RCS.WinClient.Service
{
    public class ChargeManager
    {
        /// <summary>
        /// 充电或是回家
        /// </summary>
        public static void AgvCharge()
        {
            try
            {
                foreach (Base_Agv agv in App.AgvList)
                {
                    if (agv.AgvTask != null || !agv.IsEnable || !agv.IsOnline || agv.InStock != EnumMsg.InStock.全部无货) continue;
                    //待关机车辆禁止车辆
                    if (App.offAgvNo.Contains(agv.AgvNo)) continue;
                    //当前点不在地图中,禁止充电
                    Base_Point startPoint = App.PointList.Find(t => t.Barcode == agv.Barcode);
                    if (startPoint == null) continue;
                    if (agv.AgvState == EnumMsg.State.自动空闲)
                    {
                        // 充电
                        if ((DateTime.Now - agv.FreeTime).TotalSeconds > App.GetKeyValue("MinFreeTime"))
                        {
                            if (App.TaskList.Any(t => t.TaskAgvNo == agv.AgvNo && t.TaskID == EnumMsg.TaskType.充电.ToString())) continue;
                            if (agv.Voltage <= App.GetKeyValue("DangerCharge") || agv.power <= App.GetKeyValue("DangerVoltage"))
                            {
                                NeedCharge(agv);
                                continue;
                            }
                            if (agv.Voltage < App.GetKeyValue("SafeCharge") || agv.power < App.GetKeyValue("SafeVoltage") || agv.VirtuallyVoltage < App.GetKeyValue("DangerVirtuallyVoltage"))
                            {
                                if (!App.TaskList.Any(t => t.PredictAgvNo == agv.AgvNo))
                                {
                                    NeedCharge(agv);
                                    continue;
                                }
                            }
                        }

                        // 回家
                        if ((DateTime.Now - agv.FreeTime).TotalSeconds > App.GetKeyValue("GoHomeTime"))
                        {
                            if (App.TaskList.Any(t => t.TaskAgvNo == agv.AgvNo && t.TaskID == EnumMsg.TaskType.手动行走.ToString())) continue;
                            if (agv.AgvNo == "A33_9001")
                            {
                                var stopPoint = App.PointList.FirstOrDefault(t => t.Barcode == "1326");
                                TaskManage.TaskBuild(EnumMsg.TaskType.手动行走.ToString(), EnumMsg.TaskGroup.RCS单步任务.ToString(), "1", 1, agv.AgvNo, startPoint.Barcode, stopPoint.Barcode, "", "", "");
                                continue;
                            }
                            if (agv.AgvNo == "A33_9002")
                            {
                                var stopPoint = App.PointList.FirstOrDefault(t => t.Barcode == "805");
                                TaskManage.TaskBuild(EnumMsg.TaskType.手动行走.ToString(), EnumMsg.TaskGroup.RCS单步任务.ToString(), "1", 1, agv.AgvNo, startPoint.Barcode, stopPoint.Barcode, "", "", "");
                                continue;
                            }
                        }
                    }
                    else if (agv.AgvState == EnumMsg.State.充电中)
                    {
                        //异常或者满电结束充电
                        if (agv.Error != "" || agv.Voltage >= App.GetKeyValue("FullCharge") && agv.current < App.GetKeyValue("FullCurrent") || agv.batteryflag > 0)
                        {
                            var result = TaskManage.TaskBuild(EnumMsg.TaskType.取消充电.ToString(),
                                                              EnumMsg.TaskGroup.RCS单步任务.ToString(),
                                                              "", 1, agv.AgvNo, agv.Barcode, agv.Barcode, "", "", "");
                            if (result.Success)
                                App.ExFile.MessageLog("TaskBuild", $"因为车不满足充电条件,生成AGV【{agv.AgvNo}】的【取消充电】任务!当前车辆电量【{agv.Voltage}】电流【{agv.current}】充满标识【{agv.batteryflag}】错误【{agv.Error}】");
                            else
                                App.ExFile.MessageBusinessErr("TaskBuild", $"因为车不满足充电条件,生成AGV【{agv.AgvNo}】的【取消充电】任务失败,原因:{result.Msg}!");
                            continue;
                        }
                        //当前电量小于安全电量、电压小于安全电压,就继续充电
                        if (agv.Voltage <= App.GetKeyValue("DangerCharge") || agv.power <= App.GetKeyValue("DangerVoltage")) continue;
                        //如果充电时间小于最少充电时间,继续充电
                        if ((DateTime.Now - agv.ChargeTime).TotalSeconds < App.GetKeyValue("MinChargeTime")) continue;
                        //当前AGV所属任务群组有未提交的任务
                        if (App.TaskList.Any(t => t.PredictAgvNo == agv.AgvNo))
                        {
                            //判断是否有取消充电任务,已经有取消充电的任务,就不处理
                            if (App.TaskList.Any(t => t.TaskAgvNo == agv.AgvNo && t.TaskID == EnumMsg.TaskType.取消充电.ToString())) continue;

                            var result = TaskManage.TaskBuild(EnumMsg.TaskType.取消充电.ToString(),
                                                              EnumMsg.TaskGroup.RCS单步任务.ToString(),
                                                              "1", 1, agv.AgvNo, agv.Barcode, agv.Barcode, "", "", "");
                            if (result.Success)
                                App.ExFile.MessageLog("TaskBuild", $"因为有预分配给车的任务,生成AGV【{agv.AgvNo}】的【取消充电】任务!");
                            else
                                App.ExFile.MessageBusinessErr("TaskBuild", $"因为有预分配给车的任务,生成AGV【{agv.AgvNo}】的【取消充电】任务失败,原因:{result.Msg}!");
                            continue;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                App.ExFile.MessageError("AgvCharge", $"充电和回家发生异常,原因:{ex.Message},堆栈:{ex.StackTrace}");
            }
        }

        /// <summary>
        /// 充电
        /// </summary>
        /// <param name="agv"></param>
        /// <param name="flag"></param>
        public static void NeedCharge(Base_Agv agv)
        {
            try
            {
                //一个车对应一个充电桩
                var chargeName = agv.ChargeName;
                Base_Station minCs = App.StationList.FirstOrDefault(t => t.Name == chargeName);

                if (minCs != null)
                {
                    var result = TaskManage.TaskBuild(EnumMsg.TaskType.充电.ToString(),
                                                      EnumMsg.TaskGroup.RCS单步任务.ToString(),
                                                      "1", 1, agv.AgvNo, agv.Barcode, minCs.Barcode, "", "", "");
                    if (result.Success)
                        App.ExFile.MessageLog("TaskBuild", $"生成AGV【{agv.AgvNo}】的【充电】任务!");
                    else
                        App.ExFile.MessageBusinessErr("TaskBuild", $"生成AGV【{agv.AgvNo}】的【充电】任务失败,原因:{result.Msg}!");
                }
            }
            catch (Exception ex)
            {
                App.ExFile.MessageError("NeedCharge", ex.Message.Replace(Environment.NewLine, ""));
            }
        }
    }
}