-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathScanDialog.cs
2166 lines (1882 loc) · 77.5 KB
/
ScanDialog.cs
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using DigitalPlatform;
using DigitalPlatform.CirculationClient;
using DigitalPlatform.CommonControl;
using DigitalPlatform.Core;
using DigitalPlatform.GUI;
using DigitalPlatform.LibraryServer.Common;
using DigitalPlatform.RFID;
using DigitalPlatform.RFID.UI;
using DigitalPlatform.Text;
using DigitalPlatform.Xml;
namespace RfidTool
{
public partial class ScanDialog : Form
{
string _typeOfUsage = null; // 10 图书; 80 读者证; 30 层架标
public string TypeOfUsage
{
get
{
return _typeOfUsage;
}
set
{
_typeOfUsage = value;
SetTitle();
}
}
// 当前正在寻求处理的册条码号
// string _currentBarcode = "";
public event WriteCompleteEventHandler WriteComplete = null;
ErrorTable _errorTable = null;
public ScanDialog()
{
InitializeComponent();
toolTip1.SetToolTip(this.textBox_barcode, "输入条码号");
toolTip1.SetToolTip(this.textBox_processingBarcode, "待处理的条码号");
toolTip1.SetToolTip(this.button_clearProcessingBarcode, "清除待处理的条码号");
DataModel.TagChanged += DataModel_TagChanged;
DataModel.SetError += DataModel_SetError;
_errorTable = new ErrorTable((s) =>
{
try
{
this.Invoke((Action)(() =>
{
}));
}
catch (ObjectDisposedException)
{
}
});
if (StringUtil.IsDevelopMode() == true)
this.button_test.Visible = true;
}
private void textBox_barcode_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r' || e.KeyChar == '\n')
{
// MessageBox.Show(this, $"输入 '{this.textBox_barcode.Text}'");
string barcode = this.textBox_barcode.Text.Trim(new char[] { ' ', '\r', '\n' });
var verifyBarcode = DataModel.VerifyPiiWhenWriteTag;
// 校验条码号
if (verifyBarcode == true)
{
if (string.IsNullOrEmpty(barcode) == true)
{
string text = "条码号不应为空";
FormClientInfo.Speak(text);
ShowMessageBox("input", text);
this.textBox_barcode.SelectAll();
this.textBox_barcode.Focus();
return;
}
if (string.IsNullOrWhiteSpace(DataModel.PiiVerifyRule))
{
string text = $"尚未设置条码号校验规则";
FormClientInfo.Speak(text);
ShowMessageBox("input", text);
this.textBox_barcode.SelectAll();
this.textBox_barcode.Focus();
return;
}
var verify_result = VerifyBarcode(ModifyDialog.GetVerifyType(TypeOfUsage), barcode);
if (verify_result.OK == false)
{
string text = $"条码号 {barcode} 不合法";
FormClientInfo.Speak(text);
ShowMessageBox("input", text);
this.textBox_barcode.SelectAll();
this.textBox_barcode.Focus();
return;
}
}
// 查询本地存储
if (this.UseLocalStoreage())
{
var items = EntityStoreage.FindByBarcode(barcode);
string text = "";
if (items == null || items.Count == 0)
text = $"条码号 {barcode} 没有找到册记录";
else if (items.Count > 1)
{
// TODO: 是否允许从多个中选择?
text = $"条码号 {barcode} 找到 {items.Count} 个册记录";
}
if (items.Count == 1)
{
this.ProcessingEntity = items[0];
}
else
{
this.ProcessingEntity = null;
}
ShowBookTitle(this.ProcessingEntity?.Title);
if (string.IsNullOrEmpty(text) == false)
{
FormClientInfo.Speak(text);
ShowMessageBox("input", text);
this.textBox_barcode.SelectAll();
this.textBox_barcode.Focus();
return;
}
}
else
this.ProcessingEntity = null;
ShowMessageBox("input", null);
this.ProcessingBarcode = barcode;
// this.textBox_barcode.SelectAll();
this.textBox_barcode.Clear();
e.Handled = true;
// 触发处理
ProcessBarcode(null);
}
}
private void ScanDialog_Load(object sender, EventArgs e)
{
SetTitle();
// 首次填充标签
FillAllTags();
BeginVerifyEnvironment();
}
private void ScanDialog_FormClosed(object sender, FormClosedEventArgs e)
{
DataModel.TagChanged -= DataModel_TagChanged;
DataModel.SetError -= DataModel_SetError;
}
public void BeginVerifyEnvironment()
{
Task.Run(() =>
{
if (UseLocalStoreage() && EntityStoreage.GetCount() == 0)
{
string text = "尚未导入脱机册信息";
ShowMessage(text);
ShowMessageBox("load", text);
}
else
ShowMessageBox("load", null);
});
}
void SetTitle()
{
/*
if (this.TypeOfUsage == "30")
this.Text = "扫描并写入 层架标";
else if (string.IsNullOrEmpty(this.TypeOfUsage) || this.TypeOfUsage == "10")
this.Text = "扫描并写入 图书标签";
else if (this.TypeOfUsage == "80")
this.Text = "扫描并写入 读者证";
else
this.Text = $"扫描并写入 '{this.TypeOfUsage}'";
*/
this.Text = $"扫描并写入 {GetCaption(this.TypeOfUsage)}";
}
public string GetCaption(string tu)
{
if (tu == "30")
return "层架标";
else if (string.IsNullOrEmpty(tu) || tu == "10")
return "图书标签";
else if (tu == "80")
return "读者证";
else
return $"'{tu}'";
}
private void ScanDialog_VisibleChanged(object sender, EventArgs e)
{
/*
if (this.Visible)
{
DataModel.TagChanged += DataModel_TagChanged;
DataModel.SetError += DataModel_SetError;
}
else
{
DataModel.TagChanged -= DataModel_TagChanged;
DataModel.SetError -= DataModel_SetError;
}
*/
}
private void DataModel_SetError(object sender, SetErrorEventArgs e)
{
if (string.IsNullOrEmpty(e.Error))
this.ShowMessage("", "");
else
this.ShowMessage(e.Error, "red");
}
// 读卡器上的标签发生变化
private void DataModel_TagChanged(object sender, NewTagChangedEventArgs e)
{
bool hasAdded = false;
if (e.AddTags != null && e.AddTags.Count > 0)
{
this.Invoke((Action)(() =>
{
lock (_syncRootFill)
{
UpdateTags(e.AddTags);
}
}));
hasAdded = true;
}
if (e.UpdateTags != null && e.UpdateTags.Count > 0)
{
this.Invoke((Action)(() =>
{
lock (_syncRootFill)
{
UpdateTags(e.UpdateTags);
}
}));
hasAdded = true;
}
if (hasAdded)
ProcessBarcode(null);
if (e.RemoveTags != null && e.RemoveTags.Count > 0)
{
this.Invoke((Action)(() =>
{
RemoveTags(e.RemoveTags);
}));
}
}
const int COLUMN_UID = 0;
const int COLUMN_PII = 1;
const int COLUMN_TOU = 2;
const int COLUMN_TITLE = 3; // 册记录中的书名
const int COLUMN_ACCESSNO = 4; // 册记录中的索取号
const int COLUMN_EAS = 5;
const int COLUMN_AFI = 6;
const int COLUMN_OI = 7;
const int COLUMN_AOI = 8;
const int COLUMN_SHELFLOCATION = 9; // 标签中的 ShelfLocation
const int COLUMN_ANTENNA = 10;
const int COLUMN_READERNAME = 11;
const int COLUMN_PROTOCOL = 12;
object _syncRootFill = new object();
// TODO: 注意和 DataModel_TagChanged() 处理互斥
void FillAllTags()
{
lock (_syncRootFill)
{
this.listView_tags.Items.Clear();
foreach (var tag in DataModel.TagList.Tags)
{
if (tag.OneTag.Protocol == InventoryInfo.ISO14443A)
continue;
ListViewItem item = new ListViewItem();
item.Tag = new ItemInfo { TagData = tag };
this.listView_tags.Items.Add(item);
RefreshItem(item, tag);
}
}
}
// 更新 tags
void UpdateTags(List<TagAndData> tags)
{
foreach (var tag in tags)
{
ListViewItem item = ListViewUtil.FindItem(this.listView_tags, tag.OneTag.UID, COLUMN_UID);
if (item == null)
{
// 2021/1/7
// tag.OneTag = DeepClone(tag.OneTag);
item = new ListViewItem();
item.Tag = new ItemInfo { TagData = tag };
ListViewUtil.ChangeItemText(item, COLUMN_PII, "(尚未填充)");
this.listView_tags.Items.Add(item);
}
RefreshItem(item, tag);
}
/*
OneTag DeepClone(OneTag t)
{
t = t.Clone();
if (t.TagInfo != null)
t.TagInfo = t.TagInfo.Clone();
return t;
}
*/
}
// 刷新 ListViewItem 的显示。常用于 item_info.TagData 发生改变后
void RefreshItem(ListViewItem item)
{
var item_info = item.Tag as ItemInfo;
if (item_info == null)
return;
RefreshItem(item, item_info.TagData);
}
void RemoveTags(List<TagAndData> tags)
{
foreach (var tag in tags)
{
ListViewItem item = ListViewUtil.FindItem(this.listView_tags, tag.OneTag.UID, COLUMN_UID);
if (item != null)
{
this.listView_tags.Items.Remove(item);
}
}
}
// 刷新一个 ListViewItem 的所有列显示
void RefreshItem(ListViewItem item, TagAndData tag)
{
// 2022/7/24
SetItemColor(item, "normal");
string pii = "(尚未填充)";
string tou = "";
string eas = "";
string afi = "";
string oi = "";
string aoi = "";
string shelfLocation = ""; // 标签中的 shelfLocation
var iteminfo = item.Tag as ItemInfo;
ListViewUtil.ChangeItemText(item, COLUMN_UID, tag.OneTag.UID);
ListViewUtil.ChangeItemText(item, COLUMN_ANTENNA, tag.OneTag.AntennaID.ToString());
ListViewUtil.ChangeItemText(item, COLUMN_READERNAME, tag.OneTag.ReaderName);
ListViewUtil.ChangeItemText(item, COLUMN_PROTOCOL, tag.OneTag.Protocol);
ListViewUtil.ChangeItemText(item, COLUMN_PII, "(尚未填充)");
ListViewUtil.ChangeItemText(item, COLUMN_TITLE, "");
ListViewUtil.ChangeItemText(item, COLUMN_ACCESSNO, "");
ListViewUtil.ChangeItemText(item, COLUMN_SHELFLOCATION, "");
try
{
var taginfo = tag.OneTag.TagInfo;
if (taginfo != null)
{
LogicChip chip = null;
if (taginfo.Protocol == InventoryInfo.ISO18000P6C)
{
// 注1: taginfo.EAS 在调用后可能被修改
// 注2: 本函数不再抛出异常。会在 ErrorInfo 中报错
var uhf_info = RfidTagList.GetUhfChipInfo(taginfo, "convertValueToGB"); // "dontCheckUMI"
if (string.IsNullOrEmpty(uhf_info.ErrorInfo) == false)
{
var ex = new Exception(uhf_info.ErrorInfo);
iteminfo.Exception = ex;
ListViewUtil.ChangeItemText(item, COLUMN_PII, "error:" + ex.Message);
SetItemColor(item, "error");
return;
}
// TODO: 对于 .Bytes 缺失的畸形 UHF 标签,最好是尽量解析内容,然后给出警告信息解释问题所在
// 单独严格解析一次标签内容
chip = uhf_info.Chip;
// taginfo.EAS 可能会被修改
iteminfo.UhfProtocol = uhf_info.UhfProtocol;
pii = uhf_info.PII;
oi = uhf_info.OI;
#if REMOVED
var epc_bank = Element.FromHexString(taginfo.UID);
if (UhfUtility.IsBlankTag(epc_bank, taginfo.Bytes) == true)
{
// 空白标签
pii = GetPIICaption(null);
}
else
{
var isGB = UhfUtility.IsISO285604Format(epc_bank, taginfo.Bytes);
if (isGB)
{
// *** 国标 UHF
var parse_result = UhfUtility.ParseTag(epc_bank,
taginfo.Bytes,
4);
if (parse_result.Value == -1)
throw new Exception(parse_result.ErrorInfo);
chip = parse_result.LogicChip;
taginfo.EAS = parse_result.PC.AFI == 0x07;
iteminfo.UhfProtocol = "gb";
pii = GetPIICaption(GetPiiPart(parse_result.UII));
oi = GetOiPart(parse_result.UII, false);
}
else
{
// *** 高校联盟 UHF
var parse_result = GaoxiaoUtility.ParseTag(
epc_bank,
taginfo.Bytes);
if (parse_result.Value == -1)
throw new Exception(parse_result.ErrorInfo);
chip = parse_result.LogicChip;
taginfo.EAS = !parse_result.EpcInfo.Lending;
iteminfo.UhfProtocol = "gxlm";
pii = GetPIICaption(GetPiiPart(parse_result.EpcInfo.PII));
oi = GetOiPart(parse_result.EpcInfo.PII, false);
}
}
#endif
}
else
{
// *** ISO15693 HF
if (taginfo.Bytes != null)
{
iteminfo.Exception = null;
// Exception:
// 可能会抛出异常 ArgumentException TagDataException
chip = LogicChip.From(taginfo.Bytes,
(int)taginfo.BlockSize,
"");
pii = GetPIICaption(chip.FindElement(ElementOID.PII)?.Text);
}
}
tou = chip?.FindElement(ElementOID.TypeOfUsage)?.Text;
// 2023/11/26
RfidTagList.SetTagInfoEAS(taginfo);
eas = taginfo.EAS ? "On" : "Off";
afi = Element.GetHexString(taginfo.AFI);
if (string.IsNullOrEmpty(oi))
{
oi = chip?.FindElement(ElementOID.OI)?.Text;
aoi = chip?.FindElement(ElementOID.AOI)?.Text;
}
if (taginfo.Protocol == InventoryInfo.ISO18000P6C)
{
if (iteminfo.UhfProtocol == "gxlm")
{
// 数字平台针对高校联盟扩充的 AOI
if (string.IsNullOrEmpty(oi) && string.IsNullOrEmpty(aoi))
aoi = chip?.FindElement((ElementOID)27)?.Text;
}
}
shelfLocation = chip?.FindElement(ElementOID.ShelfLocation)?.Text;
}
if (string.IsNullOrEmpty(tag.Error) == false)
{
/*
// 2022/7/23
if (iteminfo.TagData != null && iteminfo.TagData.Error == null)
iteminfo.TagData.Error = tag.Error;
*/
ListViewUtil.ChangeItemText(item, COLUMN_PII, pii + " error:" + tag.Error);
SetItemColor(item, "error");
}
else
ListViewUtil.ChangeItemText(item, COLUMN_PII, pii);
// string accessNo = ""; // 册记录中的索取号
// 设置 Title
if (this.UseLocalStoreage())
{
var uii = BuildUii(pii, oi, aoi);
var entity = EntityStoreage.FindByUII(uii);
if (entity == null)
{
ListViewUtil.ChangeItemText(item, COLUMN_TITLE, "");
ListViewUtil.ChangeItemText(item, COLUMN_ACCESSNO, "");
}
else
{
ListViewUtil.ChangeItemText(item, COLUMN_TITLE, entity.Title);
ListViewUtil.ChangeItemText(item, COLUMN_ACCESSNO, GetAccessNo(entity));
}
}
// 方括号中为标签中的索取号
ListViewUtil.ChangeItemText(item, COLUMN_SHELFLOCATION, shelfLocation);
ListViewUtil.ChangeItemText(item, COLUMN_TOU, tou);
ListViewUtil.ChangeItemText(item, COLUMN_EAS, eas);
ListViewUtil.ChangeItemText(item, COLUMN_AFI, afi);
ListViewUtil.ChangeItemText(item, COLUMN_OI, oi);
ListViewUtil.ChangeItemText(item, COLUMN_AOI, aoi);
// 刷新协议栏
if (tag.OneTag.Protocol == InventoryInfo.ISO18000P6C)
{
string name = iteminfo.UhfProtocol;
if (iteminfo.UhfProtocol == "gxlm")
name = "高校联盟";
else if (iteminfo.UhfProtocol == "gb")
name = "国标";
ListViewUtil.ChangeItemText(item, COLUMN_PROTOCOL,
string.IsNullOrEmpty(name) ? tag.OneTag.Protocol : tag.OneTag.Protocol + ":" + name);
}
}
catch (Exception ex)
{
// 2022/7/23
// iteminfo.TagData.Error = ex.Message;
iteminfo.Exception = ex;
ListViewUtil.ChangeItemText(item, COLUMN_PII, "error:" + ex.Message);
SetItemColor(item, "error");
}
}
static string BuildUii(string pii, string oi, string aoi)
{
if (string.IsNullOrEmpty(oi) && string.IsNullOrEmpty(aoi))
return pii;
if (string.IsNullOrEmpty(oi) == false)
return oi + "." + pii;
if (string.IsNullOrEmpty(aoi) == false)
return aoi + "." + pii;
return pii;
}
// 获得 oi.pii 的 oi 部分
public static string GetOiPart(string oi_pii, bool return_null)
{
if (oi_pii.IndexOf(".") == -1)
{
if (return_null)
return null;
return "";
}
var parts = StringUtil.ParseTwoPart(oi_pii, ".");
return parts[0];
}
// 获得 oi.pii 的 pii 部分
public static string GetPiiPart(string oi_pii)
{
if (oi_pii.IndexOf(".") == -1)
return oi_pii;
var parts = StringUtil.ParseTwoPart(oi_pii, ".");
return parts[1];
}
public static string GetPIICaption(string text)
{
if (string.IsNullOrEmpty(text))
return "(空)";
return text;
}
class FindTagResult : NormalResult
{
public ListViewItem Item { get; set; }
public OneTag Tag { get; set; }
}
// 寻找一个可用于写入的空白标签,或者相同 PII 的标签
FindTagResult FindBlankTag(string pii, string oi)
{
var overwrite_error_tag = DataModel.ErrorContentAsBlank;
lock (_syncRootFill)
{
List<FindTagResult> blank_results = new List<FindTagResult>();
List<FindTagResult> pii_results = new List<FindTagResult>();
List<FindTagResult> error_results = new List<FindTagResult>();
//FindTagResult blank_result = null;
//FindTagResult pii_result = null;
this.Invoke((Action)(() =>
{
foreach (ListViewItem item in this.listView_tags.Items)
{
/*
if (item.Tag is not ItemInfo info)
continue;
*/
ItemInfo info = item.Tag as ItemInfo;
if (info == null)
continue;
if (info.Exception != null && info.Exception is TagDataException)
{
error_results.Add(new FindTagResult
{
Value = 1,
Item = item,
Tag = info.TagData.OneTag,
});
continue;
}
if (string.IsNullOrEmpty(info.TagData.Error) == false)
continue;
string current_pii = ListViewUtil.GetItemText(item, COLUMN_PII);
string current_oi = ListViewUtil.GetItemText(item, COLUMN_OI);
string current_aoi = ListViewUtil.GetItemText(item, COLUMN_AOI);
if (current_pii == pii
// 2021/6/16
// 判断机构代码是否吻合
&& (oi == current_oi || oi == current_aoi))
{
pii_results.Add(new FindTagResult
{
Value = 1,
Item = item,
Tag = info.TagData.OneTag,
});
}
else if ((string.IsNullOrEmpty(current_pii) == true || current_pii == "(空)")
&& info.TagData.OneTag.TagInfo != null)
{
blank_results.Add(new FindTagResult
{
Value = 1,
Item = item,
Tag = info.TagData.OneTag,
});
}
}
}));
if (pii_results.Count + blank_results.Count == 1)
{
// 优先返回 PII 匹配的行
if (pii_results.Count == 1)
return pii_results[0];
// 次优先返回 PII 为空的行
if (blank_results.Count == 1)
return blank_results[0];
}
// 2022/7/23
// 如果有解析错误的标签,则返回
if (DataModel.ErrorContentAsBlank
&& error_results.Count == 1)
{
return error_results[0];
}
// 返回无法满足条件的具体原因
List<string> reasons = new List<string>();
if (pii_results.Count > 1)
reasons.Add($"PII '{pii}' 匹配标签不唯一 ({pii_results.Count})");
if (blank_results.Count > 1)
reasons.Add($"空白标签不唯一 ({blank_results.Count})");
if (pii_results.Count > 0 && blank_results.Count > 0)
reasons.Add($"出现了 PII 匹配,同时还有空白标签的情况");
// 没有找到
return new FindTagResult
{
Value = 0,
ErrorInfo = StringUtil.MakePathList(reasons, ";")
};
}
}
int _inProcessing = 0;
// 寻找适当的 RFID 标签完成写入操作
void ProcessBarcode(ListViewItem selectedItem)
{
_inProcessing++;
try
{
// 防止重入
if (_inProcessing > 1)
{
Console.Beep();
return;
}
string barcode = "";
EntityItem entity = ProcessingEntity;
this.Invoke((Action)(() =>
{
barcode = this.ProcessingBarcode;
}));
if (string.IsNullOrEmpty(barcode))
return;
if (string.IsNullOrEmpty(barcode) == true)
{
string text = "条码号不应为空";
FormClientInfo.Speak(text);
ShowMessageBox("processBarcode", text);
return;
}
bool localStore = this.UseLocalStoreage();
#if REMOVED
// TODO: 本地存储情况下,直接使用册记录中的 OI
// 还可以提供一种强制使用 OiSetting 中的 OI 的方法
if (localStore == false)
{
// TODO: 如果是写入超高频标签的高校联盟格式,并且不允许写入 User Bank,这时候应该允许 OI 为空
string error = VerifyOiSetting();
if (error != null)
{
FormClientInfo.Speak("O I (所属机构代码) 和 A O I (非标准所属机构代码) 尚未配置");
MessageBox.Show(this, error);
using (SettingDialog dlg = new SettingDialog())
{
GuiUtil.SetControlFont(dlg, this.Font);
ClientInfo.MemoryState(dlg, "settingDialog", "state");
dlg.ShowDialog(this);
if (dlg.DialogResult == DialogResult.OK)
DataModel.TagList.EnableTagCache = DataModel.EnableTagCache;
}
return;
}
}
#endif
string oi = DataModel.DefaultOiString;
string aoi = DataModel.DefaultAoiString;
if (localStore && entity != null)
{
oi = GetOI(entity);
aoi = "";
}
else
{
if (string.IsNullOrEmpty(oi) && string.IsNullOrEmpty(aoi))
{
ShowMessage($"警告: 尚未设置机构代码或非标准机构代码");
// TODO: 弹出对话框警告一次。可以选择不再警告
}
}
OneTag tag = null;
ItemInfo iteminfo = null;
if (selectedItem == null)
{
var find_result = FindBlankTag(barcode,
string.IsNullOrEmpty(oi) == false ? oi : aoi);
if (find_result.Value == 0)
{
FormClientInfo.Speak($"请在读写器上放好空白标签,或双击选择其他可用标签");
ShowMessage($"请在读写器上放好空白标签,或双击选择其他可用标签");
return;
}
tag = find_result.Tag;
iteminfo = find_result.Item.Tag as ItemInfo;
selectedItem = find_result.Item;
}
else
{
iteminfo = (selectedItem.Tag as ItemInfo);
tag = iteminfo.TagData.OneTag;
}
// 2021/1/7
// 克隆对象,避免后面因为标签快速拿走而被改变
if (tag != null)
{
tag = DeepClone(tag);
}
OneTag DeepClone(OneTag t)
{
/*
t = t.Clone();
if (t.TagInfo != null)
t.TagInfo = t.TagInfo.Clone();
return t;
*/
return t.Clone();
}
if (tag.TagInfo == null)
{
/*
string pii = ListViewUtil.GetItemText(selectedItem, COLUMN_PII);
throw new Exception("test");
*/
string text = "标签信息尚未填充。请稍后重试写入";
ShowMessage(text);
ShowMessageBox("processBarcode", text);
return;
}
/*
Debug.Assert(tag != null);
Debug.Assert(tag.TagInfo != null);
// testing
// DataModel.TagList.ClearTagTable(tag.UID);
Debug.Assert(tag.TagInfo != null);
*/
// 判断序列号中的功能类型
{
string function_type = "HF";
if (tag.TagInfo.Protocol == InventoryInfo.ISO18000P6C)
function_type = "UHF";
if (HasLicense(function_type) == false)
return;
}
// 检查 settings 中配置的 OI
{
// TODO: 本地存储情况下,直接使用册记录中的 OI
// 还可以提供一种强制使用 OiSetting 中的 OI 的方法
string error = null;
// 如果是写入超高频标签的高校联盟格式,并且不允许写入 User Bank,这时候应该允许 OI 为空
// (并且只能为空)
if (tag.TagInfo.Protocol == InventoryInfo.ISO18000P6C
&& DataModel.UhfWriteFormat == "高校联盟格式"
&& DataModel.WriteUhfUserBank == false)
{
// 检查
var default_oi = DataModel.DefaultOiString;
var default_aoi = DataModel.DefaultAoiString;
if (string.IsNullOrEmpty(default_oi) == false
|| string.IsNullOrEmpty(default_aoi) == false)
error = "当尝试写入高校联盟格式超高频标签的时候,配置了不写入 User Bank,那么配置的 O I (所属机构代码) 或 A O I (非标准所属机构代码) 无法写入标签。请重新配置";
}
else if (localStore == false)
{
// 其它情况的验证
error = VerifyOiSetting();
}
if (error != null)
{
FormClientInfo.Speak(error);
MessageBox.Show(this, error.Replace(" ", ""));
using (SettingDialog dlg = new SettingDialog())
{
GuiUtil.SetControlFont(dlg, this.Font);
ClientInfo.MemoryState(dlg, "settingDialog", "state");
dlg.ShowDialog(this);
if (dlg.DialogResult == DialogResult.OK)
DataModel.TagList.EnableTagCache = DataModel.EnableTagCache;
}
return;
}
}
var uid = tag.UID;
var tou = this.TypeOfUsage;
if (string.IsNullOrEmpty(tou))
tou = "10"; // 默认图书
string accessNo = GetAccessNo(entity);
var chip = new LogicChip();
chip.SetElement(ElementOID.PII, barcode);
if (string.IsNullOrEmpty(oi) == false)
chip.SetElement(ElementOID.OI, oi);
if (string.IsNullOrEmpty(aoi) == false)
chip.SetElement(ElementOID.AOI, aoi);
if (string.IsNullOrEmpty(accessNo) == false)
chip.SetElement(ElementOID.ShelfLocation, accessNo);
bool eas = false;
if (this.IsBook())
eas = true;
TagInfo new_tag_info = GetTagInfo(tag.TagInfo, chip, eas);
NormalResult write_result = null;
for (int i = 0; i < 2; i++)
{
// 重试前延时半秒
if (i > 0)
Thread.Sleep(500);
write_result = DataModel.WriteTagInfo(tag.ReaderName, tag.TagInfo,
new_tag_info);
if (write_result.Value != -1)
break;
}
if (write_result.Value == -1)
{
ShowMessage(write_result.ErrorInfo);
ShowMessageBox("processBarcode", write_result.ErrorInfo);
return;
}
WriteComplete?.Invoke(this, new WriteCompleteventArgs
{
Chip = chip,
TagInfo = new_tag_info
});
// 写入 UID-->PII 对照关系日志文件
if (tou == "10")
DataModel.WriteToUidLogFile(uid,
ModifyDialog.MakeOiPii(barcode, oi, aoi));
/*
// 2022/7/24
TagAndData data = new TagAndData();
data.OneTag.TagInfo = new_tag_info;
UpdateTags(new List<TagAndData> { data });
*/
// 语音提示写入成功
FormClientInfo.Speak($"{GetSpeakNumber(barcode)} 写入成功", false, true);
ShowMessage($"{barcode} 写入成功");
ShowMessageBox("processBarcode", null);
ClearBarcode();
}
catch (Exception ex)
{
string error = $"写入失败: {ex.Message}";