-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
84 lines (81 loc) · 1.91 KB
/
index.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
import Printer, { Providers, Response } from '../../src';
import WebSocket from 'ws';
const url = 'ws://localhost:13528';
const cainiao = new Providers.Cainiao({
options: {
WebSocket,
// maxRetries: 2,
debug: true,
},
});
const doudian = new Providers.Doudian({
options: {
WebSocket,
// maxRetries: 2,
debug: true,
},
});
const printer = new Printer();
printer.register([
{ key: cainiao.providerKey, provider: cainiao },
{ key: doudian.providerKey, provider: doudian },
]);
printer.allAgents.forEach((a) => {
a.addEventListener('close', (e) => {
console.log(`${a.providerKey} printer connect closed`, e.reason, e.code);
});
});
const cainiao_print_task = {
_agentKey: 'cainiao',
taskID: '7293666',
preview: false,
printer: '',
previewType: 'pdf',
firstDocumentNumber: 10,
totalDocumentCount: 100,
documents: [
{
documentID: '0123456789',
contents: [
{
data: {
nick: '张三',
},
templateURL: 'http://cloudprint.cainiao.com/template/standard/278250/1',
},
],
},
],
};
const douyin_print_task = {
taskID: '7293666',
// printer: '打印机',
// preview: false,
documents: [
{
documentID: '01234567891',
contents: [
{
data: {
nick: '张三',
},
templateURL: '',
},
],
},
],
};
async function run() {
const connected = await printer.connect();
console.log('connected', connected);
const version = await printer.getAgentInfo('cainiao');
console.log('version', version);
const res1 = await printer.print<Response>(cainiao_print_task);
console.log('cainiao print response', res1);
const res2 = await printer.print(douyin_print_task, 'doudian');
console.log('douyin print response', res2);
}
run();
setInterval(() => {
console.log(new Date().getTime(), printer.isConnect);
}, 10000 * Math.random());