From 9faeb53ddbefd4175df8ebddd8c003890e06b211 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Wed, 13 Feb 2019 20:43:22 +0100 Subject: [PATCH] Refactor addCoordinatorToGroup. --- lib/extension/coordinatorGroup.js | 20 +++++------------ lib/zigbee.js | 37 ++++++++++++++++++------------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/lib/extension/coordinatorGroup.js b/lib/extension/coordinatorGroup.js index 2c48e6f3..3bd4b626 100644 --- a/lib/extension/coordinatorGroup.js +++ b/lib/extension/coordinatorGroup.js @@ -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}`); }); } } diff --git a/lib/zigbee.js b/lib/zigbee.js index ae43f663..c71e24f0 100644 --- a/lib/zigbee.js +++ b/lib/zigbee.js @@ -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);