ParamManager.cs 10.9 KB
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.Tasks;
using System.Windows.Forms;
using static OnlineStore.DeviceLibrary.CountParam;

namespace OnlineStore.DeviceLibrary
{
    public class ParamManager{

        public static   string NoConfigPath = @"D:\NoConfigImg\";
        private static  Dictionary<string, CountParam> countParamMap = new Dictionary<string, CountParam>();
        private static  string ConfigFilePath = "";
        public static void Init()
        {
            ConfigFilePath = Application.StartupPath + ConfigAppSettings.GetValue(Setting_Init.CounParamConfig);
            countParamMap = LoadParamMap();
        }
        public static string[] GetPNlist() {
            return countParamMap.Keys.ToArray();
        }
        public static string GetSufanStr(int type)
        {
            SignType sign=SignType.AUTO;
            Enum.TryParse<SignType>(type.ToString(), out sign);
            return GetSufanStr(sign);
        }
            public static string GetSufanStr(SignType type)
        { 
            if (type.Equals(0))
            {
                return "算法A";
            }
            else if (type.Equals(1))
            {
                return "算法B";
            }
            else if (type.Equals(2))
            {
                return "算法C";
            }
            else if (type.Equals(3))
            {
                return "算法D";
            }
            else if (type.Equals(4))
            {
                return "算法E";
            } 
            return "默认算法";
        }
        public  static  CountParam GetParamByPN(string pn)
        { 
            if (countParamMap.ContainsKey(pn))
            {
                CountParam lastParam = countParamMap[pn];
                LogUtil.debug("【" + pn + "】使用参数 " + lastParam.ToStr() + "  ");
                return lastParam;
            }
            return null;
        }
        public static CountParam GetParamByCode(string codeStr)
        {
            //bool result = false;
            //CountParam     lastParam = new CountParam("", ThresholdValue, 3); 
            string pn = GetCodeStrPN(codeStr);
            var pns = pn.Split('.');
            if (pns.Length == 3)
                pn = pns[0] +"."+ pns[2];
            if (countParamMap.ContainsKey(pn))
            {
                CountParam lastParam = countParamMap[pn];
                LogUtil.debug("【" + codeStr + "】使用参数 " + lastParam.ToStr() + "  ");
                return lastParam;
            }
            return null;
        }
        internal static  string GetCodeStrPN(string codeStr)
        {
            string[] codeArray = codeStr.Split(';');
            if (codeArray.Length.Equals(2))
            {
                string pn = codeArray[0];
                return pn;
            }
            return "";
        }

        private static List<CountParam> ErrPnParamList = new List<CountParam>();
        public static void AddErrPNParam(CountParam errParam)
        {

        }

        public static void UpdateParam(CountParam param)
        {
            try
            {
                OldPNList.AddOne(param.PN);
                if (countParamMap.ContainsKey(param.PN))
                {
                    countParamMap[param.PN] = param;
                }
                else
                {
                    countParamMap.Add(param.PN, param);
                }
                LogUtil.info("UpdateParam   " + param.ToStr() + " ");
            }
            catch (Exception ex)
            {
                LogUtil.error("UpdateParam 出错:" + ex.ToString());
            }
        }

        public static void SaveMapToFile()
        {
            try
            {
                List<CountParam> pList = new List<CountParam>(countParamMap.Values);
                int length = pList.Count + 1;
                List<string> lineList = new List<string>();
                lineList.Add(CountParam.GetCSVTitle());
                foreach (CountParam p in pList)
                {
                    lineList.Add(p.ToCSVStr());
                }

                File.WriteAllLines(ConfigFilePath, lineList);
                LogUtil.info("SaveMapToFile  【" + ConfigFilePath + "】");

                CSVBomManager.BackFile(ConfigFilePath, lineList.ToArray());
            }
            catch (Exception ex)
            {
                LogUtil.error("SaveMapToFile 出错:" + ex.ToString());
            }
        }

