taro-bluetooth-print 2.3.0 → 2.4.0

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.
Files changed (57) hide show
  1. package/CHANGELOG.md +73 -195
  2. package/README.md +134 -386
  3. package/dist/index.cjs.js +1 -1
  4. package/dist/index.es.js +1 -1
  5. package/dist/index.umd.js +1 -1
  6. package/dist/types/config/PrinterConfigManager.d.ts +206 -0
  7. package/dist/types/config/index.d.ts +8 -0
  8. package/dist/types/core/BluetoothPrinter.d.ts +1 -1
  9. package/dist/types/core/EventEmitter.d.ts +6 -26
  10. package/dist/types/core/index.d.ts +6 -0
  11. package/dist/types/device/MultiPrinterManager.d.ts +164 -0
  12. package/dist/types/device/index.d.ts +2 -0
  13. package/dist/types/drivers/CpclDriver.d.ts +304 -0
  14. package/dist/types/drivers/GPrinterDriver.d.ts +63 -0
  15. package/dist/types/drivers/ZplDriver.d.ts +325 -0
  16. package/dist/types/drivers/index.d.ts +9 -0
  17. package/dist/types/encoding/gbk-lite.d.ts +8 -0
  18. package/dist/types/encoding/gbk-table.d.ts +8 -30
  19. package/dist/types/index.d.ts +12 -8
  20. package/dist/types/services/BatchPrintManager.d.ts +205 -0
  21. package/dist/types/services/ConnectionManager.d.ts +1 -1
  22. package/dist/types/services/PrintHistory.d.ts +142 -0
  23. package/dist/types/services/PrintJobManager.d.ts +28 -4
  24. package/dist/types/services/PrinterStatus.d.ts +97 -0
  25. package/dist/types/services/index.d.ts +11 -0
  26. package/package.json +25 -7
  27. package/src/adapters/AlipayAdapter.ts +1 -0
  28. package/src/adapters/BaiduAdapter.ts +1 -0
  29. package/src/adapters/BaseAdapter.ts +6 -8
  30. package/src/adapters/ByteDanceAdapter.ts +1 -0
  31. package/src/adapters/TaroAdapter.ts +1 -0
  32. package/src/adapters/WebBluetoothAdapter.ts +1 -1
  33. package/src/config/PrinterConfigManager.ts +519 -0
  34. package/src/config/index.ts +15 -0
  35. package/src/core/BluetoothPrinter.ts +15 -15
  36. package/src/core/EventEmitter.ts +15 -15
  37. package/src/core/index.ts +7 -0
  38. package/src/device/MultiPrinterManager.ts +470 -0
  39. package/src/device/index.ts +8 -0
  40. package/src/drivers/CpclDriver.ts +549 -0
  41. package/src/drivers/GPrinterDriver.ts +115 -0
  42. package/src/drivers/TsplDriver.ts +9 -21
  43. package/src/drivers/ZplDriver.ts +543 -0
  44. package/src/drivers/index.ts +37 -0
  45. package/src/encoding/gbk-lite.ts +113 -0
  46. package/src/encoding/gbk-table.ts +80 -58
  47. package/src/index.ts +40 -35
  48. package/src/plugins/PluginManager.ts +3 -1
  49. package/src/plugins/builtin/LoggingPlugin.ts +4 -2
  50. package/src/plugins/builtin/RetryPlugin.ts +8 -14
  51. package/src/services/BatchPrintManager.ts +500 -0
  52. package/src/services/ConnectionManager.ts +25 -22
  53. package/src/services/PrintHistory.ts +336 -0
  54. package/src/services/PrintJobManager.ts +69 -9
  55. package/src/services/PrinterStatus.ts +267 -0
  56. package/src/services/index.ts +22 -0
  57. package/src/template/TemplateEngine.ts +4 -1
