taro-bluetooth-print 2.8.4 → 2.9.1
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/CHANGELOG.md +33 -0
- package/README.md +53 -4
- package/dist/index.cjs.js +1 -1
- package/dist/index.es.js +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/types/core/BluetoothPrinter.d.ts +21 -4
- package/dist/types/core/di/Container.d.ts +84 -0
- package/dist/types/core/di/Tokens.d.ts +29 -0
- package/dist/types/core/di/index.d.ts +3 -0
- package/dist/types/core/event/EventBus.d.ts +66 -0
- package/dist/types/core/event/index.d.ts +2 -0
- package/dist/types/core/index.d.ts +5 -4
- package/dist/types/core/plugin/PluginManager.d.ts +64 -0
- package/dist/types/core/plugin/index.d.ts +2 -0
- package/dist/types/device/MultiPrinterManager.d.ts +2 -0
- package/dist/types/errors/CommandBuildError.d.ts +40 -0
- package/dist/types/errors/ConnectionError.d.ts +45 -0
- package/dist/types/errors/PrintJobError.d.ts +42 -0
- package/dist/types/errors/index.d.ts +9 -0
- package/dist/types/factory/PrinterFactory.d.ts +108 -0
- package/dist/types/factory/di-factory.d.ts +52 -0
- package/dist/types/factory/index.d.ts +6 -0
- package/dist/types/index.d.ts +9 -1
- package/dist/types/providers/ServiceProvider.d.ts +56 -0
- package/dist/types/providers/index.d.ts +2 -0
- package/dist/types/services/interfaces/ICommandBuilder.d.ts +123 -0
- package/dist/types/services/interfaces/IConnectionManager.d.ts +49 -0
- package/dist/types/services/interfaces/IPrintJobManager.d.ts +67 -0
- package/dist/types/services/interfaces/index.d.ts +6 -233
- package/dist/types/template/TemplateEngine.d.ts +24 -68
- package/dist/types/template/engines/TemplateRenderer.d.ts +71 -0
- package/dist/types/template/parsers/TemplateParser.d.ts +23 -0
- package/dist/types/utils/index.d.ts +8 -0
- package/dist/types/utils/logger.d.ts +4 -3
- package/dist/types/utils/outputLimiter.d.ts +87 -0
- package/dist/types/utils/validation.d.ts +11 -309
- package/dist/types/utils/validators/array.d.ts +19 -0
- package/dist/types/utils/validators/buffer.d.ts +18 -0
- package/dist/types/utils/validators/chain.d.ts +31 -0
- package/dist/types/utils/validators/common.d.ts +22 -0
- package/dist/types/utils/validators/number.d.ts +20 -0
- package/dist/types/utils/validators/object.d.ts +24 -0
- package/dist/types/utils/validators/printer.d.ts +40 -0
- package/dist/types/utils/validators/types.d.ts +125 -0
- package/dist/types/utils/validators/uuid.d.ts +23 -0
- package/package.json +1 -1
- package/src/core/BluetoothPrinter.ts +48 -50
- package/src/core/di/Container.ts +330 -0
- package/src/core/di/Tokens.ts +45 -0
- package/src/core/di/index.ts +3 -0
- package/src/core/event/EventBus.ts +251 -0
- package/src/core/event/index.ts +2 -0
- package/src/core/index.ts +10 -4
- package/src/core/plugin/PluginManager.ts +161 -0
- package/src/core/plugin/index.ts +2 -0
- package/src/device/MultiPrinterManager.ts +15 -6
- package/src/errors/CommandBuildError.ts +72 -0
- package/src/errors/ConnectionError.ts +78 -0
- package/src/errors/PrintJobError.ts +75 -0
- package/src/errors/index.ts +10 -0
- package/src/factory/PrinterFactory.ts +139 -0
- package/src/factory/di-factory.ts +61 -0
- package/src/factory/index.ts +12 -0
- package/src/index.ts +61 -1
- package/src/providers/ServiceProvider.ts +213 -0
- package/src/providers/index.ts +2 -0
- package/src/services/interfaces/ICommandBuilder.ts +145 -0
- package/src/services/interfaces/IConnectionManager.ts +58 -0
- package/src/services/interfaces/IPrintJobManager.ts +83 -0
- package/src/services/interfaces/index.ts +5 -265
- package/src/template/TemplateEngine.ts +27 -792
- package/src/template/engines/TemplateRenderer.ts +762 -0
- package/src/template/parsers/TemplateParser.ts +94 -0
- package/src/utils/index.ts +9 -0
- package/src/utils/logger.ts +17 -4
- package/src/utils/outputLimiter.ts +227 -0
- package/src/utils/validation.ts +21 -1138
- package/src/utils/validators/array.ts +95 -0
- package/src/utils/validators/buffer.ts +81 -0
- package/src/utils/validators/chain.ts +181 -0
- package/src/utils/validators/common.ts +216 -0
- package/src/utils/validators/number.ts +101 -0
- package/src/utils/validators/object.ts +63 -0
- package/src/utils/validators/printer.ts +294 -0
- package/src/utils/validators/types.ts +105 -0
- package/src/utils/validators/uuid.ts +49 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Print Job Error
|
|
3
|
+
*
|
|
4
|
+
* Specialized error for print job failures
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { BluetoothPrintError, ErrorCode } from './BluetoothError';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Print job-related error codes
|
|
11
|
+
*/
|
|
12
|
+
export enum PrintJobErrorCode {
|
|
13
|
+
/** Print job failed */
|
|
14
|
+
FAILED = 'PRINT_JOB_FAILED',
|
|
15
|
+
/** Print job is already in progress */
|
|
16
|
+
IN_PROGRESS = 'PRINT_JOB_IN_PROGRESS',
|
|
17
|
+
/** Print job was cancelled */
|
|
18
|
+
CANCELLED = 'PRINT_JOB_CANCELLED',
|
|
19
|
+
/** Print data is invalid */
|
|
20
|
+
INVALID_DATA = 'INVALID_IMAGE_DATA',
|
|
21
|
+
/** Write operation failed */
|
|
22
|
+
WRITE_FAILED = 'WRITE_FAILED',
|
|
23
|
+
/** Write operation timed out */
|
|
24
|
+
WRITE_TIMEOUT = 'WRITE_TIMEOUT',
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* PrintJobError - Specialized error for print job failures
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* throw new PrintJobError(
|
|
33
|
+
* PrintJobErrorCode.WRITE_FAILED,
|
|
34
|
+
* 'Failed to send data to printer',
|
|
35
|
+
* originalError
|
|
36
|
+
* );
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export class PrintJobError extends BluetoothPrintError {
|
|
40
|
+
constructor(
|
|
41
|
+
public readonly jobErrorCode: PrintJobErrorCode,
|
|
42
|
+
message: string,
|
|
43
|
+
originalError?: Error
|
|
44
|
+
) {
|
|
45
|
+
const baseCode = PrintJobError._toBaseCode(jobErrorCode);
|
|
46
|
+
super(baseCode, message, originalError);
|
|
47
|
+
this.name = 'PrintJobError';
|
|
48
|
+
|
|
49
|
+
if (Error.captureStackTrace) {
|
|
50
|
+
Error.captureStackTrace(this, PrintJobError);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Converts PrintJobErrorCode to base ErrorCode
|
|
56
|
+
*/
|
|
57
|
+
private static _toBaseCode(code: PrintJobErrorCode): ErrorCode {
|
|
58
|
+
const mapping: Partial<Record<PrintJobErrorCode, ErrorCode>> = {
|
|
59
|
+
[PrintJobErrorCode.FAILED]: ErrorCode.PRINT_JOB_FAILED,
|
|
60
|
+
[PrintJobErrorCode.IN_PROGRESS]: ErrorCode.PRINT_JOB_IN_PROGRESS,
|
|
61
|
+
[PrintJobErrorCode.CANCELLED]: ErrorCode.PRINT_JOB_CANCELLED,
|
|
62
|
+
[PrintJobErrorCode.INVALID_DATA]: ErrorCode.INVALID_IMAGE_DATA,
|
|
63
|
+
[PrintJobErrorCode.WRITE_FAILED]: ErrorCode.WRITE_FAILED,
|
|
64
|
+
[PrintJobErrorCode.WRITE_TIMEOUT]: ErrorCode.WRITE_TIMEOUT,
|
|
65
|
+
};
|
|
66
|
+
return mapping[code] ?? ErrorCode.PRINT_JOB_FAILED;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Checks if an error is a print job error
|
|
71
|
+
*/
|
|
72
|
+
static isPrintJobError(error: unknown): error is PrintJobError {
|
|
73
|
+
return error instanceof PrintJobError;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Errors Module
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export { BluetoothPrintError, ErrorCode } from './BluetoothError';
|
|
8
|
+
export { ConnectionError, ConnectionErrorCode } from './ConnectionError';
|
|
9
|
+
export { PrintJobError, PrintJobErrorCode } from './PrintJobError';
|
|
10
|
+
export { CommandBuildError, CommandBuildErrorCode } from './CommandBuildError';
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Printer Factory
|
|
3
|
+
*
|
|
4
|
+
* Factory for creating properly configured BluetoothPrinter instances.
|
|
5
|
+
* This is the recommended way to create printer instances - avoid direct
|
|
6
|
+
* constructor calls unless you need custom dependency injection.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { BluetoothPrinter } from '@/core/BluetoothPrinter';
|
|
10
|
+
import { ConnectionManager } from '@/services/ConnectionManager';
|
|
11
|
+
import { PrintJobManager } from '@/services/PrintJobManager';
|
|
12
|
+
import { CommandBuilder } from '@/services/CommandBuilder';
|
|
13
|
+
import type { IPrinterAdapter } from '@/types';
|
|
14
|
+
import type { IConnectionManager, IPrintJobManager, ICommandBuilder } from '@/services/interfaces';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Options for creating a BluetoothPrinter via the factory
|
|
18
|
+
*/
|
|
19
|
+
export interface PrinterFactoryOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Printer adapter to use for Bluetooth communication.
|
|
22
|
+
* Required if not providing custom connectionManager.
|
|
23
|
+
*
|
|
24
|
+
* Common adapters:
|
|
25
|
+
* - WebBluetoothAdapter (browser Web Bluetooth API)
|
|
26
|
+
* - TaroAdapter (Taro framework)
|
|
27
|
+
* - AlipayAdapter (Alipay mini-program)
|
|
28
|
+
* - ReactNativeAdapter (React Native)
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* import { WebBluetoothAdapter } from 'taro-bluetooth-print';
|
|
33
|
+
*
|
|
34
|
+
* const printer = createBluetoothPrinter({
|
|
35
|
+
* adapter: new WebBluetoothAdapter()
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
adapter?: IPrinterAdapter;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Custom connection manager instance.
|
|
43
|
+
* If provided, overrides the default ConnectionManager.
|
|
44
|
+
* Useful for testing or custom connection handling.
|
|
45
|
+
*/
|
|
46
|
+
connectionManager?: IConnectionManager;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Custom print job manager instance.
|
|
50
|
+
* If provided, overrides the default PrintJobManager.
|
|
51
|
+
* Useful for custom job scheduling or queue management.
|
|
52
|
+
*/
|
|
53
|
+
printJobManager?: IPrintJobManager;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Custom command builder instance.
|
|
57
|
+
* If provided, overrides the default CommandBuilder.
|
|
58
|
+
* Useful for custom command formatting or driver selection.
|
|
59
|
+
*/
|
|
60
|
+
commandBuilder?: ICommandBuilder;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Creates a new BluetoothPrinter instance with properly configured services.
|
|
65
|
+
*
|
|
66
|
+
* This is the recommended factory function for creating printer instances.
|
|
67
|
+
* It handles dependency injection and ensures all services are properly wired.
|
|
68
|
+
*
|
|
69
|
+
* @param options - Factory options for configuring the printer
|
|
70
|
+
* @returns A fully configured BluetoothPrinter instance
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* // Basic usage with WebBluetooth
|
|
75
|
+
* import { createBluetoothPrinter, WebBluetoothAdapter } from 'taro-bluetooth-print';
|
|
76
|
+
*
|
|
77
|
+
* const printer = createBluetoothPrinter({
|
|
78
|
+
* adapter: new WebBluetoothAdapter()
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* await printer.connect('device-id');
|
|
82
|
+
* await printer.text('Hello').feed(2).cut().print();
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* // Advanced usage with custom services
|
|
88
|
+
* import { createBluetoothPrinter, TaroAdapter } from 'taro-bluetooth-print';
|
|
89
|
+
*
|
|
90
|
+
* const printer = createBluetoothPrinter({
|
|
91
|
+
* adapter: new TaroAdapter(),
|
|
92
|
+
* printJobManager: customPrintJobManager,
|
|
93
|
+
* });
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export function createBluetoothPrinter(options: PrinterFactoryOptions = {}): BluetoothPrinter {
|
|
97
|
+
const { adapter, connectionManager, printJobManager, commandBuilder } = options;
|
|
98
|
+
|
|
99
|
+
// Build dependency chain: CommandBuilder -> PrintJobManager -> ConnectionManager -> Adapter
|
|
100
|
+
const finalConnectionManager = connectionManager ?? new ConnectionManager(adapter);
|
|
101
|
+
const finalPrintJobManager = printJobManager ?? new PrintJobManager(finalConnectionManager);
|
|
102
|
+
const finalCommandBuilder = commandBuilder ?? new CommandBuilder();
|
|
103
|
+
|
|
104
|
+
return new BluetoothPrinter(finalConnectionManager, finalPrintJobManager, finalCommandBuilder);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Creates a BluetoothPrinter instance for the Web Bluetooth API.
|
|
109
|
+
*
|
|
110
|
+
* This is a convenience function specifically for browser environments
|
|
111
|
+
* using the Web Bluetooth API.
|
|
112
|
+
*
|
|
113
|
+
* @param adapterOptions - Options to pass to the WebBluetoothAdapter
|
|
114
|
+
* @returns A BluetoothPrinter instance configured for Web Bluetooth
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* import { createWebBluetoothPrinter } from 'taro-bluetooth-print';
|
|
119
|
+
*
|
|
120
|
+
* const printer = createWebBluetoothPrinter();
|
|
121
|
+
* await printer.connect('device-id');
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
export async function createWebBluetoothPrinter(): Promise<BluetoothPrinter> {
|
|
125
|
+
const { WebBluetoothAdapter } = await import('@/adapters/WebBluetoothAdapter');
|
|
126
|
+
const adapter = new WebBluetoothAdapter();
|
|
127
|
+
return createBluetoothPrinter({ adapter });
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Default printer factory instance
|
|
132
|
+
*
|
|
133
|
+
* @deprecated Use the factory functions directly. This export exists
|
|
134
|
+
* for backward compatibility only.
|
|
135
|
+
*/
|
|
136
|
+
export const PrinterFactory = {
|
|
137
|
+
create: createBluetoothPrinter,
|
|
138
|
+
createWebBluetooth: createWebBluetoothPrinter,
|
|
139
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DI 版本的 BluetoothPrinter 工厂
|
|
3
|
+
*
|
|
4
|
+
* 使用依赖注入容器创建打印机实例
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { BluetoothPrinter } from '@/core/BluetoothPrinter';
|
|
8
|
+
import { createServiceProvider } from '@/providers';
|
|
9
|
+
import type { ServiceProviderOptions } from '@/providers';
|
|
10
|
+
import type { IConnectionManager, IPrintJobManager, ICommandBuilder } from '@/services/interfaces';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 创建打印机实例的选项
|
|
14
|
+
*/
|
|
15
|
+
export interface CreatePrinterOptions extends ServiceProviderOptions {
|
|
16
|
+
/** 自定义连接管理器 */
|
|
17
|
+
connectionManager?: IConnectionManager;
|
|
18
|
+
/** 自定义打印任务管理器 */
|
|
19
|
+
printJobManager?: IPrintJobManager;
|
|
20
|
+
/** 自定义命令构建器 */
|
|
21
|
+
commandBuilder?: ICommandBuilder;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 使用依赖注入创建 BluetoothPrinter 实例
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // 基本用法
|
|
30
|
+
* const printer = createPrinter();
|
|
31
|
+
*
|
|
32
|
+
* // 使用自定义配置
|
|
33
|
+
* const printer = createPrinter({
|
|
34
|
+
* config: { debug: true },
|
|
35
|
+
* useGlobalEventBus: true
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export function createPrinter(options: CreatePrinterOptions = {}): BluetoothPrinter {
|
|
40
|
+
// 注册服务(如果尚未注册)
|
|
41
|
+
const provider = createServiceProvider(options);
|
|
42
|
+
|
|
43
|
+
// 使用提供的实例或从容器解析(需要类型断言)
|
|
44
|
+
const connectionManager =
|
|
45
|
+
options.connectionManager || (provider.getConnectionManager() as IConnectionManager);
|
|
46
|
+
const printJobManager =
|
|
47
|
+
options.printJobManager || (provider.getPrintJobManager() as IPrintJobManager);
|
|
48
|
+
const commandBuilder =
|
|
49
|
+
options.commandBuilder || (provider.getCommandBuilder() as ICommandBuilder);
|
|
50
|
+
|
|
51
|
+
return new BluetoothPrinter(connectionManager, printJobManager, commandBuilder);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 获取服务提供者(用于访问所有服务)
|
|
56
|
+
*/
|
|
57
|
+
export function getServiceProvider(options: ServiceProviderOptions = {}) {
|
|
58
|
+
return createServiceProvider(options);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export { createServiceProvider, ServiceProviderOptions };
|
package/src/index.ts
CHANGED
|
@@ -84,9 +84,70 @@ export { Logger, LogLevel } from './utils/logger';
|
|
|
84
84
|
export { Encoding } from './utils/encoding';
|
|
85
85
|
export { ImageProcessing } from './utils/image';
|
|
86
86
|
export { PlatformType, detectPlatform, isPlatformSupported } from './utils/platform';
|
|
87
|
+
export {
|
|
88
|
+
truncateString,
|
|
89
|
+
truncateForLog,
|
|
90
|
+
batchProcess,
|
|
91
|
+
createLimitedLogger,
|
|
92
|
+
generateSummary,
|
|
93
|
+
} from './utils/outputLimiter';
|
|
94
|
+
export type { TruncateOptions } from './utils/outputLimiter';
|
|
87
95
|
|
|
88
96
|
// Error handling - 错误处理
|
|
89
97
|
export { BluetoothPrintError, ErrorCode } from './errors/BluetoothError';
|
|
98
|
+
export { ConnectionError, ConnectionErrorCode } from './errors/ConnectionError';
|
|
99
|
+
export { PrintJobError, PrintJobErrorCode } from './errors/PrintJobError';
|
|
100
|
+
export { CommandBuildError, CommandBuildErrorCode } from './errors/CommandBuildError';
|
|
101
|
+
|
|
102
|
+
// Factory - 工厂模式
|
|
103
|
+
export {
|
|
104
|
+
createBluetoothPrinter,
|
|
105
|
+
createWebBluetoothPrinter,
|
|
106
|
+
PrinterFactory,
|
|
107
|
+
type PrinterFactoryOptions,
|
|
108
|
+
} from './factory';
|
|
109
|
+
|
|
110
|
+
// DI Factory - 依赖注入工厂
|
|
111
|
+
export {
|
|
112
|
+
createPrinter,
|
|
113
|
+
getServiceProvider,
|
|
114
|
+
createServiceProvider,
|
|
115
|
+
type CreatePrinterOptions,
|
|
116
|
+
type ServiceProviderOptions,
|
|
117
|
+
} from './factory/di-factory';
|
|
118
|
+
|
|
119
|
+
// Core Architecture - 核心架构
|
|
120
|
+
export {
|
|
121
|
+
Container,
|
|
122
|
+
rootContainer,
|
|
123
|
+
injectable,
|
|
124
|
+
inject,
|
|
125
|
+
EventBus,
|
|
126
|
+
globalEventBus,
|
|
127
|
+
PluginManager,
|
|
128
|
+
} from './core';
|
|
129
|
+
|
|
130
|
+
export {
|
|
131
|
+
ADAPTER_TOKEN,
|
|
132
|
+
DRIVER_TOKEN,
|
|
133
|
+
DEVICE_MANAGER_TOKEN,
|
|
134
|
+
CONNECTION_MANAGER_TOKEN,
|
|
135
|
+
PRINT_JOB_MANAGER_TOKEN,
|
|
136
|
+
PRINT_QUEUE_TOKEN,
|
|
137
|
+
OFFLINE_CACHE_TOKEN,
|
|
138
|
+
CONFIG_MANAGER_TOKEN,
|
|
139
|
+
LOGGER_TOKEN,
|
|
140
|
+
EVENT_BUS_TOKEN,
|
|
141
|
+
PLUGIN_MANAGER_TOKEN,
|
|
142
|
+
PERFORMANCE_MONITOR_TOKEN,
|
|
143
|
+
COMMAND_BUILDER_TOKEN,
|
|
144
|
+
PRINTER_STATUS_TOKEN,
|
|
145
|
+
PRINT_HISTORY_TOKEN,
|
|
146
|
+
PRINT_STATISTICS_TOKEN,
|
|
147
|
+
CLOUD_PRINT_MANAGER_TOKEN,
|
|
148
|
+
SCHEDULED_RETRY_MANAGER_TOKEN,
|
|
149
|
+
BATCH_PRINT_MANAGER_TOKEN,
|
|
150
|
+
} from './core';
|
|
90
151
|
|
|
91
152
|
// Configuration - 配置
|
|
92
153
|
export { DEFAULT_CONFIG, mergeConfig } from './config/PrinterConfig';
|
|
@@ -101,7 +162,6 @@ export { PrinterConfigManager, printerConfigManager } from './config/PrinterConf
|
|
|
101
162
|
export type { SavedPrinter, GlobalConfig, IConfigStorage } from './config/PrinterConfigManager';
|
|
102
163
|
|
|
103
164
|
// Plugin System - 插件系统
|
|
104
|
-
export { PluginManager } from './plugins/PluginManager';
|
|
105
165
|
export { createLoggingPlugin, createRetryPlugin } from './plugins';
|
|
106
166
|
export type { Plugin, PluginHooks, PluginOptions, PluginFactory } from './plugins/types';
|
|
107
167
|
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Service Provider Module
|
|
3
|
+
*
|
|
4
|
+
* 将现有服务层注册到 DI 容器
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
rootContainer,
|
|
9
|
+
CONNECTION_MANAGER_TOKEN,
|
|
10
|
+
PRINT_JOB_MANAGER_TOKEN,
|
|
11
|
+
COMMAND_BUILDER_TOKEN,
|
|
12
|
+
DEVICE_MANAGER_TOKEN,
|
|
13
|
+
PRINT_QUEUE_TOKEN,
|
|
14
|
+
OFFLINE_CACHE_TOKEN,
|
|
15
|
+
CONFIG_MANAGER_TOKEN,
|
|
16
|
+
PRINTER_STATUS_TOKEN,
|
|
17
|
+
PRINT_HISTORY_TOKEN,
|
|
18
|
+
PRINT_STATISTICS_TOKEN,
|
|
19
|
+
CLOUD_PRINT_MANAGER_TOKEN,
|
|
20
|
+
SCHEDULED_RETRY_MANAGER_TOKEN,
|
|
21
|
+
BATCH_PRINT_MANAGER_TOKEN,
|
|
22
|
+
EVENT_BUS_TOKEN,
|
|
23
|
+
PLUGIN_MANAGER_TOKEN,
|
|
24
|
+
ADAPTER_TOKEN,
|
|
25
|
+
} from '@/core';
|
|
26
|
+
|
|
27
|
+
import { ConnectionManager } from '@/services/ConnectionManager';
|
|
28
|
+
import { PrintJobManager } from '@/services/PrintJobManager';
|
|
29
|
+
import { CommandBuilder } from '@/services/CommandBuilder';
|
|
30
|
+
import { DeviceManager } from '@/device/DeviceManager';
|
|
31
|
+
import { PrintQueue } from '@/queue/PrintQueue';
|
|
32
|
+
import { OfflineCache } from '@/cache/OfflineCache';
|
|
33
|
+
import { PrinterConfigManager } from '@/config/PrinterConfigManager';
|
|
34
|
+
import { PrinterStatus } from '@/services/PrinterStatus';
|
|
35
|
+
import { PrintHistory } from '@/services/PrintHistory';
|
|
36
|
+
import { PrintStatistics } from '@/services/PrintStatistics';
|
|
37
|
+
import { CloudPrintManager } from '@/services/CloudPrintManager';
|
|
38
|
+
import { ScheduledRetryManager } from '@/services/ScheduledRetryManager';
|
|
39
|
+
import { BatchPrintManager } from '@/services/BatchPrintManager';
|
|
40
|
+
import { globalEventBus, EventBus } from '@/core/event/EventBus';
|
|
41
|
+
import { PluginManager } from '@/core/plugin/PluginManager';
|
|
42
|
+
import { AdapterFactory } from '@/adapters/AdapterFactory';
|
|
43
|
+
import type { Container } from '@/core/di/Container';
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 服务提供者配置选项
|
|
47
|
+
*/
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
49
|
+
export interface ServiceProviderOptions {
|
|
50
|
+
/** 是否启用全局事件总线 */
|
|
51
|
+
useGlobalEventBus?: boolean;
|
|
52
|
+
/** 自定义配置 */
|
|
53
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
54
|
+
config?: Record<string, unknown>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 注册所有服务到 DI 容器
|
|
59
|
+
*/
|
|
60
|
+
export function registerServices(options: ServiceProviderOptions = {}): void {
|
|
61
|
+
const { useGlobalEventBus = true } = options;
|
|
62
|
+
|
|
63
|
+
// 1. 注册事件总线(全局或新建)
|
|
64
|
+
rootContainer.register(EVENT_BUS_TOKEN, {
|
|
65
|
+
useValue: useGlobalEventBus ? globalEventBus : new EventBus(),
|
|
66
|
+
lifecycle: 'singleton',
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// 2. 注册适配器(工厂模式)
|
|
70
|
+
rootContainer.register(ADAPTER_TOKEN, {
|
|
71
|
+
useFactory: () => AdapterFactory.create(),
|
|
72
|
+
lifecycle: 'singleton',
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// 3. 注册连接管理器
|
|
76
|
+
rootContainer.register(CONNECTION_MANAGER_TOKEN, {
|
|
77
|
+
useClass: ConnectionManager,
|
|
78
|
+
lifecycle: 'singleton',
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// 4. 注册打印任务管理器
|
|
82
|
+
rootContainer.register(PRINT_JOB_MANAGER_TOKEN, {
|
|
83
|
+
useClass: PrintJobManager,
|
|
84
|
+
lifecycle: 'singleton',
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// 5. 注册命令构建器
|
|
88
|
+
rootContainer.register(COMMAND_BUILDER_TOKEN, {
|
|
89
|
+
useClass: CommandBuilder,
|
|
90
|
+
lifecycle: 'transient',
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// 6. 注册设备管理器
|
|
94
|
+
rootContainer.register(DEVICE_MANAGER_TOKEN, {
|
|
95
|
+
useClass: DeviceManager,
|
|
96
|
+
lifecycle: 'singleton',
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// 7. 注册打印队列
|
|
100
|
+
rootContainer.register(PRINT_QUEUE_TOKEN, {
|
|
101
|
+
useClass: PrintQueue,
|
|
102
|
+
lifecycle: 'singleton',
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// 8. 注册离线缓存
|
|
106
|
+
rootContainer.register(OFFLINE_CACHE_TOKEN, {
|
|
107
|
+
useClass: OfflineCache,
|
|
108
|
+
lifecycle: 'singleton',
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// 9. 注册配置管理器
|
|
112
|
+
rootContainer.register(CONFIG_MANAGER_TOKEN, {
|
|
113
|
+
useClass: PrinterConfigManager,
|
|
114
|
+
lifecycle: 'singleton',
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// 10. 注册打印机状态服务
|
|
118
|
+
rootContainer.register(PRINTER_STATUS_TOKEN, {
|
|
119
|
+
useClass: PrinterStatus,
|
|
120
|
+
lifecycle: 'singleton',
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// 11. 注册打印历史服务
|
|
124
|
+
rootContainer.register(PRINT_HISTORY_TOKEN, {
|
|
125
|
+
useClass: PrintHistory,
|
|
126
|
+
lifecycle: 'singleton',
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// 12. 注册打印统计服务
|
|
130
|
+
rootContainer.register(PRINT_STATISTICS_TOKEN, {
|
|
131
|
+
useClass: PrintStatistics,
|
|
132
|
+
lifecycle: 'singleton',
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// 13. 注册云打印管理器
|
|
136
|
+
rootContainer.register(CLOUD_PRINT_MANAGER_TOKEN, {
|
|
137
|
+
useClass: CloudPrintManager,
|
|
138
|
+
lifecycle: 'singleton',
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// 14. 注册定时重试管理器
|
|
142
|
+
rootContainer.register(SCHEDULED_RETRY_MANAGER_TOKEN, {
|
|
143
|
+
useClass: ScheduledRetryManager,
|
|
144
|
+
lifecycle: 'singleton',
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// 15. 注册批量打印管理器
|
|
148
|
+
rootContainer.register(BATCH_PRINT_MANAGER_TOKEN, {
|
|
149
|
+
useClass: BatchPrintManager,
|
|
150
|
+
lifecycle: 'singleton',
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// 16. 注册插件管理器
|
|
154
|
+
rootContainer.register(PLUGIN_MANAGER_TOKEN, {
|
|
155
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unnecessary-type-assertion
|
|
156
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
157
|
+
useFactory: (container: Container) => {
|
|
158
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
159
|
+
const eventBus = container.resolve(EVENT_BUS_TOKEN) as EventBus;
|
|
160
|
+
return new PluginManager({
|
|
161
|
+
eventBus,
|
|
162
|
+
container,
|
|
163
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
164
|
+
config: (options.config || {}) as Record<string, any>,
|
|
165
|
+
});
|
|
166
|
+
},
|
|
167
|
+
lifecycle: 'singleton',
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 创建配置好的服务提供者
|
|
173
|
+
*/
|
|
174
|
+
export function createServiceProvider(options: ServiceProviderOptions = {}) {
|
|
175
|
+
registerServices(options);
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
/** 获取连接管理器 */
|
|
179
|
+
getConnectionManager: () => rootContainer.resolve(CONNECTION_MANAGER_TOKEN),
|
|
180
|
+
/** 获取打印任务管理器 */
|
|
181
|
+
getPrintJobManager: () => rootContainer.resolve(PRINT_JOB_MANAGER_TOKEN),
|
|
182
|
+
/** 获取命令构建器 */
|
|
183
|
+
getCommandBuilder: () => rootContainer.resolve(COMMAND_BUILDER_TOKEN),
|
|
184
|
+
/** 获取设备管理器 */
|
|
185
|
+
getDeviceManager: () => rootContainer.resolve(DEVICE_MANAGER_TOKEN),
|
|
186
|
+
/** 获取打印队列 */
|
|
187
|
+
getPrintQueue: () => rootContainer.resolve(PRINT_QUEUE_TOKEN),
|
|
188
|
+
/** 获取离线缓存 */
|
|
189
|
+
getOfflineCache: () => rootContainer.resolve(OFFLINE_CACHE_TOKEN),
|
|
190
|
+
/** 获取配置管理器 */
|
|
191
|
+
getConfigManager: () => rootContainer.resolve(CONFIG_MANAGER_TOKEN),
|
|
192
|
+
/** 获取打印机状态服务 */
|
|
193
|
+
getPrinterStatus: () => rootContainer.resolve(PRINTER_STATUS_TOKEN),
|
|
194
|
+
/** 获取打印历史服务 */
|
|
195
|
+
getPrintHistory: () => rootContainer.resolve(PRINT_HISTORY_TOKEN),
|
|
196
|
+
/** 获取打印统计服务 */
|
|
197
|
+
getPrintStatistics: () => rootContainer.resolve(PRINT_STATISTICS_TOKEN),
|
|
198
|
+
/** 获取云打印管理器 */
|
|
199
|
+
getCloudPrintManager: () => rootContainer.resolve(CLOUD_PRINT_MANAGER_TOKEN),
|
|
200
|
+
/** 获取定时重试管理器 */
|
|
201
|
+
getScheduledRetryManager: () => rootContainer.resolve(SCHEDULED_RETRY_MANAGER_TOKEN),
|
|
202
|
+
/** 获取批量打印管理器 */
|
|
203
|
+
getBatchPrintManager: () => rootContainer.resolve(BATCH_PRINT_MANAGER_TOKEN),
|
|
204
|
+
/** 获取事件总线 */
|
|
205
|
+
getEventBus: () => rootContainer.resolve(EVENT_BUS_TOKEN),
|
|
206
|
+
/** 获取插件管理器 */
|
|
207
|
+
getPluginManager: () => rootContainer.resolve(PLUGIN_MANAGER_TOKEN),
|
|
208
|
+
/** 获取适配器 */
|
|
209
|
+
getAdapter: () => rootContainer.resolve(ADAPTER_TOKEN),
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export type ServiceProvider = ReturnType<typeof createServiceProvider>;
|