Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Cleanup network address change code. #1201

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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