AlgoPNMatch.cs 4.4 KB
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OnlineStore.Common
{
    public class Pn_Algo_Match
    {
        static Dictionary<string, AlgoData> algopnList_v2;
        const string algopnFile = "Config\\algopn.txt";
        const string algopnFile_v2 = "Config\\algopn_V2.txt";
        static Pn_Algo_Match()
        {
            algopnList_v2 = new Dictionary<string, AlgoData>();
            LoadFile();
        }
        public static void LoadFile()
        {
            if (File.Exists(algopnFile) && !File.Exists(algopnFile_v2))
            {
                UpgradeV1toV2();
            }

            if (!File.Exists(algopnFile_v2))
            {
                SaveFile();
                return;
            }
            var ls = File.ReadAllText(algopnFile_v2);
            algopnList_v2 = JsonConvert.DeserializeObject<Dictionary<string, AlgoData>>(ls);
            if (algopnList_v2 == null)
                algopnList_v2 = new Dictionary<string, AlgoData>();

        }
        static void SaveFile()
        {
            Directory.CreateDirectory(Path.GetDirectoryName(algopnFile_v2));
            File.WriteAllText(algopnFile_v2 + ".ready", JsonConvert.SerializeObject(algopnList_v2));
            try
            {
                File.Delete(algopnFile_v2 + ".bck");
                File.Move(algopnFile_v2, algopnFile_v2 + ".bck");
                File.Move(algopnFile_v2 + ".ready", algopnFile_v2);
            }
            catch { }
        }

        static void UpgradeV1toV2()
        {
            var ls = File.ReadAllText(algopnFile);
            var algopnList = JsonConvert.DeserializeObject<Dictionary<string, string>>(ls);
            if (algopnList == null)
            {
                SaveFile();
                return;
            }
            foreach (var k in algopnList.Keys.ToArray())
            {
                AlgoData algoData = new AlgoData();
                algoData.PN = k;
                var d = algopnList[k].Split(',');
                algoData.Algo = d[0];
                if (d[0] == "IP_Template_PARTS" && d.Length > 1)
                {
                    algoData.TemplateFile = d[1];
                    if (d.Length > 3)
                        algoData.fine = double.Parse(d[3]);
                }
                else if (d.Length > 2)
                    algoData.fine = double.Parse(d[2]);

                if (!algopnList_v2.ContainsKey(k))
                    algopnList_v2.Add(k, algoData);
            }
            SaveFile();
        }
        public static void Set(string pn, string value, int? processlevel = null, double? finetuning = null)
        {
            if (string.IsNullOrEmpty(pn))
                return;
            if (string.IsNullOrEmpty(value))
                value = "auto";
            AlgoData algodata;
            if (algopnList_v2.ContainsKey(pn))
                algodata = algopnList_v2[pn];
            else
            {
                algodata = new AlgoData();
                algodata.PN = pn;
            }
            algodata.Algo = value;
            if (processlevel.HasValue)
                algodata.processlevel = processlevel.Value;
            if (finetuning.HasValue)
                algodata.fine = finetuning.Value;

            LogUtil.info($"set pnalgo pn:{pn}, algo:{value}, processlevel:{processlevel}, finetuning:{finetuning}");

            if (algopnList_v2.ContainsKey(pn))
                algopnList_v2[pn] = algodata;
            else
                algopnList_v2.Add(pn, algodata);

            SaveFile();
        }
        public static bool MatchPN(string pn, out AlgoData AlgoData)
        {
            if (algopnList_v2.ContainsKey(pn))
            {
                AlgoData = algopnList_v2[pn];
                return true;
            }
            else
            {
                AlgoData = new AlgoData() { Algo = "auto" };
                return false;
            }
        }
    }
    [Serializable]
    public class AlgoData
    {
        public string PN { get; set; }
        public string Algo { get; set; }
        public string TemplateFile { get; set; }
        public double fine { get; set; }
        public double processlevel { get; set; }
        public override string ToString()
        {
            return $"PN:{PN},{Algo},{TemplateFile},{processlevel},{fine}";
        }
    }
}