Add printer error handling

This commit is contained in:
MultiMote 2024-11-06 11:04:34 +03:00
parent c543bf3ceb
commit ad2907302f
4 changed files with 36 additions and 26 deletions

@ -1,15 +1,9 @@
import { Mutex } from "async-mutex"; import { Mutex } from "async-mutex";
import { import { ConnectEvent, DisconnectEvent, PacketReceivedEvent, RawPacketReceivedEvent, RawPacketSentEvent } from "../events";
ConnectEvent,
DisconnectEvent,
PacketReceivedEvent,
RawPacketReceivedEvent,
RawPacketSentEvent,
} from "../events";
import { ConnectionInfo, NiimbotAbstractClient } from "."; import { ConnectionInfo, NiimbotAbstractClient } from ".";
import { NiimbotPacket } from "../packets/packet"; import { NiimbotPacket } from "../packets/packet";
import { ConnectResult, ResponseCommandId } from "../packets"; import { ConnectResult, PrinterErrorCode, PrintError, ResponseCommandId } from "../packets";
import { Utils } from "../utils"; import { Utils, Validators } from "../utils";
class BleConfiguration { class BleConfiguration {
public static readonly SERVICE: string = "e7810a71-73ae-499d-8c15-faa9aef0c3f2"; public static readonly SERVICE: string = "e7810a71-73ae-499d-8c15-faa9aef0c3f2";
@ -134,13 +128,26 @@ export class NiimbotBluetoothClient extends NiimbotAbstractClient {
let timeout: NodeJS.Timeout | undefined = undefined; let timeout: NodeJS.Timeout | undefined = undefined;
const listener = (evt: PacketReceivedEvent) => { const listener = (evt: PacketReceivedEvent) => {
const pktIn = evt.packet;
const cmdIn = pktIn.command as ResponseCommandId;
if ( if (
packet.validResponseIds.length === 0 || packet.validResponseIds.length === 0 ||
packet.validResponseIds.includes(evt.packet.command as ResponseCommandId) packet.validResponseIds.includes(cmdIn) ||
[ResponseCommandId.In_PrintError, ResponseCommandId.In_NotSupported].includes(cmdIn)
) { ) {
clearTimeout(timeout); clearTimeout(timeout);
this.off("packetreceived", listener); this.off("packetreceived", listener);
resolve(evt.packet);
if (cmdIn === ResponseCommandId.In_PrintError) {
Validators.u8ArrayLengthEquals(pktIn.data, 1);
const errorName = PrinterErrorCode[pktIn.data[0]] ?? "unknown";
reject(new PrintError(`Print error ${pktIn.data[0]}: ${errorName}`, pktIn.data[0]));
} else if (cmdIn === ResponseCommandId.In_NotSupported) {
reject(new PrintError("Feature not supported", 0));
} else {
resolve(pktIn);
}
} }
}; };

@ -8,8 +8,8 @@ import {
} from "../events"; } from "../events";
import { ConnectionInfo, NiimbotAbstractClient } from "."; import { ConnectionInfo, NiimbotAbstractClient } from ".";
import { NiimbotPacket } from "../packets/packet"; import { NiimbotPacket } from "../packets/packet";
import { ConnectResult, ResponseCommandId } from "../packets"; import { ConnectResult, PrinterErrorCode, PrintError, ResponseCommandId } from "../packets";
import { Utils } from "../utils"; import { Utils, Validators } from "../utils";
/** Uses Web Serial API */ /** Uses Web Serial API */
export class NiimbotSerialClient extends NiimbotAbstractClient { export class NiimbotSerialClient extends NiimbotAbstractClient {
@ -147,13 +147,26 @@ export class NiimbotSerialClient extends NiimbotAbstractClient {
let timeout: NodeJS.Timeout | undefined = undefined; let timeout: NodeJS.Timeout | undefined = undefined;
const listener = (evt: PacketReceivedEvent) => { const listener = (evt: PacketReceivedEvent) => {
const pktIn = evt.packet;
const cmdIn = pktIn.command as ResponseCommandId;
if ( if (
packet.validResponseIds.length === 0 || packet.validResponseIds.length === 0 ||
packet.validResponseIds.includes(evt.packet.command as ResponseCommandId) packet.validResponseIds.includes(cmdIn) ||
[ResponseCommandId.In_PrintError, ResponseCommandId.In_NotSupported].includes(cmdIn)
) { ) {
clearTimeout(timeout); clearTimeout(timeout);
this.off("packetreceived", listener); this.off("packetreceived", listener);
resolve(evt.packet);
if (cmdIn === ResponseCommandId.In_PrintError) {
Validators.u8ArrayLengthEquals(pktIn.data, 1);
const errorName = PrinterErrorCode[pktIn.data[0]] ?? "unknown";
reject(new PrintError(`Print error ${pktIn.data[0]}: ${errorName}`, pktIn.data[0]));
} else if (cmdIn === ResponseCommandId.In_NotSupported) {
reject(new PrintError("Feature not supported", 0));
} else {
resolve(pktIn);
}
} }
}; };

@ -4,7 +4,6 @@ import {
ConnectResult, ConnectResult,
HeartbeatType, HeartbeatType,
LabelType, LabelType,
PrinterErrorCode,
PrinterInfoType, PrinterInfoType,
ResponseCommandId, ResponseCommandId,
SoundSettingsItemType, SoundSettingsItemType,
@ -108,15 +107,6 @@ export class Abstraction {
public async getPrintStatus(): Promise<PrintStatus> { public async getPrintStatus(): Promise<PrintStatus> {
const packet = await this.send(PacketGenerator.printStatus()); const packet = await this.send(PacketGenerator.printStatus());
if (packet.command === ResponseCommandId.In_PrintError) {
Validators.u8ArrayLengthEquals(packet.data, 1);
const errorName = PrinterErrorCode[packet.data[0]] ?? "unknown";
throw new PrintError(
`Print error (${ResponseCommandId[packet.command]} packet received, code is ${packet.data[0]} - ${errorName})`,
packet.data[0]
);
}
Validators.u8ArrayLengthAtLeast(packet.data, 4); // can be 8, 10, but ignore it for now Validators.u8ArrayLengthAtLeast(packet.data, 4); // can be 8, 10, but ignore it for now
const r = new SequentialDataReader(packet.data); const r = new SequentialDataReader(packet.data);

@ -160,7 +160,7 @@ export class PacketGenerator {
return new NiimbotPacket( return new NiimbotPacket(
RequestCommandId.PrintStatus, RequestCommandId.PrintStatus,
[1], [1],
[ResponseCommandId.In_PrintStatus, ResponseCommandId.In_PrintError] [ResponseCommandId.In_PrintStatus]
); );
} }
public static printerReset(): NiimbotPacket { public static printerReset(): NiimbotPacket {