Refactor addCoordinatorToGroup.

This commit is contained in:
Koen Kanters
2019-02-13 20:43:22 +01:00
parent 21a782d2c6
commit 9faeb53ddb
2 changed files with 28 additions and 29 deletions
+6 -14
View File
@@ -36,26 +36,18 @@ class CoordinatorGroup {
if (!deviceSettings || !deviceSettings.coordinator_group) {
logger.error(
`Device ${deviceLog} requires extra configuration!` +
` Please see https://github.com/Koenkk/zigbee2mqtt/blob/dev/docs/getting_started/pairing_devices.md`
` Please see https://www.zigbee2mqtt.io/getting_started/pairing_devices.html`
);
return;
}
// Add the coordinator to the group, first check if it's already in the group.
const findPayload = {endpoint: 1, groupid: deviceSettings.coordinator_group};
this.zigbee.shepherd.controller.request('ZDO', 'extFindGroup', findPayload, (error, data) => {
if (error) {
const addPayload = {endpoint: 1, groupid: deviceSettings.coordinator_group, namelen: 0, groupname: ''};
this.zigbee.shepherd.controller.request('ZDO', 'extAddGroup', addPayload, (error, data) => {
if (!error) {
logger.info(`Successfully applied coordinator group for ${deviceLog}`);
} else {
logger.error(`Failed to apply coordinator group for ${deviceLog}`);
}
});
this.zigbee.addCoordinatorToGroup(deviceSettings.coordinator_group, (error) => {
if (!error) {
logger.info(`Successfully applied coordinator group for ${deviceLog}`);
} else {
logger.error(`Failed to apply coordinator group for ${deviceLog}`);
}
logger.info(`Successfully applied coordinator group for ${deviceLog}`);
});
}
}
+22 -15
View File
@@ -119,21 +119,7 @@ class Zigbee {
}
// Add the coordinator to group 99
this.shepherd.controller.request('ZDO', 'extFindGroup', {groupid: 99, endpoint: 1}, (err) => {
// Error means that the coordinator is not in the group yet.
if (err) {
const payload = {groupid: 99, endpoint: 1, namelen: 0, groupname: ''};
this.shepherd.controller.request('ZDO', 'extAddGroup', payload, (err) => {
if (err) {
logger.info('Successfully added coordinator to group 99');
} else {
logger.error('Failed to add coordinator to group 99');
}
});
} else {
logger.debug('Coordinator is already in group 99');
}
});
this.addCoordinatorToGroup(99);
// Wait some time before we start the queue, many calls skip this queue which hangs the stick
setTimeout(() => {
@@ -144,6 +130,27 @@ class Zigbee {
logger.info('zigbee-shepherd ready');
}
addCoordinatorToGroup(groupID, callback=() => {}) {
this.shepherd.controller.request('ZDO', 'extFindGroup', {groupid: groupID, endpoint: 1}, (err) => {
// Error means that the coordinator is not in the group yet.
if (err) {
const payload = {groupid: groupID, endpoint: 1, namelen: 0, groupname: ''};
this.shepherd.controller.request('ZDO', 'extAddGroup', payload, (err) => {
if (err) {
logger.info(`Successfully added coordinator to group '${groupID}'`);
callback(false);
} else {
logger.error(`Failed to add coordinator to group '${groupID}'`);
callback(true);
}
});
} else {
logger.debug(`Coordinator is already in group '${groupID}'`);
callback(true);
}
});
}
onError(message) {
// This event may appear if zigbee-shepherd cannot decode bad packets (invalid checksum).
logger.error(message);