-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
154 lines (130 loc) · 4.74 KB
/
cli.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
import { program, Option, InvalidArgumentError } from "@commander-js/extra-typings";
import { NiimbotHeadlessSerialClient, NiimbotHeadlessBluetoothClient, ImageEncoder } from ".";
import {
Utils,
RequestCommandId,
ResponseCommandId,
PrintTaskName,
NiimbotAbstractClient,
PrintProgressEvent,
PacketSentEvent,
PacketReceivedEvent,
PrintDirection,
EncodedImage,
printTaskNames,
} from "@mmote/niimbluelib";
import { AbstractPrintTask } from "@mmote/niimbluelib/dist/print_tasks/AbstractPrintTask";
import { PNG } from "pngjs";
type ConnectionType = "serial" | "bluetooth";
type PrintOptions = {
direction?: PrintDirection;
transport: ConnectionType;
printTask?: PrintTaskName;
address: string;
debug: boolean;
quantity: number;
};
type InfoOptions = {
transport: ConnectionType;
address: string;
debug: boolean;
};
const initClient = (transport: ConnectionType, address: string, debug: boolean): NiimbotAbstractClient => {
let client = null;
if (transport === "serial") {
client = new NiimbotHeadlessSerialClient(address);
} else if (transport === "bluetooth") {
client = new NiimbotHeadlessBluetoothClient(address);
} else {
throw new Error("Invalid transport");
}
client.addEventListener("printprogress", (e: PrintProgressEvent) => {
console.log(`Page ${e.page}/${e.pagesTotal}, Page print ${e.pagePrintProgress}%, Page feed ${e.pageFeedProgress}%`);
});
if (debug) {
client.addEventListener("packetsent", (e: PacketSentEvent) => {
console.log(`>> ${Utils.bufToHex(e.packet.toBytes())} (${RequestCommandId[e.packet.command]})`);
});
client.addEventListener("packetreceived", (e: PacketReceivedEvent) => {
console.log(`<< ${Utils.bufToHex(e.packet.toBytes())} (${ResponseCommandId[e.packet.command]})`);
});
client.addEventListener("connect", () => {
console.log("Connected");
});
client.addEventListener("disconnect", () => {
console.log("Disconnected");
});
}
return client;
};
const printImage = async (path: string, options: PrintOptions) => {
const png: PNG = await ImageEncoder.loadPng(path);
const client: NiimbotAbstractClient = initClient(options.transport, options.address, options.debug);
await client.connect();
const printTaskName: PrintTaskName | undefined = options.printTask ?? client.getPrintTaskType();
if (printTaskName === undefined) {
throw new InvalidArgumentError("Unable to detect print task, please set it manually");
}
let encoded: EncodedImage = ImageEncoder.encodePng(png, options.direction ?? client.getModelMetadata()?.printDirection);
const printTask: AbstractPrintTask = client.abstraction.newPrintTask(printTaskName, {
totalPages: options.quantity,
statusPollIntervalMs: 100,
statusTimeoutMs: 8_000,
});
try {
await printTask.printInit();
await printTask.printPage(encoded, options.quantity);
await printTask.waitForFinished();
} catch (e) {
console.error(e);
}
await client.abstraction.printEnd();
await client.disconnect();
};
const intOption = (value: string): number => {
const parsed = parseInt(value, 10);
if (isNaN(parsed) || parsed < 0) {
throw new InvalidArgumentError("Integer required");
}
return parsed;
};
const printerInfo = async (options: InfoOptions) => {
const client: NiimbotAbstractClient = initClient(options.transport, options.address, options.debug);
await client.connect();
console.log("Printer info:", client.getPrinterInfo());
console.log("Model metadata:", client.getModelMetadata());
console.log("Detected print task:", client.getPrintTaskType());
await client.disconnect();
};
program
.name("niimblue-cli");
program
.command("info")
.description("Printer information")
.option("-d, --debug", "Debug information", false)
.addOption(
new Option("-t, --transport <type>", "Transport")
.makeOptionMandatory()
.choices(["bluetooth", "serial"] as ConnectionType[])
)
.requiredOption("-a, --address <string>", "Device bluetooth address or serial port name/path")
.action(printerInfo);
program
.command("print")
.description("Prints image")
.argument("<path>", "PNG image path")
.option("-d, --debug", "Debug information", false)
.addOption(
new Option("-t, --transport <type>", "Transport")
.makeOptionMandatory()
.choices(["bluetooth", "serial"] as ConnectionType[])
)
.requiredOption("-a, --address <string>", "Device bluetooth address or serial port name/path")
.addOption(
new Option("-o, --direction <dir>", "Print direction")
.choices(["left", "top"] as PrintDirection[])
)
.addOption(new Option("-p, --print-task <type>", "Print task").choices(printTaskNames))
.requiredOption("-q, --quantity <number>", "Quantity", intOption, 1)
.action(printImage);
program.parse();