AlgoPNMatch.cs
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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";
public static Dictionary<string, AlgoData> List { get => algopnList_v2; }
public static event EventHandler<string[]> OnloadAlgoPN;
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>();
OnloadAlgoPN?.Invoke(null, algopnList_v2.Keys.ToArray());
}
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}";
}
}
}