From 87c0c5020ea1db00d476e9a59b0475ea51f479d5 Mon Sep 17 00:00:00 2001 From: slugzero <2014249+slugzero@users.noreply.github.com> Date: Thu, 11 Jan 2024 19:56:05 +0100 Subject: [PATCH] fix: Add setter for checkinInterval (#867) --- src/controller/model/device.ts | 14 ++++++++++++-- test/controller.test.ts | 3 ++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/controller/model/device.ts b/src/controller/model/device.ts index d149167c15..48c807f072 100755 --- a/src/controller/model/device.ts +++ b/src/controller/model/device.ts @@ -102,6 +102,10 @@ class Device extends Entity { get skipTimeResponse(): boolean {return this._skipTimeResponse;} set skipTimeResponse(skipTimeResponse: boolean) {this._skipTimeResponse = skipTimeResponse;} get checkinInterval(): number {return this._checkinInterval;} + set checkinInterval(checkinInterval: number) { + this._checkinInterval = checkinInterval; + this.resetPendingRequestTimeout(); + }; get pendingRequestTimeout(): number {return this._pendingRequestTimeout;} set pendingRequestTimeout(pendingRequestTimeout: number) {this._pendingRequestTimeout = pendingRequestTimeout;} @@ -198,6 +202,12 @@ class Device extends Entity { this._lastSeen = Date.now(); } + private resetPendingRequestTimeout(): void { + // pendingRequestTimeout can be changed dynamically at runtime, and it is not persisted. + // Default timeout is one checkin interval in milliseconds. + this._pendingRequestTimeout = this._checkinInterval * 1000; + } + private hasPendingRequests(): boolean { return this.endpoints.find(e => e.hasPendingRequests()) !== undefined; } @@ -271,7 +281,7 @@ class Device extends Entity { const pollPeriod = await endpoint.read('genPollCtrl', ['checkinInterval'], {sendPolicy: 'immediate'}); this._checkinInterval = pollPeriod.checkinInterval / 4; // convert to seconds - this.pendingRequestTimeout = this._checkinInterval * 1000; // milliseconds + this.resetPendingRequestTimeout(); debug.log(`Request Queue (${ this.ieeeAddr}): default expiration timeout set to ${this.pendingRequestTimeout}`); } @@ -732,7 +742,7 @@ class Device extends Entity { await endpoint.bind('genPollCtrl', coordinator.endpoints[0]); const pollPeriod = await endpoint.read('genPollCtrl', ['checkinInterval'], {sendPolicy: 'immediate'}); this._checkinInterval = pollPeriod.checkinInterval / 4; // convert to seconds - this.pendingRequestTimeout = this._checkinInterval * 1000; // milliseconds + this.resetPendingRequestTimeout(); } } catch (error) { /* istanbul ignore next */ diff --git a/test/controller.test.ts b/test/controller.test.ts index 74d1beae31..47b09165d6 100755 --- a/test/controller.test.ts +++ b/test/controller.test.ts @@ -4615,7 +4615,8 @@ describe('Controller', () => { await controller.start(); await mockAdapterEvents['deviceJoined']({networkAddress: 129, ieeeAddr: '0x129'}); const device = controller.getDeviceByIeeeAddr('0x129'); - device.pendingRequestTimeout = 10000; + device.checkinInterval = 10; + expect(device.pendingRequestTimeout).toStrictEqual(10000); const endpoint = device.getEndpoint(1); // We need to wait for the data to be queued const origQueueRequest = endpoint.pendingRequests.queue;