This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQmEscPos.ts
259 lines (230 loc) · 7.12 KB
/
QmEscPos.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// @ts-ignore
import * as escpos from "escpos";
import * as usb from 'usb';
import {AlginType, fontSize as fs, FontSize, PAGE_ROW_SIZE, columnsText} from "./core";
export type PrintOpts = {
align?: AlginType;
content: string;
fontSize?: FontSize;
}
type QmEscPosAlignType = 'CT' | 'LT' | 'RT';
const getAlignType = (align: AlginType): QmEscPosAlignType => {
switch (align) {
case 2:
case '2':
case 'right':
return 'RT';
case 0:
case '0':
case 'left':
return 'LT';
case 1:
case '1':
case 'center':
return 'CT';
}
return 'LT';
};
const noop = (device: usb.Device) => {};
export type DeviceConnectedHandler = (device?: usb.Device) => void;
export type DeviceDetachHandler = (device?: usb.Device) => void;
export class QmEscPos {
private static defaultOpts = {encoding: "GBK" /* default */};
private printer: any;
private device: any;
private isOpen = false;
private readonly PAGE_ROW_SIZE = 28;
private static instance: QmEscPos;
private detachHandler = noop;
private connectedHandler = noop;
public static create() {
if (!QmEscPos.instance) {
try {
QmEscPos.instance = new QmEscPos();
} catch (e) {
throw e;
}
}
return QmEscPos.instance;
}
private constructor() {
const {defaultOpts} = QmEscPos;
this.device = new escpos.USB();
this.device.on('detach', (device: usb.Device) => {
console.warn(`-`.repeat(50) + 'device detach' + `-`.repeat(50));
console.log(device);
console.warn(`-`.repeat(50) + 'device detach' + `-`.repeat(50));
if (this.detachHandler) {
this.detachHandler(device);
}
});
this.device.on('connect', (device: usb.Device) => {
console.log(`-`.repeat(50) + 'device connected' + `-`.repeat(50));
console.log(device);
console.log(`-`.repeat(50) + 'device connected' + `-`.repeat(50));
if (this.connectedHandler) {
this.connectedHandler(device);
// setImmediate(() => {
// this.connectedHandler(device);
// });
}
});
this.printer = new escpos.Printer(this.device, defaultOpts);
console.log(this.device);
console.log(this.printer);
}
private open = () => {
if (this.isOpen) {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
this.device.open((err: any) => {
if (err) {
return reject(err);
}
this.isOpen = true;
resolve();
})
});
};
public print = async (opts: PrintOpts) => {
try {
await this.open();
} catch (e) {
throw e;
}
const {printer} = this;
const {align, fontSize} = opts;
if (align) {
this.align(align);
}
if (fontSize) {
switch (fontSize) {
case 1:
case 'small':
this.size(1, 1);
break;
case 2:
case 'middle':
this.size(2, 2);
break;
case 'big':
case 3:
this.size(3, 3);
break;
default:
break;
}
}
printer.style('NORMAL').text(opts.content);
};
public printText = async (text: string, fontSize: FontSize = 'small', align: AlginType = 0) => {
fontSize = typeof fontSize === 'number' ? fontSize : fs(fontSize);
console.log(...arguments);
return this.print({content: text, fontSize, align});
};
public printlnText = async (text: String, fontSize: FontSize = 'small', align: AlginType = 0) => {
return this.print({
content: `${text}\n`,
align,
fontSize
})
};
public printColumnsText = async (texts: Array<string>, weights: Array<number>, aligns: Array<AlginType>, zoom = true) => {
const text = columnsText(texts, weights, aligns, PAGE_ROW_SIZE, zoom).reduce((prev, next) => {
// return prev + next + '\n' ;
return prev + next;
}, '');
return this.printText(text);
};
public lineWrap = async (nums: number) => {
return this.printText('\n'.repeat(nums));
};
public line = async () => {
const lineStr = ('-').repeat(this.PAGE_ROW_SIZE);
this.printlnText(lineStr);
};
public printQrCode = async (text: string) => {
await this.open();
// this.lineWrap(2);
console.log('------ printQrCode -----', text);
const {printer} = this;
return new Promise((resolve, reject) => {
this.align('center');
printer.qrimage(text, (err: any) => {
if (err) {
reject(err);
return;
}
resolve();
})
});
};
public startPrint = async () => {
return this.open();
};
public endPrint = async () => {
this.lineWrap(5);
};
public close = async () => {
const {printer} = this;
// printer.cut();
printer.close();
this.isOpen = false;
};
public getDevice = () => {
return this.device;
};
public getPrinter = () => {
return this.printer;
};
private align = (alignType: AlginType = 0) => {
const {printer} = this;
try {
printer.align(getAlignType(alignType));
} catch (e) {
throw e;
}
};
public cut = (part = true, feel = 2) => {
try {
this.printer.cut(part, feel);
} catch (e) {
throw e;
}
};
public size = (width: number, height: number) => {
try {
this.printer.size(width, height);
} catch (e) {
throw e;
}
};
public setDetachListener = (handler: DeviceDetachHandler) => {
this.detachHandler = handler;
};
public setConnectedListener = (handler: DeviceConnectedHandler) => {
this.connectedHandler = handler;
};
public getDeviceInfo = () => {
// {
// "bLength": 18,
// "bDescriptorType": 1,
// "bcdUSB": 512,
// "bDeviceClass": 0,
// "bDeviceSubClass": 0,
// "bDeviceProtocol": 0,
// "bMaxPacketSize0": 64,
// "idVendor": 1155,
// "idProduct": 22304,
// "bcdDevice": 512,
// "iManufacturer": 1,
// "iProduct": 2,
// "iSerialNumber": 3,
// "bNumConfigurations": 1
// }
// console.log(this.device.device.deviceDescriptor);
const {idVendor, idProduct, iManufacturer} = this.device.device.deviceDescriptor;
return {idVendor, idProduct, iManufacturer};
}
}