id-scanner-lib 1.6.4 → 1.6.6

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.
@@ -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
- }