FrmMain.cs
17.7 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
using Asa.FaceControl;
using BLL;
using DocumentFormat.OpenXml.Bibliography;
using Model;
using Newtonsoft.Json;
using OnlineStore.Common.util;
using SmartScan.Form;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;
namespace SmartScan
{
public partial class FrmMain : FaceFormNormal
{
private MonitorMouseKeyboard monitor;
private ScanWork scanWork;
public FrmMain()
{
InitializeComponent();
BtnStart.Tag = "not";
}
private bool CheckCamera()
{
if (BLLCommon.config.EnabledCamera)
{
if (!Camera.IsConnected())
{
LblCameraExist.BackColor = Color.Red;
return false;
}
else
{
LblCameraExist.ForeColor = Color.Lime;
return true;
}
}
else
{
LogNet.log.Info("相机已禁用");
LblCameraExist.ForeColor = Color.Lime;
return true;
}
}
private bool CheckIOModule()
{
if (BLLCommon.config.EnabledIO)
{
BLLCommon.ioModule.DI_Changed_Event += IoModule_DI_Changed_Event;
if (BLLCommon.ioModule.IsConn)
{
LogNet.log.Info($"IO模块 {BLLCommon.ioModule.IP} 连接成功");
LblIOExist.ForeColor = Color.Lime;
return true;
}
else
{
LogNet.log.Info($"IO模块 {BLLCommon.ioModule.IP} 没有连接");
LblIOExist.BackColor = Color.Red;
return false;
}
}
else
{
LogNet.log.Info("IO模块已禁用");
LblIOExist.ForeColor = Color.Lime;
return true;
}
}
private void SaveRetrospect(Bitmap labelBmp, string[] barcode, Dictionary<string, string> content)
{
string path = FilePath.RETROSPECT_DIR + string.Format("{0:yyyy-MM-dd}\\", DateTime.Now);
if (!System.IO.Directory.Exists(path))
System.IO.Directory.CreateDirectory(path);
string fileName = string.Format("{0:HHmmssfff}", DateTime.Now);
switch (BLLCommon.config.HistoryImage)
{
case HistoryImage.Original:
if (PicShow.Image != null)
PicShow.Image.Save(path + fileName + "_camera.png", System.Drawing.Imaging.ImageFormat.Png);
if (labelBmp != null)
labelBmp.Save(path + fileName + "_label.png", System.Drawing.Imaging.ImageFormat.Png);
break;
case HistoryImage.Condense:
if (PicShow.Image != null)
{
int w = 1024;
int h = w * PicShow.Image.Height / PicShow.Image.Width;
Bitmap bmp = new(w, h);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(PicShow.Image, new Rectangle(0, 0, w, h), new Rectangle(0, 0, PicShow.Image.Width, PicShow.Image.Height), GraphicsUnit.Pixel);
g.Save();
bmp.Save(path + fileName + "_camera.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
if (labelBmp != null)
labelBmp.Save(path + fileName + "_label.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case HistoryImage.NoImage:
break;
}
//if (workCodeInfo != null)
//{
// object[] obj = new object[workCodeInfo.Count];
// for (int i = 0; i < workCodeInfo.Count; i++)
// {
// Dictionary<string, string> dicCode = new()
// {
// { "X", workCodeInfo[i].Center.X.ToString() },
// { "Y", workCodeInfo[i].Center.Y.ToString() },
// { "Text", workCodeInfo[i].Text }
// };
// obj[i] = dicCode;
// }
Dictionary<string, object[]> dic = new()
{
{ "Code", scanWork.SaveCodeInfo() },
{ "Label", barcode },
{"Content",new object[]{ content } },
};
JavaScriptSerializer serializer = new();
string json = serializer.Serialize(dic);
System.IO.File.WriteAllText(path + fileName + "_code.json", json);
//}
LogNet.log.Info("保存历史记录");
}
private void IoModule_DI_Changed_Event(BLL.Status[] sta)
{
if (sta == null)
LogNet.log.Info("Work IO Status = null");
else if (sta.Length <= BLLCommon.config.IOTouch)
LogNet.log.Info($"Work Status.Length({sta.Length}) <= {BLLCommon.config.IOTouch}");
else if (sta[BLLCommon.config.IOTouch] == BLL.Status.Off)
scanWork.TouchOff();
else
{
LogNet.log.Info("IO模块触发Work");
scanWork.Scan();
}
}
private void Extension_SaveRetrospect(Dictionary<string, string> content)
{
try
{
string str = "追溯内容:";
foreach (string key in content.Keys)
str += string.Format("({0}:{1})", key, content[key]);
LogNet.log.Info(str);
BLLCommon.SCMM.SendData(content);
Bitmap labelBmp = BLLCommon.labelEdit.PrintImage(BLLCommon.config.DefaultPrintLabel, content, out _);
//Common.labelEdit.PrintLast(Common.config.DefaultPrintLabel, Common.config.PrinterName, Common.config.PrintLandscape, content, out string[] barcode);
//LogNet.log.Info(string.Format("打印标签 Label[{0}] Printer[{1}]", Common.config.DefaultPrintLabel, Common.config.PrinterName));
var barcode = content.Values.ToArray();
SaveRetrospect(labelBmp, barcode, content);
}
catch (Exception ex)
{
LogNet.log.Error("Extension_Printing", ex);
}
}
string dir_Res = ConfigHelper.Config.Get("DirReelResult", ".\\ReelResult");
string reelResultFileNameKey = ConfigHelper.Config.Get("FileNameKeyReelResult", "");
/// <summary>
/// 保存检测结果
/// </summary>
/// <param name="content"></param>
void SaveResult(Dictionary<string, string> content)
{
//if (string.IsNullOrEmpty(dir_Res))
// return;
//if(!Directory.Exists(dir_Res))
// Directory.CreateDirectory(dir_Res);
//string filename = "";
//if (string.IsNullOrEmpty(reelResultFileNameKey))
//{
// filename = dir_Res + DateTime.Now.ToString() + ".csv";
//}
//else
//{
// filename = dir_Res + content[reelResultFileNameKey] + ".csv";
//}
//StringBuilder sb = new StringBuilder();
//sb.AppendLine("ReelID,PartNumber,Vendor,Lot,UserData1,UserData2,UserData3,UserData4,UserData5,InitialQuantity,MSDLevel,MSDInitialFloorTime ,MSDBagSealDate,MarketUsage,QuantityOverride,ShelfTime,SPMaterialName,WarningLimit,MaximumLimit,Comments,WarmupTime,StorageUnit,SubStorageUnit,LocationOverride,ExpirationDate,ManufacturingDate,PartClass,PSDOverride,AltPartNumber");
//sb.AppendLine($"1,1005C,,,,,,,,2,,,,,1,,,,,,,SMT,,,,,,,");
////sb.AppendLine(string.Join(",", content.Keys.ToArray()));
////sb.AppendLine(string.Join(",", content.Values.ToArray()));
//System.IO.File.WriteAllText(filename, sb.ToString(), Encoding.UTF8);
}
private void Extension_Printing(Dictionary<string, string> content)
{
string str = "打印内容:";
try
{
foreach (string key in content.Keys)
{
str += string.Format("({0}:{1})", key, content[key]);
}
LogNet.log.Info(str);
SaveResult(content);
//Bitmap labelBmp = Common.labelEdit.PrintImage(Common.config.DefaultPrintLabel, content, out _);
BLLCommon.labelEdit.PrintLast(BLLCommon.config.DefaultPrintLabel, BLLCommon.config.PrinterName, BLLCommon.config.PrintLandscape, content, out string[] barcode);
LogNet.log.Info(string.Format("打印标签 Label[{0}] Printer[{1}]", BLLCommon.config.DefaultPrintLabel, BLLCommon.config.PrinterName));
var bmp = BLLCommon.labelEdit.PrintImage(BLLCommon.config.DefaultPrintLabel, content, out _);
if (bmp != null)
_ =UnifiedDataHandler.PostSmfImageAsync(bmp, new Dictionary<string, string> { { "cid", BLLCommon.config.CID+"_2" } }, bmp.Width, bmp.Height);
bmp.Dispose();
//SaveRetrospect(labelBmp, barcode);
UnifiedDataHandler.RecordPrintNg(false, true, out string[] strarrys);
if (BLLCommon.config.PrintCompletedClear)
BLLCommon.extension.Clear();
}
catch (Exception ex)
{
LogNet.log.Error($"Extension_Printing", ex);
}
}
private void FrmMain_Load(object sender, EventArgs e)
{
bool rtn = UserLoginWindow.Show();
if (!rtn)
{
Close();
return;
}
BLLCommon.SCMM = new();
if (!BLL.Config.Backgrounder)
{
monitor = new();
Application.AddMessageFilter(monitor);
monitor.Timeout += Monitor_Timeout;
monitor.Start(BLLCommon.config.OperateTimeout);
}
scanWork = new();
LblVersion.Text = BLLCommon.config.SoftVersion;
LblUserName.Text = BLLCommon.config.UserName;
if (LblUserName.Text == "None user")
LblUserName.Visible = false;
BtnSet.Enabled = BLLCommon.config.UserLevel == UserLevel.Admin;
FrmRetrospect.Print += Extension_Printing;
UsrWorkMode.Printing += Extension_Printing;
//扩展面板
PnlExtension.Width = BLLCommon.config.ExtensionWidth;
PnlExtension.Left = Width - PnlExtension.Width - 12;
PicShow.Width = PnlExtension.Left - PicShow.Left - 6;
BLLCommon.extension.Printing += Extension_Printing;
BLLCommon.extension.SaveRetrospect += Extension_SaveRetrospect;
BLLCommon.extension.LoadPanel(PnlExtension, BLLCommon.macroKey);
//语言
CboLanguage.Items.AddRange(Language.Name.ToArray());
CboLanguage.SelectedText = BLLCommon.config.Language;
BLLCommon.AutoGenRules = AutoGenRule.LoadFile();
if (BLLCommon.config.OpenMaximize) Maximize();
if (CheckCamera() && CheckIOModule())
{
BtnStart.Enabled = true;
if (BLLCommon.config.OpenStartWork)
{
BtnStart.HoldPress = true;
scanWork.Open();
}
}
else
{
BtnStart.Enabled = false;
}
changeBtnStartText();
}
private void Monitor_Timeout(object sender, EventArgs e)
{
if (InvokeRequired)
{
Invoke(new EventHandler(Monitor_Timeout), sender, e);
}
else
{
//Hide();
bool rtn = UserLoginWindow.Show();
if (rtn)
{
Show();
monitor.Start(BLLCommon.config.OperateTimeout);
}
else
{
Close();
}
}
}
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
monitor?.Stop();
BLLCommon.extension?.Dispose();
//if (Common.config.IOLight == -1)
// Common.SerialPort.Close();
}
private void FrmMain_Activated(object sender, EventArgs e)
{
if (BLL.Config.Backgrounder) Hide();
}
private void CboLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
if (BLLCommon.config.Language != CboLanguage.Text)
{
Language.LoadLanguage(CboLanguage.Text);
BLLCommon.config.Language = CboLanguage.Text;
BLLCommon.config.Save();
changeBtnStartText();
Language.SetLanguage(this);
}
}
void changeBtnStartText()
{
if(BLLCommon.config.Language.Equals("English"))
{
BtnStart.Font = new Font("Arial",14,FontStyle.Bold);
}
else
{
BtnStart.Font = new Font("微软雅黑", 14, FontStyle.Bold);
}
if(scanWork?.isRun??false)
{
BtnStart.Text = Language.Dialog("Exit", "退出");
}
else
{
BtnStart.Text = Language.Dialog("Start", "开始");
}
}
#region 通过webservice触发
public WebCodeAll[] WebTouchWork()
{
LogNet.log.Info("Web触发Work");
scanWork.Scan();
scanWork.TouchOff();
return scanWork.GetWebCodeAll();
}
public WebCodeText[] WebTouchWork(string[] code)
{
LogNet.log.Info("Web触发Work code");
scanWork.Scan(code);
scanWork.TouchOff();
return scanWork.GetWebCodeText();
}
#endregion
#region 左侧菜单
private void BtnStart_Click(object sender, EventArgs e)
{
if (BtnStart.HoldPress)
scanWork.Open();
else
scanWork.Close();
changeBtnStartText();
}
private void BtnRetrospect_Click(object sender, EventArgs e)
{
monitor.Pause = true;
new FrmRetrospect().ShowDialog();
monitor.Pause = false;
}
private void BtnSet_Click(object sender, EventArgs e)
{
monitor.Pause = true;
FrmSet set = new FrmSet();
set.Width = PicShow.Width;
set.Height = PicShow.Height;
set.ShowDialog();
monitor.Pause = false;
}
private void BtnAbout_Click(object sender, EventArgs e)
{
monitor.Pause = true;
new FrmAbout().ShowDialog();
monitor.Pause = false;
}
private void BtnTriggerIO_Click(object sender, EventArgs e)
{
LogNet.log.Info("按钮点击触发Work");
Task.Run(() =>
{
scanWork.Scan();
//scanWork.TouchOff();
});
}
private void BtnMatchedName_Click(object sender, EventArgs e)
{
new FrmMatchingInfo((sender as FaceButton).Text, scanWork.workCodeKeyword).ShowDialog();
}
#endregion
public DialogResult ShowWaittingDialog()
{
Common.frmWaitting = new FrmWaitting();
return Common.frmWaitting.ShowDialog();
}
public void CloseWaittingDialog()
{
if (Common.frmMain.InvokeRequired)
{
if (Common.frmWaitting.Created)
{
try
{
Common.frmMain.Invoke(delegate ()
{
CloseWaittingDialog();
});
}
catch { }
};
return;
}
Common.frmWaitting.Close();
}
public void SetWaittingMsg(string msg,int keepsec=3)
{
if (Common.frmMain.InvokeRequired)
{
Common.frmMain.Invoke(delegate ()
{
SetWaittingMsg(msg);
});
return;
}
//BLLCommon.SCMM.ShowMsg(msg, keepsec, msgType.INFO);
Common.frmWaitting.SetMessage(msg);
Application.DoEvents();
}
public void DrawTextForm(Dictionary<string,string> valuePairs)
{
//
if (Common.frmMain.InvokeRequired)
{
Common.frmMain.Invoke(delegate ()
{
DrawTextForm(valuePairs);
});
return;
}
//BLLCommon.SCMM.ShowMsg(msg, 3, msgType.INFO);
//Common.frmWaitting.SetMessage(msg);
BLLCommon.extension.DrawTextBackground(valuePairs);
Application.DoEvents();
}
private void LblVersion_Click(object sender, EventArgs e)
{
//var HCEditor = new HCEditor();
//if (Config.Get<int>("HB_ENABLE") >= 1)
// new HBEditor();
ConfigHelper.AdvanceConfigForm.ShowEditDialog(this, true);
}
}
}