StoreManager.cs 10.7 KB
using log4net;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OnlineStore.DeviceLibrary
{
    public class StoreManager
    {
        public static int OutMoveId = 7;
        public static bool TrayToOutLineTest = false;

        public static bool DisGetWare = false;

        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 XLRStoreBean XLRStore = null;
        public static XLRStore_Config Config = null;
        public static Dictionary<int, DeviceConfig> allConfigMap = null;
        public StoreManager()
        {
        }
        public static bool CheckEnum(Type type)
        {
            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);
                }
            }
            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(StepEnum)))
                    {
                        return false;
                    }
                    if (!CheckEnum(typeof(DeviceStatus)))
                    {
                        return false;
                    }
                    if (!CheckEnum(typeof(RunStatus)))
                    {
                        return false;
                    }

                    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>();



                    allConfigMap = new Dictionary<int, DeviceConfig>();
                    isInit = true;
                    LogUtil.info("  开始加载配置");

                    string appPath = Application.StartupPath;
                    string CID = ConfigAppSettings.GetValue(Setting_Init.Line_CID);
                    string storeConfigPath = appPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_XLRStore);

                    Config = CSVConfigReader.LoadBoxConfig(0, CID, DeviceType.Store, storeConfigPath);
                    allConfigMap.Add(0, Config);
                    string inputConfigPath = appPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_Input);

                    InputEquip_Config inputConfig = CSVConfigReader.LoadInputConfig(1, DeviceType.InputEquip, inputConfigPath);
                    inputConfig.CID = CID;
                    inputConfig.SetIO(1);
                    allConfigMap.Add(1, inputConfig);
                    string boxPath = appPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_Box);

                    BoxEquip_Config boxConfig = CSVConfigReader.LoadBoxConfig(2, DeviceType.BoxEquip, boxPath);
                    boxConfig.SetIO(2);
                    boxConfig.CID = CID;
                    allConfigMap.Add(2, boxConfig);
                    //加载点位
                    string positionConfigFile = appPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_BoxPosition);
                    CSVPositionReader<BoxPosition>.AddCSVFile(positionConfigFile);

                    XLRStore = new XLRStoreBean(Config, inputConfig, boxConfig);
                    LogUtil.info("加载 完成!");
                    return true;
                }

                else if (XLRStore != null)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("出错:", ex);
                MessageBox.Show(ex.ToString(), "加载配置错误(请检查配置)");
                Application.Exit();
            }
            return false;
        }

        public static bool checkWatch(Stopwatch watch, int targetMs, bool isStop = true)
        {
            if (!watch.IsRunning)
            {
                watch.Restart();
                return false;
            }
            else if (watch.ElapsedMilliseconds >= targetMs)
            {
                if (isStop)
                {
                    watch.Stop();
                }
                return true;
            }
            return false;
        }

        public static bool SaveBoxConfig(BoxEquip_Config config)
        {
            try
            {
                //位置配置到文件中
                string appPath = Application.StartupPath;
                string boxConfig = appPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_Box);

                allConfigMap[config.Id] = config;

                bool result = CSVConfigReader.SaveConfig(boxConfig, config, typeof(BoxEquip_Config));
                if (!result)
                {
                    LogUtil.error("保存配置文件失败:" + boxConfig);
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                LogUtil.error("出错:", ex);
                return false;
            }
        }
        public static void SaveInputEquipConfig(InputEquip_Config config)
        {
            try
            {
                //位置配置到文件中
                string appPath = Application.StartupPath;
                string configFile = appPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_Input);

                allConfigMap[config.Id] = config;
                bool result = CSVConfigReader.SaveConfig(configFile, config, typeof(InputEquip_Config));
                if (!result)
                {
                    LogUtil.error("保存配置文件失败:" + configFile);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("出错:", ex);
            }
        }


        private static List<int> trayHeightList = new List<int>();
        public static List<int> GetTrayList()
        {
            if (trayHeightList.Count <= 0)
            {
                string config = ConfigAppSettings.GetValue(Setting_Init.TrayHeightList);
                string[] array = config.Split(';');
                foreach (string s in array)
                {
                    try
                    {
                        int v = Convert.ToInt32(s.Trim());
                        if (v > 0)
                        {
                            trayHeightList.Add(v);
                        }
                    }
                    catch (Exception ex)
                    {

                    }
                }
                if (trayHeightList.Count <= 0)
                {
                    trayHeightList = new List<int>() { 8, 12, 16, 20, 24, 28, 36, 48 };
                }
            }
            return trayHeightList;
        }


        private static List<string> PnList = null;
        public static List<string> GetPnList()
        {
            if (PnList == null)
            {
                PnList = new List<string>();
                string config = ConfigAppSettings.GetValue(Setting_Init.PNList);
                string[] array = config.Split('#');
                foreach (string s in array)
                {
                    if (!String.IsNullOrEmpty(s))
                    {
                        PnList.Add(s);
                    }
                }
            }
            return PnList;
        }
        public static void UpdatePnList()
        {
            if (PnList == null)
            {
                return;
            }
            string str = "";
            foreach (string obj in PnList)
            {
                str += obj + "#";
            }
            ConfigAppSettings.SaveValue(Setting_Init.PNList, str);
            LogUtil.info("更改脆盘料号集合:" + str);
        }
        public static void RemovePN(string pn)
        {
            if (PnList.Contains(pn))
            {
                PnList.Remove(pn);
                UpdatePnList();
            }
        }
        public static void AddPN(string pn)
        {
            if (!PnList.Contains(pn))
            {
                PnList.Add(pn);
                UpdatePnList();
            }
        }

        public static bool IsCrispReel(params string[] codes)
        {
            try
            {
                List<string> list = GetPnList();
                foreach (string code in codes)
                {
                    string[] array = code.Split('*');
                    if (array.Length >= 5)
                    {
                        string codePn = array[1];
                        foreach (string pn in list)
                        {
                            if (codePn.Equals(pn))
                            {
                                LogUtil.info("IsCrispReel:条码[" + code + "]料号[" + pn + "]是脆盘");
                                return true;
                            }
                            else if (codePn.StartsWith(pn))
                            {
                                LogUtil.info("IsCrispReel:条码[" + code + "]料号[" + pn + "]认为是脆盘");
                                return true;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("IsCrispReel 出错:" + ex.ToString());
            }
            return false;
        }
        public static bool NeedWaitHassReel(params string[] codes)
        {
            return true;
            //return !IsCrispReel(codes);
        }
    }
}