ALineManager.cs 13.2 KB
using log4net;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Management;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace OnlineStore.DeviceLibrary
{
    public class LineManager
    {
        public static bool UseBuzzer = ConfigAppSettings.GetIntValue(Setting_Init.UseBuzzer).Equals(1);
        private static bool isInit = false;
        public static bool IsConnectServer = !ConfigAppSettings.GetValue(Setting_Init.http_server).Equals("");

        public static LineBean Line = null;
        public static Line_Config Config = null;
        public static Dictionary<int, DeviceConfig> allConfigMap = null;
        static LineManager()
        {
            int take_x = ConfigHelper.Config.Get("takePosX", 23781);
            int take_y = ConfigHelper.Config.Get("takePosY", 20612);
            AgvPoints.Add("TAKE", new List<Point>() { new Point { X = take_x, Y = take_y } });//取料点
            int put_x = ConfigHelper.Config.Get("putPosX", 23781);
            int put_y = ConfigHelper.Config.Get("putPosY", 22812);
            AgvPoints.Add("PUT", new List<Point>() { new Point { X = put_x, Y = put_y } });//放料点
            int standby_x = ConfigHelper.Config.Get("standbyPosX", 27564);
            int standby_y = ConfigHelper.Config.Get("standbyPosY", 20282);
            AgvPoints.Add("STANDBY", new List<Point>() { new Point { X = standby_x, Y = standby_y } });
            int charge_x = ConfigHelper.Config.Get("chargePosX", 22081);
            int charge_y = ConfigHelper.Config.Get("chargePosY", 13187);
            AgvPoints.Add("CHARGE", new List<Point>() { new Point() { X = charge_x, Y = charge_y } });
        }
        public static Dictionary<string, string> StepDesMap = new Dictionary<string, string>();

        public static DischargeEquip GetDischargeEquip()
        {
            return (DischargeEquip)Line.AllEquipMap[102];
        }
        public static bool CheckEnum(Type type, bool isStep = false)
        {
            if (type.IsEnum)
            {
                List<int> valueList = new List<int>();
                Array array = Enum.GetValues(type);
                foreach (int item in array)
                {
                    if (valueList.Contains(item))
                    {
                        LogUtil.error(type.Name + "枚举值:" + item + "重复存在,请检查代码!程序退出。");
                        Application.Exit();
                        return false;
                    }
                    valueList.Add(item);

                    if (isStep)
                    {
                        LineMoveStep en = (LineMoveStep)item;
                        string des = EnumDesHelper.GetStepDes(en);
                        StepDesMap.Add(en.ToString(), des);
                    }
                }
            }
            return true;
        }
        public static bool Init()
        {
            try
            {
                if (!isInit)
                {
                    string server = ConfigAppSettings.GetValue(Setting_Init.http_server);
                    if (server.Equals(""))
                    {
                        IsConnectServer = false;
                    }
                    else
                    {
                        IsConnectServer = true;
                    }
                    if (!CheckEnum(typeof(LineMoveStep), true))
                    {
                        return false;
                    }
                    if (!CheckEnum(typeof(LineStatus)))
                    {
                        return false;
                    }
                    if (!CheckEnum(typeof(LineRunStatus)))
                    {
                        return false;
                    }
                    Dictionary<int, AGVEquip_Config> agvECMap = null;
                    Dictionary<int, StationEquip_Config> stationECMap = null;
                    Dictionary<int, HYEquip_Config> HYECMap = null;

                    DeviceConfig.SubDIList = new Dictionary<int, Dictionary<string, ConfigIO>>();
                    DeviceConfig.SubDOList = new Dictionary<int, Dictionary<string, ConfigIO>>();
                    DeviceConfig.ProIOIpMap = new Dictionary<string, string>();
                    DeviceConfig.ProRFIpMap = new Dictionary<string, string>();

                    agvECMap = new Dictionary<int, AGVEquip_Config>();
                    stationECMap = new Dictionary<int, StationEquip_Config>();
                    HYECMap = new Dictionary<int, HYEquip_Config>();
                    allConfigMap = new Dictionary<int, DeviceConfig>();


                    isInit = true;
                    string lineType = ConfigAppSettings.GetValue(Setting_Init.Line_Type);
                    LogUtil.info("类型=" + lineType + ",开始加载 配置");
                    if (lineType == DeviceType.Line)
                    {
                        string appPath = Application.StartupPath;
                        string CID = ConfigAppSettings.GetValue(Setting_Init.Line_CID);
                        string linefilePath = appPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_Line);

                        Config = CSVConfigReader.LoadLineConfig(0, CID, DeviceType.Line, linefilePath);
                        allConfigMap.Add(0, Config);
                        for (int i = 1; i <= 5; i++)
                        {
                            StationEquip_Config config = CSVConfigReader.LoadStationConfig(i, DeviceType.StationEquip, "");
                            config.SetIO(i);
                            stationECMap.Add(i, config);
                            allConfigMap.Add(i, config);
                        }
                        for (int i = 1; i <= 2; i++)
                        {
                            int subType = i + 100;
                            AGVEquip_Config moveConfig = CSVConfigReader.LoadMoveConfig(subType, DeviceType.AGVEquip, "");
                            moveConfig.SetIO(subType);
                            agvECMap.Add(subType, moveConfig);
                            allConfigMap.Add(subType, agvECMap[subType]);
                        }
                        for (int i = 1; i <= 4; i++)
                        {
                            int subType = 200 + i;
                            HYEquip_Config moveConfig = CSVConfigReader.LoadHYConfig(subType, DeviceType.HYEquip, "");
                            moveConfig.LoadParam();
                            moveConfig.SetIO(subType);
                            HYECMap.Add(subType, moveConfig);
                            allConfigMap.Add(subType, moveConfig);
                        }

                        Config.SetIO(1000);
                        Line = new LineBean(Config, agvECMap, stationECMap, HYECMap);
                        LogUtil.info("加载 完成!");
                        return true;
                    }
                }
                else if (Line != null)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("出错:", ex);
                MessageBox.Show(ex.ToString(), "加载配置错误(请检查配置)");
                Application.Exit();
            }
            return false;
        }
        /// <summary>
        /// 箱子是否可以进入料工位
        /// </summary>
        /// <returns></returns>
        public static bool CanBoxIntoFeeding()
        {
            return Line?.AllEquipMap[101]?.IsIdle() ?? false;
        }
        public static bool IsDisChargeIdle()
        {
            return Line?.AllEquipMap[102]?.IsIdle() ?? false;
        }
        public static void StartDisChargeMove()
        {
            Line?.AllEquipMap[102]?.StartMove();
        }
        public static void StartStationInStore(InOutParam inOutParam)
        {
            if (Line?.AllEquipMap.ContainsKey(inOutParam.DeviceId) ?? false)
            {
                Line?.AllEquipMap[inOutParam.DeviceId].StartInStoreMove(inOutParam);
            }
        }
        /// <summary>
        /// 是否有箱子
        /// </summary>
        /// <returns></returns>
        public static bool IsFeedingHasBox()
        {
            return Line?.AllEquipMap[101]?.BoxCheck() ?? false;
        }

        #region 机器人
        /// <summary>
        /// 是否有机器人空闲:背篓为空且空闲
        /// </summary>
        /// <param name="robotStates"></param>
        /// <returns>true:空闲</returns>
        public static bool RobotIsIdle(RobotStates robotStates)
        {
            try
            {
                if (robotStates != null && robotStates.robots != null)
                {
                    foreach (var item in robotStates.robots)
                    {
                        if (!item.state.Equals("IDLE")) continue;
                        var fullTray = item.trays.FindAll(s => s.containerCode != null);
                        if (fullTray == null || fullTray.Count == 0)
                        {
                            LogUtil.info($"机器人空闲且背篓为空,允许发送出库任务:【{JsonHelper.SerializeObject(item)}】");
                            return true;
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                LogUtil.error("RobotTrayIsEmpty", ex);
            }
            return false;
        }
        static Dictionary<string, List<Point>> AgvPoints = new Dictionary<string, List<Point>>();
        public static void UploadAGVStatus(RobotStates robotStates)
        {
            try
            {
                if (robotStates != null && robotStates.robots != null)
                {
                    List<AgvStatus> agvs = new List<AgvStatus>();
                    for (int j = 0; j < robotStates.robots.Count; j++)
                    {
                        var item = robotStates.robots[j];
                        AgvStatus status = new AgvStatus();
                        status.elec = item.energyLevel.ToString();
                        status.name = item.robotCode;
                        status.type = j + 1;
                        foreach (var key in AgvPoints.Keys)
                        {
                            var find = AgvPoints[key].Find(s => s.X.Equals(item.positionX) && s.Y.Equals(item.positionY));
                            if (find != null)
                            {
                                status.loc = key;
                                break;
                            }
                        }
                        if (string.IsNullOrEmpty(status.loc))
                        {
                            status.loc = "WAREHOUSE";
                        }
                        agvs.Add(status);
                    }
                    SServerManager.UpdateAgvStatus(agvs);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error($"UploadAGVStatus:{JsonHelper.SerializeObject(robotStates)}", ex);
            }
        }
        /// <summary>
        /// 检查机器人背篓是否是入库容器
        /// </summary>
        /// <param name="robotStates"></param>
        /// <returns>true:是入库容器</returns>
        public static bool CheckRobotTrayIsInstore(RobotStates robotStates)
        {
            try
            {
                if (robotStates != null && robotStates.robots != null)
                {
                    foreach (var item in robotStates.robots)
                    {
                        var fullTray = item.trays.FindAll(s => s.containerCode != null);
                        if (fullTray != null)
                        {
                            foreach (var tray in fullTray)
                            {
                                var instore = ContainerManager.GetTrayInfo(tray.containerCode);
                                if (instore != null && instore.InOrOutStore.Equals(ContainerType.InStore))
                                {
                                    LogUtil.info($"机器人背篓有入库容器,允许发送出库任务:【{JsonHelper.SerializeObject(item)}】【{JsonHelper.SerializeObject(instore)}】");
                                    return true;
                                }
                            }
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                LogUtil.error("CheckRobotTrayIsInstore", ex);
            }
            return false;
        }
        static int taskCnt = ConfigAppSettings.GetIntValue("TaskCnt");
        /// <summary>
        /// 检查容器出库任务数是否超限
        /// </summary>
        /// <returns></returns>
        public static bool CheckTaskCntIsInLimit()
        {
            var outTask = ContainerManager.getTrayList().FindAll(s => s.InOrOutStore.Equals(ContainerType.OutStore));
            if (taskCnt > 0)
                return outTask?.Count <= taskCnt;//30
            else
                return outTask?.Count <= 8;//30
        }
        #endregion
    }
}