Alcoelectro.cs
14.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
/*
* 西班牙 Alcoelectro
*/
using System;
using System.Collections.Generic;
using System.Linq;
using Model;
using Asa.FaceControl;
using System.Web.Script.Serialization;
namespace ExtensionGroup
{
public class Alcoelectro : IExtension
{
private List<ExtensionControl> extensions;
private int reelID;
private ServerContent[] serverContent;
//private string reelIDKey = "";
//private System.IO.FileStream streamID;
public event IExtension.PrintDelegate Printing;
public event IExtension.PrintDelegate SaveRetrospect;
public Alcoelectro()
{
reelID = 0;
}
public void Dispose()
{
//streamID?.Close();
//streamID?.Dispose();
}
public void Load(List<ExtensionControl> extensions)
{
this.extensions = extensions;
try
{
//streamID = new System.IO.FileStream(FilePath.CONFIG_REELID, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None);
//streamID.Seek(0, System.IO.SeekOrigin.Begin);
//byte[] array = new byte[streamID.Length];
//streamID.Read(array, 0, array.Length);
//string text = System.Text.Encoding.UTF8.GetString(array);
//int.TryParse(text, out reelID);
if (!System.IO.File.Exists(FilePath.CONFIG_REELID))
{
System.IO.FileStream fs = System.IO.File.Create(FilePath.CONFIG_REELID);
fs.Close();
}
string text = System.IO.File.ReadAllText(FilePath.CONFIG_REELID);
int.TryParse(text, out reelID);
string str = "Load reelID:" + text;
Log.Info(str);
}
catch (Exception ex)
{
Log.Error("Alcoelectro", ex);
System.Windows.Forms.MessageBox.Show(ex.Message, "Warning", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
}
Clear();
}
public void Clear()
{
Log.Info("Clear()");
for (int i = 0; i < extensions.Count; i++)
{
if (!extensions[i].CanClear) continue;
if (extensions[i].Control is FaceComboBox box)
box.Items.Clear();
extensions[i].Control.Text = "";
}
int idx = extensions.FindIndex(match => match.Control.Name == "BtnPrint");
if (idx > -1) extensions[idx].Control.Enabled = false;
Update();
}
public void SetKey(string[] originalCode, Dictionary<string, string> key)
{
Log.Info("SetKey()");
for (int i = 0; i < extensions.Count; i++)
{
if (key.ContainsKey(extensions[i].Key))
extensions[i].Control.Text = key[extensions[i].Key];
}
int index = extensions.FindIndex(match => match.Control.Name == "TxtSupplierPN");
if (index > -1) PNPress(extensions[index].Control, EventArgs.Empty);
}
public void Update()
{
Log.Info("Update()");
AppConfig.Read(AppConfigKey.SELECT_HTTP_PN, out bool value);
int index = extensions.FindIndex(match => match.Control.Name == "CboCustomerPN");
if (index > -1) ((FaceComboBox)extensions[index].Control).ReadOnly = value;
index = extensions.FindIndex(match => match.Control.Name == "BtnSetID");
if (index > -1) ((FaceButton)extensions[index].Control).Enabled = !value;
}
private class ServerContent
{
public string producer { set; get; }
public string partNumber { set; get; }
public string description { set; get; }
}
private void SaveReelID()
{
try
{
//if (streamID != null)
//{
// byte[] array = System.Text.Encoding.UTF8.GetBytes(reelID.ToString());
// streamID.Seek(0, System.IO.SeekOrigin.Begin);
// streamID.Write(array, 0, array.Length);
// streamID.Flush();
// Log.Info("Save ReelID" + reelID);
//}
System.IO.File.WriteAllText(FilePath.CONFIG_REELID, reelID.ToString());
Log.Info("Save ReelID:" + reelID);
}
catch (Exception ex)
{
Log.Error("SaveReelID error", ex);
}
}
private void GetHttpJson(ControlBase ctl, out string json)
{
json = "";
AppConfig.Read(AppConfigKey.SELECT_HTTP_PN, out bool value);
if (value)
{
string url = AppConfig.Read(AppConfigKey.HTTP_SERVER) + ctl.Text;
json = Http.Get(url);
}
else
{
if (System.IO.File.Exists(FilePath.CONFIG_HTTP_TEST_DATA))
json = System.IO.File.ReadAllText(FilePath.CONFIG_HTTP_TEST_DATA);
}
}
private void ReadHttpJsonContent(string json)
{
serverContent = null;
JavaScriptSerializer serializer = new();
object[] obj = (object[])serializer.DeserializeObject(json);
if (obj == null || obj.Length == 0)
{
ClearHttpContent();
new FaceMessageBox("", Language.Dialog("ServerJson"), System.Windows.Forms.MessageBoxButtons.OK).ShowDialog();
return;
}
serverContent = new ServerContent[obj.Length];
for (int i = 0; i < serverContent.Length; i++)
{
Dictionary<string, object> dic = (Dictionary<string, object>)obj[i];
serverContent[i] = new();
System.Reflection.PropertyInfo[] info = serverContent[i].GetType().GetProperties();
for (int j = 0; j < info.Length; j++)
{
if (dic.TryGetValue(info[j].Name, out object value))
{
if (value == null) value = "";
info[j].SetValue(serverContent[i], value.ToString());
}
}
}
}
private void WriteConntrolText(ControlBase ctl)
{
if (serverContent == null) return;
//获取linkKey的控件
int index = extensions.FindIndex(match => match.Control == ctl);
if (index == -1) return;
string linkKey = extensions[index].LinkName;
index = extensions.FindIndex(match => match.Control.Name == linkKey);
if (index == -1) return;
FaceComboBox cbo = (FaceComboBox)extensions[index].Control;
cbo.Items.Clear();
string[] items = new string[serverContent.Length];
for (int i = 0; i < serverContent.Length; i++)
items[i] = serverContent[i].partNumber;
if (items.Length > 0)
{
cbo.Items.AddRange(items);
cbo.SelectedIndex = 0;
}
index = extensions.FindIndex(match => match.Control.Name == "BtnSetID");
if (index > -1) extensions[index].Control.Enabled = true;
index = extensions.FindIndex(match => match.Control.Name == "BtnPrint");
if (index > -1) extensions[index].Control.Enabled = false;
}
/// <summary>
/// Clear text in "Customer PN", "Description", "Supplier Name" and "Barcode" when server doesn't return information
/// </summary>
private void ClearHttpContent()
{
int idx = extensions.FindIndex(match => match.Control.Name == "CboCustomerPN");
if (idx > -1)
{
((FaceComboBox)extensions[idx].Control).Items.Clear();
extensions[idx].Control.Text = "";
}
idx = extensions.FindIndex(match => match.Control.Name == "TxtDescription");
if (idx > -1) extensions[idx].Control.Text = "";
//idx = extensions.FindIndex(match => match.Control.Name == "TxtSupplierPN");
//if (idx > -1) extensions[idx].Control.Text = "";
idx = extensions.FindIndex(match => match.Control.Name == "TxtSupplierName");
if (idx > -1) extensions[idx].Control.Text = "";
idx = extensions.FindIndex(match => match.Control.Name == "TxtBarcode");
if (idx > -1) extensions[idx].Control.Text = "";
idx = extensions.FindIndex(match => match.Control.Name == "BtnPrint");
if (idx > -1) extensions[idx].Control.Enabled = false;
Update();
}
//===== 以下为 Extension.json 的 Event =====
private void PNPress(object sender, EventArgs e)
{
try
{
Log.Debug("Enter PNPress Method");
ControlBase ctl = sender as ControlBase;
GetHttpJson(ctl, out string json);
if (string.IsNullOrWhiteSpace(json))
{
ClearHttpContent();
new FaceMessageBox("", Language.Dialog("ServerReturn"), System.Windows.Forms.MessageBoxButtons.OK).ShowDialog();
}
else
{
ReadHttpJsonContent(json);
WriteConntrolText(ctl);
}
}
catch (Exception ex)
{
Log.Error("PNPress", ex);
}
}
private void CustomerPNSelected(object sender, EventArgs e)
{
try
{
Log.Debug("Enter CustomerPNSelected Method");
FaceComboBox ctl = sender as FaceComboBox;
if (ctl.SelectedIndex == -1) return;
int index = extensions.FindIndex(match => match.Control == ctl);
if (index == -1) return;
string[] linkKey = extensions[index].LinkName.Split(',');
if (linkKey.Length != 2) return;
if (linkKey[0] == "TxtDescription")
{
index = extensions.FindIndex(match => match.Control.Name == linkKey[0]);
if (index > -1)
((FaceTextBox)extensions[index].Control).Text = serverContent[ctl.SelectedIndex].description;
}
if (linkKey[1] == "TxtSupplierName")
{
index = extensions.FindIndex(match => match.Control.Name == linkKey[1]);
if (index > -1)
((FaceTextBox)extensions[index].Control).Text = serverContent[ctl.SelectedIndex].producer;
}
}
catch (Exception ex)
{
Log.Error("CustomerPNSelected", ex);
}
}
private void GetReelid(object sender, EventArgs e)
{
Log.Debug("Enter GetReelid Method");
//读取参数
string matchID = AppConfig.Read(AppConfigKey.REEL_ID_MATCH);
AppConfig.Read(AppConfigKey.REEL_ID_PLACES, out int matchPlace);
AppConfig.Read(AppConfigKey.REEL_ID_FILL_ZERO, out bool matchFill);
string matchPrefix = AppConfig.Read(AppConfigKey.REEL_ID_PREFIX);
string matchPostfix = AppConfig.Read(AppConfigKey.REEL_ID_POSTFIX);
//把key替换成实际值
string[] keys = ObjConversion.StrGetKey(matchID);
for (int i = 0; i < keys.Length; i++)
{
int index = extensions.FindIndex(match => match.Key == keys[i]);
if (index == -1) continue;
string oldValue = string.Format("[{0}]", keys[i]);
matchID = matchID.Replace(oldValue, extensions[index].Control.Text);
}
//查找是否存在
int val = reelID + 1;
//reelIDKey = matchID;
//填充0
string text = matchPrefix;
if (matchFill)
text += string.Format("{0:d" + matchPlace + "}", val);
else
text += val.ToString();
text += matchPostfix;
Log.Info(string.Format("GetReelid match={0} id={1} text={2}", matchID, val, text));
//填入key=reelid的控件
for (int i = 0; i < extensions.Count; i++)
{
if (extensions[i].Key.ToUpper() == "REELID")
{
extensions[i].Control.Text = text;
break;
}
}
int idx = extensions.FindIndex(match => match.Control.Name == "BtnPrint");
if (idx > -1) extensions[idx].Control.Enabled = true;
}
private void PrintLabel(object sender, EventArgs e)
{
Log.Debug("Enter PrintLabel Method");
AppConfig.Read(AppConfigKey.LABEL_EMPTY_CHECK, out bool check);
if (check)
{
bool find = false;
for (int i = 0; i < extensions.Count; i++)
{
if (extensions[i].Key != "" && extensions[i].Control.Text == "")
{
find = true;
break;
}
}
if (find)
{
string text = Language.Dialog("LabelEmpty");
FaceMessageBox dlg = new("", text, System.Windows.Forms.MessageBoxButtons.YesNo);
System.Windows.Forms.DialogResult dr = dlg.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.No)
return;
}
}
Dictionary<string, string> key = new();
for (int i = 0; i < extensions.Count; i++)
{
if (extensions[i].Key == "") continue;
if (key.ContainsKey(extensions[i].Key))
key[extensions[i].Key] = extensions[i].Control.Text;
else
key.Add(extensions[i].Key, extensions[i].Control.Text);
}
Log.Info("打印标签前的ID:" + reelID);
reelID++;
SaveReelID();
Printing?.Invoke(key);
}
public bool SetKey(string[] originalCode, Dictionary<string, string> key, bool hasMatch, out string errmsg)
{
throw new NotImplementedException();
}
public void DrawTextBackground(Dictionary<string, string> key)
{
throw new NotImplementedException();
}
public void Print(bool match, Dictionary<string, string> key)
{
throw new NotImplementedException();
}
}
}