2019-09-09 10:48:09 -07:00
|
|
|
/* istanbul ignore file */
|
2019-02-18 10:21:54 -07:00
|
|
|
const settings = require('../util/settings');
|
|
|
|
const logger = require('../util/logger');
|
2019-09-17 09:32:16 -07:00
|
|
|
const BaseExtension = require('./baseExtension');
|
2019-02-18 10:21:54 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const topicRegex = new RegExp(`^${settings.get().mqtt.base_topic}/bridge/device/(.+)/get_group_membership$`);
|
2019-02-18 10:21:54 -07:00
|
|
|
|
2019-09-17 09:32:16 -07:00
|
|
|
class DeviceGroupMembership extends BaseExtension {
|
2019-02-18 10:21:54 -07:00
|
|
|
onMQTTConnected() {
|
|
|
|
this.mqtt.subscribe(`${settings.get().mqtt.base_topic}/bridge/device/+/get_group_membership`);
|
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async onMQTTMessage(topic, message) {
|
|
|
|
const match = topic.match(topicRegex);
|
|
|
|
if (!match) {
|
2019-02-18 10:21:54 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-09-23 13:21:27 -07:00
|
|
|
const entity = this.zigbee.resolveEntity(match[1]);
|
2019-09-09 10:48:09 -07:00
|
|
|
if (!entity || entity.type !== 'device') {
|
|
|
|
logger.error(`Device '${match[1]}' does not exist`);
|
2019-02-18 10:21:54 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const response = await entity.endpoint.command(
|
|
|
|
`genGroups`, 'getMembership', {groupcount: 0, grouplist: []}, {}, true
|
2019-02-18 10:21:54 -07:00
|
|
|
);
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const {grouplist, capacity} = response;
|
|
|
|
const msgGroupList = `${entity.device.ieeeAddr} is in groups [${grouplist}]`;
|
|
|
|
let msgCapacity;
|
|
|
|
if (capacity === 254) {
|
|
|
|
msgCapacity = 'it can be a part of at least 1 more group';
|
|
|
|
} else {
|
|
|
|
msgCapacity = `its remaining group capacity is ${capacity === 255 ? 'unknown' : capacity}`;
|
|
|
|
}
|
|
|
|
logger.info(`${msgGroupList} and ${msgCapacity}`);
|
|
|
|
|
|
|
|
this.publishEntityState(entity.device.ieeeAddr, {group_list: grouplist, group_capacity: capacity});
|
2019-02-18 10:21:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DeviceGroupMembership;
|