        internal static Dictionary<string, CountParam> LoadParamMap( )
        {
            Dictionary<string, CountParam> countParamMap = new Dictionary<string, CountParam>();

            if (File.Exists(ConfigFilePath))
            {
                try
                {
                    string[] lines = File.ReadAllLines(ConfigFilePath);
                    int index = -1;
                    foreach (string line in lines)
                    {
                        index++;
                        if (index.Equals(0))
                        {
                            continue;
                        }
                        CountParam param = CountParam.NewParam(line);
                        if (param != null)
                        {
                            if (!countParamMap.ContainsKey(param.PN))
                            {
                                countParamMap.Add(param.PN, param);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogUtil.error("解析文件【" + ConfigFilePath + "】出错:" + ex.ToString());
                }
                LogUtil.info("加载点料参数共【" + countParamMap.Count + "】条有效数据" + ConfigFilePath);
            }
            else
            {
                LogUtil.error("未找到文件:" + ConfigFilePath);
            }
            return countParamMap;
        }
        /// <summary>
        /// 获取最优算法
        /// </summary>
        /// <param name="currPN"></param>
        /// <returns></returns>
        public static CountParam GetOptimalParamByPN(string currPN)
        {
            CountParam param = GetParamByPN(currPN);
            if (param == null)
            {
                //TODO  
                ComponetInfo com = CSVBomManager.GetCom(currPN);
                if (com == null)
                {
                    LogUtil.info("元器件库未找到 PN=" + currPN + " 的元器件");
                    return param;
                }

                List<ComponetInfo> FZList = new List<ComponetInfo>();
                Dictionary<int, int> sufaCount = new Dictionary<int, int>();

                SignType maxSufa = SignType.AUTO;
                int maxCount = 0;
                foreach (ComponetInfo obj in CSVBomManager.allComMap.Values)
                {
                    if (obj.Encapsulations.Trim().Equals(com.Encapsulations.Trim()))
                    {
                        CountParam p = GetParamByPN(obj.PartNum);
                        if (p != null)
                        {
                            obj.Sufan = (int)p.Sign;

                            if (sufaCount.ContainsKey(obj.Sufan))
                            {
                                sufaCount[obj.Sufan]++;
                            }
                            else
                            {
                                sufaCount.Add(obj.Sufan, 1);
                            }

                            if (sufaCount[obj.Sufan] > maxCount)
                            {
                                maxCount = sufaCount[obj.Sufan];
                                Enum.TryParse<SignType>(obj.Sufan.ToString(),out maxSufa);
                            }
                        }
                        FZList.Add(obj);
                    }
                }

                if (maxCount >= 1)
                {
                    LogUtil.info("元器件【" + com.ToStr() + "】查找到最优算法:【" + maxSufa + "】");
                    param = new CountParam(currPN, RobotManager.robot.XrayBean.ThresholdValue, 3, maxSufa);
                } 
            }
            return param;
        }
        public static Image FormImage(string fileName)
        {
            try
            {
                Image img = Image.FromFile(fileName);
                return img;
            }
            catch (Exception ex)
            {
                LogUtil.info(ex.ToString());
            }
            return null;
        }
    }
    public class CountParam
    {
        public CountParam(string pn, int th = 0, int size = 0, SignType sign=SignType.AUTO, int value = 0)
        {
            var pns = pn.Split('.');
            if (pns.Length == 3)
                pn = pns[0] + "." + pns[2];
            this.PN = pn;
            this.Threshold = th;
            this.WindowSize = size;
            this.Sign = sign;
            this.Value = value;
        }
        public string PN = "";
        public int Threshold = 0;
        public int WindowSize = 0;
        public string AreaValue = "";
        public SignType Sign = 0;
        public int Value = 0;

        public string ToStr()
        {
            return "【PN=" + PN + ",sign=" + Sign.ToString()+ ",value=" + Value + ",th=" + Threshold + ",wsize=" + WindowSize + "】";
        }
        public string ToCSVStr()
        {
            //type,sign,value,threshold,windowSize
            return PN + spilt + Sign + spilt + Value + spilt + Threshold + spilt + WindowSize;
        }
        private static char spilt = ',';
        internal static CountParam NewParam(string line)
        {
            try
            {
                string[] array = line.Split(spilt);
                if (array.Length >= 5)
                {
                    string pn = array[0].Trim();
                    var pns = pn.Split('.');
                    if (pns.Length == 3)
                        pn = pns[0] + "." + pns[2];
                    SignType sign = SignType.AUTO;
                    Enum.TryParse<SignType>(array[1].Trim(), out sign);
            
                    int value = Convert.ToInt32(array[2].Trim());
                    int th = Convert.ToInt32(array[3].Trim());
                    int size = Convert.ToInt32(array[4].Trim());
                    return new CountParam(pn, th, size, sign, value);
                }
            }
            catch (Exception ex)
            {
            }

            return null;
        }
        public enum SignType
        {
            IP_SMALL_PARTS=0,
            IP_LARGE_PARTS = 1,
            IP_LONG_PARTS = 2,
            IP_SQUARE_PARTS = 3,
            IP_GEN_PARTS = 4,
            AUTO = 99,
        }
        internal static string GetCSVTitle()
        {
            return "type,sign,value,threshold,windowSize";
        }
    }
}