UsrNanRui.cs
9.0 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MES.Interface;
namespace MES
{
public partial class UsrNanRui : Asa.Face.BaseCtls, IUserControl
{
private WebService web;
private string[][] jsonData;
private Dictionary<string, string> printText;
private const string VENDER = "vender";
private const string QTY = "qty";
private const string BATCH = "batch";
public UsrNanRui()
{
InitializeComponent();
printText = new Dictionary<string, string>();
LblVender.Text += string.Format(" ({0})", VENDER);
LblQty.Text += string.Format(" ({0})", QTY);
LblBatch.Text += string.Format(" ({0})", BATCH);
}
/// <summary>
/// SuperDOG的特征ID
/// </summary>
public int ID => 1; //固定,不允许改变
/// <summary>
/// MES是否连接
/// </summary>
public bool IsConn { get; set; } = false;
/// <summary>
/// 整个标签是否需要OCR
/// </summary>
public bool LabelOCR { get; set; } = false;
/// <summary>
/// 是否需要匹配模板
/// </summary>
public bool Match { set; get; } = false;
public event Log LogOut;
public event PrintEvent Printing;
public event SerialNoEvent GetSN;
public event PreviewEvent Preview;
/// <summary>
/// 连接MES
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public bool Connect(string ip)
{
try
{
web = new WebService(ip);
IsConn = web.Ping();
LogOut?.Invoke(web.Log);
if (IsConn)
{
BtnConn.Text = "MES已连接";
BtnConn.StateColor = Color.Lime;
}
else
{
BtnConn.Text = "MES未连接";
BtnConn.StateColor = Color.Red;
}
}
catch (Exception ex)
{
IsConn = false;
LogOut?.Invoke(ex.Message);
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return IsConn;
}
/// <summary>
/// 获取控件
/// </summary>
/// <returns></returns>
public Control GetControl()
{
return this;
}
/// <summary>
/// 获取打印文本
/// </summary>
/// <returns></returns>
public Dictionary<string, string> GetPrint()
{
return printText;
}
public void SetCode(string[] code)
{
if (code == null) return;
CboCode.Text = "";
CboCode.ItemClear();
CboCode.ItemAdd(code);
CboCode.SelectedIndex = 0;
CboBatch.Text = "";
CboBatch.ItemClear();
CboBatch.ItemAdd(code);
CboBatch.SelectedIndex = 0;
}
public void SetCode(Dictionary<string, string> code)
{
if (code == null) return;
bool rtn = code.TryGetValue(VENDER, out string value);
if (rtn)
{
CboCode.Text = "";
CboCode.ItemClear();
CboCode.ItemAdd(value);
CboCode.SelectedIndex = 0;
printText.Add(VENDER, value);
}
rtn = code.TryGetValue(QTY, out value);
if (rtn)
{
TxtQty.Text = value;
printText.Add(QTY, value);
}
rtn = code.TryGetValue(BATCH, out value);
if (rtn)
{
CboBatch.Text = "";
CboBatch.ItemClear();
CboBatch.ItemAdd(value);
CboBatch.SelectedIndex = 0;
printText.Add(BATCH, value);
}
}
public void SetOcrText(Dictionary<string, string> text)
{
if (text == null) return;
bool rtn = text.TryGetValue(VENDER, out string value);
if (rtn)
{
CboCode.Text = "";
CboCode.ItemClear();
CboCode.ItemAdd(value);
printText.Add(VENDER, value);
}
rtn = text.TryGetValue(QTY, out value);
if (rtn)
{
TxtQty.Text = value;
printText.Add(QTY, value);
}
rtn = text.TryGetValue(BATCH, out value);
if (rtn)
{
CboBatch.Text = "";
CboBatch.ItemClear();
CboBatch.ItemAdd(value);
CboBatch.SelectedIndex = 0;
printText.Add(BATCH, value);
}
}
public bool Print(int time)
{
for (int i = 0; i < time; i++)
{
Printing?.Invoke();
System.Threading.Thread.Sleep(500);
}
return true;
}
private void BtnConn_Click(object sender)
{
IsConn = web.Ping();
if (IsConn)
{
BtnConn.Text = "MES已连接";
BtnConn.StateColor = Color.Lime;
Asa.Face.MessageBox.Show("MES已连接");
}
else
{
BtnConn.Text = "MES未连接";
BtnConn.StateColor = Color.Red;
Asa.Face.MessageBox.ShowError(web.ErrInfo);
}
}
private void BtnVender_Click(object sender)
{
if (!IsConn) return;
if (CboCode.Text.Length == 0) return;
try
{
CboOrder.ItemClear();
TxtOrderNum.Text = "";
bool rtn = web.UploadVender(CboCode.Text, out jsonData);
LogOut?.Invoke(web.Log);
if (rtn)
{
for (int i = 0; i < jsonData.Length; i++)
CboOrder.ItemAdd(jsonData[i][0]);
if (CboOrder.Count > 0)
CboOrder.SelectedIndex = 0;
}
else
{
Asa.Face.MessageBox.Show(web.ErrInfo);
}
}
catch (Exception ex)
{
LogOut?.Invoke(ex.Message);
Asa.Face.MessageBox.ShowError(ex.Message);
}
}
private void BtnBind_Click(object sender)
{
if (CboCode.Text == "" || TxtCode.Text == "") return;
if (!IsConn) return;
bool rtn = web.UploadMaterial(CboCode.Text, TxtCode.Text);
LogOut?.Invoke(web.Log);
if (rtn)
Asa.Face.MessageBox.Show("绑定成功");
else
MessageBox.Show(web.ErrInfo, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void CboOrder_SelectedIndexChanged(object sender)
{
//try
//{
// TxtOrderNum.Text = jsonData[CboOrder.SelectedIndex][1];
//}
//catch (Exception ex)
//{
// LogOut?.Invoke(ex.Message);
//}
}
private void BtnUpload_Click(object sender)
{
if (CboCode.Text.Length == 0 || CboOrder.Text.Length == 0 || TxtOrderNum.Text.Length == 0)
return;
if (!IsConn) return;
try
{
bool rtn = web.UploadOrder(CboOrder.Text, TxtOrderNum.Text, TxtQty.Text, out printText);
LogOut?.Invoke(web.Log);
//if (rtn)
//{
// int n = GetSN("sn");
// printText.Add("FACTORYLOT", CboBatch.Text);
// printText.Add("SN", string.Format("{0:0000}", n));
// string s = Printing?.Invoke("sn");
// if (string.IsNullOrWhiteSpace(s)) return;
// rtn = web.UploadMessage(s, out string[] value);
// LogOut?.Invoke(web.Log);
// if (rtn)
// Asa.Face.MessageBox.Show("上传完成");
// else
// Asa.Face.MessageBox.Show(web.ErrInfo);
//}
//else
//{
// MessageBox.Show(web.ErrInfo, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
//}
}
catch (Exception ex)
{
LogOut?.Invoke(ex.Message);
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void BtnPreview_Click(object sender)
{
Preview?.Invoke();
}
}
}