From 9b9f43f5521f29a54c77ed40b487e25856aef52e Mon Sep 17 00:00:00 2001 From: Robert Eckhoff <54947543+apella12@users.noreply.github.com> Date: Mon, 25 Sep 2023 04:46:41 -0400 Subject: [PATCH] feat: make timeout to send devices back to sleep configurable, lower default to 250 ms (#6312) Signed-off-by: Bob Eckhoff Co-authored-by: Dominic Griesel --- packages/zwave-js/src/lib/driver/Driver.ts | 14 +++++++++++++- packages/zwave-js/src/lib/driver/ZWaveOptions.ts | 6 ++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/zwave-js/src/lib/driver/Driver.ts b/packages/zwave-js/src/lib/driver/Driver.ts index a543707b89de..e870427d2781 100644 --- a/packages/zwave-js/src/lib/driver/Driver.ts +++ b/packages/zwave-js/src/lib/driver/Driver.ts @@ -253,6 +253,7 @@ const defaultOptions: ZWaveOptions = { report: 1000, // ReportTime timeout SHOULD be set to CommandTime + 1 second nonce: 5000, sendDataCallback: 65000, // as defined in INS13954 + sendToSleep: 250, // The default should be enough time for applications to react to devices waking up refreshValue: 5000, // Default should handle most slow devices until we have a better solution refreshValueAfterTransition: 1000, // To account for delays in the device serialAPIStarted: 5000, @@ -312,6 +313,14 @@ function checkOptions(options: ZWaveOptions): void { ZWaveErrorCodes.Driver_InvalidOptions, ); } + if ( + options.timeouts.sendToSleep < 10 || options.timeouts.sendToSleep > 5000 + ) { + throw new ZWaveError( + `The Send To Sleep timeout must be between 10 and 5000 milliseconds!`, + ZWaveErrorCodes.Driver_InvalidOptions, + ); + } if (options.timeouts.sendDataCallback < 10000) { throw new ZWaveError( `The Send Data Callback timeout must be at least 10000 milliseconds!`, @@ -5882,7 +5891,10 @@ ${handlers.length} left`, if (wakeUpInterval !== 0) { this.sendNodeToSleepTimers.set( node.id, - setTimeout(() => sendNodeToSleep(node), 1000).unref(), + setTimeout( + () => sendNodeToSleep(node), + this.options.timeouts.sendToSleep, + ).unref(), ); } } diff --git a/packages/zwave-js/src/lib/driver/ZWaveOptions.ts b/packages/zwave-js/src/lib/driver/ZWaveOptions.ts index 4e342ec16050..646e7227c618 100644 --- a/packages/zwave-js/src/lib/driver/ZWaveOptions.ts +++ b/packages/zwave-js/src/lib/driver/ZWaveOptions.ts @@ -29,6 +29,12 @@ export interface ZWaveOptions extends ZWaveHostOptions { /** How long generated nonces are valid */ nonce: number; // [3000...20000], default: 5000 ms + /** + * How long to wait without pending commands before sending a node back to sleep. + * Should be as short as possible to save battery, but long enough to give applications time to react. + */ + sendToSleep: number; // [10...5000], default: 250 ms + /** * **!!! INTERNAL !!!** *