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: endpoint device & group members typing #1330

Merged
merged 2 commits into from
Mar 6, 2025
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
18 changes: 15 additions & 3 deletions src/controller/model/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ export class Endpoint extends Entity {
}

get configuredReportings(): ConfiguredReporting[] {
const device = this.getDevice();

return this._configuredReportings.map((entry, index) => {
const cluster = Zcl.Utils.getCluster(entry.cluster, entry.manufacturerCode, this.getDevice().customClusters);
const cluster = Zcl.Utils.getCluster(entry.cluster, entry.manufacturerCode, device.customClusters);
const attribute: ZclTypes.Attribute = cluster.hasAttribute(entry.attrId)
? cluster.getAttribute(entry.attrId)
: {ID: entry.attrId, name: `attr${index}`, type: Zcl.DataType.UNKNOWN, manufacturerCode: undefined};
Expand Down Expand Up @@ -166,7 +168,14 @@ export class Endpoint extends Entity {
* Get device of this endpoint
*/
public getDevice(): Device {
return Device.byIeeeAddr(this.deviceIeeeAddress)!; // XXX: no way for device to not exist?
const device = Device.byIeeeAddr(this.deviceIeeeAddress);

if (!device) {
logger.error(`Tried to get unknown/deleted device ${this.deviceIeeeAddress} from endpoint ${this.ID}.`, NS);
logger.debug(new Error().stack!, NS);
}

return device!;
}

/**
Expand Down Expand Up @@ -883,7 +892,10 @@ export class Endpoint extends Entity {
}

private getCluster(clusterKey: number | string, device: Device | undefined = undefined): ZclTypes.Cluster {
device = device ?? this.getDevice();
if (!device) {
device = this.getDevice();
}

return Zcl.Utils.getCluster(clusterKey, device.manufacturerID, device.customClusters);
}

Expand Down
43 changes: 28 additions & 15 deletions src/controller/model/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ interface OptionsWithDefaults extends Options {
export class Group extends Entity {
private databaseID: number;
public readonly groupID: number;
private readonly _members: Set<Endpoint>;
get members(): Endpoint[] {
return Array.from(this._members).filter((e) => e.getDevice());
}
private readonly _members: Endpoint[];
// Can be used by applications to store data.
public readonly meta: KeyValue;

Expand All @@ -38,7 +35,12 @@ export class Group extends Entity {
private static readonly groups: Map<number /* groupID */, Group> = new Map();
private static loadedFromDatabase: boolean = false;

private constructor(databaseID: number, groupID: number, members: Set<Endpoint>, meta: KeyValue) {
/** Member endpoints with valid devices (not unknown/deleted) */
get members(): Endpoint[] {
return this._members.filter((e) => e.getDevice() !== undefined);
}

private constructor(databaseID: number, groupID: number, members: Endpoint[], meta: KeyValue) {
super();
this.databaseID = databaseID;
this.groupID = groupID;
Expand All @@ -59,7 +61,8 @@ export class Group extends Entity {
}

private static fromDatabaseEntry(entry: DatabaseEntry): Group {
const members = new Set<Endpoint>();
// db is expected to never contain duplicate, so no need for explicit check
const members: Endpoint[] = [];

for (const member of entry.members) {
const device = Device.byIeeeAddr(member.deviceIeeeAddr);
Expand All @@ -68,7 +71,7 @@ export class Group extends Entity {
const endpoint = device.getEndpoint(member.endpointID);

if (endpoint) {
members.add(endpoint);
members.push(endpoint);
}
}
}
Expand All @@ -79,8 +82,12 @@ export class Group extends Entity {
private toDatabaseRecord(): DatabaseEntry {
const members: DatabaseEntry['members'] = [];

for (const member of this.members) {
members.push({deviceIeeeAddr: member.getDevice().ieeeAddr, endpointID: member.ID});
for (const member of this._members) {
const device = member.getDevice();

if (device) {
members.push({deviceIeeeAddr: device.ieeeAddr, endpointID: member.ID});
}
}

return {id: this.databaseID, type: 'Group', groupID: this.groupID, members, meta: this.meta};
Expand Down Expand Up @@ -133,7 +140,7 @@ export class Group extends Entity {
}

const databaseID = Entity.database!.newID();
const group = new Group(databaseID, groupID, new Set(), {});
const group = new Group(databaseID, groupID, [], {});
Entity.database!.insert(group.toDatabaseRecord());

Group.groups.set(group.groupID, group);
Expand Down Expand Up @@ -163,17 +170,23 @@ export class Group extends Entity {
}

public addMember(endpoint: Endpoint): void {
this._members.add(endpoint);
this.save();
if (!this._members.includes(endpoint)) {
this._members.push(endpoint);
this.save();
}
}

public removeMember(endpoint: Endpoint): void {
this._members.delete(endpoint);
this.save();
const i = this._members.indexOf(endpoint);

if (i > -1) {
this._members.splice(i, 1);
this.save();
}
}

public hasMember(endpoint: Endpoint): boolean {
return this._members.has(endpoint);
return this._members.includes(endpoint);
}

/*
Expand Down
22 changes: 19 additions & 3 deletions test/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5279,6 +5279,22 @@ describe('Controller', () => {
});
});

it('Try to get deleted device from endpoint', async () => {
await controller.start();
await mockAdapterEvents['deviceJoined']({networkAddress: 129, ieeeAddr: '0x129'});
const device = controller.getDeviceByIeeeAddr('0x129')!;
const endpoint = device.getEndpoint(1)!;
await mockAdapterEvents['deviceLeave']({networkAddress: 129, ieeeAddr: '0x129'});
// @ts-expect-error private
expect(Device.devices.size).toStrictEqual(1);
// @ts-expect-error private
expect(Device.deletedDevices.size).toStrictEqual(1);
const delDevice = endpoint.getDevice();

expect(delDevice).toBeUndefined();
expect(mockLogger.error).toHaveBeenCalledWith('Tried to get unknown/deleted device 0x129 from endpoint 1.', 'zh:controller:endpoint');
});

it('Command response', async () => {
await controller.start();
await mockAdapterEvents['deviceJoined']({networkAddress: 129, ieeeAddr: '0x129'});
Expand Down Expand Up @@ -6852,15 +6868,15 @@ describe('Controller', () => {
expect((await controller.getGroups()).length).toBe(2);

const group1 = controller.getGroupByID(1)!;
expect(deepClone(group1)).toStrictEqual(deepClone({_events: {}, _eventsCount: 0, databaseID: 2, groupID: 1, _members: new Set(), meta: {}}));
expect(deepClone(group1)).toStrictEqual(deepClone({_events: {}, _eventsCount: 0, databaseID: 2, groupID: 1, _members: [], meta: {}}));
const group2 = controller.getGroupByID(2)!;
expect(deepClone(group2)).toStrictEqual(
deepClone({
_events: {},
_eventsCount: 0,
databaseID: 5,
groupID: 2,
_members: new Set([
_members: [
{
meta: {},
_binds: [],
Expand All @@ -6877,7 +6893,7 @@ describe('Controller', () => {
pendingRequests: {ID: 1, deviceIeeeAddress: '0x000b57fffec6a5b2', sendInProgress: false},
profileID: 49246,
},
]),
],
meta: {},
}),
);
Expand Down