Skip to content

Commit

Permalink
fix: Cleanup network address change code (#1201)
Browse files Browse the repository at this point in the history
* fix: Cleanup network address change code.

* `updateNetworkAddress` => `requestNetworkAddress`
  • Loading branch information
Nerivec authored Sep 23, 2024
1 parent 80e34b8 commit d6eef4d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 73 deletions.
53 changes: 22 additions & 31 deletions src/controller/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,16 @@ class Controller extends events.EventEmitter<ControllerEventMap> {
const response = await this.adapter.sendZdo(ZSpec.BLANK_EUI64, nwkAddress, clusterId, zdoPayload, false);

if (Zdo.Buffalo.checkStatus(response)) {
// XXX: race with onIEEEAddress triggered from onZdoResponse or not?
// this duplicates the triggering but makes sure device is updated before going further...
this.onIEEEAddress(response[1]);
const payload = response[1];
const device = Device.byIeeeAddr(payload.eui64);

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

return device;
} else {
logger.debug(`Failed to retrieve IEEE address for device '${nwkAddress}': ${Zdo.Status[response[0]]}`, NS);
}
Expand All @@ -556,6 +561,16 @@ class Controller extends events.EventEmitter<ControllerEventMap> {
// This only lasts until next reboot (runtime Set), allowing to 'force' another trigger if necessary.
}

private checkDeviceNetworkAddress(device: Device, ieeeAddress: string, nwkAddress: number): void {
if (device.networkAddress !== nwkAddress) {
logger.debug(`Device '${ieeeAddress}' got new networkAddress '${nwkAddress}'`, NS);
device.networkAddress = nwkAddress;
device.save();

this.selfAndDeviceEmit(device, 'deviceNetworkAddressChanged', {device});
}
}

private onNetworkAddress(payload: ZdoTypes.NetworkAddressResponse): void {
logger.debug(`Network address from '${payload.eui64}:${payload.nwkAddress}'`, NS);
const device = Device.byIeeeAddr(payload.eui64);
Expand All @@ -567,14 +582,7 @@ class Controller extends events.EventEmitter<ControllerEventMap> {

device.updateLastSeen();
this.selfAndDeviceEmit(device, 'lastSeenChanged', {device, reason: 'networkAddress'});

if (device.networkAddress !== payload.nwkAddress) {
logger.debug(`Device '${payload.eui64}' got new networkAddress '${payload.nwkAddress}'`, NS);
device.networkAddress = payload.nwkAddress;
device.save();

this.selfAndDeviceEmit(device, 'deviceNetworkAddressChanged', {device});
}
this.checkDeviceNetworkAddress(device, payload.eui64, payload.nwkAddress);
}

private onIEEEAddress(payload: ZdoTypes.IEEEAddressResponse): void {
Expand All @@ -588,16 +596,7 @@ class Controller extends events.EventEmitter<ControllerEventMap> {

device.updateLastSeen();
this.selfAndDeviceEmit(device, 'lastSeenChanged', {device, reason: 'networkAddress'});

if (device.networkAddress !== payload.nwkAddress) {
logger.debug(`Device '${payload.eui64}' got new networkAddress '${payload.nwkAddress}'`, NS);
device.networkAddress = payload.nwkAddress;
device.save();

this.selfAndDeviceEmit(device, 'deviceNetworkAddressChanged', {device});
}

this.unknownDevices.delete(payload.nwkAddress);
this.checkDeviceNetworkAddress(device, payload.eui64, payload.nwkAddress);
}

private onDeviceAnnounce(payload: ZdoTypes.EndDeviceAnnounce): void {
Expand All @@ -612,15 +611,7 @@ class Controller extends events.EventEmitter<ControllerEventMap> {
device.updateLastSeen();
this.selfAndDeviceEmit(device, 'lastSeenChanged', {device, reason: 'deviceAnnounce'});
device.implicitCheckin();

if (device.networkAddress !== payload.nwkAddress) {
logger.debug(`Device '${payload.eui64}' announced with new networkAddress '${payload.nwkAddress}'`, NS);
device.networkAddress = payload.nwkAddress;
device.save();

this.selfAndDeviceEmit(device, 'deviceNetworkAddressChanged', {device});
}

this.checkDeviceNetworkAddress(device, payload.eui64, payload.nwkAddress);
this.selfAndDeviceEmit(device, 'deviceAnnounce', {device});
}

Expand Down
7 changes: 2 additions & 5 deletions src/controller/model/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1091,14 +1091,11 @@ class Device extends Entity<ControllerEventMap> {
* Request device to advertise its network address.
* Note: This does not actually update the device property (if needed), as this is already done with `zdoResponse` event in Controller.
*/
public async updateNetworkAddress(): Promise<void> {
public async requestNetworkAddress(): Promise<void> {
const clusterId = Zdo.ClusterId.NETWORK_ADDRESS_REQUEST;
const zdoPayload = Zdo.Buffalo.buildRequest(Entity.adapter!.hasZdoMessageOverhead, clusterId, this.ieeeAddr as EUI64, false, 0);
const response = await Entity.adapter!.sendZdo(this.ieeeAddr, ZSpec.BroadcastAddress.RX_ON_WHEN_IDLE, clusterId, zdoPayload, false);

if (!Zdo.Buffalo.checkStatus(response)) {
throw new Zdo.StatusError(response[0]);
}
await Entity.adapter!.sendZdo(this.ieeeAddr, ZSpec.BroadcastAddress.RX_ON_WHEN_IDLE, clusterId, zdoPayload, true);
}

public async removeFromNetwork(): Promise<void> {
Expand Down
Loading

0 comments on commit d6eef4d

Please sign in to comment.