From a75b98882b24670dfced1d272c21034ba2840724 Mon Sep 17 00:00:00 2001 From: Joris Griffioen Date: Wed, 21 Sep 2022 22:54:54 +0200 Subject: [PATCH] feat: retry failed commands --- src/BaseDevice.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/BaseDevice.ts b/src/BaseDevice.ts index 23267de..9d5ab12 100644 --- a/src/BaseDevice.ts +++ b/src/BaseDevice.ts @@ -247,9 +247,22 @@ export class BaseDevice { command: Command, options: CommandOptions = {} ): Promise { - return this.#queue.enqueue(() => - this.runCommand(command, options) - ) as Promise; + const debug = createDebug('pmu:queueCommand'); + + return this.#queue.enqueue(async () => { + // The API has issues with rapid-fire commands, a single retry appears to be enough to be reliable + try { + const response = await this.runCommand(command, options); // DO NOT "simplify" this, we need to await in try + return response; + } catch (error) { + if (typeof error === 'string' && error.includes('timed out')) { + debug('Command timed out, retrying'); + await this.runCommand(Command.Reset); + return this.runCommand(command, options); + } + throw error; + } + }) as Promise; } disconnect(): Promise {