Skip to content

Commit

Permalink
fix: Fix crash on IEEE address request timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Sep 29, 2024
1 parent 5d02efe commit 7d64845
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
30 changes: 18 additions & 12 deletions src/controller/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,21 +540,27 @@ class Controller extends events.EventEmitter<ControllerEventMap> {
this.unknownDevices.add(nwkAddress);
const clusterId = Zdo.ClusterId.IEEE_ADDRESS_REQUEST;
const zdoPayload = Zdo.Buffalo.buildRequest(this.adapter.hasZdoMessageOverhead, clusterId, nwkAddress, false, 0);
const response = await this.adapter.sendZdo(ZSpec.BLANK_EUI64, nwkAddress, clusterId, zdoPayload, false);

if (Zdo.Buffalo.checkStatus(response)) {
const payload = response[1];
const device = Device.byIeeeAddr(payload.eui64);
try {
const response = await this.adapter.sendZdo(ZSpec.BLANK_EUI64, nwkAddress, clusterId, zdoPayload, false);

/* istanbul ignore else */
if (device) {
this.checkDeviceNetworkAddress(device, payload.eui64, payload.nwkAddress);
this.unknownDevices.delete(payload.nwkAddress);
}
if (Zdo.Buffalo.checkStatus(response)) {
const payload = response[1];
const device = Device.byIeeeAddr(payload.eui64);

return device;
} else {
logger.debug(`Failed to retrieve IEEE address for device '${nwkAddress}': ${Zdo.Status[response[0]]}`, NS);
/* istanbul ignore else */
if (device) {
this.checkDeviceNetworkAddress(device, payload.eui64, payload.nwkAddress);
this.unknownDevices.delete(payload.nwkAddress);
}

return device;
} else {
throw new Zdo.StatusError(response[0]);
}
} catch (error) {
// Catches 2 types of exception: Zdo.StatusError and no response from `adapter.sendZdo()`.
logger.debug(`Failed to retrieve IEEE address for device '${nwkAddress}': ${error}`, NS);
}

// NOTE: by keeping nwkAddress in `this.unknownDevices` on fail, it prevents a non-responding device from potentially spamming identify.
Expand Down
2 changes: 1 addition & 1 deletion test/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10385,7 +10385,7 @@ describe('Controller', () => {
expect(device.networkAddress).toStrictEqual(oldNwkAddress);
expect(device.modelID).toBe('TRADFRI bulb E27 WS opal 980lm');
expect(mockLogger.debug).toHaveBeenCalledWith(
`Failed to retrieve IEEE address for device '${newNwkAddress}': INV_REQUESTTYPE`,
`Failed to retrieve IEEE address for device '${newNwkAddress}': Error: Status 'INV_REQUESTTYPE'`,
'zh:controller',
);
expect(events.lastSeenChanged.length).toBe(0);
Expand Down

0 comments on commit 7d64845

Please sign in to comment.