Skip to content

Commit

Permalink
fix: Add setter for checkinInterval (#867)
Browse files Browse the repository at this point in the history
  • Loading branch information
slugzero committed Jan 11, 2024
1 parent de43275 commit 87c0c50
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/controller/model/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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}`);
}
Expand Down Expand Up @@ -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 */
Expand Down
3 changes: 2 additions & 1 deletion test/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 87c0c50

Please sign in to comment.