id-scanner-lib 1.6.5 → 1.6.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/id-scanner-lib.esm.js +107 -67
- package/dist/id-scanner-lib.esm.js.map +1 -1
- package/dist/id-scanner-lib.js +107 -66
- package/dist/id-scanner-lib.js.map +1 -1
- package/package.json +1 -1
- package/src/core/event-emitter.ts +9 -0
- package/src/core/logger.ts +52 -34
- package/src/core/resource-manager.ts +23 -9
- package/src/core/result.ts +3 -3
- package/src/index.ts +5 -5
- package/src/modules/face/face-detector.ts +8 -6
- package/src/modules/face/liveness-detector.ts +3 -2
- package/src/modules/id-card/index.ts +10 -8
- package/src/utils/image-processing.ts +22 -31
- package/src/core/plugin-manager.ts +0 -429
- package/src/core/scanner-factory.ts +0 -236
|
@@ -1,429 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file 插件管理器
|
|
3
|
-
* @description 提供插件的注册、初始化和管理功能
|
|
4
|
-
* @module core/plugin-manager
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { EventEmitter } from 'events';
|
|
8
|
-
import { Logger } from './logger';
|
|
9
|
-
import { ConfigManager } from './config';
|
|
10
|
-
import { IDScannerError } from './errors';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 插件优先级枚举
|
|
14
|
-
*/
|
|
15
|
-
export enum PluginPriority {
|
|
16
|
-
/** 高优先级,最先初始化和激活 */
|
|
17
|
-
HIGH = 'high',
|
|
18
|
-
/** 中等优先级 */
|
|
19
|
-
NORMAL = 'normal',
|
|
20
|
-
/** 低优先级,最后初始化和激活 */
|
|
21
|
-
LOW = 'low'
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 插件状态枚举
|
|
26
|
-
*/
|
|
27
|
-
export enum PluginStatus {
|
|
28
|
-
/** 已注册 */
|
|
29
|
-
REGISTERED = 'registered',
|
|
30
|
-
/** 初始化中 */
|
|
31
|
-
INITIALIZING = 'initializing',
|
|
32
|
-
/** 已初始化 */
|
|
33
|
-
INITIALIZED = 'initialized',
|
|
34
|
-
/** 激活中 */
|
|
35
|
-
ACTIVATING = 'activating',
|
|
36
|
-
/** 已激活 */
|
|
37
|
-
ACTIVE = 'active',
|
|
38
|
-
/** 已停用 */
|
|
39
|
-
INACTIVE = 'inactive',
|
|
40
|
-
/** 错误状态 */
|
|
41
|
-
ERROR = 'error'
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* 插件接口
|
|
46
|
-
*/
|
|
47
|
-
export interface Plugin {
|
|
48
|
-
/** 插件ID */
|
|
49
|
-
id: string;
|
|
50
|
-
/** 插件名称 */
|
|
51
|
-
name: string;
|
|
52
|
-
/** 插件版本 */
|
|
53
|
-
version: string;
|
|
54
|
-
/** 插件描述 */
|
|
55
|
-
description?: string;
|
|
56
|
-
/** 插件优先级 */
|
|
57
|
-
priority?: PluginPriority;
|
|
58
|
-
/** 依赖的插件ID列表 */
|
|
59
|
-
dependencies?: string[];
|
|
60
|
-
/** 初始化方法 */
|
|
61
|
-
initialize?: (api: PluginAPI) => Promise<void>;
|
|
62
|
-
/** 激活方法 */
|
|
63
|
-
activate?: (api: PluginAPI) => Promise<void>;
|
|
64
|
-
/** 停用方法 */
|
|
65
|
-
deactivate?: (api: PluginAPI) => Promise<void>;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* 插件API接口
|
|
70
|
-
* 提供给插件使用的API
|
|
71
|
-
*/
|
|
72
|
-
export interface PluginAPI {
|
|
73
|
-
/** 日志记录器 */
|
|
74
|
-
logger: Logger;
|
|
75
|
-
/** 配置管理器 */
|
|
76
|
-
config: ConfigManager;
|
|
77
|
-
/** 事件发射器 */
|
|
78
|
-
eventBus: EventEmitter;
|
|
79
|
-
/** 获取插件 */
|
|
80
|
-
getPlugin: (id: string) => Plugin | undefined;
|
|
81
|
-
/** 获取所有插件 */
|
|
82
|
-
getAllPlugins: () => Map<string, Plugin>;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* 插件管理器
|
|
87
|
-
* 提供插件的注册、初始化和管理功能
|
|
88
|
-
*/
|
|
89
|
-
export class PluginManager extends EventEmitter {
|
|
90
|
-
/** 单例实例 */
|
|
91
|
-
private static instance: PluginManager;
|
|
92
|
-
|
|
93
|
-
/** 日志记录器 */
|
|
94
|
-
private logger: Logger;
|
|
95
|
-
|
|
96
|
-
/** 配置管理器 */
|
|
97
|
-
private config: ConfigManager;
|
|
98
|
-
|
|
99
|
-
/** 插件映射表 */
|
|
100
|
-
private plugins: Map<string, Plugin> = new Map();
|
|
101
|
-
|
|
102
|
-
/** 插件状态 */
|
|
103
|
-
private pluginStatus: Map<string, PluginStatus> = new Map();
|
|
104
|
-
|
|
105
|
-
/** 插件API */
|
|
106
|
-
private pluginAPI: PluginAPI;
|
|
107
|
-
|
|
108
|
-
/** 初始化状态 */
|
|
109
|
-
private initialized: boolean = false;
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* 私有构造函数
|
|
113
|
-
*/
|
|
114
|
-
private constructor() {
|
|
115
|
-
super();
|
|
116
|
-
|
|
117
|
-
this.logger = Logger.getInstance();
|
|
118
|
-
this.config = ConfigManager.getInstance();
|
|
119
|
-
|
|
120
|
-
// 创建插件API
|
|
121
|
-
this.pluginAPI = {
|
|
122
|
-
logger: this.logger,
|
|
123
|
-
config: this.config,
|
|
124
|
-
eventBus: this,
|
|
125
|
-
getPlugin: this.getPlugin.bind(this),
|
|
126
|
-
getAllPlugins: this.getAllPlugins.bind(this)
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* 获取单例实例
|
|
132
|
-
*/
|
|
133
|
-
public static getInstance(): PluginManager {
|
|
134
|
-
if (!PluginManager.instance) {
|
|
135
|
-
PluginManager.instance = new PluginManager();
|
|
136
|
-
}
|
|
137
|
-
return PluginManager.instance;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* 初始化插件管理器
|
|
142
|
-
*/
|
|
143
|
-
public async initialize(): Promise<void> {
|
|
144
|
-
if (this.initialized) {
|
|
145
|
-
this.logger.debug('PluginManager', '插件管理器已初始化');
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
this.logger.debug('PluginManager', '初始化插件管理器');
|
|
150
|
-
|
|
151
|
-
this.initialized = true;
|
|
152
|
-
this.emit('manager:initialized', {});
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* 注册插件
|
|
157
|
-
* @param plugin 插件
|
|
158
|
-
*/
|
|
159
|
-
public registerPlugin(plugin: Plugin): void {
|
|
160
|
-
if (!plugin.id) {
|
|
161
|
-
throw new Error('插件ID不能为空');
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if (this.plugins.has(plugin.id)) {
|
|
165
|
-
this.logger.warn('PluginManager', `插件已注册: ${plugin.id}`);
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// 设置默认优先级
|
|
170
|
-
if (!plugin.priority) {
|
|
171
|
-
plugin.priority = PluginPriority.NORMAL;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
this.plugins.set(plugin.id, plugin);
|
|
175
|
-
this.pluginStatus.set(plugin.id, PluginStatus.REGISTERED);
|
|
176
|
-
|
|
177
|
-
this.logger.info('PluginManager', `注册插件: ${plugin.name} (${plugin.id}) v${plugin.version}`);
|
|
178
|
-
this.emit('plugin:registered', { plugin });
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* 卸载插件
|
|
183
|
-
* @param id 插件ID
|
|
184
|
-
*/
|
|
185
|
-
public unregisterPlugin(id: string): boolean {
|
|
186
|
-
if (!this.plugins.has(id)) {
|
|
187
|
-
return false;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// 如果插件处于激活状态,先停用
|
|
191
|
-
if (this.pluginStatus.get(id) === PluginStatus.ACTIVE) {
|
|
192
|
-
this.deactivatePlugin(id);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
const plugin = this.plugins.get(id)!;
|
|
196
|
-
this.plugins.delete(id);
|
|
197
|
-
this.pluginStatus.delete(id);
|
|
198
|
-
|
|
199
|
-
this.logger.info('PluginManager', `卸载插件: ${plugin.name} (${id})`);
|
|
200
|
-
this.emit('plugin:unregistered', { plugin });
|
|
201
|
-
|
|
202
|
-
return true;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* 初始化插件
|
|
207
|
-
* @param id 插件ID
|
|
208
|
-
*/
|
|
209
|
-
public async initializePlugin(id: string): Promise<boolean> {
|
|
210
|
-
if (!this.plugins.has(id)) {
|
|
211
|
-
this.logger.warn('PluginManager', `初始化失败: 插件不存在 ${id}`);
|
|
212
|
-
return false;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
const plugin = this.plugins.get(id)!;
|
|
216
|
-
const status = this.pluginStatus.get(id)!;
|
|
217
|
-
|
|
218
|
-
if (status === PluginStatus.INITIALIZED || status === PluginStatus.ACTIVE) {
|
|
219
|
-
this.logger.debug('PluginManager', `插件已初始化: ${id}`);
|
|
220
|
-
return true;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
if (status === PluginStatus.INITIALIZING) {
|
|
224
|
-
this.logger.warn('PluginManager', `插件正在初始化中: ${id}`);
|
|
225
|
-
return false;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
// 检查依赖
|
|
229
|
-
if (plugin.dependencies && plugin.dependencies.length > 0) {
|
|
230
|
-
for (const depId of plugin.dependencies) {
|
|
231
|
-
if (!this.plugins.has(depId)) {
|
|
232
|
-
this.logger.error('PluginManager', `初始化失败: 依赖的插件不存在 ${depId}`);
|
|
233
|
-
this.pluginStatus.set(id, PluginStatus.ERROR);
|
|
234
|
-
return false;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// 初始化依赖插件
|
|
238
|
-
const depStatus = this.pluginStatus.get(depId);
|
|
239
|
-
if (depStatus !== PluginStatus.INITIALIZED && depStatus !== PluginStatus.ACTIVE) {
|
|
240
|
-
const success = await this.initializePlugin(depId);
|
|
241
|
-
if (!success) {
|
|
242
|
-
this.logger.error('PluginManager', `初始化失败: 依赖的插件初始化失败 ${depId}`);
|
|
243
|
-
this.pluginStatus.set(id, PluginStatus.ERROR);
|
|
244
|
-
return false;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
this.pluginStatus.set(id, PluginStatus.INITIALIZING);
|
|
251
|
-
this.emit('plugin:initializing', { plugin });
|
|
252
|
-
|
|
253
|
-
try {
|
|
254
|
-
if (plugin.initialize) {
|
|
255
|
-
await plugin.initialize(this.pluginAPI);
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
this.pluginStatus.set(id, PluginStatus.INITIALIZED);
|
|
259
|
-
this.logger.debug('PluginManager', `插件初始化成功: ${plugin.name} (${id})`);
|
|
260
|
-
this.emit('plugin:initialized', { plugin });
|
|
261
|
-
|
|
262
|
-
return true;
|
|
263
|
-
} catch (error) {
|
|
264
|
-
this.pluginStatus.set(id, PluginStatus.ERROR);
|
|
265
|
-
this.logger.error('PluginManager', `插件初始化失败: ${id}`, error as Error);
|
|
266
|
-
this.emit('plugin:error', { plugin, error });
|
|
267
|
-
|
|
268
|
-
return false;
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* 激活插件
|
|
274
|
-
* @param id 插件ID
|
|
275
|
-
*/
|
|
276
|
-
public async activatePlugin(id: string): Promise<boolean> {
|
|
277
|
-
if (!this.plugins.has(id)) {
|
|
278
|
-
this.logger.warn('PluginManager', `激活失败: 插件不存在 ${id}`);
|
|
279
|
-
return false;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
const plugin = this.plugins.get(id)!;
|
|
283
|
-
const status = this.pluginStatus.get(id)!;
|
|
284
|
-
|
|
285
|
-
if (status === PluginStatus.ACTIVE) {
|
|
286
|
-
this.logger.debug('PluginManager', `插件已激活: ${id}`);
|
|
287
|
-
return true;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
if (status === PluginStatus.ACTIVATING) {
|
|
291
|
-
this.logger.warn('PluginManager', `插件正在激活中: ${id}`);
|
|
292
|
-
return false;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
if (status !== PluginStatus.INITIALIZED) {
|
|
296
|
-
// 尝试初始化插件
|
|
297
|
-
const success = await this.initializePlugin(id);
|
|
298
|
-
if (!success) {
|
|
299
|
-
this.logger.error('PluginManager', `激活失败: 插件初始化失败 ${id}`);
|
|
300
|
-
return false;
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
// 激活依赖插件
|
|
305
|
-
if (plugin.dependencies && plugin.dependencies.length > 0) {
|
|
306
|
-
for (const depId of plugin.dependencies) {
|
|
307
|
-
const depStatus = this.pluginStatus.get(depId);
|
|
308
|
-
if (depStatus !== PluginStatus.ACTIVE) {
|
|
309
|
-
const success = await this.activatePlugin(depId);
|
|
310
|
-
if (!success) {
|
|
311
|
-
this.logger.error('PluginManager', `激活失败: 依赖的插件激活失败 ${depId}`);
|
|
312
|
-
return false;
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
this.pluginStatus.set(id, PluginStatus.ACTIVATING);
|
|
319
|
-
this.emit('plugin:activating', { plugin });
|
|
320
|
-
|
|
321
|
-
try {
|
|
322
|
-
if (plugin.activate) {
|
|
323
|
-
await plugin.activate(this.pluginAPI);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
this.pluginStatus.set(id, PluginStatus.ACTIVE);
|
|
327
|
-
this.logger.info('PluginManager', `插件激活成功: ${plugin.name} (${id})`);
|
|
328
|
-
this.emit('plugin:activated', { plugin });
|
|
329
|
-
|
|
330
|
-
return true;
|
|
331
|
-
} catch (error) {
|
|
332
|
-
this.pluginStatus.set(id, PluginStatus.ERROR);
|
|
333
|
-
this.logger.error('PluginManager', `插件激活失败: ${id}`, error as Error);
|
|
334
|
-
this.emit('plugin:error', { plugin, error });
|
|
335
|
-
|
|
336
|
-
return false;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
/**
|
|
341
|
-
* 停用插件
|
|
342
|
-
* @param id 插件ID
|
|
343
|
-
*/
|
|
344
|
-
public async deactivatePlugin(id: string): Promise<boolean> {
|
|
345
|
-
if (!this.plugins.has(id)) {
|
|
346
|
-
this.logger.warn('PluginManager', `停用失败: 插件不存在 ${id}`);
|
|
347
|
-
return false;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
const plugin = this.plugins.get(id)!;
|
|
351
|
-
const status = this.pluginStatus.get(id)!;
|
|
352
|
-
|
|
353
|
-
if (status !== PluginStatus.ACTIVE) {
|
|
354
|
-
this.logger.debug('PluginManager', `插件未激活: ${id}`);
|
|
355
|
-
return true;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
// 检查依赖关系:如果有其他激活的插件依赖于此插件,则不能停用
|
|
359
|
-
for (const [pluginId, p] of this.plugins.entries()) {
|
|
360
|
-
if (p.dependencies && p.dependencies.includes(id) && this.pluginStatus.get(pluginId) === PluginStatus.ACTIVE) {
|
|
361
|
-
this.logger.warn('PluginManager', `停用失败: 插件 ${pluginId} 依赖于此插件`);
|
|
362
|
-
return false;
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
this.emit('plugin:deactivating', { plugin });
|
|
367
|
-
|
|
368
|
-
try {
|
|
369
|
-
if (plugin.deactivate) {
|
|
370
|
-
await plugin.deactivate(this.pluginAPI);
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
this.pluginStatus.set(id, PluginStatus.INACTIVE);
|
|
374
|
-
this.logger.info('PluginManager', `插件停用成功: ${plugin.name} (${id})`);
|
|
375
|
-
this.emit('plugin:deactivated', { plugin });
|
|
376
|
-
|
|
377
|
-
return true;
|
|
378
|
-
} catch (error) {
|
|
379
|
-
this.pluginStatus.set(id, PluginStatus.ERROR);
|
|
380
|
-
this.logger.error('PluginManager', `插件停用失败: ${id}`, error as Error);
|
|
381
|
-
this.emit('plugin:error', { plugin, error });
|
|
382
|
-
|
|
383
|
-
return false;
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
/**
|
|
388
|
-
* 获取插件
|
|
389
|
-
* @param id 插件ID
|
|
390
|
-
*/
|
|
391
|
-
public getPlugin(id: string): Plugin | undefined {
|
|
392
|
-
return this.plugins.get(id);
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
/**
|
|
396
|
-
* 获取所有插件
|
|
397
|
-
*/
|
|
398
|
-
public getAllPlugins(): Map<string, Plugin> {
|
|
399
|
-
return new Map(this.plugins);
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
/**
|
|
403
|
-
* 获取插件状态
|
|
404
|
-
* @param id 插件ID
|
|
405
|
-
*/
|
|
406
|
-
public getPluginStatus(id: string): PluginStatus | undefined {
|
|
407
|
-
return this.pluginStatus.get(id);
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
/**
|
|
411
|
-
* 按优先级获取插件
|
|
412
|
-
* @param priority 优先级
|
|
413
|
-
*/
|
|
414
|
-
public getPluginsByPriority(priority: PluginPriority): Plugin[] {
|
|
415
|
-
return Array.from(this.plugins.values())
|
|
416
|
-
.filter(plugin => plugin.priority === priority);
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
/**
|
|
420
|
-
* 获取按优先级排序的所有插件
|
|
421
|
-
*/
|
|
422
|
-
public getSortedPlugins(): Plugin[] {
|
|
423
|
-
const high = this.getPluginsByPriority(PluginPriority.HIGH);
|
|
424
|
-
const normal = this.getPluginsByPriority(PluginPriority.NORMAL);
|
|
425
|
-
const low = this.getPluginsByPriority(PluginPriority.LOW);
|
|
426
|
-
|
|
427
|
-
return [...high, ...normal, ...low];
|
|
428
|
-
}
|
|
429
|
-
}
|
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file 扫描器工厂
|
|
3
|
-
* @description 提供统一的组件创建和访问接口
|
|
4
|
-
* @module core/scanner-factory
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { ConfigManager, GlobalConfig } from './config';
|
|
8
|
-
import { Logger } from './logger';
|
|
9
|
-
import { ResourceManager } from './resource-manager';
|
|
10
|
-
import { EventEmitter } from './event-emitter';
|
|
11
|
-
import { InitializationError } from './errors';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 扫描器初始化选项
|
|
15
|
-
*/
|
|
16
|
-
export interface ScannerFactoryOptions {
|
|
17
|
-
/** 配置选项 */
|
|
18
|
-
config?: Partial<GlobalConfig>;
|
|
19
|
-
/** 资源基础路径 */
|
|
20
|
-
resourceBasePath?: string;
|
|
21
|
-
/** 调试模式 */
|
|
22
|
-
debug?: boolean;
|
|
23
|
-
/** 自动初始化模块 */
|
|
24
|
-
autoInitModules?: boolean;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 扫描器工厂
|
|
29
|
-
* 作为整个库的核心入口点,管理组件生命周期并提供统一接口
|
|
30
|
-
*/
|
|
31
|
-
export class ScannerFactory extends EventEmitter {
|
|
32
|
-
/** 单例实例 */
|
|
33
|
-
private static instance: ScannerFactory;
|
|
34
|
-
/** 配置管理器 */
|
|
35
|
-
private readonly config: ConfigManager;
|
|
36
|
-
/** 日志记录器 */
|
|
37
|
-
private readonly logger: Logger;
|
|
38
|
-
/** 资源管理器 */
|
|
39
|
-
private readonly resources: ResourceManager;
|
|
40
|
-
/** 是否已初始化 */
|
|
41
|
-
private initialized: boolean = false;
|
|
42
|
-
/** 初始化锁,防止多次调用 */
|
|
43
|
-
private initializing: boolean = false;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* 私有构造函数
|
|
47
|
-
*/
|
|
48
|
-
private constructor() {
|
|
49
|
-
super();
|
|
50
|
-
this.config = ConfigManager.getInstance();
|
|
51
|
-
this.logger = Logger.getInstance();
|
|
52
|
-
this.resources = ResourceManager.getInstance();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* 获取单例实例
|
|
57
|
-
*/
|
|
58
|
-
public static getInstance(): ScannerFactory {
|
|
59
|
-
if (!ScannerFactory.instance) {
|
|
60
|
-
ScannerFactory.instance = new ScannerFactory();
|
|
61
|
-
}
|
|
62
|
-
return ScannerFactory.instance;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* 初始化扫描器工厂
|
|
67
|
-
* @param options 初始化选项
|
|
68
|
-
*/
|
|
69
|
-
async initialize(options: ScannerFactoryOptions = {}): Promise<boolean> {
|
|
70
|
-
// 防止重复初始化
|
|
71
|
-
if (this.initialized) {
|
|
72
|
-
this.logger.warn('ScannerFactory', 'Already initialized');
|
|
73
|
-
return true;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (this.initializing) {
|
|
77
|
-
this.logger.warn('ScannerFactory', 'Initialization already in progress');
|
|
78
|
-
return false;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
this.initializing = true;
|
|
82
|
-
|
|
83
|
-
try {
|
|
84
|
-
const {
|
|
85
|
-
config = {},
|
|
86
|
-
resourceBasePath = '',
|
|
87
|
-
debug = false,
|
|
88
|
-
autoInitModules = true
|
|
89
|
-
} = options;
|
|
90
|
-
|
|
91
|
-
// 应用配置
|
|
92
|
-
if (debug) {
|
|
93
|
-
config.debug = true;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
this.config.updateConfig(config);
|
|
97
|
-
|
|
98
|
-
// 设置资源基础路径
|
|
99
|
-
if (resourceBasePath) {
|
|
100
|
-
this.resources.setBasePath(resourceBasePath);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// 记录初始化日志
|
|
104
|
-
this.logger.info('ScannerFactory', `Initializing ID Scanner Library v1.4.0, debug: ${this.config.get('debug', false)}`);
|
|
105
|
-
|
|
106
|
-
// 如果启用了自动初始化模块,则加载相应模块
|
|
107
|
-
if (autoInitModules) {
|
|
108
|
-
await this.initEnabledModules();
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
this.initialized = true;
|
|
112
|
-
this.initializing = false;
|
|
113
|
-
|
|
114
|
-
this.emit('initialized', { success: true });
|
|
115
|
-
this.logger.info('ScannerFactory', 'ID Scanner Library initialized successfully');
|
|
116
|
-
|
|
117
|
-
return true;
|
|
118
|
-
} catch (error) {
|
|
119
|
-
this.initializing = false;
|
|
120
|
-
|
|
121
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
122
|
-
this.logger.error('ScannerFactory', `Initialization failed: ${errorMessage}`, error instanceof Error ? error : undefined);
|
|
123
|
-
|
|
124
|
-
this.emit('initialized', { success: false, error });
|
|
125
|
-
|
|
126
|
-
throw new InitializationError(
|
|
127
|
-
'扫描器初始化失败',
|
|
128
|
-
errorMessage
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* 初始化已启用的模块
|
|
135
|
-
*/
|
|
136
|
-
private async initEnabledModules(): Promise<void> {
|
|
137
|
-
const enabledModules = [];
|
|
138
|
-
|
|
139
|
-
// 检查每个模块的启用状态
|
|
140
|
-
if (this.config.isModuleEnabled('face')) {
|
|
141
|
-
enabledModules.push(this.initFaceModule());
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if (this.config.isModuleEnabled('qr')) {
|
|
145
|
-
enabledModules.push(this.initQRModule());
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (this.config.isModuleEnabled('idcard')) {
|
|
149
|
-
enabledModules.push(this.initIDCardModule());
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (this.config.isModuleEnabled('ocr')) {
|
|
153
|
-
enabledModules.push(this.initOCRModule());
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// 并行初始化所有启用的模块
|
|
157
|
-
if (enabledModules.length > 0) {
|
|
158
|
-
await Promise.all(enabledModules);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* 初始化人脸识别模块
|
|
164
|
-
*/
|
|
165
|
-
private async initFaceModule(): Promise<void> {
|
|
166
|
-
this.logger.info('ScannerFactory', 'Initializing Face module');
|
|
167
|
-
// 实际初始化代码将在模块实现中完成
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* 初始化二维码扫描模块
|
|
172
|
-
*/
|
|
173
|
-
private async initQRModule(): Promise<void> {
|
|
174
|
-
this.logger.info('ScannerFactory', 'Initializing QR Code module');
|
|
175
|
-
// 实际初始化代码将在模块实现中完成
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* 初始化身份证扫描模块
|
|
180
|
-
*/
|
|
181
|
-
private async initIDCardModule(): Promise<void> {
|
|
182
|
-
this.logger.info('ScannerFactory', 'Initializing ID Card module');
|
|
183
|
-
// 实际初始化代码将在模块实现中完成
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* 初始化OCR模块
|
|
188
|
-
*/
|
|
189
|
-
private async initOCRModule(): Promise<void> {
|
|
190
|
-
this.logger.info('ScannerFactory', 'Initializing OCR module');
|
|
191
|
-
// 实际初始化代码将在模块实现中完成
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* 销毁实例,释放资源
|
|
196
|
-
*/
|
|
197
|
-
destroy(): void {
|
|
198
|
-
if (!this.initialized) return;
|
|
199
|
-
|
|
200
|
-
this.logger.info('ScannerFactory', 'Destroying ID Scanner Library');
|
|
201
|
-
|
|
202
|
-
// 释放所有资源
|
|
203
|
-
this.resources.releaseAll();
|
|
204
|
-
|
|
205
|
-
this.initialized = false;
|
|
206
|
-
this.emit('destroyed');
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* 获取配置管理器
|
|
211
|
-
*/
|
|
212
|
-
getConfig(): ConfigManager {
|
|
213
|
-
return this.config;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* 获取日志记录器
|
|
218
|
-
*/
|
|
219
|
-
getLogger(): Logger {
|
|
220
|
-
return this.logger;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* 获取资源管理器
|
|
225
|
-
*/
|
|
226
|
-
getResources(): ResourceManager {
|
|
227
|
-
return this.resources;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* 检查是否已初始化
|
|
232
|
-
*/
|
|
233
|
-
isInitialized(): boolean {
|
|
234
|
-
return this.initialized;
|
|
235
|
-
}
|
|
236
|
-
}
|