MainMachine_SelfAudit.cs
14.4 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using OnlineStore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CodeLibrary;
using System.Security.Cryptography;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web;
using System.Xml.Linq;
namespace DeviceLibrary
{
/// <summary>
/// 盘点功能
/// </summary>
partial class MainMachine
{
public string selfAuditNo = "";
public SelfAuditPosInfo currSAPos = null;
public bool StartSelfAudit(string selfAuditNo,out string errmsg)
{
errmsg = "";
if (!boxTransport.IsComplateOrFree)
{
errmsg = crc.GetString("Res0002", "料仓忙碌中,无法启动");
return false;
}
SelfAuditMoveInfo.NextMoveStep(MoveStep.SA01_Start);
return true;
}
Task<SelfAuditPosInfo> GetSAPosTask = null;
Task<string > UpdateResTask = null;
private void SA02_GetPos()
{
SelfAuditMoveInfo.NextMoveStep(MoveStep.SA02_GetPos);
SelfAuditMoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
SelfAuditMoveInfo.log("开始获取盘点库位号");
try
{
GetSAPosTask = Task.Run(new Func<SelfAuditPosInfo>(() =>
{
currSAPos = ServerCM.getNetPos(Name, out string msg);
return currSAPos;
}));
}
catch (Exception ex)
{
LogUtil.error("SA02_GetPos 获取判断库位号出错:", ex);
}
}
private void SA07_UpdateRes()
{
SelfAuditMoveInfo.NextMoveStep(MoveStep.SA07_UpdateRes);
SelfAuditMoveInfo.WaitList.Add(WaitResultInfo.WaitTime(100));
SelfAuditMoveInfo.log($"盘点{currSAPos.batchNo},库位号{currSAPos.posName},库存条码{currSAPos.barcode},实际条码{currSAPos.actBarcode},上传结果给服务器");
try
{
UpdateResTask = Task.Run(new Func<string >(() =>
{
string msg= ServerCM.posValidationEnd(Name, currSAPos.posName,currSAPos.barcode,currSAPos.actBarcode );
return msg;
}));
}
catch (Exception ex)
{
LogUtil.error("SA07_UpdateRes 上传结果出错:", ex);
}
}
private bool InSelfAudit()
{
return SelfAuditMoveInfo.MoveStep != MoveStep.Wait;
}
void SelfAuditProcess()
{
if (CheckWait(SelfAuditMoveInfo))
return;
switch (SelfAuditMoveInfo.MoveStep)
{
case MoveStep.Wait:
if (selfAuditNo != "")
{
if( StartSelfAudit(selfAuditNo, out string errMsg))
{
SelfAuditMoveInfo.log("启动判断成功,批次号:" + selfAuditNo);
}
else
{
SelfAuditMoveInfo.log("启动盘点失败,批次号:" + selfAuditNo+", "+errMsg);
}
}
break;
case MoveStep.SA01_Start:
SA02_GetPos();
break;
case MoveStep.SA02_GetPos:
if(GetSAPosTask== null|| GetSAPosTask.IsCompleted)
{
if(currSAPos==null||currSAPos.posName=="")
{
SelfAuditMoveInfo.log($"未获取到盘点库位号,结束处理");
SelfAuditMoveInfo.EndMove();
}
else
{
ACStorePosition position = CSVPositionReader<ACStorePosition>.GetPositon(currSAPos.posName);
if (position == null)
{
SelfAuditMoveInfo.log($"盘点库位号{currSAPos.posName},未找到库位信息,结束处理");
SelfAuditMoveInfo.EndMove();
}
else
{
SelfAuditMoveInfo.NextMoveStep(MoveStep.SA03_WaitToPos);
SelfAuditMoveInfo.log("准备取目的库位");
}
}
}else if (SelfAuditMoveInfo.IsTimeOut())
{
Msg.add(crc.GetString(L.wait_getselfauditPos, "等待获取盘点库位号超时"), MsgLevel.warning);
SelfAuditMoveInfo.log($"等待获取盘点库位号超时");
}
break;
case MoveStep.SA03_WaitToPos:
ACStorePosition pos = CSVPositionReader<ACStorePosition>.GetPositon(currSAPos.posName);
bool result= boxTransport.Start(null, new BoxStorePosition(Config, pos, new ReelParam(currSAPos.barcode)), StoreMoveType.SelfAudit);
if (result)
{
SelfAuditMoveInfo.NextMoveStep(MoveStep.SA04_Pos);
SelfAuditMoveInfo.log("开始取目标库位点");
}
else if(SelfAuditMoveInfo.IsTimeOut())
{
Msg.add(crc.GetString(L.wat_startTosaPos, "等待去盘点库位超时"), MsgLevel.warning);
SelfAuditMoveInfo.log($"等待去盘点库位点超时");
}
break;
case MoveStep.SA04_Pos:
if (boxTransport.IsComplateOrFree)
{
SelfAuditMoveInfo.NextMoveStep(MoveStep.SA05_InPos);
SelfAuditMoveInfo.log("到达目标位置");
}
else if (SelfAuditMoveInfo.IsTimeOut())
{
Msg.add(crc.GetString(L.wat_waitInsaPos, "等待到达盘点库位超时"), MsgLevel.warning);
SelfAuditMoveInfo.log($"等待到达盘点库位超时");
}
break;
case MoveStep.SA05_InPos:
SelfAuditMoveInfo.NextMoveStep(MoveStep.SA06_Check);
bool checkOk = false ;
string msg = GetSelfAuditImg();
int ret = ReelCheckUtil.ReelCheck(SelfAuditMoveInfo.Name, msg);
checkOk = (ret == 1);
if (checkOk)
{
currSAPos.actBarcode = currSAPos.barcode;
}
else
{
currSAPos.actBarcode = "";
}
SelfAuditMoveInfo.log($"取图{msg},检测结果{ret}, 默认实际条码="+ currSAPos.actBarcode);
break;
case MoveStep.SA06_Check:
SA07_UpdateRes();
break;
case MoveStep.SA07_UpdateRes:
if(UpdateResTask==null|| UpdateResTask.IsCompleted)
{
SA02_GetPos();
}
break;
case MoveStep.SA08_ToNext:
SelfAuditMoveInfo.NextMoveStep(MoveStep.SA09_Back);
SelfAuditMoveInfo.log("升降轴旋转轴返回待机点");
break;
case MoveStep.SA09_Back:
SelfAuditMoveInfo.NextMoveStep(MoveStep.SA10_End);
SelfAuditMoveInfo.log("结束盘点");
break;
default:
SelfAuditMoveInfo.log($"未找到对应步骤:{SelfAuditMoveInfo.MoveStep}");
break;
}
}
#region 取图
public static Bitmap StreamToBitmap(Stream stream)
{
try
{
BinaryFormatter bf = new BinaryFormatter();
Bitmap bitmap = (Bitmap)bf.Deserialize(stream);
return bitmap;
}
catch (Exception e)
{
LogUtil.error("StreamToBitmap :"+e.ToString());
}
return null;
}
public string GetSelfAuditImg()
{
string caName = Setting_Init.Device_SelfAudit_CameraName;
try
{
if (ipCam == null)
{
ipCam = new IPCamera();
}
string base64 = ipCam.GetImgBase64(caName);
//LogUtil.info(base64);
//if (base64 == "")
//{
// base64 = File.ReadAllText(@"F:\inputStr.txt");
//}
if (base64 != null)
{
//Bitmap bitmap = Base64StringToImage(base64);
//if (bitmap != null)
//{
// string imgName = CodeManager.SaveIoFile("SelfAudit", caName, bitmap);
// LogUtil.info("盘点取图完成,图片保存在:" + imgName);
// return imgName;
//}
//try
//{
// if (base64 != "")
// {
// string f1 = @"D:\inputStr.txt";
// if (File.Exists(f1))
// {
// File.Delete(f1);
// }
// File.WriteAllText(f1, base64);
// }
//}
//catch (Exception ex)
//{
//}
string dump = ParseBase64Str(base64);
//try
//{
// if (dump != "")
// {
// string f2 = @"D:\dump.txt";
// if (File.Exists(f2))
// {
// File.Delete(f2);
// }
// File.WriteAllText(f2, dump);
// }
//}
//catch (Exception ex)
//{
//}
byte[] bt = Convert.FromBase64String(dump);
using (MemoryStream ms = new MemoryStream(bt))
{
Bitmap bmp = new Bitmap(ms);
string imgName = CodeManager.SaveIoFile("SelfAudit", caName, bmp);
LogUtil.info("盘点取图完成,图片保存在:" + imgName);
ms.Close();
return imgName;
//return bmp;
}
}
}
catch (Exception e)
{
LogUtil.error($"{Name} 获取盘点图片出错:" + e.ToString());
}
return "";
}
//public string GetSelfAuditImg()
//{
// string caName = Setting_Init.Device_SelfAudit_CameraName;
// try
// {
// if (ipCam == null)
// {
// ipCam = new IPCamera();
// }
// Stream returnData = ipCam.grabOneImgByStream(caName);
// if (returnData != null)
// {
// Bitmap bitmap = StreamToBitmap(returnData);
// if (bitmap != null)
// {
// string imgName = CodeManager.SaveImageToFile("SelfAudit", caName, bitmap);
// LogUtil.info("盘点取图完成,图片保存在:" + imgName);
// return imgName;
// }
// }
// }
// catch (Exception e)
// {
// LogUtil.error($"{Name} 获取盘点图片出错:" + e.ToString());
// }
// return "";
//}
private string ParseBase64Str(string base64)
{
if (base64 == null) return "";
string dump = base64.Trim('"', '"').Replace("%", "").Replace(",", "").Replace(" ", "+").Replace("\\", "");
if (dump.Length % 4 > 0)
{
if (dump.EndsWith("="))
{
dump = dump.TrimEnd('=');
}
dump = dump.PadRight(dump.Length + 4 - dump.Length % 4, '=');
}
return dump;
}
//base64编码的文本转为图片
private Bitmap Base64StringToImage(string inputStr)
{
try
{
try
{
if (inputStr != "")
{
string f1 = @"D:\inputStr.txt";
if (File.Exists(f1)){
File.Delete(f1);
}
File.WriteAllText(f1, inputStr);
}
}catch( Exception ex)
{
}
string dump = ParseBase64Str(inputStr);
try
{
if (dump != "")
{
string f2 = @"D:\dump.txt";
if (File.Exists(f2))
{
File.Delete(f2);
}
File.WriteAllText(f2, dump);
}
}
catch (Exception ex)
{
}
byte[] bt = Convert.FromBase64String(dump);
using (MemoryStream ms = new MemoryStream(bt))
{
Bitmap bmp = new Bitmap(ms);
ms.Close();
return bmp;
}
}
catch (Exception ex)
{
LogUtil.error("Base64StringToImage 转换失败/nException:" + ex.ToString());
return null;
}
}
#endregion
}
}