Skip to content

Commit

Permalink
Fix permitJoinTimeout never undefined (0 equals disabled).
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerivec committed Oct 10, 2024
1 parent b4fc365 commit 4abaaa8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
16 changes: 10 additions & 6 deletions src/controller/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Controller extends events.EventEmitter<ControllerEventMap> {
private touchlink: Touchlink;

private permitJoinTimeoutTimer: NodeJS.Timeout | undefined;
private permitJoinTimeout: number | undefined;
private permitJoinTimeout: number;
private backupTimer: NodeJS.Timeout | undefined;
private databaseSaveTimer: NodeJS.Timeout | undefined;
private stopping: boolean;
Expand All @@ -99,6 +99,7 @@ class Controller extends events.EventEmitter<ControllerEventMap> {
this.adapterDisconnected = true; // set false after adapter.start() is successfully called
this.options = mixinDeep(JSON.parse(JSON.stringify(DefaultOptions)), options);
this.unknownDevices = new Set();
this.permitJoinTimeout = 0;

// Validate options
for (const channel of this.options.network.channelList) {
Expand Down Expand Up @@ -276,7 +277,7 @@ class Controller extends events.EventEmitter<ControllerEventMap> {
public async permitJoin(time: number, device?: Device): Promise<void> {
clearInterval(this.permitJoinTimeoutTimer);
this.permitJoinTimeoutTimer = undefined;
this.permitJoinTimeout = undefined;
this.permitJoinTimeout = 0;

if (time > 0) {
// never permit more than uint8, and never permit 255 that is often equal to "forever"
Expand All @@ -291,12 +292,12 @@ class Controller extends events.EventEmitter<ControllerEventMap> {
this.permitJoinTimeout = time;
this.permitJoinTimeoutTimer = setInterval(async (): Promise<void> => {
// assumed valid number while in interval
this.permitJoinTimeout!--;
this.permitJoinTimeout--;

if (this.permitJoinTimeout! <= 0) {
if (this.permitJoinTimeout <= 0) {
clearInterval(this.permitJoinTimeoutTimer);
this.permitJoinTimeoutTimer = undefined;
this.permitJoinTimeout = undefined;
this.permitJoinTimeout = 0;

this.emit('permitJoinChanged', {permitted: false, timeout: this.permitJoinTimeout});
} else {
Expand All @@ -315,7 +316,10 @@ class Controller extends events.EventEmitter<ControllerEventMap> {
}
}

public getPermitJoinTimeout(): number | undefined {
/**
* @returns Timeout until permit joining expires. [0-254], with 0 being "not permitting joining".
*/
public getPermitJoinTimeout(): number {
return this.permitJoinTimeout;
}

Expand Down
16 changes: 8 additions & 8 deletions test/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2391,8 +2391,8 @@ describe('Controller', () => {

expect(mockAdapterPermitJoin).toHaveBeenCalledTimes(1);
expect(events.permitJoinChanged.length).toStrictEqual(255);
expect(events.permitJoinChanged[254]).toStrictEqual({permitted: false, timeout: undefined});
expect(controller.getPermitJoinTimeout()).toStrictEqual(undefined);
expect(events.permitJoinChanged[254]).toStrictEqual({permitted: false, timeout: 0});
expect(controller.getPermitJoinTimeout()).toStrictEqual(0);

// Green power
expect(mocksendZclFrameToAll).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -2428,8 +2428,8 @@ describe('Controller', () => {
expect(mockAdapterPermitJoin).toHaveBeenCalledTimes(2);
expect(mockAdapterPermitJoin.mock.calls[1][0]).toStrictEqual(0);
expect(events.permitJoinChanged.length).toStrictEqual(252);
expect(events.permitJoinChanged[251]).toStrictEqual({permitted: false, timeout: undefined});
expect(controller.getPermitJoinTimeout()).toStrictEqual(undefined);
expect(events.permitJoinChanged[251]).toStrictEqual({permitted: false, timeout: 0});
expect(controller.getPermitJoinTimeout()).toStrictEqual(0);

// Green power
const commissionFrameDisable = Zcl.Frame.create(1, 1, true, undefined, 3, 'commisioningMode', 33, {options: 0x0a, commisioningWindow: 0}, {});
Expand All @@ -2454,7 +2454,7 @@ describe('Controller', () => {
await jest.advanceTimersByTimeAsync(300 * 1000);

expect(mockAdapterPermitJoin).toHaveBeenCalledTimes(1);
expect(controller.getPermitJoinTimeout()).toStrictEqual(undefined);
expect(controller.getPermitJoinTimeout()).toStrictEqual(0);
});

it('Controller permit joining for specific time', async () => {
Expand All @@ -2478,8 +2478,8 @@ describe('Controller', () => {

expect(mockAdapterPermitJoin).toHaveBeenCalledTimes(1);
expect(events.permitJoinChanged.length).toStrictEqual(11);
expect(events.permitJoinChanged[10]).toStrictEqual({permitted: false, timeout: undefined});
expect(controller.getPermitJoinTimeout()).toStrictEqual(undefined);
expect(events.permitJoinChanged[10]).toStrictEqual({permitted: false, timeout: 0});
expect(controller.getPermitJoinTimeout()).toStrictEqual(0);
});

it('Controller permit joining for too long time is clamped to max', async () => {
Expand All @@ -2495,7 +2495,7 @@ describe('Controller', () => {
// Timer expired
await jest.advanceTimersByTimeAsync(300 * 1000);

expect(controller.getPermitJoinTimeout()).toStrictEqual(undefined);
expect(controller.getPermitJoinTimeout()).toStrictEqual(0);
});

it('Shouldnt create backup when adapter doesnt support it', async () => {
Expand Down

0 comments on commit 4abaaa8

Please sign in to comment.