taro-bluetooth-print 2.2.1 → 2.3.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.
Files changed (56) hide show
  1. package/CHANGELOG.md +57 -165
  2. package/README.md +142 -285
  3. package/dist/index.cjs.js +1 -1
  4. package/dist/index.es.js +1 -81644
  5. package/dist/index.umd.js +1 -1
  6. package/dist/types/adapters/AdapterFactory.d.ts +0 -1
  7. package/dist/types/adapters/AlipayAdapter.d.ts +0 -1
  8. package/dist/types/adapters/BaiduAdapter.d.ts +0 -1
  9. package/dist/types/adapters/BaseAdapter.d.ts +0 -1
  10. package/dist/types/adapters/ByteDanceAdapter.d.ts +0 -1
  11. package/dist/types/adapters/TaroAdapter.d.ts +0 -1
  12. package/dist/types/adapters/WebBluetoothAdapter.d.ts +0 -1
  13. package/dist/types/config/PrinterConfig.d.ts +0 -1
  14. package/dist/types/core/BluetoothPrinter.d.ts +1 -2
  15. package/dist/types/core/EventEmitter.d.ts +6 -26
  16. package/dist/types/core/index.d.ts +6 -0
  17. package/dist/types/drivers/CpclDriver.d.ts +304 -0
  18. package/dist/types/drivers/EscPos.d.ts +0 -1
  19. package/dist/types/drivers/GPrinterDriver.d.ts +63 -0
  20. package/dist/types/drivers/TsplDriver.d.ts +251 -0
  21. package/dist/types/drivers/ZplDriver.d.ts +325 -0
  22. package/dist/types/drivers/index.d.ts +9 -0
  23. package/dist/types/encoding/gbk-lite.d.ts +8 -0
  24. package/dist/types/encoding/gbk-table.d.ts +8 -30
  25. package/dist/types/index.d.ts +10 -5
  26. package/dist/types/plugins/PluginManager.d.ts +87 -0
  27. package/dist/types/plugins/builtin/LoggingPlugin.d.ts +14 -0
  28. package/dist/types/plugins/builtin/RetryPlugin.d.ts +18 -0
  29. package/dist/types/plugins/index.d.ts +7 -0
  30. package/dist/types/plugins/types.d.ts +97 -0
  31. package/dist/types/services/CommandBuilder.d.ts +0 -1
  32. package/dist/types/services/ConnectionManager.d.ts +1 -2
  33. package/dist/types/services/PrintJobManager.d.ts +0 -1
  34. package/dist/types/services/index.d.ts +8 -0
  35. package/dist/types/services/interfaces/index.d.ts +0 -1
  36. package/dist/types/template/TemplateEngine.d.ts +0 -1
  37. package/package.json +36 -20
  38. package/src/adapters/BaseAdapter.ts +6 -8
  39. package/src/core/BluetoothPrinter.ts +15 -15
  40. package/src/core/EventEmitter.ts +15 -15
  41. package/src/core/index.ts +7 -0
  42. package/src/drivers/CpclDriver.ts +549 -0
  43. package/src/drivers/GPrinterDriver.ts +115 -0
  44. package/src/drivers/TsplDriver.ts +405 -0
  45. package/src/drivers/ZplDriver.ts +543 -0
  46. package/src/drivers/index.ts +37 -0
  47. package/src/encoding/gbk-lite.ts +108 -0
  48. package/src/encoding/gbk-table.ts +80 -58
  49. package/src/index.ts +27 -23
  50. package/src/plugins/PluginManager.ts +195 -0
  51. package/src/plugins/builtin/LoggingPlugin.ts +99 -0
  52. package/src/plugins/builtin/RetryPlugin.ts +103 -0
  53. package/src/plugins/index.ts +10 -0
  54. package/src/plugins/types.ts +119 -0
  55. package/src/services/ConnectionManager.ts +22 -22
  56. package/src/services/index.ts +16 -0
