Slight restructure of inspectDevicesDiscovered method to avoid scattered returns

This commit is contained in:
Victor Hagelbäck 2021-02-08 20:08:40 +01:00
parent c6d7bc2e3e
commit 7c0fc24bc6

View file

@ -141,10 +141,10 @@ class PlejBLEHandler extends EventEmitter {
}
async _inspectDevicesDiscovered() {
try {
if (this.bleDevices.length === 0) {
logger.error('Discovery timeout elapsed, no devices found. Starting reconnect loop...');
this.startReconnectPeriodicallyLoop();
return;
throw new Error('Discovery timeout elapsed');
}
logger.info(`Device discovery done, found ${this.bleDevices.length} Plejd devices`);
@ -192,19 +192,17 @@ class PlejBLEHandler extends EventEmitter {
logger.verbose('Trying again...');
await this._startGetPlejdDevice();
} catch (errInner) {
logger.error('Failed to retry internalInit. Starting reconnect loop');
this.startReconnectPeriodicallyLoop();
return;
logger.error('Failed to retry internalInit. Starting reconnect loop', errInner);
throw new Error('Failed to retry internalInit');
}
}
logger.error('Failed to start discovery. Make sure no other add-on is currently scanning.');
return;
throw new Error('Failed to start discovery');
}
if (!this.connectedDevice) {
logger.error('Could not connect to any Plejd device. Starting reconnect loop...');
this.startReconnectPeriodicallyLoop();
return;
throw new Error('Could not connect to any Plejd device');
}
logger.info(`BLE Connected to ${this.connectedDevice.name}`);
@ -222,6 +220,12 @@ class PlejBLEHandler extends EventEmitter {
// invalidated (third param),
) => this.onLastDataUpdated(iface, properties));
this.characteristics.lastData.StartNotify();
} catch (err) {
// This method is run on a timer, so errors can't e re-thrown.
// Start reconnect loop if errors occur here
logger.debug(`Starting reconnect loop due to ${err.message}`);
this.startReconnectPeriodicallyLoop();
}
}
async _getInterface() {