diff --git a/src/client/bluetooth_impl.ts b/src/client/bluetooth_impl.ts index 719cc1d..fbc7c2f 100644 --- a/src/client/bluetooth_impl.ts +++ b/src/client/bluetooth_impl.ts @@ -1,15 +1,9 @@ import { Mutex } from "async-mutex"; -import { - ConnectEvent, - DisconnectEvent, - PacketReceivedEvent, - RawPacketReceivedEvent, - RawPacketSentEvent, -} from "../events"; +import { ConnectEvent, DisconnectEvent, PacketReceivedEvent, RawPacketReceivedEvent, RawPacketSentEvent } from "../events"; import { ConnectionInfo, NiimbotAbstractClient } from "."; import { NiimbotPacket } from "../packets/packet"; -import { ConnectResult, ResponseCommandId } from "../packets"; -import { Utils } from "../utils"; +import { ConnectResult, PrinterErrorCode, PrintError, ResponseCommandId } from "../packets"; +import { Utils, Validators } from "../utils"; class BleConfiguration { 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; const listener = (evt: PacketReceivedEvent) => { + const pktIn = evt.packet; + const cmdIn = pktIn.command as ResponseCommandId; + if ( 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); 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); + } } }; diff --git a/src/client/serial_impl.ts b/src/client/serial_impl.ts index e5c846f..e315372 100644 --- a/src/client/serial_impl.ts +++ b/src/client/serial_impl.ts @@ -8,8 +8,8 @@ import { } from "../events"; import { ConnectionInfo, NiimbotAbstractClient } from "."; import { NiimbotPacket } from "../packets/packet"; -import { ConnectResult, ResponseCommandId } from "../packets"; -import { Utils } from "../utils"; +import { ConnectResult, PrinterErrorCode, PrintError, ResponseCommandId } from "../packets"; +import { Utils, Validators } from "../utils"; /** Uses Web Serial API */ export class NiimbotSerialClient extends NiimbotAbstractClient { @@ -147,13 +147,26 @@ export class NiimbotSerialClient extends NiimbotAbstractClient { let timeout: NodeJS.Timeout | undefined = undefined; const listener = (evt: PacketReceivedEvent) => { + const pktIn = evt.packet; + const cmdIn = pktIn.command as ResponseCommandId; + if ( 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); 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); + } } }; diff --git a/src/packets/abstraction.ts b/src/packets/abstraction.ts index bb8a664..e7a249e 100644 --- a/src/packets/abstraction.ts +++ b/src/packets/abstraction.ts @@ -4,7 +4,6 @@ import { ConnectResult, HeartbeatType, LabelType, - PrinterErrorCode, PrinterInfoType, ResponseCommandId, SoundSettingsItemType, @@ -108,15 +107,6 @@ export class Abstraction { public async getPrintStatus(): Promise<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 const r = new SequentialDataReader(packet.data); diff --git a/src/packets/packet_generator.ts b/src/packets/packet_generator.ts index db798a5..76bb371 100644 --- a/src/packets/packet_generator.ts +++ b/src/packets/packet_generator.ts @@ -160,7 +160,7 @@ export class PacketGenerator { return new NiimbotPacket( RequestCommandId.PrintStatus, [1], - [ResponseCommandId.In_PrintStatus, ResponseCommandId.In_PrintError] + [ResponseCommandId.In_PrintStatus] ); } public static printerReset(): NiimbotPacket {