Skip to content

Commit

Permalink
fix: move duplicate code to private function
Browse files Browse the repository at this point in the history
  • Loading branch information
sjorge committed Dec 28, 2023
1 parent 7baaf8b commit 98fb478
Showing 1 changed file with 45 additions and 56 deletions.
101 changes: 45 additions & 56 deletions src/controller/model/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,25 +362,9 @@ class Endpoint extends Entity {
): Promise<void> {
const cluster = Zcl.Utils.getCluster(clusterKey);
options = this.getOptionsWithDefaults(options, true, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);

const manufacturerCodes = new Set(Object.keys(attributes).map((nameOrID): string|number => {
// we fall back to caller|cluster provided manufacturerCode
if (cluster.hasAttribute(nameOrID)) {
const attribute = cluster.getAttribute(nameOrID);
return (attribute.manufacturerCode === undefined) ?
options.manufacturerCode :
attribute.manufacturerCode;
} else {
// unknown attribute, we should not fail on this here
return options.manufacturerCode;
}
}));
if (manufacturerCodes.size == 1) {
options.manufacturerCode = manufacturerCodes.values().next().value;
} else {
throw new Error('Cannot have attributes with different manufacturerCode ' +
"in single 'write' call");
}
options.manufacturerCode = this.ensureManufacturerCodeIsUnique(
cluster, Object.keys(attributes), options.manufacturerCode, 'write',
);

const payload: {attrId: number; dataType: number; attrData: number| string | boolean}[] = [];
for (const [nameOrID, value] of Object.entries(attributes)) {
Expand Down Expand Up @@ -461,25 +445,9 @@ class Endpoint extends Entity {
): Promise<KeyValue> {
const cluster = Zcl.Utils.getCluster(clusterKey);
options = this.getOptionsWithDefaults(options, true, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);

const manufacturerCodes = new Set(attributes.map((nameOrID): string|number => {
// we fall back to caller|cluster provided manufacturerCode
if (cluster.hasAttribute(nameOrID)) {
const attribute = cluster.getAttribute(nameOrID);
return (attribute.manufacturerCode === undefined) ?
options.manufacturerCode :
attribute.manufacturerCode;
} else {
// unknown attribute, we should not fail on this here
return options.manufacturerCode;
}
}));
if (manufacturerCodes.size == 1) {
options.manufacturerCode = manufacturerCodes.values().next().value;
} else {
throw new Error('Cannot have attributes with different manufacturerCode ' +
"in single 'read' call");
}
options.manufacturerCode = this.ensureManufacturerCodeIsUnique(
cluster, attributes, options.manufacturerCode, 'read',
);

const payload: {attrId: number}[] = [];
for (const attribute of attributes) {
Expand Down Expand Up @@ -661,24 +629,9 @@ class Endpoint extends Entity {
): Promise<void> {
const cluster = Zcl.Utils.getCluster(clusterKey);
options = this.getOptionsWithDefaults(options, true, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);

const manufacturerCodes = new Set(items.map((item): string|number => {
// we fall back to caller|cluster provided manufacturerCode
if ((typeof item.attribute !== 'object') && (cluster.hasAttribute(item.attribute))) {
const attribute = cluster.getAttribute(item.attribute);
return (attribute.manufacturerCode === undefined) ?
options.manufacturerCode :
attribute.manufacturerCode;
} else {
return options.manufacturerCode;
}
}));
if (manufacturerCodes.size == 1) {
options.manufacturerCode = manufacturerCodes.values().next().value;
} else {
throw new Error('Cannot have attributes with different manufacturerCode ' +
"in single 'configureReporting' call");
}
options.manufacturerCode = this.ensureManufacturerCodeIsUnique(
cluster, items, options.manufacturerCode, 'configureReporting',
);

const payload = items.map((item): KeyValue => {
let dataType, attrId;
Expand Down Expand Up @@ -882,6 +835,42 @@ class Endpoint extends Entity {
};
}

private ensureManufacturerCodeIsUnique(
cluster: Zcl.TsType.Cluster, attributes: (string|number)[]|ConfigureReportingItem[],
fallbackManufacturerCode: number, caller: string,
): number {
const manufacturerCodes = new Set(attributes.map((nameOrID): number => {
let attributeID;
if (typeof nameOrID == 'object') {
// ConfigureReportingItem
if (typeof nameOrID.attribute !== 'object') {
attributeID = nameOrID.attribute;
} else {
return fallbackManufacturerCode;
}
} else {
// string || number
attributeID = nameOrID;
}

// we fall back to caller|cluster provided manufacturerCode
if (cluster.hasAttribute(attributeID)) {
const attribute = cluster.getAttribute(attributeID);
return (attribute.manufacturerCode === undefined) ?
fallbackManufacturerCode :
attribute.manufacturerCode;
} else {
// unknown attribute, we should not fail on this here
return fallbackManufacturerCode;
}
}));
if (manufacturerCodes.size == 1) {
return manufacturerCodes.values().next().value;
} else {
throw new Error(`Cannot have attributes with different manufacturerCode in single '${caller}' call`);
}
}

public async addToGroup(group: Group): Promise<void> {
await this.command('genGroups', 'add', {groupid: group.groupID, groupname: ''});
group.addMember(this);
Expand Down

0 comments on commit 98fb478

Please sign in to comment.