-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathBioUtil.cs
1724 lines (1505 loc) · 61.8 KB
/
BioUtil.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;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using DigitalPlatform.IO;
using DigitalPlatform.Xml;
using DigitalPlatform.Text;
using DigitalPlatform.ResultSet;
using DigitalPlatform.Interfaces;
using DigitalPlatform.LibraryClient;
using DigitalPlatform.LibraryClient.localhost;
namespace DigitalPlatform.CirculationClient
{
/// <summary>
/// 和生物识别有关的实用功能
/// 主要是从读者记录中析出 fingerprint 和 face 信息
/// </summary>
public class BioUtil : BioBase, IDisposable
{
// 2021/3/22
// 配置参数表
public IDictionary<string, string> ConfigTable = new Dictionary<string, string>();
public event GetImageEventHandler GetImage = null;
// 2021/5/16
public event GetImageEventHandler GetIrImage = null;
public virtual string BioTypeName
{
get
{
throw new NotImplementedException();
}
}
public virtual string DriverName
{
get
{
throw new NotImplementedException();
}
}
// 算法版本号
public virtual string AlgorithmVersion
{
get
{
throw new NotImplementedException();
}
}
internal ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
public void Lock()
{
_lock.EnterWriteLock();
}
public void Unlock()
{
_lock.ExitWriteLock();
}
public void LockForRead()
{
_lock.EnterReadLock();
}
public void UnlockForRead()
{
_lock.ExitReadLock();
}
// 设备列表
List<string> _dev_list = new List<string>();
public List<string> DeviceList
{
get
{
return new List<string>(_dev_list);
}
}
public class ReplicationResult : NormalResult
{
public string LastDate { get; set; }
public long LastIndex { get; set; }
// [out] 返回处理概述信息
public ProcessInfo ProcessInfo { get; set; }
public override string ToString()
{
return $"LastDate={LastDate}, LastIndex={LastIndex}, ProcessInfo={ProcessInfo?.ToString()}";
}
}
public virtual int AddItems(
List<FingerprintItem> items,
ProcessInfo info,
out string strError)
{
strError = "尚未重载 AddItems() 函数";
return -1;
}
public virtual int ItemCount
{
get
{
throw new NotImplementedException("尚未重载 ItemCount");
}
}
public virtual NormalResult Init(int dev_index)
{
return new NormalResult { Value = -1,
ErrorInfo = "尚未重载 Init() 函数",
ErrorCode = "notSupport",
};
}
public virtual NormalResult Free()
{
return new NormalResult { Value = -1,
ErrorInfo = "尚未重载 Free() 函数",
ErrorCode = "notSupport",
};
}
// GetRegisterString() 过程中所使用的 CancellationTokenSource 对象
public CancellationTokenSource _cancelOfRegister = new CancellationTokenSource();
public virtual void CancelRegisterString()
{
_cancelOfRegister?.Cancel();
}
public void TriggerGetImage(GetImageEventArgs e)
{
this.GetImage?.Invoke(this, e);
}
public Image TryGetImage()
{
var e = new GetImageEventArgs();
this.TriggerGetImage(e);
return e.Image;
}
// 2021/11/2
// 还能获得文本附加信息
public Image TryGetImage(out string text)
{
var e = new GetImageEventArgs();
this.TriggerGetImage(e);
text = e.Text;
return e.Image;
}
public void TriggerGetIrImage(GetImageEventArgs e)
{
this.GetIrImage?.Invoke(this, e);
}
public Image TryGetIrImage()
{
var e = new GetImageEventArgs();
this.TriggerGetIrImage(e);
return e.Image;
}
public virtual TextResult GetRegisterString(Image image,
string strExcludeBarcodes)
{
return new TextResult
{
Value = -1,
ErrorInfo = "尚未重载 GetRegisterString(Image, string) 函数",
ErrorCode = "notSupport",
};
}
// 2021/5/17
// 新版本,支持双目图像
public virtual TextResult GetRegisterString(Image image,
Image irImage,
string strExcludeBarcodes)
{
return new TextResult
{
Value = -1,
ErrorInfo = "尚未重载 GetRegisterString(Image, Image, string) 函数",
ErrorCode = "notSupport",
};
}
public virtual void StartCapture(CancellationToken token)
{
}
// parameters:
// style 处理风格。(2023/12/29 新增的此参数)
public virtual RecognitionFaceResult RecongnitionFace(Image image,
Image irImage,
string style,
CancellationToken token)
{
return new RecognitionFaceResult
{
Value = -1,
ErrorInfo = "尚未重载 RecongnitionFace() 函数",
ErrorCode = "notSupport",
};
}
// 活体检测
public virtual LivenessResult Liveness(Image image,
Image irImage,
string style,
CancellationToken token)
{
return new LivenessResult
{
Value = -1,
ErrorInfo = "尚未重载 Liveness() 函数",
ErrorCode = "notSupport",
};
}
// 同步
// 注:中途遇到异常(例如 Loader 抛出异常),可能会丢失 INSERT_BATCH 条以内的日志记录写入 operlog 表
// parameters:
// strLastDate 处理中断或者结束时返回最后处理过的日期
// last_index 处理或中断返回时最后处理过的位置。以后继续处理的时候可以从这个偏移开始
// return:
// -1 出错
// 0 中断
// 1 完成
public ReplicationResult DoReplication(
LibraryChannel channel,
string strStartDate,
string strEndDate,
LogType logType,
string serverVersion,
CancellationToken token)
{
string strLastDate = "";
long last_index = -1; // -1 表示尚未处理
// bool bUserChanged = false;
ProcessInfo info = new ProcessInfo();
// strStartDate 里面可能会包含 ":1-100" 这样的附加成分
StringUtil.ParseTwoPart(strStartDate,
":",
out string strLeft,
out string strRight);
strStartDate = strLeft;
if (string.IsNullOrEmpty(strStartDate) == true)
{
return new ReplicationResult
{
Value = -1,
ErrorInfo = "DoReplication() 出错: strStartDate 参数值不应为空"
};
}
try
{
int nRet = OperLogLoader.MakeLogFileNames(strStartDate,
strEndDate,
true, // 是否包含扩展名 ".log"
out List<string> dates,
out string strWarning,
out string strError);
if (nRet == -1)
{
return new ReplicationResult
{
Value = -1,
ErrorInfo = strError
};
}
if (dates.Count > 0 && string.IsNullOrEmpty(strRight) == false)
{
dates[0] = dates[0] + ":" + strRight;
}
// using (SQLiteConnection connection = new SQLiteConnection(this._connectionString))
{
ProgressEstimate estimate = new ProgressEstimate();
OperLogLoader loader = new OperLogLoader
{
Channel = channel,
Stop = null, // this.Progress;
// loader.owner = this;
Estimate = estimate,
Dates = dates,
Level = 0, // 2019/7/23 注:2 最简略。不知何故以前用了这个级别。缺点是 oldRecord 元素缺乏 InnerText
AutoCache = false,
CacheDir = "",
LogType = logType,
Filter = "setReaderInfo",
ServerVersion = serverVersion
};
TimeSpan old_timeout = channel.Timeout;
channel.Timeout = new TimeSpan(0, 2, 0); // 二分钟
loader.Prompt += Loader_Prompt;
try
{
// int nRecCount = 0;
string strLastItemDate = "";
long lLastItemIndex = -1;
// TODO: 计算遍历耗费的时间。如果太短了,要想办法让调主知道这一点,放缓重新调用的节奏,以避免 CPU 和网络资源太高
foreach (OperLogItem item in loader)
{
token.ThrowIfCancellationRequested();
//if (stop != null)
// stop.SetMessage("正在同步 " + item.Date + " " + item.Index.ToString() + " " + estimate.Text + "...");
if (string.IsNullOrEmpty(item.Xml) == true)
goto CONTINUE;
XmlDocument dom = new XmlDocument();
try
{
dom.LoadXml(item.Xml);
}
catch (Exception ex)
{
if (this.HasLoaderPrompt())
{
strError = logType.ToString() + "日志记录 " + item.Date + " " + item.Index.ToString() + " XML 装入 DOM 的时候发生错误: " + ex.Message;
MessagePromptEventArgs e = new MessagePromptEventArgs
{
MessageText = strError + "\r\n\r\n是否跳过此条继续处理?\r\n\r\n(确定: 跳过; 取消: 停止全部操作)",
IncludeOperText = true,
// + "\r\n\r\n是否跳过此条继续处理?",
Actions = "yes,cancel"
};
Loader_Prompt(channel, e);
if (e.ResultAction == "cancel")
throw new ChannelException(channel.ErrorCode, strError);
else if (e.ResultAction == "yes")
continue;
else
{
// no 也是抛出异常。因为继续下一批代价太大
throw new ChannelException(channel.ErrorCode, strError);
}
}
else
throw new ChannelException(channel.ErrorCode, strError);
}
string strOperation = DomUtil.GetElementText(dom.DocumentElement, "operation");
if (strOperation == "setReaderInfo")
{
nRet = TraceSetReaderInfo(
dom,
info,
out strError);
}
else
continue;
if (nRet == -1)
{
strError = "同步 " + item.Date + " " + item.Index.ToString() + " 时出错: " + strError;
if (this.HasLoaderPrompt())
{
MessagePromptEventArgs e = new MessagePromptEventArgs
{
MessageText = strError + "\r\n\r\n是否跳过此条继续处理?\r\n\r\n(确定: 跳过; 取消: 停止全部操作)",
IncludeOperText = true,
// + "\r\n\r\n是否跳过此条继续处理?",
Actions = "yes,cancel"
};
Loader_Prompt(channel, e);
if (e.ResultAction == "cancel")
throw new Exception(strError);
else if (e.ResultAction == "yes")
continue;
else
{
// no 也是抛出异常。因为继续下一批代价太大
throw new Exception(strError);
}
}
else
throw new ChannelException(channel.ErrorCode, strError);
}
// lProcessCount++;
CONTINUE:
// 便于循环外获得这些值
strLastItemDate = item.Date;
lLastItemIndex = item.Index + 1;
// index = 0; // 第一个日志文件后面的,都从头开始了
}
// 记忆
strLastDate = strLastItemDate;
last_index = lLastItemIndex;
}
finally
{
loader.Prompt -= Loader_Prompt;
channel.Timeout = old_timeout;
}
}
return new ReplicationResult
{
Value = last_index == -1 ? 0 : 1,
LastDate = strLastDate,
LastIndex = last_index,
ProcessInfo = info
};
}
catch (ChannelException ex)
{
return new ReplicationResult
{
Value = -1,
ErrorInfo = ex.Message,
ProcessInfo = info
};
}
catch (InterruptException ex)
{
// 2019/7/4
return new ReplicationResult
{
Value = -1,
ErrorInfo = ex.Message,
ProcessInfo = info
};
}
catch (Exception ex)
{
string strError = "ReportForm DoReplication() exception: " + ExceptionUtil.GetDebugText(ex);
return new ReplicationResult
{
Value = -1,
ErrorInfo = strError,
ProcessInfo = info
};
}
}
// TODO: 考虑有一种机制可以让 fingerprintcenter 或者 facecenter 的操作历史中能显示出变化情况,至少是缓存事项的数量变化
// SetReaderInfo() API 恢复动作
/*
<root>
<operation>setReaderInfo</operation> 操作类型
<action>...</action> 具体动作。有new change delete move 4种
<record recPath='...'>...</record> 新记录
<oldRecord recPath='...'>...</oldRecord> 被覆盖或者删除的记录 动作为change和delete时具备此元素
<changedEntityRecord itemBarcode='...' recPath='...' oldBorrower='...' newBorrower='...' /> 若干个元素。表示连带发生修改的册记录
<operator>test</operator> 操作者
<operTime>Fri, 08 Dec 2006 09:01:38 GMT</operTime> 操作时间
</root>
注: new 的时候只有<record>元素,delete的时候只有<oldRecord>元素,change的时候两者都有
* */
int TraceSetReaderInfo(
XmlDocument domLog,
ProcessInfo info,
out string strError)
{
strError = "";
string strAction = DomUtil.GetElementText(domLog.DocumentElement, "action");
if (strAction == "new"
|| strAction == "change"
|| strAction == "move")
{
string strNewRecPath = "";
string strRecord = DomUtil.GetElementText(domLog.DocumentElement,
"record",
out XmlNode node);
if (node == null)
{
// 2019/11/5
// 注: move 操作,分馆账户获得日志记录时候可能会被 dp2library 滤除 record 元素。
// 此种情况可以理解为 delete 操作
if (strAction != "move")
{
strError = $"日志记录中缺<record>元素。日志记录内容如下:{domLog.OuterXml}";
return -1;
}
}
else
{
strNewRecPath = DomUtil.GetAttr(node, "recPath");
}
string strOldRecord = "";
string strOldRecPath = "";
// if (strAction == "move")
{
strOldRecord = DomUtil.GetElementText(domLog.DocumentElement,
"oldRecord",
out node);
/*
if (node == null)
{
strError = "日志记录中缺<oldRecord>元素";
return -1;
}*/
if (node != null)
{
strOldRecPath = DomUtil.GetAttr(node, "recPath");
if (string.IsNullOrEmpty(strOldRecPath) == true)
{
strError = "日志记录中<oldRecord>元素内缺recPath属性值";
return -1;
}
}
// 如果移动过程中没有修改,则要用旧的记录内容写入目标
// 注意:如果 record 元素都不存在,则应该理解为 delete。如果 record 元素存在,即 recPath 属性存在但 InnerText 不存在,则当作移动过程记录没有变化,即采用 oldRecord 的 InnerText 作为新记录内容
if (string.IsNullOrEmpty(strRecord) == true
&& string.IsNullOrEmpty(strNewRecPath) == false)
strRecord = strOldRecord;
}
// TODO: change 动作也可能删除 face 元素
/*
// 删除旧记录对应的指纹缓存
if (strAction == "move"
&& string.IsNullOrEmpty(strOldRecord) == false)
{
if (DeleteFingerPrint(strOldRecord, info, out strError) == -1)
return -1;
}
*/
/*
if (AddFingerPrint(strRecord, info, out strError) == -1)
return -1;
*/
if (ModifyFingerPrint(strOldRecord,
strRecord,
info,
out strError) == -1)
return -1;
}
else if (strAction == "delete")
{
string strOldRecord = DomUtil.GetElementText(domLog.DocumentElement,
"oldRecord",
out XmlNode node);
if (node == null)
{
strError = "日志记录中缺<oldRecord>元素";
return -1;
}
string strRecPath = DomUtil.GetAttr(node, "recPath");
/*
if (string.IsNullOrEmpty(strOldRecord) == false)
{
if (DeleteFingerPrint(strOldRecord, info, out strError) == -1)
return -1;
}
*/
if (ModifyFingerPrint(strOldRecord,
"<root />",
info,
out strError) == -1)
return -1;
}
else
{
strError = "无法识别的<action>内容 '" + strAction + "'";
return -1;
}
return 0;
}
// 修改指纹缓存,或者删除指纹缓存
int ModifyFingerPrint(
string strOldRecord,
string strNewRecord,
ProcessInfo info,
out string strError)
{
strError = "";
XmlDocument old_dom = new XmlDocument();
try
{
if (string.IsNullOrEmpty(strOldRecord))
old_dom.LoadXml("<root />");
else
old_dom.LoadXml(strOldRecord);
}
catch (Exception ex)
{
strError = $"strOldRecord 装载到 XmlDocument 时出现异常: {ex.Message}";
return -1;
}
XmlDocument new_dom = new XmlDocument();
try
{
if (string.IsNullOrEmpty(strNewRecord))
new_dom.LoadXml("<root />");
else
new_dom.LoadXml(strNewRecord);
}
catch (Exception ex)
{
strError = $"strNewRecord 装载到 XmlDocument 时出现异常: {ex.Message}";
return -1;
}
string strOldReaderBarcode = GetReaderBarcode(old_dom);
string strOldFingerPrintString = DomUtil.GetElementText(old_dom.DocumentElement,
this.ElementName);
string strNewReaderBarcode = GetReaderBarcode(new_dom);
//if (string.IsNullOrEmpty(strNewReaderBarcode))
// return 0;
string strNewFingerPrintString = DomUtil.GetElementText(new_dom.DocumentElement,
this.ElementName);
// *** 看新旧记录之间 fingerprint 之间的差异。有差异才需要覆盖进入高速缓存
// 证条码号没有发生变化的情况
if (strOldReaderBarcode == strNewReaderBarcode)
{
if (strOldFingerPrintString == strNewFingerPrintString)
return 0; // 指纹特征没有发生变化
if (string.IsNullOrEmpty(strOldReaderBarcode))
return 0; // 空条码号忽视处理
FingerprintItem item = new FingerprintItem
{
FingerprintString = strNewFingerPrintString,
ReaderBarcode = strNewReaderBarcode
};
// return:
// 0 成功
// 其他 失败。错误码
int nRet = AddItems(
new List<FingerprintItem> { item },
info,
out strError);
if (nRet != 0)
return -1;
return 1;
}
// 证条码号发生了变化的情况。两步处理
// 1) 删除旧的
ProcessInfo info1 = new ProcessInfo();
if (string.IsNullOrEmpty(strOldReaderBarcode) == false)
{
FingerprintItem item = new FingerprintItem
{
FingerprintString = "",
ReaderBarcode = strOldReaderBarcode
};
// return:
// 0 成功
// 其他 失败。错误码
int nRet = AddItems(
new List<FingerprintItem> { item },
info1,
out strError);
if (nRet != 0)
return -1;
}
// 2) 增加新的
ProcessInfo info2 = new ProcessInfo();
if (string.IsNullOrEmpty(strNewReaderBarcode) == false)
{
FingerprintItem item = new FingerprintItem
{
FingerprintString = strNewFingerPrintString,
ReaderBarcode = strNewReaderBarcode
};
// return:
// 0 成功
// 其他 失败。错误码
int nRet = AddItems(
new List<FingerprintItem> { item },
info2,
out strError);
if (nRet != 0)
return -1;
}
if (info != null)
{
info.ChangeCount += info1.ChangeCount + info2.ChangeCount;
info.DeleteCount += info1.DeleteCount + info2.DeleteCount;
info.NewCount += info1.NewCount + info2.NewCount;
}
return 1;
}
#if NO
// 写入新记录的指纹缓存,或者删除指纹缓存
int AddFingerPrint(string strRecord,
ProcessInfo info,
out string strError)
{
strError = "";
XmlDocument new_dom = new XmlDocument();
new_dom.LoadXml(strRecord);
string strReaderBarcode = GetReaderBarcode(new_dom);
if (string.IsNullOrEmpty(strReaderBarcode))
return 0;
string strFingerPrintString = DomUtil.GetElementText(new_dom.DocumentElement,
this.ElementName);
// TODO: 看新旧记录之间 fingerprint 之间的差异。有差异才需要覆盖进入高速缓存
FingerprintItem item = new FingerprintItem
{
FingerprintString = strFingerPrintString,
ReaderBarcode = strReaderBarcode
};
// return:
// 0 成功
// 其他 失败。错误码
int nRet = AddItems(
new List<FingerprintItem> { item },
info,
out strError);
if (nRet != 0)
return -1;
return 1;
}
#endif
#if NO
int DeleteFingerPrint(string strOldRecord,
ProcessInfo info,
out string strError)
{
strError = "";
XmlDocument old_dom = new XmlDocument();
old_dom.LoadXml(strOldRecord);
string strReaderBarcode = GetReaderBarcode(old_dom);
if (string.IsNullOrEmpty(strReaderBarcode) == false)
{
FingerprintItem item = new FingerprintItem
{
FingerprintString = "",
ReaderBarcode = strReaderBarcode
};
// return:
// 0 成功
// 其他 失败。错误码
int nRet = AddItems(
new List<FingerprintItem> { item },
info,
out strError);
if (nRet != 0)
return -1;
}
return 0;
}
#endif
static string GetReaderBarcode(XmlDocument dom)
{
string strReaderBarcode = DomUtil.GetElementText(dom.DocumentElement,
"barcode");
if (string.IsNullOrEmpty(strReaderBarcode) == false)
return strReaderBarcode;
string strRefID = DomUtil.GetElementText(dom.DocumentElement, "refID");
if (string.IsNullOrEmpty(strRefID))
return "";
return "@refID:" + strRefID;
}
public class ReplicationPlan : NormalResult
{
public string StartDate { get; set; }
}
// 整体获得读者指纹信息以前,预备获得同步计划信息
// 也就是第一次同步开始的位置信息
public static ReplicationPlan GetReplicationPlan(LibraryChannel channel)
{
// 开始处理时的日期
string strEndDate = DateTimeUtil.DateTimeToString8(DateTime.Now);
// 获得日志文件中记录的总数
// parameters:
// strDate 日志文件的日期,8 字符
// return:
// -2 此类型的日志在 dp2library 端尚未启用
// -1 出错
// 0 日志文件不存在,或者记录数为 0
// >0 记录数
long lCount = OperLogLoader.GetOperLogCount(
null,
channel,
strEndDate,
LogType.OperLog,
out string strError);
if (lCount < 0)
{
// errorCode: "RequestError" 服务器没有响应
return new ReplicationPlan { Value = -1, ErrorInfo = strError, ErrorCode = channel.ErrorCode.ToString() };
}
return new ReplicationPlan { StartDate = strEndDate + ":" + lCount + "-" };
}
static int GetDbNamesByCacheDir(string strDir,
out List<string> dbnames,
out string strError)
{
strError = "";
dbnames = new List<string>();
DirectoryInfo di = new DirectoryInfo(strDir);
FileInfo[] fis = di.GetFiles("*.");
foreach (var fi in fis)
{
dbnames.Add(fi.Name);
}
return 1;
}
// parameters:
// channel 通讯通道。如果为 null,表示希望根据以前的缓存文件初始化生物特征高速缓存,而不是从 dp2library 获取和更新信息
// style force_update/force_create/in_memory
// return:
// -1 出错
// >=0 成功。返回实际初始化的事项
public NormalResult InitFingerprintCache(
LibraryChannel channel,
string strDir,
string style,
CancellationToken token)
{
string strError = "";
try
{
// 清空以前的全部缓存内容,以便重新建立
// return:
// -1 出错
// >=0 实际发送给接口程序的事项数目
int nRet = CreateFingerprintCache(null, null,
out strError);
if (nRet == -1 || nRet == -2)
return new NormalResult { Value = nRet, ErrorInfo = strError };
// this.Prompt("正在初始化指纹缓存 ...\r\n请不要关闭本窗口\r\n\r\n(在此过程中,与指纹识别无关的窗口和功能不受影响,可前往使用)\r\n");
List<string> readerdbnames = null;
if (channel == null)
{
// 根据已存在的缓存文件名列出读者库名
nRet = GetDbNamesByCacheDir(strDir,
out readerdbnames,
out strError);
if (nRet == -1)
{
return new NormalResult { Value = -1, ErrorInfo = strError, ErrorCode = channel.ErrorCode.ToString() };
}
}
else
{
nRet = GetCurrentOwnerReaderNameList(
channel,
out readerdbnames,
out strError);
if (nRet == -1)
{
return new NormalResult { Value = -1, ErrorInfo = strError, ErrorCode = channel.ErrorCode.ToString() };
}
}
if (readerdbnames.Count == 0)
{
strError = $"因当前用户没有管辖任何读者库,初始化{this.BioTypeName}缓存的操作无法完成";
return new NormalResult { Value = -1, ErrorInfo = strError };
}
this.SetProgress(0, readerdbnames.Count);
int nCount = 0;
// 对这些读者库逐个进行高速缓存的初始化
// 使用 特殊的 browse 格式,以便获得读者记录中的 fingerprint timestamp字符串,或者兼获得 fingerprint string
// <fingerprint timestamp='XXXX'></fingerprint>
int i = 0;
foreach (string strReaderDbName in readerdbnames)
{
// 初始化一个读者库的指纹缓存
// return:
// -1 出错
// >=0 实际发送给接口程序的事项数目
nRet = BuildOneDbCache(
channel,
strDir,
strReaderDbName,
style,
token,
out strError);
if (nRet == -1)
return new NormalResult { Value = -1, ErrorInfo = strError };
nCount += nRet;
i++;
this.SetProgress(i, readerdbnames.Count);
}
#if NO
if (nCount == 0)
{
strError = "因当前用户管辖的读者库 " + StringUtil.MakePathList(readerdbnames) + " 中没有任何具有指纹信息的读者记录,初始化指纹缓存的操作没有完成";
return -1;
}
#endif
if (nCount == 0)
{
strError = $"当前用户管辖的读者库 { StringUtil.MakePathList(readerdbnames) } 中没有任何具有{this.BioTypeName}信息的读者记录,{this.BioTypeName}缓存为空";
return new NormalResult();
}
this.ShowMessage($"{this.BioTypeName}缓存初始化成功");
return new NormalResult { Value = nCount };
}
catch (Exception ex)
{
strError = ex.Message;
return new NormalResult { Value = -1, ErrorInfo = strError };
}
}
// 根据结果集文件初始化指纹高速缓存
// parameters:
// resultset 用于初始化的结果集对象。如果为 null,表示希望清空指纹高速缓存
// 一般可用 null 调用一次,然后用多个 resultset 对象逐个调用一次
// return:
// -1 出错
// >=0 实际发送给接口程序的事项数目
int CreateFingerprintCache(DpResultSet resultset,
ProcessInfo info,
out string strError)
{
strError = "";
int nRet = 0;
this.ShowMessage("加入高速缓存");
try
{
if (resultset == null)