@@ -0,0 +1,119 @@
1
+ /**
2
+ * Plugin System Types
3
+ * Defines interfaces for extending BluetoothPrinter functionality
4
+ */
5
+
6
+ import { BluetoothPrintError } from '@/errors/BluetoothError';
7
+ import { PrinterState } from '@/types';
8
+
9
+ /**
10
+ * Plugin lifecycle hooks
11
+ */
12
+ export interface PluginHooks {
13
+ /**
14
+ * Called before connecting to a device
15
+ * @param deviceId - Target device ID
16
+ * @returns Modified device ID or void
17
+ */
18
+ beforeConnect?: (deviceId: string) => string | void | Promise<string | void>;
19
+
20
+ /**
21
+ * Called after successful connection
22
+ * @param deviceId - Connected device ID
23
+ */
24
+ afterConnect?: (deviceId: string) => void | Promise<void>;
25
+
26
+ /**
27
+ * Called before disconnecting
28
+ * @param deviceId - Device ID to disconnect
29
+ */
30
+ beforeDisconnect?: (deviceId: string) => void | Promise<void>;
31
+
32
+ /**
33
+ * Called after disconnection
34
+ * @param deviceId - Disconnected device ID
35
+ */
36
+ afterDisconnect?: (deviceId: string) => void | Promise<void>;
37
+
38
+ /**
39
+ * Called before sending print data
40
+ * @param buffer - Data buffer to send
41
+ * @returns Modified buffer or void
42
+ */
43
+ beforePrint?: (buffer: Uint8Array) => Uint8Array | void | Promise<Uint8Array | void>;
44
+
45
+ /**
46
+ * Called after print job completes
47
+ * @param bytesSent - Total bytes sent
48
+ */
49
+ afterPrint?: (bytesSent: number) => void | Promise<void>;
50
+
51
+ /**
52
+ * Called when an error occurs
53
+ * @param error - The error that occurred
54
+ * @returns Whether to suppress the error (true = suppress)
55
+ */
56
+ onError?: (error: BluetoothPrintError) => boolean | void | Promise<boolean | void>;
57
+
58
+ /**
59
+ * Called when printer state changes
60
+ * @param state - New printer state
61
+ * @param previousState - Previous state
62
+ */
63
+ onStateChange?: (state: PrinterState, previousState: PrinterState) => void | Promise<void>;
64
+
65
+ /**
66
+ * Called during print progress
67
+ * @param sent - Bytes sent
68
+ * @param total - Total bytes
69
+ */
70
+ onProgress?: (sent: number, total: number) => void | Promise<void>;
71
+ }
72
+
73
+ /**
74
+ * Plugin configuration options
75
+ */
76
+ export interface PluginOptions {
77
+ [key: string]: unknown;
78
+ }
79
+
80
+ /**
81
+ * Plugin interface
82
+ */
83
+ export interface Plugin {
84
+ /**
85
+ * Unique plugin name
86
+ */
87
+ name: string;
88
+
89
+ /**
90
+ * Plugin version
91
+ */
92
+ version?: string;
93
+
94
+ /**
95
+ * Plugin description
96
+ */
97
+ description?: string;
98
+
99
+ /**
100
+ * Plugin hooks
101
+ */
102
+ hooks: PluginHooks;
103
+
104
+ /**
105
+ * Plugin initialization
106
+ * @param options - Plugin options
107
+ */
108
+ init?: (options?: PluginOptions) => void | Promise<void>;
109
+
110
+ /**
111
+ * Plugin cleanup
112
+ */
113
+ destroy?: () => void | Promise<void>;
114
+ }
115
+
116
+ /**
117
+ * Plugin factory function type
118
+ */
119
+ export type PluginFactory = (options?: PluginOptions) => Plugin;
@@ -71,7 +71,7 @@ export class ConnectionManager
71
71
  private adapter: IPrinterAdapter;
72
72
  private deviceId: string | null = null;
73
73
  private state: PrinterState = PrinterState.DISCONNECTED;
74
- private readonly logger = Logger.scope('ConnectionManager');
74
+ private readonly connLogger = Logger.scope('ConnectionManager');
75
75
  private readonly config: Required<ConnectionManagerConfig>;
76
76
 
77
77
  // Heartbeat state
