Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerivec committed Nov 30, 2024
1 parent 0063ef6 commit 1fcbc85
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/controller/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Controller extends events.EventEmitter<ControllerEventMap> {
private touchlink: Touchlink;

private permitJoinTimer: NodeJS.Timeout | undefined;
private permitJoinEnd?: number;
private backupTimer: NodeJS.Timeout | undefined;
private databaseSaveTimer: NodeJS.Timeout | undefined;
private stopping: boolean;
Expand Down Expand Up @@ -284,6 +285,7 @@ class Controller extends events.EventEmitter<ControllerEventMap> {
public async permitJoin(time: number, device?: Device): Promise<void> {
clearTimeout(this.permitJoinTimer);
this.permitJoinTimer = undefined;
this.permitJoinEnd = undefined;

if (time > 0) {
// never permit more than uint8, and never permit 255 that is often equal to "forever"
Expand All @@ -292,11 +294,14 @@ class Controller extends events.EventEmitter<ControllerEventMap> {
await this.adapter.permitJoin(time, device?.networkAddress);
await this.greenPower.permitJoin(time, device?.networkAddress);

const timeMs = time * 1000;
this.permitJoinEnd = Date.now() + timeMs;
this.permitJoinTimer = setTimeout((): void => {
this.emit('permitJoinChanged', {permitted: false});

this.permitJoinTimer = undefined;
}, time * 1000);
this.permitJoinEnd = undefined;
}, timeMs);

this.emit('permitJoinChanged', {permitted: true, time});
} else {
Expand All @@ -313,6 +318,10 @@ class Controller extends events.EventEmitter<ControllerEventMap> {
return this.permitJoinTimer !== undefined;
}

public getPermitJoinEnd(): number | undefined {
return this.permitJoinEnd;
}

public isStopping(): boolean {
return this.stopping;
}
Expand Down
19 changes: 19 additions & 0 deletions test/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2343,13 +2343,18 @@ describe('Controller', () => {

it('Controller permit joining all, disabled automatically', async () => {
await controller.start();

expect(controller.getPermitJoin()).toStrictEqual(false);
expect(controller.getPermitJoinEnd()).toBeUndefined();

await controller.permitJoin(254);

expect(mockAdapterPermitJoin).toHaveBeenCalledTimes(1);
expect(mockAdapterPermitJoin).toHaveBeenNthCalledWith(1, 254, undefined);
expect(events.permitJoinChanged.length).toStrictEqual(1);
expect(events.permitJoinChanged[0]).toStrictEqual({permitted: true, time: 254});
expect(controller.getPermitJoin()).toStrictEqual(true);
expect(controller.getPermitJoinEnd()).toBeGreaterThan(0);

// Green power
const commisionFrameEnable = Zcl.Frame.create(1, 1, true, undefined, 2, 'commisioningMode', 33, {options: 0x0b, commisioningWindow: 254}, {});
Expand All @@ -2369,6 +2374,7 @@ describe('Controller', () => {
expect(mocksendZclFrameToAll).toHaveBeenCalledTimes(1);
expect(mockAdapterPermitJoin).toHaveBeenCalledTimes(1);
expect(controller.getPermitJoin()).toStrictEqual(true);
expect(controller.getPermitJoinEnd()).toBeGreaterThan(0);

// Timer expired
await jest.advanceTimersByTimeAsync(10 * 1000);
Expand All @@ -2377,20 +2383,26 @@ describe('Controller', () => {
expect(events.permitJoinChanged.length).toStrictEqual(2);
expect(events.permitJoinChanged[1]).toStrictEqual({permitted: false});
expect(controller.getPermitJoin()).toStrictEqual(false);
expect(controller.getPermitJoinEnd()).toBeUndefined();

// Green power
expect(mocksendZclFrameToAll).toHaveBeenCalledTimes(1);
});

it('Controller permit joining all, disabled manually', async () => {
await controller.start();

expect(controller.getPermitJoin()).toStrictEqual(false);
expect(controller.getPermitJoinEnd()).toBeUndefined();

await controller.permitJoin(254);

expect(mockAdapterPermitJoin).toHaveBeenCalledTimes(1);
expect(mockAdapterPermitJoin).toHaveBeenNthCalledWith(1, 254, undefined);
expect(events.permitJoinChanged.length).toStrictEqual(1);
expect(events.permitJoinChanged[0]).toStrictEqual({permitted: true, time: 254});
expect(controller.getPermitJoin()).toStrictEqual(true);
expect(controller.getPermitJoinEnd()).toBeGreaterThan(0);

// Green power
const commisionFrameEnable = Zcl.Frame.create(1, 1, true, undefined, 2, 'commisioningMode', 33, {options: 0x0b, commisioningWindow: 254}, {});
Expand All @@ -2410,6 +2422,7 @@ describe('Controller', () => {
expect(mocksendZclFrameToAll).toHaveBeenCalledTimes(1);
expect(mockAdapterPermitJoin).toHaveBeenCalledTimes(1);
expect(controller.getPermitJoin()).toStrictEqual(true);
expect(controller.getPermitJoinEnd()).toBeGreaterThan(0);

// Disable
await controller.permitJoin(0);
Expand All @@ -2419,6 +2432,7 @@ describe('Controller', () => {
expect(events.permitJoinChanged.length).toStrictEqual(2);
expect(events.permitJoinChanged[1]).toStrictEqual({permitted: false});
expect(controller.getPermitJoin()).toStrictEqual(false);
expect(controller.getPermitJoinEnd()).toBeUndefined();

// Green power
const commissionFrameDisable = Zcl.Frame.create(1, 1, true, undefined, 3, 'commisioningMode', 33, {options: 0x0a, commisioningWindow: 0}, {});
Expand All @@ -2444,10 +2458,12 @@ describe('Controller', () => {
expect(events.permitJoinChanged.length).toStrictEqual(1);
expect(events.permitJoinChanged[0]).toStrictEqual({permitted: true, time: 254});
expect(controller.getPermitJoin()).toStrictEqual(true);
expect(controller.getPermitJoinEnd()).toBeGreaterThan(0);

await jest.advanceTimersByTimeAsync(120 * 1000);

expect(controller.getPermitJoin()).toStrictEqual(true);
expect(controller.getPermitJoinEnd()).toBeGreaterThan(0);

// Timer expired
await jest.advanceTimersByTimeAsync(300 * 1000);
Expand All @@ -2456,6 +2472,7 @@ describe('Controller', () => {
expect(events.permitJoinChanged.length).toStrictEqual(2);
expect(events.permitJoinChanged[1]).toStrictEqual({permitted: false});
expect(controller.getPermitJoin()).toStrictEqual(false);
expect(controller.getPermitJoinEnd()).toBeUndefined();
});

it('Controller permit joining for specific time', async () => {
Expand All @@ -2471,6 +2488,7 @@ describe('Controller', () => {
await jest.advanceTimersByTimeAsync(5 * 1000);

expect(controller.getPermitJoin()).toStrictEqual(true);
expect(controller.getPermitJoinEnd()).toBeGreaterThan(0);

// Timer expired
await jest.advanceTimersByTimeAsync(7 * 1000);
Expand All @@ -2479,6 +2497,7 @@ describe('Controller', () => {
expect(events.permitJoinChanged.length).toStrictEqual(2);
expect(events.permitJoinChanged[1]).toStrictEqual({permitted: false});
expect(controller.getPermitJoin()).toStrictEqual(false);
expect(controller.getPermitJoinEnd()).toBeUndefined();
});

it('Controller permit joining for too long time throws', async () => {
Expand Down

0 comments on commit 1fcbc85

Please sign in to comment.