Slight restructure of inspectDevicesDiscovered method to avoid scattered returns
This commit is contained in:
parent
c6d7bc2e3e
commit
7c0fc24bc6
1 changed files with 74 additions and 70 deletions
|
|
@ -141,87 +141,91 @@ class PlejBLEHandler extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
async _inspectDevicesDiscovered() {
|
async _inspectDevicesDiscovered() {
|
||||||
if (this.bleDevices.length === 0) {
|
try {
|
||||||
logger.error('Discovery timeout elapsed, no devices found. Starting reconnect loop...');
|
if (this.bleDevices.length === 0) {
|
||||||
this.startReconnectPeriodicallyLoop();
|
logger.error('Discovery timeout elapsed, no devices found. Starting reconnect loop...');
|
||||||
return;
|
throw new Error('Discovery timeout elapsed');
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(`Device discovery done, found ${this.bleDevices.length} Plejd devices`);
|
logger.info(`Device discovery done, found ${this.bleDevices.length} Plejd devices`);
|
||||||
|
|
||||||
const sortedDevices = this.bleDevices.sort((a, b) => b.rssi - a.rssi);
|
const sortedDevices = this.bleDevices.sort((a, b) => b.rssi - a.rssi);
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
|
for (const plejd of sortedDevices) {
|
||||||
|
try {
|
||||||
|
logger.verbose(`Inspecting ${plejd.path}`);
|
||||||
|
if (plejd.instance) {
|
||||||
|
logger.info(`Connecting to ${plejd.path}`);
|
||||||
|
// eslint-disable-next-line no-await-in-loop
|
||||||
|
await plejd.instance.Connect();
|
||||||
|
|
||||||
|
logger.verbose('Connected. Waiting for timeout before reading characteristics...');
|
||||||
|
// eslint-disable-next-line no-await-in-loop
|
||||||
|
await delay(this.config.connectionTimeout * 1000);
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-await-in-loop
|
||||||
|
const connectedPlejdDevice = await this._onDeviceConnected(plejd);
|
||||||
|
if (connectedPlejdDevice) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
logger.warn('Unable to connect.', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line no-restricted-syntax
|
|
||||||
for (const plejd of sortedDevices) {
|
|
||||||
try {
|
try {
|
||||||
logger.verbose(`Inspecting ${plejd.path}`);
|
logger.verbose('Stopping discovery...');
|
||||||
if (plejd.instance) {
|
await this.adapter.StopDiscovery();
|
||||||
logger.info(`Connecting to ${plejd.path}`);
|
logger.verbose('Stopped BLE discovery');
|
||||||
// eslint-disable-next-line no-await-in-loop
|
} catch (err) {
|
||||||
await plejd.instance.Connect();
|
logger.error('Failed to stop discovery.', err);
|
||||||
|
if (err.message.includes('Operation already in progress')) {
|
||||||
logger.verbose('Connected. Waiting for timeout before reading characteristics...');
|
logger.info(
|
||||||
// eslint-disable-next-line no-await-in-loop
|
'If you continue to get "operation already in progress" error, you can try power cycling the bluetooth adapter. Get root console access, run "bluetoothctl" => "power off" => "power on" => "exit" => restart addon.',
|
||||||
await delay(this.config.connectionTimeout * 1000);
|
);
|
||||||
|
try {
|
||||||
// eslint-disable-next-line no-await-in-loop
|
await delay(250);
|
||||||
const connectedPlejdDevice = await this._onDeviceConnected(plejd);
|
logger.verbose('Power cycling...');
|
||||||
if (connectedPlejdDevice) {
|
await this._powerCycleAdapter();
|
||||||
break;
|
logger.verbose('Trying again...');
|
||||||
|
await this._startGetPlejdDevice();
|
||||||
|
} catch (errInner) {
|
||||||
|
logger.error('Failed to retry internalInit. Starting reconnect loop', errInner);
|
||||||
|
throw new Error('Failed to retry internalInit');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
logger.error('Failed to start discovery. Make sure no other add-on is currently scanning.');
|
||||||
logger.warn('Unable to connect.', err);
|
throw new Error('Failed to start discovery');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
if (!this.connectedDevice) {
|
||||||
logger.verbose('Stopping discovery...');
|
logger.error('Could not connect to any Plejd device. Starting reconnect loop...');
|
||||||
await this.adapter.StopDiscovery();
|
throw new Error('Could not connect to any Plejd device');
|
||||||
logger.verbose('Stopped BLE discovery');
|
}
|
||||||
|
|
||||||
|
logger.info(`BLE Connected to ${this.connectedDevice.name}`);
|
||||||
|
this.emit('connected');
|
||||||
|
|
||||||
|
// Connected and authenticated, start ping
|
||||||
|
this.startPing();
|
||||||
|
this.startWriteQueue();
|
||||||
|
|
||||||
|
// After we've authenticated, we need to hook up the event listener
|
||||||
|
// for changes to lastData.
|
||||||
|
this.characteristics.lastDataProperties.on('PropertiesChanged', (
|
||||||
|
iface,
|
||||||
|
properties,
|
||||||
|
// invalidated (third param),
|
||||||
|
) => this.onLastDataUpdated(iface, properties));
|
||||||
|
this.characteristics.lastData.StartNotify();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error('Failed to stop discovery.', err);
|
// This method is run on a timer, so errors can't e re-thrown.
|
||||||
if (err.message.includes('Operation already in progress')) {
|
// Start reconnect loop if errors occur here
|
||||||
logger.info(
|
logger.debug(`Starting reconnect loop due to ${err.message}`);
|
||||||
'If you continue to get "operation already in progress" error, you can try power cycling the bluetooth adapter. Get root console access, run "bluetoothctl" => "power off" => "power on" => "exit" => restart addon.',
|
|
||||||
);
|
|
||||||
try {
|
|
||||||
await delay(250);
|
|
||||||
logger.verbose('Power cycling...');
|
|
||||||
await this._powerCycleAdapter();
|
|
||||||
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 start discovery. Make sure no other add-on is currently scanning.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.connectedDevice) {
|
|
||||||
logger.error('Could not connect to any Plejd device. Starting reconnect loop...');
|
|
||||||
this.startReconnectPeriodicallyLoop();
|
this.startReconnectPeriodicallyLoop();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(`BLE Connected to ${this.connectedDevice.name}`);
|
|
||||||
this.emit('connected');
|
|
||||||
|
|
||||||
// Connected and authenticated, start ping
|
|
||||||
this.startPing();
|
|
||||||
this.startWriteQueue();
|
|
||||||
|
|
||||||
// After we've authenticated, we need to hook up the event listener
|
|
||||||
// for changes to lastData.
|
|
||||||
this.characteristics.lastDataProperties.on('PropertiesChanged', (
|
|
||||||
iface,
|
|
||||||
properties,
|
|
||||||
// invalidated (third param),
|
|
||||||
) => this.onLastDataUpdated(iface, properties));
|
|
||||||
this.characteristics.lastData.StartNotify();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async _getInterface() {
|
async _getInterface() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue