BoardManager.cs
10.5 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
using log4net;
using URSoldering.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
namespace URSoldering.DeviceLibrary
{
public class BoardManager
{
private static int MaxId = 0;
/// <summary>
/// 板子列表
/// </summary>
public static List<BoardInfo> boardList = new List<BoardInfo>();
public static Image defaultImage = null;
/// <summary>
/// 当前正在使用的板子
/// </summary>
public static BoardInfo CurrBoard = null;
public static int CurrBoardId = ConfigAppSettings.GetIntValue(Setting_Init.Default_BoardID);
public static void UpdateCurrBoard(int boardID)
{
foreach (BoardInfo obj in boardList)
{
if (obj.boardId.Equals(boardID))
{
CurrBoard = obj;
CurrBoardId = boardID;
LogUtil.info( "切换到新板子:" + CurrBoard.boardName);
break;
}
}
}
public static int GetNextId()
{
MaxId++;
return MaxId;
}
public static string GetConfigPath()
{
string appPath = Application.StartupPath;
string filePath = appPath + ConfigAppSettings.GetValue(Setting_Init.Board_ConfigPath);
return filePath;
}
public static void LoadBoard()
{
CurrBoardId = ConfigAppSettings.GetIntValue(Setting_Init.Default_BoardID);
string filePath = GetConfigPath();
if (!File.Exists(filePath))
{
LogUtil.error("加载产品失败:文件[" + filePath + "]不存在");
return;
}
string[] lines = File.ReadAllLines(filePath, Encoding.GetEncoding("gbk"));
boardList = new List<BoardInfo>();
List<int> idList = new List<int>();
List<string> nameList = new List<string>();
foreach (string str in lines)
{
try
{
string newStr = str;
string errMsg="\"System.Drawing.Bitmap\"";
if (str.Contains(errMsg))
{
newStr = str.Replace(errMsg, "null");
}
BoardInfo board = JsonHelper.DeserializeJsonToObject<BoardInfo>(newStr);
bool isUpdate = false;
if (board != null)
{
if (board.boardId > MaxId)
{
MaxId = board.boardId;
}
if (idList.Contains(board.boardId))
{
LogUtil.error("加载产品配置出错:id=" + board.boardId + "的数据重复!");
}
if (nameList.Contains(board.boardName))
{
LogUtil.error("加载产品配置出错:name=" + board.boardName + "的数据重复!");
}
//如果料号是空,用产品名称作为料号
if(board.PartNumber==null){
board.PartNumber = board.boardName;
isUpdate = true;
}
if (board.WareCode == null)
{
board.WareCode = "";
}
idList.Add(board.boardId);
nameList.Add(board.boardName);
if (board.boardId.Equals(CurrBoardId))
{
LogUtil.info("加载产品配置:name=" + board.boardName + "为启动时的默认板子!");
CurrBoard = board;
}
//if (board.orgType == 0)
//{
// board.orgType = 1;
//}
boardList.Add(board);
}
if (isUpdate)
{
SaveListToFile(boardList);
}
}
catch (Exception ex)
{
LogUtil.error("出错:" + ex.ToString());
}
}
string path = ConfigAppSettings.GetValue(Setting_Init.BOARD_IMAGE_PATH);
string imagePath = Application.StartupPath + "/" + path + "/" + ConfigAppSettings.GetValue(Setting_Init.BOARD_IMAGE_DEFAULT);
if ( File.Exists(imagePath))
{
defaultImage = Image.FromFile(imagePath);
}
else
{
LogUtil.error("未找到默认图片:"+imagePath);
}
//如果有大于一块板子,必须有默认的电路板
try
{
if (CurrBoard == null && boardList.Count > 0)
{
int id = boardList[0].boardId;
UpdateCurrBoard(id);
ConfigAppSettings.SaveValue(Setting_Init.Default_BoardID, boardList[0].boardId);
LogUtil.info("未找到默认的电路板,更新第一块电路板 id=【" + id + "】为默认电路板");
}
}
catch (Exception ex)
{
LogUtil.error("设置默认的电路板出错:" + ex.ToString());
}
}
public static void Update(BoardInfo board)
{
int index = 0;
board.myImage = null;
foreach (BoardInfo bo in boardList)
{
if (bo.boardId.Equals(board.boardId))
{
boardList[index] = board;
break;
}
index++;
}
SaveListToFile(boardList);
}
public static void Add(BoardInfo board)
{
boardList.Add(board);
SaveListToFile(boardList);
}
private static bool SaveListToFile(List<BoardInfo> list)
{
string[] lines = new string[boardList.Count];
int index = 0;
foreach (BoardInfo bo in list)
{
lines[index] = JsonHelper.SerializeObject(bo);
index++;
}
string filePath = GetConfigPath();
return FileUtil.SaveListToFile(lines, filePath);
}
public static void Delete(BoardInfo board)
{
boardList.Remove(board);
string[] lines = new string[boardList.Count];
int index = 0;
if (board.boardId.Equals(CurrBoardId))
{
CurrBoardId = 0;
CurrBoard = null;
ConfigAppSettings.SaveValue(Setting_Init.Default_BoardID, 0);
}
foreach (BoardInfo bo in boardList)
{
if (CurrBoardId.Equals(0))
{
ConfigAppSettings.SaveValue(Setting_Init.Default_BoardID, bo.boardId);
UpdateCurrBoard(bo.boardId);
}
lines[index] = JsonHelper.SerializeObject(bo);
index++;
}
SaveListToFile(boardList);
}
public static bool HasId(int boardId)
{
foreach (BoardInfo board in boardList)
{
if (board.boardId.Equals(boardId))
{
return true;
}
} return false;
}
public static BoardInfo getBoardById(int PreboardId)
{
foreach (BoardInfo board in boardList)
{
if (board.boardId.Equals(PreboardId))
{
return board;
}
}
return null;
}
public static BoardInfo getBoardByName(string name)
{
foreach (BoardInfo board in boardList)
{
if (board.boardName.Equals(name))
{
return board;
}
}
return null;
}
public static BoardInfo getBoardByCode(string code)
{
List<BoardInfo> list = (from m in boardList where m.WareCode.Equals(code) select m).ToList<BoardInfo>();
if (list.Count > 0)
{
return list[0];
}return null;
}
public static Dictionary<int, BoardInfo> GetBoardMapByPartNum(string partNum)
{
Dictionary<int, BoardInfo> boardMap = new Dictionary<int, BoardInfo>();
List<BoardInfo> list = (from m in boardList where m.PartNumber.Equals(partNum) orderby m.StationNum ascending select m).ToList<BoardInfo>();
foreach (BoardInfo board in list)
{
if (!boardMap.ContainsKey(board.StationNum))
{ boardMap.Add(board.StationNum, board); }
}
return boardMap;
}
public static bool CanUpdate(string oldPartNum, string newPartNum)
{
foreach (BoardInfo board in boardList)
{
if (!board.PartNumber.Equals(oldPartNum))
{
if (board.PartNumber.Equals(newPartNum))
{
return false;
}
}
} return true;
}
/// <summary>
/// 根据一个产品的ID获取此产品在指定编码的程序
/// </summary>
/// <param name="BoardId"></param>
/// <param name="LocationNum"></param>
/// <returns></returns>
public static BoardInfo GetSolderingBoard(string partNum, int LocationNum)
{
Dictionary<int, BoardInfo> map = GetBoardMapByPartNum(partNum);
if (map.ContainsKey(LocationNum))
{
return map[LocationNum];
}
return null;
}
/// <summary>
/// 根据料号和编码来找产品
/// </summary>
/// <param name="newPart"></param>
/// <param name="newStation"></param>
/// <returns></returns>
public static BoardInfo getBoard(string partNum, int stationNum)
{
Dictionary<int, BoardInfo> boardMap = new Dictionary<int, BoardInfo>();
List<BoardInfo> list = (from m in boardList where m.PartNumber.Equals(partNum)&&m.StationNum.Equals(stationNum)
orderby m.StationNum ascending select m).ToList<BoardInfo>();
if (list.Count > 0)
{
return list[0];
}
return null;
}
}
}