@@ -0,0 +1,206 @@
1
+ /**
2
+ * Printer Configuration Manager
3
+ *
4
+ * Manages persistent printer configurations including:
5
+ * - Saved devices and their settings
6
+ * - Default print parameters
7
+ * - User preferences
8
+ *
9
+ * Supports localStorage and file-based storage backends.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * const configManager = new PrinterConfigManager();
14
+ *
15
+ * // Save a printer configuration
16
+ * configManager.savePrinter({
17
+ * id: 'my-printer',
18
+ * deviceId: 'XX:XX:XX:XX:XX:XX',
19
+ * name: 'Kitchen Printer',
20
+ * isDefault: true
21
+ * });
22
+ *
23
+ * // Get all saved printers
24
+ * const printers = configManager.getSavedPrinters();
25
+ *
26
+ * // Load configuration for a printer
27
+ * const config = configManager.loadPrinterConfig('my-printer');
28
+ * ```
29
+ */
30
+ /**
31
+ * Print configuration for a device
32
+ */
33
+ export interface PrintConfig {
34
+ /** Encoding (default: 'GBK') */
35
+ encoding?: string;
36
+ /** Chunk size for data transfer */
37
+ chunkSize?: number;
38
+ /** Delay between chunks in ms */
39
+ chunkDelay?: number;
40
+ /** Number of retries */
41
+ retries?: number;
42
+ }
43
+ /**
44
+ * Saved printer configuration
45
+ */
46
+ export interface SavedPrinter {
47
+ /** Unique printer ID */
48
+ id: string;
49
+ /** Bluetooth device ID */
50
+ deviceId: string;
51
+ /** Friendly name */
52
+ name: string;
53
+ /** Printer model/type */
54
+ model?: string;
55
+ /** Whether this is the default printer */
56
+ isDefault?: boolean;
57
+ /** Print configuration */
58
+ printConfig?: PrintConfig;
59
+ /** Auto-reconnect on disconnect */
60
+ autoReconnect?: boolean;
61
+ /** Last connected timestamp */
62
+ lastConnected?: number;
63
+ /** Creation timestamp */
64
+ createdAt: number;
65
+ /** Update timestamp */
66
+ updatedAt: number;
67
+ }
68
+ /**
69
+ * Global configuration
70
+ */
71
+ export interface GlobalConfig {
72
+ /** Default encoding */
73
+ defaultEncoding: string;
74
+ /** Default chunk size */
75
+ defaultChunkSize: number;
76
+ /** Default chunk delay */
77
+ defaultChunkDelay: number;
78
+ /** Default retries */
79
+ defaultRetries: number;
80
+ /** Auto-reconnect by default */
81
+ defaultAutoReconnect: boolean;
82
+ /** Scan timeout in ms */
83
+ scanTimeout: number;
84
+ /** Enable logging */
85
+ enableLogging: boolean;
86
+ }
87
+ /**
88
+ * Configuration storage interface
89
+ */
90
+ export interface IConfigStorage {
91
+ get<T>(key: string, defaultValue: T): T;
92
+ set<T>(key: string, value: T): void;
93
+ remove(key: string): void;
94
+ clear(): void;
95
+ }
96
+ /**
97
+ * LocalStorage-based implementation
98
+ */
99
+ export declare class LocalStorage implements IConfigStorage {
100
+ private prefix;
101
+ constructor(prefix?: string);
102
+ get<T>(key: string, defaultValue: T): T;
103
+ set<T>(key: string, value: T): void;
104
+ remove(key: string): void;
105
+ clear(): void;
106
+ }
107
+ /**
108
+ * Printer Configuration Manager
109
+ */
110
+ export declare class PrinterConfigManager {
111
+ private readonly logger;
112
+ private readonly storage;
113
+ private printers;
114
+ private globalConfig;
115
+ private lastUsedPrinterId;
116
+ /**
117
+ * Creates a new PrinterConfigManager instance
118
+ *
119
+ * @param storage - Storage backend (defaults to LocalStorage)
120
+ */
121
+ constructor(storage?: IConfigStorage);
122
+ /**
123
+ * Load configuration from storage
124
+ */
125
+ private load;
126
+ /**
127
+ * Save configuration to storage
128
+ */
129
+ private save;
130
+ /**
131
+ * Save a printer configuration
132
+ *
133
+ * @param printer - Printer configuration to save
134
+ * @returns The saved printer ID
135
+ */
136
+ savePrinter(printer: SavedPrinter): string;
137
+ /**
138
+ * Get a saved printer by ID
139
+ */
140
+ getPrinter(id: string): SavedPrinter | undefined;
141
+ /**
142
+ * Get all saved printers
143
+ */
144
+ getSavedPrinters(): SavedPrinter[];
145
+ /**
146
+ * Get default printer
147
+ */
148
+ getDefaultPrinter(): SavedPrinter | undefined;
149
+ /**
150
+ * Remove a saved printer
151
+ */
152
+ removePrinter(id: string): boolean;
153
+ /**
154
+ * Set printer as default
155
+ */
156
+ setDefaultPrinter(id: string): void;
157
+ /**
158
+ * Update last used printer
159
+ */
160
+ setLastUsed(id: string): void;
161
+ /**
162
+ * Get last used printer ID
163
+ */
164
+ getLastUsedId(): string | null;
165
+ /**
166
+ * Get global configuration
167
+ */
168
+ getGlobalConfig(): GlobalConfig;
169
+ /**
170
+ * Update global configuration
171
+ */
172
+ updateGlobalConfig(updates: Partial<GlobalConfig>): void;
173
+ /**
174
+ * Reset global config to defaults
175
+ */
176
+ resetGlobalConfig(): void;
177
+ /**
178
+ * Load print config for a printer (with defaults applied)
179
+ */
180
+ loadPrinterConfig(printerId: string): PrintConfig;
181
+ /**
182
+ * Export all configuration as JSON
183
+ */
184
+ export(): string;
185
+ /**
186
+ * Import configuration from JSON
187
+ *
188
+ * @param json - JSON string to import
189
+ * @param merge - If true, merge with existing config; if false, replace
190
+ * @returns Number of printers imported
191
+ */
192
+ import(json: string, merge?: boolean): number;
193
+ /**
194
+ * Clear all configuration
195
+ */
196
+ clear(): void;
197
+ /**
198
+ * Get statistics
199
+ */
200
+ getStats(): {
201
+ printerCount: number;
202
+ hasDefault: boolean;
203
+ lastUsed: string | null;
204
+ };
205
+ }
206
+ export declare const printerConfigManager: PrinterConfigManager;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Config Module
3
+ * 配置模块 - 提供打印机配置管理
4
+ */
5
+ export { PrinterConfigManager, printerConfigManager } from './PrinterConfigManager';
6
+ export type { SavedPrinter, PrintConfig, GlobalConfig, IConfigStorage, } from './PrinterConfigManager';
7
+ export { DEFAULT_CONFIG, mergeConfig } from './PrinterConfig';
8
+ export type { PrinterConfig, AdapterConfig, DriverConfig, LoggingConfig } from './PrinterConfig';
@@ -50,7 +50,7 @@ export interface PrinterEvents {
50
50
  * ```
51
51
  */
52
52
  export declare class BluetoothPrinter extends EventEmitter<PrinterEvents> {
53
- private readonly logger;
53
+ private readonly printerLogger;
54
54
  /** Current printer state */
55
55
  state: PrinterState;
56
56
  /** Connection manager instance */
@@ -1,32 +1,12 @@
1
- /**
2
- * Type-safe event emitter for pub/sub pattern
3
- *
4
- * @template T - Event map type with event names as keys and payload types as values
5
- *
6
- * @example
7
- * ```typescript
8
- * interface MyEvents {
9
- * 'connected': { deviceId: string };
10
- * 'error': Error;
11
- * 'progress': { current: number; total: number };
12
- * 'ready': void;
13
- * }
14
- *
15
- * class MyClass extends EventEmitter<MyEvents> {
16
- * connect() {
17
- * this.emit('connected', { deviceId: '123' });
18
- * }
19
- * }
20
- *
21
- * const instance = new MyClass();
22
- * const unsubscribe = instance.on('connected', (data) => {
23
- * console.log('Connected to:', data.deviceId);
24
- * });
25
- * ```
26
- */
27
1
  export declare class EventEmitter<T> {
28
2
  private listeners;
29
3
  private debugMode;
4
+ protected readonly logger: {
5
+ debug: (message: string, ...args: unknown[]) => void;
6
+ info: (message: string, ...args: unknown[]) => void;
7
+ warn: (message: string, ...args: unknown[]) => void;
8
+ error: (message: string, ...args: unknown[]) => void;
9
+ };
30
10
  /**
31
11
  * Subscribe to an event
32
12
  *
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Core Module - 核心模块
3
+ */
4
+ export { BluetoothPrinter } from './BluetoothPrinter';
5
+ export type { PrinterEvents } from './BluetoothPrinter';
6
+ export { EventEmitter } from './EventEmitter';
@@ -0,0 +1,164 @@
1
+ import { BluetoothPrinter } from '../core/BluetoothPrinter';
2
+ /**
3
+ * Printer connection info
4
+ */
5
+ export interface PrinterConnection {
6
+ /** Custom printer ID */
7
+ printerId: string;
8
+ /** Device ID */
9
+ deviceId: string;
10
+ /** Device name */
11
+ name: string;
12
+ /** BluetoothPrinter instance */
13
+ printer: BluetoothPrinter;
14
+ /** Connection timestamp */
15
+ connectedAt: number;
16
+ /** Last activity timestamp */
17
+ lastActivity?: number;
18
+ }
19
+ /**
20
+ * Connection options
21
+ */
22
+ export interface MultiConnectOptions {
23
+ /** Custom printer ID (optional, auto-generated if not provided) */
24
+ printerId?: string;
25
+ /** Device ID to connect */
26
+ deviceId: string;
27
+ /** Connection timeout in ms */
28
+ timeout?: number;
29
+ }
30
+ /**
31
+ * Broadcast options
32
+ */
33
+ export interface BroadcastOptions {
34
+ /** Parallel or sequential broadcast */
35
+ parallel?: boolean;
36
+ /** Continue on individual failure */
37
+ continueOnError?: boolean;
38
+ }
39
+ /**
40
+ * Multi Printer Manager Events
41
+ */
42
+ export interface MultiPrinterManagerEvents {
43
+ /** Emitted when a printer connects */
44
+ 'printer-connected': (data: PrinterConnection) => void;
45
+ /** Emitted when a printer disconnects */
46
+ 'printer-disconnected': (data: {
47
+ printerId: string;
48
+ deviceId: string;
49
+ }) => void;
50
+ /** Emitted when a printer has an error */
51
+ 'printer-error': (data: {
52
+ printerId: string;
53
+ error: Error;
54
+ }) => void;
55
+ /** Emitted when broadcast completes */
56
+ 'broadcast-complete': (data: {
57
+ success: number;
58
+ failed: number;
59
+ }) => void;
60
+ }
61
+ /**
62
+ * Multi Printer Manager
63
+ *
64
+ * Manages multiple Bluetooth printer connections and supports:
65
+ * - Concurrent connections to multiple printers
66
+ * - Broadcasting print jobs to all printers
67
+ * - Individual printer control
68
+ * - Automatic reconnection
69
+ */
70
+ export declare class MultiPrinterManager {
71
+ private readonly logger;
72
+ private readonly printers;
73
+ private readonly deviceToPrinter;
74
+ private readonly listeners;
75
+ /**
76
+ * Creates a new MultiPrinterManager instance
77
+ */
78
+ constructor();
79
+ /**
80
+ * Register event listener
81
+ */
82
+ on<K extends keyof MultiPrinterManagerEvents>(event: K, callback: MultiPrinterManagerEvents[K]): void;
83
+ /**
84
+ * Remove event listener
85
+ */
86
+ off<K extends keyof MultiPrinterManagerEvents>(event: K, callback: MultiPrinterManagerEvents[K]): void;
87
+ /**
88
+ * Emit an event
89
+ */
90
+ private emit;
91
+ /**
92
+ * Connect to a printer
93
+ *
94
+ * @param printerId - Custom ID for this printer (will be auto-generated if not provided)
95
+ * @param deviceId - Bluetooth device ID
96
+ * @param deviceName - Optional device name
97
+ * @returns The printer ID used
98
+ */
99
+ connect(printerIdOrDeviceId: string, deviceId?: string, deviceName?: string): Promise<string>;
100
+ /**
101
+ * Disconnect a printer
102
+ */
103
+ disconnect(printerId: string): Promise<void>;
104
+ /**
105
+ * Disconnect all printers
106
+ */
107
+ disconnectAll(): Promise<void>;
108
+ /**
109
+ * Get a printer by ID
110
+ */
111
+ getPrinter(printerId: string): BluetoothPrinter | undefined;
112
+ /**
113
+ * Get connection info for a printer
114
+ */
115
+ getConnection(printerId: string): PrinterConnection | undefined;
116
+ /**
117
+ * Get all connected printers
118
+ */
119
+ getAllPrinters(): PrinterConnection[];
120
+ /**
121
+ * Get printer count
122
+ */
123
+ get count(): number;
124
+ /**
125
+ * Check if a printer is connected
126
+ */
127
+ isConnected(printerId: string): boolean;
128
+ /**
129
+ * Update last activity timestamp for a printer
130
+ */
131
+ touch(printerId: string): void;
132
+ /**
133
+ * Print to a specific printer
134
+ */
135
+ print(printerId: string, data: Uint8Array): Promise<void>;
136
+ /**
137
+ * Broadcast data to all connected printers
138
+ */
139
+ broadcast(data: Uint8Array, options?: BroadcastOptions): Promise<{
140
+ success: number;
141
+ failed: number;
142
+ }>;
143
+ /**
144
+ * Find idle printers (for load balancing)
145
+ */
146
+ getIdlePrinters(): PrinterConnection[];
147
+ /**
148
+ * Get printer statistics
149
+ */
150
+ getStats(): {
151
+ total: number;
152
+ connected: number;
153
+ byName: Record<string, number>;
154
+ };
155
+ /**
156
+ * Clean up inactive printers (based on last activity)
157
+ */
158
+ cleanupInactive(maxIdleMs?: number): Promise<number>;
159
+ /**
160
+ * Destroy the manager and disconnect all printers
161
+ */
162
+ destroy(): Promise<void>;
163
+ }
164
+ export declare const multiPrinterManager: MultiPrinterManager;
@@ -4,3 +4,5 @@
4
4
  */
5
5
  export { DeviceManager, deviceManager } from './DeviceManager';
6
6
  export type { BluetoothDevice, ScanOptions, DeviceManagerEvents, IDeviceManager, } from './DeviceManager';
7
+ export { MultiPrinterManager, multiPrinterManager } from './MultiPrinterManager';
8
+ export type { PrinterConnection, MultiConnectOptions, BroadcastOptions, MultiPrinterManagerEvents, } from './MultiPrinterManager';