@@ -102,7 +102,7 @@ export class ConnectionManager
102
102
  private handleStateChange(newState: PrinterState): void {
103
103
  const previousState = this.state;
104
104
  this.state = newState;
105
- this.logger.debug('State changed:', { from: previousState, to: newState });
105
+ this.connLogger.debug('State changed:', { from: previousState, to: newState });
106
106
  this.emit('state-change', newState);
107
107
 
108
108
  // Handle unexpected disconnection
@@ -112,7 +112,7 @@ export class ConnectionManager
112
112
  this.deviceId &&
113
113
  !this.isReconnecting
114
114
  ) {
115
- this.logger.warn('Unexpected disconnection detected');
115
+ this.connLogger.warn('Unexpected disconnection detected');
116
116
  this.emit('disconnected', this.deviceId);
117
117
  this.stopHeartbeat();
118
118
 
@@ -127,7 +127,7 @@ export class ConnectionManager
127
127
  * Connects to a Bluetooth device
128
128
  */
129
129
  async connect(deviceId: string, options?: { retries?: number; timeout?: number }): Promise<void> {
130
- this.logger.info('Connecting to device:', deviceId);
130
+ this.connLogger.info('Connecting to device:', deviceId);
131
131
 
132
132
  const { retries = 0, timeout = this.config.connectionTimeout } = options || {};
133
133
  let attempts = 0;
@@ -159,7 +159,7 @@ export class ConnectionManager
159
159
  this.state = PrinterState.CONNECTED;
160
160
  this.emit('state-change', PrinterState.CONNECTED);
161
161
  this.emit('connected', deviceId);
162
- this.logger.info('Connected successfully');
162
+ this.connLogger.info('Connected successfully');
163
163
 
164
164
  // Start heartbeat if enabled
165
165
  if (this.config.heartbeatEnabled) {
@@ -181,11 +181,11 @@ export class ConnectionManager
181
181
  `Connection failed after ${attempts} attempts`,
182
182
  error as Error
183
183
  );
184
- this.logger.error('Connection failed:', printError);
184
+ this.connLogger.error('Connection failed:', printError);
185
185
  this.emit('error', printError);
186
186
  throw printError;
187
187
  }
188
- this.logger.warn(`Connection attempt ${attempts}/${retries} failed, retrying...`, error);
188
+ this.connLogger.warn(`Connection attempt ${attempts}/${retries} failed, retrying...`, error);
189
189
  await new Promise(resolve => setTimeout(resolve, 1000));
190
190
  }
191
191
  }
@@ -200,12 +200,12 @@ export class ConnectionManager
200
200
  this.isReconnecting = false;
201
201
 
202
202
  if (!this.deviceId) {
203
- this.logger.warn('Disconnect called but no device connected');
203
+ this.connLogger.warn('Disconnect called but no device connected');
204
204
  return;
205
205
  }
206
206
 
207
207
  const deviceId = this.deviceId;
208
- this.logger.info('Disconnecting from device:', deviceId);
208
+ this.connLogger.info('Disconnecting from device:', deviceId);
209
209
 
210
210
  try {
211
211
  await this.adapter.disconnect(deviceId);
@@ -213,14 +213,14 @@ export class ConnectionManager
213
213
  this.state = PrinterState.DISCONNECTED;
214
214
  this.emit('state-change', PrinterState.DISCONNECTED);
215
215
  this.emit('disconnected', deviceId);
216
- this.logger.info('Disconnected successfully');
216
+ this.connLogger.info('Disconnected successfully');
217
217
  } catch (error) {
218
218
  const printError = new BluetoothPrintError(
219
219
  ErrorCode.DEVICE_DISCONNECTED,
220
220
  'Disconnect failed',
221
221
  error as Error
222
222
  );
223
- this.logger.error('Disconnect failed:', printError);
223
+ this.connLogger.error('Disconnect failed:', printError);
224
224
  this.emit('error', printError);
225
225
  throw printError;
226
226
  }
@@ -236,7 +236,7 @@ export class ConnectionManager
236
236
  this.checkHeartbeat();
237
237
  }, this.config.heartbeatInterval);
238
238
 
239
- this.logger.debug('Heartbeat started with interval:', this.config.heartbeatInterval);
239
+ this.connLogger.debug('Heartbeat started with interval:', this.config.heartbeatInterval);
240
240
  }
241
241
 
