From 643a52e0de864a69d87d58507a879212c833faae Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Fri, 29 Nov 2024 21:52:33 +0100 Subject: [PATCH 1/4] Add logging --- src/adapter/z-stack/adapter/zStackAdapter.ts | 1 + src/utils/queue.ts | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/src/adapter/z-stack/adapter/zStackAdapter.ts b/src/adapter/z-stack/adapter/zStackAdapter.ts index bf93c6b4ff..5c969d06d9 100644 --- a/src/adapter/z-stack/adapter/zStackAdapter.ts +++ b/src/adapter/z-stack/adapter/zStackAdapter.ts @@ -417,6 +417,7 @@ class ZStackAdapter extends Adapter { disableRecovery: boolean, sourceEndpoint?: number, ): Promise { + logger.debug(`== sendZclFrameToEndpoint add to queue - ${ieeeAddr}/${networkAddress}`, NS); return await this.queue.execute(async () => { this.checkInterpanLock(); return await this.sendZclFrameToEndpointInternal( diff --git a/src/utils/queue.ts b/src/utils/queue.ts index b10911bb28..6e7cf09109 100644 --- a/src/utils/queue.ts +++ b/src/utils/queue.ts @@ -1,9 +1,13 @@ +import {logger} from './logger'; + interface Job { key?: string | number; running: boolean; start?: () => void; } +const NS = 'zh:queue'; + class Queue { private jobs: Job[]; private readonly concurrent: number; @@ -14,6 +18,7 @@ class Queue { } public async execute(func: () => Promise, key?: string | number): Promise { + logger.debug(`== queue add - ${key} (keys=${this.jobs.map((j) => j.key)})`, NS); const job: Job = {key, running: false}; this.jobs.push(job); @@ -33,9 +38,11 @@ class Queue { } try { + logger.debug(`== queue execute func - ${key} (keys=${this.jobs.map((j) => j.key)})`, NS); return await func(); } finally { this.jobs.splice(this.jobs.indexOf(job), 1); + logger.debug(`== queue remove - ${key} (keys=${this.jobs.map((j) => j.key)})`, NS); this.executeNext(); } } From ca313a389f6e3c4f0da396ed074b2ebf97161865 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Tue, 3 Dec 2024 21:51:57 +0100 Subject: [PATCH 2/4] Remove network address disovery --- src/adapter/z-stack/adapter/zStackAdapter.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/adapter/z-stack/adapter/zStackAdapter.ts b/src/adapter/z-stack/adapter/zStackAdapter.ts index 5c969d06d9..63733e215c 100644 --- a/src/adapter/z-stack/adapter/zStackAdapter.ts +++ b/src/adapter/z-stack/adapter/zStackAdapter.ts @@ -600,15 +600,15 @@ class ZStackAdapter extends Adapter { // Figure out once if the network address has been changed. try { checkedNetworkAddress = true; - const actualNetworkAddress = await this.requestNetworkAddress(ieeeAddr); - if (networkAddress !== actualNetworkAddress) { - logger.debug(`Failed because request was done with wrong network address`, NS); - discoveredRoute = true; - networkAddress = actualNetworkAddress; - await this.discoverRoute(actualNetworkAddress); - } else { - logger.debug('Network address did not change', NS); - } + // const actualNetworkAddress = await this.requestNetworkAddress(ieeeAddr); + // if (networkAddress !== actualNetworkAddress) { + // logger.debug(`Failed because request was done with wrong network address`, NS); + // discoveredRoute = true; + // networkAddress = actualNetworkAddress; + // await this.discoverRoute(actualNetworkAddress); + // } else { + // logger.debug('Network address did not change', NS); + // } } catch { /* empty */ } From da7948f65d6a982bd3b1b509d41965e4ffddea72 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Sun, 8 Dec 2024 21:50:41 +0100 Subject: [PATCH 3/4] updates --- src/adapter/z-stack/adapter/zStackAdapter.ts | 53 +++++++++++++++----- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/src/adapter/z-stack/adapter/zStackAdapter.ts b/src/adapter/z-stack/adapter/zStackAdapter.ts index 63733e215c..419582419b 100644 --- a/src/adapter/z-stack/adapter/zStackAdapter.ts +++ b/src/adapter/z-stack/adapter/zStackAdapter.ts @@ -258,7 +258,7 @@ class ZStackAdapter extends Adapter { const clusterId = Zdo.ClusterId.NETWORK_ADDRESS_REQUEST; const zdoPayload = Zdo.Buffalo.buildRequest(this.hasZdoMessageOverhead, clusterId, ieeeAddr as EUI64, false, 0); - const result = await this.sendZdo(ieeeAddr, ZSpec.NULL_NODE_ID, clusterId, zdoPayload, false); + const result = await this.sendZdoInternal(ieeeAddr, ZSpec.NULL_NODE_ID, clusterId, zdoPayload, false, true); /* istanbul ignore else */ if (Zdo.Buffalo.checkStatus(result)) { @@ -308,7 +308,35 @@ class ZStackAdapter extends Adapter { payload: Buffer, disableResponse: boolean, ): Promise { - return await this.queue.execute(async () => { + // @ts-expect-error TODO fix + return await this.sendZdoInternal(ieeeAddress, networkAddress, clusterId, payload, disableResponse, true); + } + + private async sendZdoInternal( + ieeeAddress: string, + networkAddress: number, + clusterId: Zdo.ClusterId, + payload: Buffer, + disableResponse: true, + skipQueue: boolean, + ): Promise; + private async sendZdoInternal( + ieeeAddress: string, + networkAddress: number, + clusterId: K, + payload: Buffer, + disableResponse: false, + skipQueue: boolean, + ): Promise; + private async sendZdoInternal( + ieeeAddress: string, + networkAddress: number, + clusterId: K, + payload: Buffer, + disableResponse: boolean, + skipQueue: boolean, + ): Promise { + const func = async (): Promise => { this.checkInterpanLock(); // stack-specific requirements @@ -404,7 +432,8 @@ class ZStackAdapter extends Adapter { return response.payload.zdo; } - }, networkAddress); + }; + return skipQueue ? await func() : await this.queue.execute(func, networkAddress); } public async sendZclFrameToEndpoint( @@ -600,15 +629,15 @@ class ZStackAdapter extends Adapter { // Figure out once if the network address has been changed. try { checkedNetworkAddress = true; - // const actualNetworkAddress = await this.requestNetworkAddress(ieeeAddr); - // if (networkAddress !== actualNetworkAddress) { - // logger.debug(`Failed because request was done with wrong network address`, NS); - // discoveredRoute = true; - // networkAddress = actualNetworkAddress; - // await this.discoverRoute(actualNetworkAddress); - // } else { - // logger.debug('Network address did not change', NS); - // } + const actualNetworkAddress = await this.requestNetworkAddress(ieeeAddr); + if (networkAddress !== actualNetworkAddress) { + logger.debug(`Failed because request was done with wrong network address`, NS); + discoveredRoute = true; + networkAddress = actualNetworkAddress; + await this.discoverRoute(actualNetworkAddress); + } else { + logger.debug('Network address did not change', NS); + } } catch { /* empty */ } From bb4f1d839bdf44d0db16820e5dfce5ad7ca0eeca Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Sun, 8 Dec 2024 21:51:40 +0100 Subject: [PATCH 4/4] whoops --- src/adapter/z-stack/adapter/zStackAdapter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adapter/z-stack/adapter/zStackAdapter.ts b/src/adapter/z-stack/adapter/zStackAdapter.ts index 419582419b..2eba7b6659 100644 --- a/src/adapter/z-stack/adapter/zStackAdapter.ts +++ b/src/adapter/z-stack/adapter/zStackAdapter.ts @@ -309,7 +309,7 @@ class ZStackAdapter extends Adapter { disableResponse: boolean, ): Promise { // @ts-expect-error TODO fix - return await this.sendZdoInternal(ieeeAddress, networkAddress, clusterId, payload, disableResponse, true); + return await this.sendZdoInternal(ieeeAddress, networkAddress, clusterId, payload, disableResponse, false); } private async sendZdoInternal(