ParamManager.cs
7.3 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using OnlineStore.Common;
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;
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 = "";
internal static void Init()
{
ConfigFilePath = Application.StartupPath + ConfigAppSettings.GetValue(Setting_Init.CounParamConfig);
countParamMap = LoadParamMap( );
}
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);
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
{
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 + "】");
}
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
}
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, int sign = 0, int value = 0)
{
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 int Sign = 0;
public int Value = 0;
public string ToStr()
{
return "【PN=" + PN + ",sign=" + Sign + ",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();
int sign = Convert.ToInt32(array[1].Trim());
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;
}
internal static string GetCSVTitle()
{
return "type,sign,value,threshold,windowSize";
}
}
}