Skip to content

Commit

Permalink
Add NiimbotAbstractClient.waitForPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
MultiMote committed Jan 8, 2025
1 parent 2659b82 commit 5f1f984
Showing 1 changed file with 49 additions and 34 deletions.
83 changes: 49 additions & 34 deletions src/client/abstract_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export abstract class NiimbotAbstractClient extends EventEmitter<ClientEventMap>
*
* @throws {@link PrintError} when {@link ResponseCommandId.In_PrintError} or {@link ResponseCommandId.In_NotSupported} received.
*
* @returns {NiimbotPacket} Printer response object.
* @returns {NiimbotPacket} Packet object.
*/
public async sendPacketWaitResponse(packet: NiimbotPacket, timeoutMs: number = 1000): Promise<NiimbotPacket> {
return this.mutex.runExclusive(async () => {
Expand All @@ -110,40 +110,57 @@ export abstract class NiimbotAbstractClient extends EventEmitter<ClientEventMap>
return new NiimbotPacket(ResponseCommandId.Invalid, []); // or undefined is better?
}

return new Promise((resolve, reject) => {
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(cmdIn) ||
[ResponseCommandId.In_PrintError, ResponseCommandId.In_NotSupported].includes(cmdIn)
) {
clearTimeout(timeout);
this.off("packetreceived", listener);

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);
}
}
};
return this.waitForPacket(packet.validResponseIds, true, timeoutMs);
});
}

timeout = setTimeout(() => {
/**
* Send wait for response for {@link timeoutMs} milliseconds.
*
* If {@link ids} is set, it will wait for packet with this command ids.
*
* @throws {@link PrintError} when {@link ResponseCommandId.In_PrintError} or {@link ResponseCommandId.In_NotSupported} received and {@link catchErrorPackets} is true.
*
* @returns {NiimbotPacket} Packet object.
*/
public async waitForPacket(
ids: ResponseCommandId[] = [],
catchErrorPackets: boolean = true,
timeoutMs: number = 1000
): Promise<NiimbotPacket> {
return new Promise((resolve, reject) => {
let timeout: NodeJS.Timeout | undefined = undefined;

const listener = (evt: PacketReceivedEvent) => {
const pktIn = evt.packet;
const cmdIn = pktIn.command as ResponseCommandId;

if (
ids.length === 0 ||
ids.includes(cmdIn) ||
(catchErrorPackets && [ResponseCommandId.In_PrintError, ResponseCommandId.In_NotSupported].includes(cmdIn))
) {
clearTimeout(timeout);
this.off("packetreceived", listener);
reject(new Error(`Timeout waiting response (waited for ${Utils.bufToHex(packet.validResponseIds, ", ")})`));
}, timeoutMs ?? 1000);

this.on("packetreceived", listener);
});
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);
}
}
};

timeout = setTimeout(() => {
this.off("packetreceived", listener);
reject(new Error(`Timeout waiting response (waited for ${Utils.bufToHex(ids, ", ")})`));
}, timeoutMs ?? 1000);

this.on("packetreceived", listener);
});
}

Expand All @@ -156,8 +173,6 @@ export abstract class NiimbotAbstractClient extends EventEmitter<ClientEventMap>
return;
}

console.log("processRawPacket")

if (data instanceof DataView) {
data = new Uint8Array(data.buffer);
}
Expand Down

0 comments on commit 5f1f984

Please sign in to comment.