242
242
  /**
@@ -246,7 +246,7 @@ export class ConnectionManager
246
246
  if (this.heartbeatTimer) {
247
247
  clearInterval(this.heartbeatTimer);
248
248
  this.heartbeatTimer = null;
249
- this.logger.debug('Heartbeat stopped');
249
+ this.connLogger.debug('Heartbeat stopped');
250
250
  }
251
251
  }
252
252
 
@@ -262,12 +262,12 @@ export class ConnectionManager
262
262
  const isConnected = this.isConnected();
263
263
 
264
264
  if (isConnected) {
265
- this.logger.debug('Heartbeat OK');
265
+ this.connLogger.debug('Heartbeat OK');
266
266
  } else {
267
267
  this.handleHeartbeatLost();
268
268
  }
269
269
  } catch (error) {
270
- this.logger.warn('Heartbeat check failed:', error);
270
+ this.connLogger.warn('Heartbeat check failed:', error);
271
271
  this.handleHeartbeatLost();
272
272
  }
273
273
  }
@@ -278,7 +278,7 @@ export class ConnectionManager
278
278
  private handleHeartbeatLost(): void {
279
279
  if (!this.deviceId) return;
280
280
 
281
- this.logger.warn('Heartbeat lost for device:', this.deviceId);
281
+ this.connLogger.warn('Heartbeat lost for device:', this.deviceId);
282
282
  this.emit('heartbeat-lost', this.deviceId);
283
283
  this.stopHeartbeat();
284
284
 
@@ -317,7 +317,7 @@ export class ConnectionManager
317
317
  const deviceId = this.deviceId;
318
318
 
319
319
  if (this.reconnectAttempts > this.config.maxReconnectAttempts) {
320
- this.logger.error('Max reconnect attempts reached');
320
+ this.connLogger.error('Max reconnect attempts reached');
321
321
  this.isReconnecting = false;
322
322
  this.emit('reconnect-failed', {
323
323
  deviceId,
@@ -330,7 +330,7 @@ export class ConnectionManager
330
330
  return;
331
331
  }
332
332
 
333
- this.logger.info(
333
+ this.connLogger.info(
334
334
  `Reconnect attempt ${this.reconnectAttempts}/${this.config.maxReconnectAttempts}`
335
335
  );
336
336
  this.emit('reconnecting', {
@@ -345,7 +345,7 @@ export class ConnectionManager
345
345
  this.adapter
346
346
  .connect(deviceId)
347
347
  .then(() => {
348
- this.logger.info('Reconnected successfully');
348
+ this.connLogger.info('Reconnected successfully');
349
349
  this.isReconnecting = false;
350
350
  this.reconnectAttempts = 0;
351
351
  this.state = PrinterState.CONNECTED;
@@ -357,7 +357,7 @@ export class ConnectionManager
357
357
  }
358
358
  })
359
359
  .catch(error => {
360
- this.logger.warn(`Reconnect attempt ${this.reconnectAttempts} failed:`, error);
360
+ this.connLogger.warn(`Reconnect attempt ${this.reconnectAttempts} failed:`, error);
361
361
 
362
362
  this.reconnectTimer = setTimeout(() => {
363
363
  this.attemptReconnect();
@@ -434,7 +434,7 @@ export class ConnectionManager
434
434
  }
435
435
 
436
436
  if (this.isReconnecting) {
437
- this.logger.warn('Reconnect already in progress');
437
+ this.connLogger.warn('Reconnect already in progress');
438
438
  return;
439
439
  }
440
440
 
@@ -448,7 +448,7 @@ export class ConnectionManager
448
448
  this.clearReconnectTimer();
449
449
  this.isReconnecting = false;
450
450
  this.reconnectAttempts = 0;
451
- this.logger.info('Reconnect stopped');
451
+ this.connLogger.info('Reconnect stopped');
452
452
  }
453
453
 
454
454
  /**
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Services Module
3
+ * 服务模块 - 提供连接管理、命令构建、任务管理等功能
4
+ */
5
+
6
+ export {
7
+ ConnectionManager,
8
+ type ConnectionManagerConfig,
9
+ type ConnectionManagerEvents,
10
+ } from './ConnectionManager';
11
+
12
+ export { CommandBuilder } from './CommandBuilder';
13
+
14
+ export { PrintJobManager } from './PrintJobManager';
15
+
16
+ export * from './interfaces';