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() { async _inspectDevicesDiscovered() {
try {
if (this.bleDevices.length === 0) { if (this.bleDevices.length === 0) {
logger.error('Discovery timeout elapsed, no devices found. Starting reconnect loop...'); logger.error('Discovery timeout elapsed, no devices found. Starting reconnect loop...');
this.startReconnectPeriodicallyLoop(); throw new Error('Discovery timeout elapsed');
return;
} }
logger.info(`Device discovery done, found ${this.bleDevices.length} Plejd devices`); logger.info(`Device discovery done, found ${this.bleDevices.length} Plejd devices`);
@ -192,19 +192,17 @@ class PlejBLEHandler extends EventEmitter {
logger.verbose('Trying again...'); logger.verbose('Trying again...');
await this._startGetPlejdDevice(); await this._startGetPlejdDevice();
} catch (errInner) { } catch (errInner) {
logger.error('Failed to retry internalInit. Starting reconnect loop'); logger.error('Failed to retry internalInit. Starting reconnect loop', errInner);
this.startReconnectPeriodicallyLoop(); throw new Error('Failed to retry internalInit');
return;
} }
} }
logger.error('Failed to start discovery. Make sure no other add-on is currently scanning.'); 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) { if (!this.connectedDevice) {
logger.error('Could not connect to any Plejd device. Starting reconnect loop...'); logger.error('Could not connect to any Plejd device. Starting reconnect loop...');
this.startReconnectPeriodicallyLoop(); throw new Error('Could not connect to any Plejd device');
return;
} }
logger.info(`BLE Connected to ${this.connectedDevice.name}`); logger.info(`BLE Connected to ${this.connectedDevice.name}`);
@ -222,6 +220,12 @@ class PlejBLEHandler extends EventEmitter {
// invalidated (third param), // invalidated (third param),
) => this.onLastDataUpdated(iface, properties)); ) => this.onLastDataUpdated(iface, properties));
this.characteristics.lastData.StartNotify(); 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() { async _getInterface() {