-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathDataModel.cs
762 lines (669 loc) · 24.9 KB
/
DataModel.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using RfidDrivers.First;
using DigitalPlatform;
using DigitalPlatform.IO;
using DigitalPlatform.RFID;
using DigitalPlatform.Text;
using DigitalPlatform.CirculationClient;
namespace RfidTool
{
/// <summary>
/// 数据模型
/// </summary>
public static class DataModel
{
static RfidDriver1 _driver = new RfidDriver1();
static CancellationTokenSource _cancelRfidManager = new CancellationTokenSource();
public static NewTagList TagList = new NewTagList();
/*
static string _prev_uids = "";
static int _prev_count = 0;
*/
// 用于控制暂停的事件
#if EVENT_PAUSE
static ManualResetEvent _eventPause = new ManualResetEvent(true);
#else
static volatile bool _pause = false;
#endif
static Task _task = null;
public static InitializeDriverResult InitialDriver(
bool reset_hint_table = false)
{
_cancelRfidManager?.Cancel();
_cancelRfidManager?.Dispose();
_cancelRfidManager = new CancellationTokenSource();
var token = _cancelRfidManager.Token;
var existing_hint_table = GetHintTable();
// _driver.ReleaseDriver();
string cfgFileName = Path.Combine(ClientInfo.UserDir, "readers.xml");
var initial_result = _driver.InitializeDriver(
cfgFileName,
"", // style,
reset_hint_table ? null : existing_hint_table);
if (initial_result.Value == -1)
return initial_result;
// 记忆
if (reset_hint_table || existing_hint_table == null)
SetHintTable(initial_result.HintTable);
// 首次设置是否启用缓存
TagList.EnableTagCache = EnableTagCache;
_task = Task.Factory.StartNew(
async () =>
{
while (token.IsCancellationRequested == false)
{
await Task.Delay(TimeSpan.FromMilliseconds(100), token);
#if EVENT_PAUSE
int index = WaitHandle.WaitAny(new WaitHandle[] {
_eventPause,
token.WaitHandle,
});
if (index == 1)
return;
#else
if (_pause == true)
continue;
#endif
string readerNameList = "*";
var result = ListTags(readerNameList, "");
if (result.Value == -1)
SetError?.Invoke(result,
new SetErrorEventArgs { Error = result.ErrorInfo });
else
{
SetError?.Invoke(result,
new SetErrorEventArgs { Error = null }); // 清除以前的报错
}
/*
// TODO: 归纳一下 UID 列表,如果不一样才继续往后处理
int count = result.Results.Count;
if (count == _prev_count)
{
string uids = GetUidString(result.Results);
if (uids == _prev_uids)
continue;
_prev_uids = uids;
}
_prev_count = result.Results.Count;
*/
if (result.Results != null)
TagList.Refresh(// sender as BaseChannel<IRfid>,
readerNameList,
result.Results,
(readerName, uid, antennaID, protocol) =>
{
InventoryInfo info = new InventoryInfo
{
Protocol = protocol,
UID = uid,
AntennaID = antennaID
};
return GetTagInfo(readerName, info, "");
},
(add_tags, update_tags, remove_tags) =>
{
TagChanged?.Invoke(_driver, new NewTagChangedEventArgs
{
AddTags = add_tags,
UpdateTags = update_tags,
RemoveTags = remove_tags,
Source = "driver",
});
},
(type, text) =>
{
SetError?.Invoke(null/*this*/, new SetErrorEventArgs { Error = text });
});
}
},
token,
TaskCreationOptions.LongRunning,
TaskScheduler.Default);
return initial_result;
}
// 暂停循环
public static void PauseLoop()
{
#if EVENT_PAUSE
_eventPause.Reset();
#else
_pause = true;
#endif
}
// 继续循环
public static void ContinueLoop()
{
#if EVENT_PAUSE
_eventPause.Set();
#else
_pause = false;
#endif
}
static List<HintInfo> GetHintTable()
{
string value = ClientInfo.Config.Get("readers", "hint_table");
if (string.IsNullOrEmpty(value))
return null;
return JsonConvert.DeserializeObject<List<HintInfo>>(value);
}
static void SetHintTable(List<HintInfo> hint_table)
{
string value = JsonConvert.SerializeObject(hint_table);
ClientInfo.Config.Set("readers", "hint_table", value);
}
public static void ReopenBluetoothReaders()
{
_driver.ReopenBluetoothReaders();
}
public static List<string> GetReadNameList(string style)
{
bool driver_version = StringUtil.IsInList("driverVersion", style);
bool device_sn = StringUtil.IsInList("deviceSN", style);
bool device_type = StringUtil.IsInList("deviceType", style);
bool comm_type = StringUtil.IsInList("commType", style);
List<string> results = new List<string>();
foreach (var reader in _driver.Readers)
{
List<string> columns = new List<string>();
columns.Add(reader.Name);
if (driver_version)
columns.Add("固件版本号: " + reader.DriverVersion);
if (device_sn)
columns.Add("设备序列号: " + reader.DeviceSN);
if (device_type)
columns.Add("设备类型: " + reader.DriverName);
if (comm_type)
columns.Add("通讯方式: " + reader.Type);
results.Add(StringUtil.MakePathList(columns, ", "));
}
return results;
}
static string GetUidString(List<OneTag> tags)
{
StringBuilder result = new StringBuilder();
foreach (var tag in tags)
{
result.Append($"{tag.UID},");
}
return result.ToString();
}
public static void ReleaseDriver()
{
_cancelRfidManager?.Cancel();
try
{
_task?.Wait(TimeSpan.FromSeconds(5));
}
catch
{
}
_driver.ReleaseDriver();
}
// 拟写入 RFID 标签的 OI 字符串
public static string DefaultOiString
{
get
{
return ClientInfo.Config.Get("rfid", "default_oi", null);
}
set
{
ClientInfo.Config.Set("rfid", "default_oi", value);
}
}
// 拟写入 RFID 标签的 AOI 字符串
public static string DefaultAoiString
{
get
{
return ClientInfo.Config.Get("rfid", "default_aoi", null);
}
set
{
ClientInfo.Config.Set("rfid", "default_aoi", value);
}
}
/*
高校联盟格式
国标格式
空白标签用高校联盟格式,其余依从原格式
空白标签用国标格式,其余依从原格式
* */
// 写入 UHF 标签时所用的数据格式
public static string UhfWriteFormat
{
get
{
return ClientInfo.Config.Get("rfid", "uhf_write_format", "国标格式");
}
set
{
ClientInfo.Config.Set("rfid", "uhf_write_format", value);
}
}
//
public static bool WarningWhenUhfFormatMismatch
{
get
{
return ClientInfo.Config.GetBoolean("rfid", "uhf_warningWhenFormatMismatch", true);
}
set
{
ClientInfo.Config.SetBoolean("rfid", "uhf_warningWhenFormatMismatch", value);
}
}
public static bool WriteUhfUserBank
{
get
{
return ClientInfo.Config.GetBoolean("rfid", "uhf_writeUserBank", true);
}
set
{
ClientInfo.Config.SetBoolean("rfid", "uhf_writeUserBank", value);
}
}
// 启用标签缓存
// 缺省为 false (2021/5/12)
public static bool EnableTagCache
{
get
{
return ClientInfo.Config.GetBoolean("general", "enableTagCache", false);
}
set
{
ClientInfo.Config.SetBoolean("general", "enableTagCache", value);
}
}
// 扫描前倒计时秒数
public static int BeforeScanSeconds
{
get
{
return ClientInfo.Config.GetInt("writeTag", "beforeScanSeconds", 5);
}
set
{
ClientInfo.Config.SetInt("writeTag", "beforeScanSeconds", value);
}
}
// PII 号码校验规则
public static string PiiVerifyRule
{
get
{
return ClientInfo.Config.Get("general", "pii_verify_rule", null);
}
set
{
ClientInfo.Config.Set("general", "pii_verify_rule", value);
}
}
// 当写入标签的时候是否校验条码号
public static bool VerifyPiiWhenWriteTag
{
get
{
return ClientInfo.Config.GetBoolean("writeTag", "verifyBarcode", false);
}
set
{
ClientInfo.Config.SetBoolean("writeTag", "verifyBarcode", value);
}
}
// 使用本地存储(写入标签时会查找利用本地存储中的册记录)
public static bool UseLocalStoreage
{
get
{
return ClientInfo.Config.GetBoolean("writeTag", "useLocalStorage", false);
}
set
{
ClientInfo.Config.SetBoolean("writeTag", "useLocalStorage", value);
}
}
// 2022/7/23
// 写入时是否把解析错误的标签当作空白标签直接覆盖
public static bool ErrorContentAsBlank
{
get
{
return ClientInfo.Config.GetBoolean("writeTag", "errorContentAsBlank", false);
}
set
{
ClientInfo.Config.SetBoolean("writeTag", "errorContentAsBlank", value);
}
}
// 写入标签
public static NormalResult WriteTagInfo(string one_reader_name,
TagInfo old_tag_info,
TagInfo new_tag_info)
{
_driver.IncApiCount();
try
{
var result = _driver.WriteTagInfo(one_reader_name, old_tag_info, new_tag_info);
// UHF 保存后 EPC 会发生变化,为了避免引起不必要的 GetTagInfo 动作,ClearTagTable() 时第二参数应该为 false
bool clearTagInfo = (old_tag_info.Protocol == InventoryInfo.ISO15693 ? true : false);
// 清除缓存
TagList.ClearTagTable(old_tag_info.UID, clearTagInfo);
if (old_tag_info.UID != new_tag_info.UID)
TagList.ClearTagTable(new_tag_info.UID, clearTagInfo);
return result;
}
finally
{
_driver.DecApiCount();
}
}
public static SetEasResult SetEAS(string reader_name,
string uid,
uint antenna_id,
bool enable,
string style)
{
_driver.IncApiCount();
try
{
return _driver.SetEAS(reader_name,
uid,
antenna_id,
enable,
style);
}
finally
{
_driver.DecApiCount();
}
}
/*
public static void StartRfidManager(string url)
{
_cancelRfidManager?.Cancel();
_cancelRfidManager = new CancellationTokenSource();
RfidManager.Base.Name = "RFID 中心";
RfidManager.Url = url;
// RfidManager.AntennaList = "1|2|3|4"; // testing
// RfidManager.SetError += RfidManager_SetError;
RfidManager.ListTags += RfidManager_ListTags;
RfidManager.Start(_cancelRfidManager.Token);
}
*/
// parameters:
// reader_name_list 读卡器名字列表。形态为 "*" 或 "name1,name2" 或 "name1:1|2|3|4,name2"
// style 如果为 "getTagInfo",表示要在结果中返回 TagInfo
public static ListTagsResult ListTags(string reader_name_list, string style)
{
_driver.IncApiCount();
try
{
InventoryResult result = new InventoryResult();
List<OneTag> tags = new List<OneTag>();
// uid --> OneTag
Hashtable uid_table = new Hashtable();
foreach (Reader reader in _driver.Readers)
{
// 顺便要从 reader_name_list 中解析出天线部分
if (Reader.MatchReaderName(reader_name_list, reader.Name, out string antenna_list) == false)
continue;
InventoryResult inventory_result = null;
inventory_result = _driver.Inventory(reader.Name,
antenna_list,
style // ""
);
/*
// testing
inventory_result.Value = -1;
inventory_result.ErrorInfo = "模拟 inventory 出错";
inventory_result.ErrorCode = "test";
*/
// TODO: 不要中断其他处理。可以设法警告或者报错
if (inventory_result.Value == -1)
{
/*
// TODO: 统计单位时间内出错的总数,如果超过一定限度则重新初始化全部读卡器
_ = _compactLog.Add("inventory 出错: {0}", new object[] { inventory_result.ErrorInfo });
_inventoryErrorCount++;
// 每隔一段时间写入日志一次
if (DateTime.Now - _lastCompactTime > _compactLength)
{
_compactLog?.WriteToLog((text) =>
{
Log.Logger.Error(text);
Program.MainForm.OutputHistory(text, 2);
});
_lastCompactTime = DateTime.Now;
if (_inventoryErrorCount > 10)
{
// 发出信号,重启
Program.MainForm.RestartRfidDriver($"因最近阶段内 inventory 出错次数为 {_inventoryErrorCount}");
_inventoryErrorCount = 0;
}
}
*/
if (reader.Type == "BLUETOOTH")
continue;
return new ListTagsResult
{
Value = -1,
ErrorInfo = inventory_result.ErrorInfo,
ErrorCode = inventory_result.ErrorCode
};
}
foreach (InventoryInfo info in inventory_result.Results)
{
OneTag tag = null;
if (uid_table.ContainsKey(info.UID))
{
// 重复出现的,追加 读卡器名字
tag = (OneTag)uid_table[info.UID];
tag.ReaderName += "," + reader.Name;
}
else
{
// 首次出现
tag = new OneTag
{
Protocol = info.Protocol,
ReaderName = reader.Name,
UID = info.UID,
DSFID = info.DsfID,
AntennaID = info.AntennaID, // 2019/9/25
// InventoryInfo = info // 有些冗余的字段
};
/*
// testing
tag.AntennaID = _currenAntenna;
if (DateTime.Now - _lastTime > TimeSpan.FromSeconds(5))
{
_currenAntenna++;
if (_currenAntenna > 50)
_currenAntenna = 1;
_lastTime = DateTime.Now;
}
*/
uid_table[info.UID] = tag;
tags.Add(tag);
}
if (StringUtil.IsInList("getTagInfo", style)
&& tag.TagInfo == null)
{
// TODO: 这里要利用 Hashtable 缓存
GetTagInfoResult result0 = null;
result0 = _driver.GetTagInfo(reader.Name, info);
if (result0.Value == -1)
{
tag.TagInfo = null;
// TODO: 如何报错?写入操作历史?
// $"读取标签{info.UID}信息时出错:{result0.ToString()}"
}
else
{
tag.TagInfo = result0.TagInfo;
}
}
}
}
return new ListTagsResult { Results = tags };
}
finally
{
_driver.DecApiCount();
}
}
public static GetTagInfoResult GetTagInfo(
string one_reader_name,
InventoryInfo info,
string style)
{
_driver.IncApiCount();
try
{
return _driver.GetTagInfo(one_reader_name, info, style);
}
finally
{
_driver.DecApiCount();
}
}
public static void IncApiCount()
{
_driver.IncApiCount();
}
public static void DecApiCount()
{
_driver.DecApiCount();
}
public static event SetErrorEventHandler SetError = null;
public static event NewTagChangedEventHandler TagChanged = null;
#if REMOVED
private static void RfidManager_ListTags(object sender, ListTagsEventArgs e)
{
// 标签总数显示
if (e.Result.Results != null)
{
TagList.Refresh(// sender as BaseChannel<IRfid>,
e.ReaderNameList,
e.Result.Results,
(readerName, uid, antennaID) =>
{
var channel = sender as BaseChannel<IRfid>;
return channel.Object.GetTagInfo(readerName, uid, antennaID);
},
(add_tags, update_tags, remove_tags) =>
{
TagChanged?.Invoke(sender, new NewTagChangedEventArgs
{
AddTags = add_tags,
UpdateTags = update_tags,
RemoveTags = remove_tags,
Source = e.Source,
});
},
(type, text) =>
{
RfidManager.TriggerSetError(null/*this*/, new SetErrorEventArgs { Error = text });
});
}
}
#endif
/*
public static void StopRfidManager()
{
_cancelRfidManager?.Cancel();
RfidManager.Url = "";
RfidManager.ListTags -= RfidManager_ListTags;
}
*/
#region UID-->PII 对照关系日志文件
// 去重用
static Hashtable _uidTable = new Hashtable();
static StreamWriter _uidWriter = null;
// 写入 UID-->UII(OI.PII) 对照关系日志文件
public static void WriteToUidLogFile(string uid,
string uii)
{
// 2021/4/29
if (uid != null && uid.Contains(" "))
{
string error = $"WriteToUidLogFile() uid='{uid}' 出现了意外的空格字符";
ClientInfo.WriteErrorLog(error);
throw new Exception(error);
}
if (uii != null && uii.Contains(" "))
{
string error = $"WriteToUidLogFile() uii='{uii}' 出现了意外的空格字符";
ClientInfo.WriteErrorLog(error);
throw new Exception(error);
}
lock (_uidTable.SyncRoot)
{
if (_uidTable.ContainsKey(uid) == true)
{
string old_pii = (string)_uidTable[uid];
if (old_pii == uii)
return; // 去重
}
// 防止占用内存太大
if (_uidTable.Count > 1000)
_uidTable.Clear();
_uidTable[uid] = uii;
}
if (_uidWriter == null)
{
string fileName = Path.Combine(ClientInfo.UserDir, "uid.txt");
_uidWriter = new StreamWriter(fileName, true, Encoding.ASCII);
}
_uidWriter.WriteLine($"{uid}\t{uii}");
}
public static void CloseUidLogFile()
{
try
{
if (_uidWriter != null)
{
_uidWriter.Close();
_uidWriter = null;
}
}
catch(Exception ex)
{
ClientInfo.WriteErrorLog($"关闭 UID 对照文件时出现异常: {ExceptionUtil.GetDebugText(ex)}");
}
}
#endregion
}
public delegate void NewTagChangedEventHandler(object sender,
NewTagChangedEventArgs e);
/// <summary>
/// 设置标签变化事件的参数
/// </summary>
public class NewTagChangedEventArgs : EventArgs
{
public List<TagAndData> AddTags { get; set; }
public List<TagAndData> UpdateTags { get; set; }
public List<TagAndData> RemoveTags { get; set; }
public string Source { get; set; } // 触发者
}
public delegate void SetErrorEventHandler(object sender,
SetErrorEventArgs e);
/// <summary>
/// 设置出错信息事件的参数
/// </summary>
public class SetErrorEventArgs : EventArgs
{
public string Error { get; set; }
}
}