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.
- package/CHANGELOG.md +73 -195
- package/README.md +134 -386
- package/dist/index.cjs.js +1 -1
- package/dist/index.es.js +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/types/config/PrinterConfigManager.d.ts +206 -0
- package/dist/types/config/index.d.ts +8 -0
- package/dist/types/core/BluetoothPrinter.d.ts +1 -1
- package/dist/types/core/EventEmitter.d.ts +6 -26
- package/dist/types/core/index.d.ts +6 -0
- package/dist/types/device/MultiPrinterManager.d.ts +164 -0
- package/dist/types/device/index.d.ts +2 -0
- package/dist/types/drivers/CpclDriver.d.ts +304 -0
- package/dist/types/drivers/GPrinterDriver.d.ts +63 -0
- package/dist/types/drivers/ZplDriver.d.ts +325 -0
- package/dist/types/drivers/index.d.ts +9 -0
- package/dist/types/encoding/gbk-lite.d.ts +8 -0
- package/dist/types/encoding/gbk-table.d.ts +8 -30
- package/dist/types/index.d.ts +12 -8
- package/dist/types/services/BatchPrintManager.d.ts +205 -0
- package/dist/types/services/ConnectionManager.d.ts +1 -1
- package/dist/types/services/PrintHistory.d.ts +142 -0
- package/dist/types/services/PrintJobManager.d.ts +28 -4
- package/dist/types/services/PrinterStatus.d.ts +97 -0
- package/dist/types/services/index.d.ts +11 -0
- package/package.json +25 -7
- package/src/adapters/AlipayAdapter.ts +1 -0
- package/src/adapters/BaiduAdapter.ts +1 -0
- package/src/adapters/BaseAdapter.ts +6 -8
- package/src/adapters/ByteDanceAdapter.ts +1 -0
- package/src/adapters/TaroAdapter.ts +1 -0
- package/src/adapters/WebBluetoothAdapter.ts +1 -1
- package/src/config/PrinterConfigManager.ts +519 -0
- package/src/config/index.ts +15 -0
- package/src/core/BluetoothPrinter.ts +15 -15
- package/src/core/EventEmitter.ts +15 -15
- package/src/core/index.ts +7 -0
- package/src/device/MultiPrinterManager.ts +470 -0
- package/src/device/index.ts +8 -0
- package/src/drivers/CpclDriver.ts +549 -0
- package/src/drivers/GPrinterDriver.ts +115 -0
- package/src/drivers/TsplDriver.ts +9 -21
- package/src/drivers/ZplDriver.ts +543 -0
- package/src/drivers/index.ts +37 -0
- package/src/encoding/gbk-lite.ts +113 -0
- package/src/encoding/gbk-table.ts +80 -58
- package/src/index.ts +40 -35
- package/src/plugins/PluginManager.ts +3 -1
- package/src/plugins/builtin/LoggingPlugin.ts +4 -2
- package/src/plugins/builtin/RetryPlugin.ts +8 -14
- package/src/services/BatchPrintManager.ts +500 -0
- package/src/services/ConnectionManager.ts +25 -22
- package/src/services/PrintHistory.ts +336 -0
- package/src/services/PrintJobManager.ts +69 -9
- package/src/services/PrinterStatus.ts +267 -0
- package/src/services/index.ts +22 -0
- package/src/template/TemplateEngine.ts +4 -1
|
@@ -1,53 +1,31 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* GBK Encoding Table
|
|
2
|
+
* GBK Encoding Table - 懒加载版本
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* 优化策略:
|
|
5
|
+
* 1. 默认使用精简版编码表 (gbk-lite.ts,约 3500 常用字)
|
|
6
|
+
* 2. 遇到非常用字时动态加载完整编码表
|
|
7
|
+
* 3. 二分查找代替 Map,大幅减少内存占用
|
|
7
8
|
*
|
|
8
|
-
* GBK encoding uses double-byte encoding for Chinese characters:
|
|
9
|
-
* - First byte: 0x81-0xFE
|
|
10
|
-
* - Second byte: 0x40-0xFE (excluding 0x7F)
|
|
11
|
-
*
|
|
12
|
-
* 映射数据存储在 gbk-data.ts 中,运行时解码为 Map。
|
|
13
9
|
* GBK: 23940 个字符映射
|
|
14
10
|
* Big5: 13911 个字符映射
|
|
15
11
|
*/
|
|
16
|
-
/**
|
|
17
|
-
* Unicode to GBK mapping table
|
|
18
|
-
* Maps Unicode code points to GBK byte pairs
|
|
19
|
-
*/
|
|
20
12
|
export declare const unicodeToGbk: Map<number, number>;
|
|
21
|
-
/**
|
|
22
|
-
* GBK to Unicode mapping table
|
|
23
|
-
* Maps GBK byte pairs to Unicode code points
|
|
24
|
-
*/
|
|
25
13
|
export declare const gbkToUnicode: Map<number, number>;
|
|
26
|
-
/**
|
|
27
|
-
* Unicode to Big5 mapping table
|
|
28
|
-
*/
|
|
29
14
|
export declare const unicodeToBig5: Map<number, number>;
|
|
30
|
-
/**
|
|
31
|
-
* Big5 to Unicode mapping table
|
|
32
|
-
*/
|
|
33
15
|
export declare const big5ToUnicode: Map<number, number>;
|
|
34
16
|
/**
|
|
35
17
|
* Get GBK bytes for a Unicode character
|
|
36
|
-
*
|
|
37
|
-
* @returns GBK byte pair [high, low] or null if not found
|
|
18
|
+
* 先查精简表,查不到再懒加载完整表
|
|
38
19
|
*/
|
|
39
20
|
export declare function getGbkBytes(unicode: number): [number, number] | null;
|
|
40
21
|
/**
|
|
41
22
|
* Get Unicode character from GBK bytes
|
|
42
|
-
*
|
|
43
|
-
* @param low - Low byte
|
|
44
|
-
* @returns Unicode code point or null if not found
|
|
23
|
+
* 懒加载完整表
|
|
45
24
|
*/
|
|
46
25
|
export declare function getUnicodeFromGbk(high: number, low: number): number | null;
|
|
47
26
|
/**
|
|
48
27
|
* Get Big5 bytes for a Unicode character
|
|
49
|
-
*
|
|
50
|
-
* @returns Big5 byte pair or null if not found
|
|
28
|
+
* 懒加载完整表
|
|
51
29
|
*/
|
|
52
30
|
export declare function getBig5Bytes(unicode: number): [number, number] | null;
|
|
53
31
|
/**
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,24 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Taro Bluetooth Print Library
|
|
3
|
-
*
|
|
3
|
+
* 轻量级、高性能的蓝牙打印库
|
|
4
4
|
*
|
|
5
5
|
* @packageDocumentation
|
|
6
6
|
*/
|
|
7
7
|
export { BluetoothPrinter } from './core/BluetoothPrinter';
|
|
8
8
|
export type { PrinterEvents } from './core/BluetoothPrinter';
|
|
9
9
|
export { EventEmitter } from './core/EventEmitter';
|
|
10
|
-
export
|
|
11
|
-
export { TsplDriver } from './drivers/TsplDriver';
|
|
12
|
-
export type { LabelSize, TextOptions as TsplTextOptions, BarcodeOptions as TsplBarcodeOptions, QRCodeOptions as TsplQRCodeOptions, BoxOptions, LineOptions, } from './drivers/TsplDriver';
|
|
10
|
+
export * from './drivers';
|
|
13
11
|
export { TaroAdapter } from './adapters/TaroAdapter';
|
|
12
|
+
export { AlipayAdapter } from './adapters/AlipayAdapter';
|
|
13
|
+
export { BaiduAdapter } from './adapters/BaiduAdapter';
|
|
14
|
+
export { ByteDanceAdapter } from './adapters/ByteDanceAdapter';
|
|
15
|
+
export { WebBluetoothAdapter } from './adapters/WebBluetoothAdapter';
|
|
14
16
|
export { AdapterFactory } from './adapters/AdapterFactory';
|
|
15
17
|
export { BaseAdapter } from './adapters/BaseAdapter';
|
|
16
|
-
export { WebBluetoothAdapter } from './adapters/WebBluetoothAdapter';
|
|
17
18
|
export type { WebBluetoothRequestOptions } from './adapters/WebBluetoothAdapter';
|
|
18
|
-
export
|
|
19
|
-
export
|
|
20
|
-
export { DeviceManager } from './device/DeviceManager';
|
|
19
|
+
export * from './services';
|
|
20
|
+
export { DeviceManager, deviceManager } from './device/DeviceManager';
|
|
21
21
|
export type { BluetoothDevice, ScanOptions, DeviceManagerEvents } from './device/DeviceManager';
|
|
22
|
+
export { MultiPrinterManager, multiPrinterManager } from './device/MultiPrinterManager';
|
|
23
|
+
export type { PrinterConnection, MultiConnectOptions, BroadcastOptions, MultiPrinterManagerEvents, } from './device/MultiPrinterManager';
|
|
22
24
|
export { PrintQueue } from './queue/PrintQueue';
|
|
23
25
|
export type { PrintJob, PrintJobStatus, PrintJobPriority, QueueConfig, PrintQueueEvents, } from './queue/PrintQueue';
|
|
24
26
|
export { OfflineCache } from './cache/OfflineCache';
|
|
@@ -40,6 +42,8 @@ export { PlatformType, detectPlatform, isPlatformSupported } from './utils/platf
|
|
|
40
42
|
export { BluetoothPrintError, ErrorCode } from './errors/BluetoothError';
|
|
41
43
|
export { DEFAULT_CONFIG, mergeConfig } from './config/PrinterConfig';
|
|
42
44
|
export type { PrinterConfig, AdapterConfig, DriverConfig, LoggingConfig, } from './config/PrinterConfig';
|
|
45
|
+
export { PrinterConfigManager, printerConfigManager } from './config/PrinterConfigManager';
|
|
46
|
+
export type { SavedPrinter, GlobalConfig, IConfigStorage, } from './config/PrinterConfigManager';
|
|
43
47
|
export { PluginManager } from './plugins/PluginManager';
|
|
44
48
|
export { createLoggingPlugin, createRetryPlugin } from './plugins';
|
|
45
49
|
export type { Plugin, PluginHooks, PluginOptions, PluginFactory } from './plugins/types';
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Batch Print Manager
|
|
3
|
+
*
|
|
4
|
+
* Optimizes printing multiple jobs by:
|
|
5
|
+
* - Merging small jobs into larger chunks
|
|
6
|
+
* - Reducing Bluetooth communication overhead
|
|
7
|
+
* - Prioritizing urgent jobs
|
|
8
|
+
* - Batching similar content for efficiency
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const batchManager = new BatchPrintManager();
|
|
13
|
+
*
|
|
14
|
+
* // Add print jobs
|
|
15
|
+
* batchManager.addJob({ data: buffer1, priority: 1 });
|
|
16
|
+
* batchManager.addJob({ data: buffer2, priority: 2 });
|
|
17
|
+
*
|
|
18
|
+
* // Process batch when ready
|
|
19
|
+
* await batchManager.processBatch();
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Batch job entry
|
|
24
|
+
*/
|
|
25
|
+
export interface BatchJob {
|
|
26
|
+
/** Unique job ID */
|
|
27
|
+
id: string;
|
|
28
|
+
/** Print data */
|
|
29
|
+
data: Uint8Array;
|
|
30
|
+
/** Priority (higher = more urgent) */
|
|
31
|
+
priority: number;
|
|
32
|
+
/** Timestamp when added */
|
|
33
|
+
addedAt: number;
|
|
34
|
+
/** Metadata */
|
|
35
|
+
metadata?: Record<string, unknown>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Batch configuration
|
|
39
|
+
*/
|
|
40
|
+
export interface BatchConfig {
|
|
41
|
+
/** Maximum batch size in bytes */
|
|
42
|
+
maxBatchSize: number;
|
|
43
|
+
/** Maximum wait time before processing in ms */
|
|
44
|
+
maxWaitTime: number;
|
|
45
|
+
/** Minimum jobs before batching */
|
|
46
|
+
minBatchSize: number;
|
|
47
|
+
/** Merge similar content */
|
|
48
|
+
enableMerging: boolean;
|
|
49
|
+
/** Auto-process interval in ms (0 = disabled) */
|
|
50
|
+
autoProcessInterval: number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Batch statistics
|
|
54
|
+
*/
|
|
55
|
+
export interface BatchStats {
|
|
56
|
+
/** Total jobs added */
|
|
57
|
+
totalJobs: number;
|
|
58
|
+
/** Total bytes processed */
|
|
59
|
+
totalBytes: number;
|
|
60
|
+
/** Batches processed */
|
|
61
|
+
batchesProcessed: number;
|
|
62
|
+
/** Average batch size */
|
|
63
|
+
avgBatchSize: number;
|
|
64
|
+
/** Merged jobs count */
|
|
65
|
+
mergedJobs: number;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Batch events
|
|
69
|
+
*/
|
|
70
|
+
export interface BatchEvents {
|
|
71
|
+
'batch-ready': (data: BatchJob[]) => void;
|
|
72
|
+
'batch-processed': (data: {
|
|
73
|
+
jobCount: number;
|
|
74
|
+
bytes: number;
|
|
75
|
+
}) => void;
|
|
76
|
+
'job-added': (data: BatchJob) => void;
|
|
77
|
+
'job-rejected': (data: {
|
|
78
|
+
reason: string;
|
|
79
|
+
}) => void;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Batch Print Manager
|
|
83
|
+
*
|
|
84
|
+
* Collects print jobs and processes them in optimized batches.
|
|
85
|
+
* Reduces Bluetooth communication overhead by combining small jobs.
|
|
86
|
+
*/
|
|
87
|
+
export declare class BatchPrintManager {
|
|
88
|
+
private readonly logger;
|
|
89
|
+
private readonly jobs;
|
|
90
|
+
private readonly listeners;
|
|
91
|
+
private config;
|
|
92
|
+
private isProcessing;
|
|
93
|
+
private waitTimer;
|
|
94
|
+
private autoProcessTimer;
|
|
95
|
+
private stats;
|
|
96
|
+
/**
|
|
97
|
+
* Creates a new BatchPrintManager instance
|
|
98
|
+
*/
|
|
99
|
+
constructor(config?: Partial<BatchConfig>);
|
|
100
|
+
/**
|
|
101
|
+
* Register event listener
|
|
102
|
+
*/
|
|
103
|
+
on<K extends keyof BatchEvents>(event: K, callback: BatchEvents[K]): void;
|
|
104
|
+
/**
|
|
105
|
+
* Remove event listener
|
|
106
|
+
*/
|
|
107
|
+
off<K extends keyof BatchEvents>(event: K, callback: BatchEvents[K]): void;
|
|
108
|
+
/**
|
|
109
|
+
* Emit an event
|
|
110
|
+
*/
|
|
111
|
+
private emit;
|
|
112
|
+
/**
|
|
113
|
+
* Add a job to the batch queue
|
|
114
|
+
*
|
|
115
|
+
* @param data - Print data
|
|
116
|
+
* @param priority - Job priority (higher = more urgent)
|
|
117
|
+
* @param metadata - Optional metadata
|
|
118
|
+
* @returns Job ID
|
|
119
|
+
*/
|
|
120
|
+
addJob(data: Uint8Array, priority?: number, metadata?: Record<string, unknown>): string;
|
|
121
|
+
/**
|
|
122
|
+
* Add multiple jobs at once
|
|
123
|
+
*/
|
|
124
|
+
addJobs(jobs: Array<{
|
|
125
|
+
data: Uint8Array;
|
|
126
|
+
priority?: number;
|
|
127
|
+
metadata?: Record<string, unknown>;
|
|
128
|
+
}>): string[];
|
|
129
|
+
/**
|
|
130
|
+
* Cancel a job by ID
|
|
131
|
+
*/
|
|
132
|
+
cancelJob(id: string): boolean;
|
|
133
|
+
/**
|
|
134
|
+
* Cancel all jobs
|
|
135
|
+
*/
|
|
136
|
+
cancelAll(): void;
|
|
137
|
+
/**
|
|
138
|
+
* Get pending job count
|
|
139
|
+
*/
|
|
140
|
+
get pendingCount(): number;
|
|
141
|
+
/**
|
|
142
|
+
* Get pending jobs
|
|
143
|
+
*/
|
|
144
|
+
getPendingJobs(): BatchJob[];
|
|
145
|
+
/**
|
|
146
|
+
* Get current statistics
|
|
147
|
+
*/
|
|
148
|
+
getStats(): BatchStats;
|
|
149
|
+
/**
|
|
150
|
+
* Update configuration
|
|
151
|
+
*/
|
|
152
|
+
updateConfig(updates: Partial<BatchConfig>): void;
|
|
153
|
+
/**
|
|
154
|
+
* Process the current batch
|
|
155
|
+
*
|
|
156
|
+
* @param processor - Function to send batch data to printer
|
|
157
|
+
* @returns Number of jobs processed
|
|
158
|
+
*/
|
|
159
|
+
processBatch(processor: (data: Uint8Array) => Promise<void>): Promise<number>;
|
|
160
|
+
/**
|
|
161
|
+
* Prepare batch from pending jobs
|
|
162
|
+
*/
|
|
163
|
+
private prepareBatch;
|
|
164
|
+
/**
|
|
165
|
+
* Merge multiple jobs into a single buffer
|
|
166
|
+
*/
|
|
167
|
+
private mergeJobs;
|
|
168
|
+
/**
|
|
169
|
+
* Check if we should process immediately
|
|
170
|
+
*/
|
|
171
|
+
private shouldProcessImmediately;
|
|
172
|
+
/**
|
|
173
|
+
* Start the wait timer
|
|
174
|
+
*/
|
|
175
|
+
private startWaitTimer;
|
|
176
|
+
/**
|
|
177
|
+
* Clear wait timer
|
|
178
|
+
*/
|
|
179
|
+
private clearWaitTimer;
|
|
180
|
+
/**
|
|
181
|
+
* Start auto-process timer
|
|
182
|
+
*/
|
|
183
|
+
private startAutoProcess;
|
|
184
|
+
/**
|
|
185
|
+
* Stop auto-process timer
|
|
186
|
+
*/
|
|
187
|
+
private stopAutoProcess;
|
|
188
|
+
/**
|
|
189
|
+
* Clear all timers
|
|
190
|
+
*/
|
|
191
|
+
private clearTimers;
|
|
192
|
+
/**
|
|
193
|
+
* Generate unique job ID
|
|
194
|
+
*/
|
|
195
|
+
private generateId;
|
|
196
|
+
/**
|
|
197
|
+
* Reset statistics
|
|
198
|
+
*/
|
|
199
|
+
resetStats(): void;
|
|
200
|
+
/**
|
|
201
|
+
* Destroy the manager
|
|
202
|
+
*/
|
|
203
|
+
destroy(): void;
|
|
204
|
+
}
|
|
205
|
+
export declare const batchPrintManager: BatchPrintManager;
|
|
@@ -54,7 +54,7 @@ export declare class ConnectionManager extends EventEmitter<ConnectionManagerEve
|
|
|
54
54
|
private adapter;
|
|
55
55
|
private deviceId;
|
|
56
56
|
private state;
|
|
57
|
-
private readonly
|
|
57
|
+
private readonly connLogger;
|
|
58
58
|
private readonly config;
|
|
59
59
|
private heartbeatTimer;
|
|
60
60
|
private reconnectAttempts;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { PrintJobStatus, PrintJobPriority } from '../queue/PrintQueue';
|
|
2
|
+
/**
|
|
3
|
+
* Print history entry
|
|
4
|
+
*/
|
|
5
|
+
export interface PrintHistoryEntry {
|
|
6
|
+
/** Unique entry ID */
|
|
7
|
+
id: string;
|
|
8
|
+
/** Job ID from PrintJobManager */
|
|
9
|
+
jobId?: string;
|
|
10
|
+
/** Print data size in bytes */
|
|
11
|
+
dataSize: number;
|
|
12
|
+
/** Job status */
|
|
13
|
+
status: PrintJobStatus | 'unknown';
|
|
14
|
+
/** Priority */
|
|
15
|
+
priority: PrintJobPriority;
|
|
16
|
+
/** Creation timestamp */
|
|
17
|
+
createdAt: number;
|
|
18
|
+
/** Start timestamp */
|
|
19
|
+
startedAt?: number;
|
|
20
|
+
/** Completion timestamp */
|
|
21
|
+
completedAt?: number;
|
|
22
|
+
/** Duration in milliseconds */
|
|
23
|
+
duration?: number;
|
|
24
|
+
/** Error message if failed */
|
|
25
|
+
error?: string;
|
|
26
|
+
/** Device ID */
|
|
27
|
+
deviceId?: string;
|
|
28
|
+
/** Device name */
|
|
29
|
+
deviceName?: string;
|
|
30
|
+
/** Metadata */
|
|
31
|
+
metadata?: Record<string, unknown>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* History statistics
|
|
35
|
+
*/
|
|
36
|
+
export interface PrintHistoryStats {
|
|
37
|
+
/** Total jobs */
|
|
38
|
+
total: number;
|
|
39
|
+
/** Completed jobs */
|
|
40
|
+
completed: number;
|
|
41
|
+
/** Failed jobs */
|
|
42
|
+
failed: number;
|
|
43
|
+
/** Cancelled jobs */
|
|
44
|
+
cancelled: number;
|
|
45
|
+
/** Average duration in ms */
|
|
46
|
+
avgDuration: number;
|
|
47
|
+
/** Total bytes printed */
|
|
48
|
+
totalBytes: number;
|
|
49
|
+
/** Success rate */
|
|
50
|
+
successRate: number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Query options for history
|
|
54
|
+
*/
|
|
55
|
+
export interface HistoryQueryOptions {
|
|
56
|
+
/** Start date filter */
|
|
57
|
+
startDate?: number;
|
|
58
|
+
/** End date filter */
|
|
59
|
+
endDate?: number;
|
|
60
|
+
/** Status filter */
|
|
61
|
+
status?: PrintJobStatus | PrintJobStatus[];
|
|
62
|
+
/** Device ID filter */
|
|
63
|
+
deviceId?: string;
|
|
64
|
+
/** Limit results */
|
|
65
|
+
limit?: number;
|
|
66
|
+
/** Offset for pagination */
|
|
67
|
+
offset?: number;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Print History Service
|
|
71
|
+
*/
|
|
72
|
+
export declare class PrintHistory {
|
|
73
|
+
private readonly logger;
|
|
74
|
+
private readonly entries;
|
|
75
|
+
private counter;
|
|
76
|
+
private readonly maxEntries;
|
|
77
|
+
/**
|
|
78
|
+
* Creates a new PrintHistory instance
|
|
79
|
+
* @param maxEntries - Maximum number of entries to keep (default: 1000)
|
|
80
|
+
*/
|
|
81
|
+
constructor(maxEntries?: number);
|
|
82
|
+
/**
|
|
83
|
+
* Add a new print job to history
|
|
84
|
+
*/
|
|
85
|
+
addJob(params: {
|
|
86
|
+
jobId?: string;
|
|
87
|
+
data: Uint8Array;
|
|
88
|
+
status: PrintJobStatus | 'unknown';
|
|
89
|
+
priority?: PrintJobPriority;
|
|
90
|
+
deviceId?: string;
|
|
91
|
+
deviceName?: string;
|
|
92
|
+
metadata?: Record<string, unknown>;
|
|
93
|
+
}): string;
|
|
94
|
+
/**
|
|
95
|
+
* Update job status
|
|
96
|
+
*/
|
|
97
|
+
updateJob(id: string, updates: Partial<{
|
|
98
|
+
status: PrintJobStatus | 'unknown';
|
|
99
|
+
startedAt: number;
|
|
100
|
+
completedAt: number;
|
|
101
|
+
error: string;
|
|
102
|
+
}>): void;
|
|
103
|
+
/**
|
|
104
|
+
* Get entry by ID
|
|
105
|
+
*/
|
|
106
|
+
getEntry(id: string): PrintHistoryEntry | undefined;
|
|
107
|
+
/**
|
|
108
|
+
* Get recent jobs
|
|
109
|
+
*/
|
|
110
|
+
getRecent(count?: number): PrintHistoryEntry[];
|
|
111
|
+
/**
|
|
112
|
+
* Query history with filters
|
|
113
|
+
*/
|
|
114
|
+
query(options?: HistoryQueryOptions): PrintHistoryEntry[];
|
|
115
|
+
/**
|
|
116
|
+
* Get statistics
|
|
117
|
+
*/
|
|
118
|
+
getStats(options?: {
|
|
119
|
+
days?: number;
|
|
120
|
+
}): PrintHistoryStats;
|
|
121
|
+
/**
|
|
122
|
+
* Clear all history
|
|
123
|
+
*/
|
|
124
|
+
clear(): void;
|
|
125
|
+
/**
|
|
126
|
+
* Export history as JSON
|
|
127
|
+
*/
|
|
128
|
+
export(): string;
|
|
129
|
+
/**
|
|
130
|
+
* Import history from JSON
|
|
131
|
+
*/
|
|
132
|
+
import(json: string): number;
|
|
133
|
+
/**
|
|
134
|
+
* Generate unique ID
|
|
135
|
+
*/
|
|
136
|
+
private generateId;
|
|
137
|
+
/**
|
|
138
|
+
* Enforce maximum entries limit
|
|
139
|
+
*/
|
|
140
|
+
private enforceMaxEntries;
|
|
141
|
+
}
|
|
142
|
+
export declare const printHistory: PrintHistory;
|
|
@@ -4,8 +4,15 @@ import { IPrintJobManager, IConnectionManager } from './interfaces';
|
|
|
4
4
|
* Print Job Manager implementation
|
|
5
5
|
*/
|
|
6
6
|
export declare class PrintJobManager implements IPrintJobManager {
|
|
7
|
-
/**
|
|
8
|
-
private
|
|
7
|
+
/** Instance-level job state storage (per-printer support) */
|
|
8
|
+
private instanceJobStateStore;
|
|
9
|
+
/** Static job state store for backward compatibility */
|
|
10
|
+
private static _jobStateStore;
|
|
11
|
+
/**
|
|
12
|
+
* Get the static job state store (for backward compatibility)
|
|
13
|
+
* @deprecated Use instance-level store instead for multi-printer support
|
|
14
|
+
*/
|
|
15
|
+
private static get jobStateStore();
|
|
9
16
|
private adapter;
|
|
10
17
|
private connectionManager;
|
|
11
18
|
private jobBuffer;
|
|
@@ -65,8 +72,8 @@ export declare class PrintJobManager implements IPrintJobManager {
|
|
|
65
72
|
/**
|
|
66
73
|
* Saves the current job state for resume later.
|
|
67
74
|
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
75
|
+
* Uses instance-level storage by default. Falls back to static store
|
|
76
|
+
* for backward compatibility.
|
|
70
77
|
*/
|
|
71
78
|
private saveJobState;
|
|
72
79
|
/**
|
|
@@ -79,6 +86,23 @@ export declare class PrintJobManager implements IPrintJobManager {
|
|
|
79
86
|
* Clears the current job state
|
|
80
87
|
*/
|
|
81
88
|
private clearJobState;
|
|
89
|
+
/**
|
|
90
|
+
* Cleanup resources and clear all job state.
|
|
91
|
+
* Call this when the printer is no longer needed.
|
|
92
|
+
*/
|
|
93
|
+
destroy(): void;
|
|
94
|
+
/**
|
|
95
|
+
* Clean up expired job states from static store.
|
|
96
|
+
* Call this periodically to prevent memory leaks.
|
|
97
|
+
*
|
|
98
|
+
* @param maxAge - Maximum age in ms (default: 1 hour)
|
|
99
|
+
*/
|
|
100
|
+
static cleanupExpiredJobs(maxAge?: number): number;
|
|
101
|
+
/**
|
|
102
|
+
* Get count of pending job states in static store.
|
|
103
|
+
* Useful for debugging memory usage.
|
|
104
|
+
*/
|
|
105
|
+
static getStaticStoreSize(): number;
|
|
82
106
|
/**
|
|
83
107
|
* Emits job state change event
|
|
84
108
|
*
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Printer Status Service
|
|
3
|
+
*
|
|
4
|
+
* Queries printer status including paper, battery, and error states.
|
|
5
|
+
* Works with ESC/POS printers that support status commands.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const status = new PrinterStatus(printer);
|
|
10
|
+
* const paperStatus = await status.getPaperStatus();
|
|
11
|
+
* console.log('Paper:', paperStatus);
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Paper status
|
|
16
|
+
*/
|
|
17
|
+
export declare enum PaperStatus {
|
|
18
|
+
/** Paper is present and OK */
|
|
19
|
+
OK = "ok",
|
|
20
|
+
/** Paper is low */
|
|
21
|
+
LOW = "low",
|
|
22
|
+
/** Paper is out */
|
|
23
|
+
OUT = "out",
|
|
24
|
+
/** Paper status unknown */
|
|
25
|
+
UNKNOWN = "unknown"
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Printer status
|
|
29
|
+
*/
|
|
30
|
+
export interface PrinterStatusInfo {
|
|
31
|
+
/** Paper status */
|
|
32
|
+
paper: PaperStatus;
|
|
33
|
+
/** Cover open (if supported) */
|
|
34
|
+
coverOpen?: boolean;
|
|
35
|
+
/** Cutter error (if supported) */
|
|
36
|
+
cutterError?: boolean;
|
|
37
|
+
/** Motor error (if supported) */
|
|
38
|
+
motorError?: boolean;
|
|
39
|
+
/** Temperature issue (if supported) */
|
|
40
|
+
overTemp?: boolean;
|
|
41
|
+
/** Battery level (0-100, if supported) */
|
|
42
|
+
batteryLevel?: number;
|
|
43
|
+
/** Timestamp of status check */
|
|
44
|
+
timestamp: number;
|
|
45
|
+
/** Raw status bytes */
|
|
46
|
+
rawStatus?: Uint8Array;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Status query options
|
|
50
|
+
*/
|
|
51
|
+
export interface StatusQueryOptions {
|
|
52
|
+
/** Timeout for status query in ms */
|
|
53
|
+
timeout?: number;
|
|
54
|
+
/** Include raw status bytes */
|
|
55
|
+
includeRaw?: boolean;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Printer Status Service
|
|
59
|
+
*/
|
|
60
|
+
export declare class PrinterStatus {
|
|
61
|
+
private readonly logger;
|
|
62
|
+
/**
|
|
63
|
+
* Creates a new PrinterStatus instance
|
|
64
|
+
*/
|
|
65
|
+
constructor();
|
|
66
|
+
/**
|
|
67
|
+
* Get printer status
|
|
68
|
+
*
|
|
69
|
+
* Sends ESC/POS status query command and parses the response.
|
|
70
|
+
*
|
|
71
|
+
* @param writeFunc - Function to write data to printer
|
|
72
|
+
* @param readFunc - Function to read response from printer
|
|
73
|
+
* @param options - Query options
|
|
74
|
+
* @returns Printer status info
|
|
75
|
+
*/
|
|
76
|
+
getStatus(writeFunc: (data: ArrayBuffer) => Promise<void>, readFunc: () => Promise<ArrayBuffer>, options?: StatusQueryOptions): Promise<PrinterStatusInfo>;
|
|
77
|
+
/**
|
|
78
|
+
* Parse status bytes from printer
|
|
79
|
+
*
|
|
80
|
+
* Different printers return different status formats.
|
|
81
|
+
* This implementation handles common ESC/POS status responses.
|
|
82
|
+
*/
|
|
83
|
+
private parseStatus;
|
|
84
|
+
/**
|
|
85
|
+
* Check if paper is available
|
|
86
|
+
*/
|
|
87
|
+
checkPaper(writeFunc: (data: ArrayBuffer) => Promise<void>, readFunc: () => Promise<ArrayBuffer>): Promise<PaperStatus>;
|
|
88
|
+
/**
|
|
89
|
+
* Check if printer is ready
|
|
90
|
+
*/
|
|
91
|
+
isReady(writeFunc: (data: ArrayBuffer) => Promise<void>, readFunc: () => Promise<ArrayBuffer>): Promise<boolean>;
|
|
92
|
+
/**
|
|
93
|
+
* Get human-readable status string
|
|
94
|
+
*/
|
|
95
|
+
static toString(status: PrinterStatusInfo): string;
|
|
96
|
+
}
|
|
97
|
+
export declare const printerStatus: PrinterStatus;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Services Module
|
|
3
|
+
* 服务模块 - 提供连接管理、命令构建、任务管理等功能
|
|
4
|
+
*/
|
|
5
|
+
export { ConnectionManager, type ConnectionManagerConfig, type ConnectionManagerEvents, } from './ConnectionManager';
|
|
6
|
+
export { CommandBuilder } from './CommandBuilder';
|
|
7
|
+
export { PrintJobManager } from './PrintJobManager';
|
|
8
|
+
export { PrintHistory, printHistory, type PrintHistoryEntry, type PrintHistoryStats, type HistoryQueryOptions } from './PrintHistory';
|
|
9
|
+
export { PrinterStatus, printerStatus, type PrinterStatusInfo, type StatusQueryOptions, type PaperStatus } from './PrinterStatus';
|
|
10
|
+
export { BatchPrintManager, batchPrintManager, type BatchJob, type BatchConfig, type BatchStats, type BatchEvents } from './BatchPrintManager';
|
|
11
|
+
export * from './interfaces';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "taro-bluetooth-print",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"description": "Taro 蓝牙打印库 v2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
|
+
"description": "Taro 蓝牙打印库 v2.4 - 轻量级、高性能、跨平台支持微信、支付宝、百度、字节跳动小程序及H5 Web Bluetooth",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs.js",
|
|
7
7
|
"module": "dist/index.es.js",
|
|
@@ -13,6 +13,18 @@
|
|
|
13
13
|
"require": "./dist/index.cjs.js",
|
|
14
14
|
"default": "./dist/index.umd.js"
|
|
15
15
|
},
|
|
16
|
+
"./core": {
|
|
17
|
+
"types": "./dist/types/core/index.d.ts",
|
|
18
|
+
"import": "./dist/core/index.es.js"
|
|
19
|
+
},
|
|
20
|
+
"./drivers": {
|
|
21
|
+
"types": "./dist/types/drivers/index.d.ts",
|
|
22
|
+
"import": "./dist/drivers/index.es.js"
|
|
23
|
+
},
|
|
24
|
+
"./adapters": {
|
|
25
|
+
"types": "./dist/types/adapters/index.d.ts",
|
|
26
|
+
"import": "./dist/adapters/index.es.js"
|
|
27
|
+
},
|
|
16
28
|
"./package.json": "./package.json"
|
|
17
29
|
},
|
|
18
30
|
"files": [
|
|
@@ -49,13 +61,19 @@
|
|
|
49
61
|
],
|
|
50
62
|
"author": "Agions",
|
|
51
63
|
"license": "MIT",
|
|
52
|
-
"
|
|
53
|
-
"@tarojs/taro": "^
|
|
64
|
+
"peerDependencies": {
|
|
65
|
+
"@tarojs/taro": "^3.6.22"
|
|
66
|
+
},
|
|
67
|
+
"peerDependenciesMeta": {
|
|
68
|
+
"@tarojs/taro": {
|
|
69
|
+
"optional": true
|
|
70
|
+
}
|
|
54
71
|
},
|
|
55
72
|
"devDependencies": {
|
|
56
73
|
"@types/node": "^20.14.8",
|
|
57
|
-
"@
|
|
58
|
-
"@typescript-eslint/
|
|
74
|
+
"@types/web-bluetooth": "^0.0.21",
|
|
75
|
+
"@typescript-eslint/eslint-plugin": "^8.57.1",
|
|
76
|
+
"@typescript-eslint/parser": "^8.57.1",
|
|
59
77
|
"@vitest/coverage-v8": "^4.0.18",
|
|
60
78
|
"eslint": "^8.56.0",
|
|
61
79
|
"eslint-config-prettier": "^9.1.0",
|
|
@@ -67,7 +85,7 @@
|
|
|
67
85
|
"typescript": "^5.9.3",
|
|
68
86
|
"vite": "^7.3.1",
|
|
69
87
|
"vite-plugin-dts": "^4.5.4",
|
|
70
|
-
"vitepress": "^1.
|
|
88
|
+
"vitepress": "^1.6.4",
|
|
71
89
|
"vitest": "^4.0.18"
|
|
72
90
|
},
|
|
73
91
|
"sideEffects": false
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import { MiniProgramAdapter, MiniProgramBLEApi } from './BaseAdapter';
|
|
7
7
|
|
|
8
8
|
// Declare Alipay global for TypeScript
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
9
10
|
interface AlipayGlobal extends MiniProgramBLEApi {}
|
|
10
11
|
|
|
11
12
|
declare const my: AlipayGlobal;
|