From 954187a7018ba126e3c65f84a5d5083591e2eb57 Mon Sep 17 00:00:00 2001 From: Alexander Herlin Date: Thu, 15 Aug 2024 23:28:49 +0200 Subject: [PATCH] Yet another try at the re-connection issue. --- CHANGELOG.md | 4 ++++ package.json | 2 +- src/plejdService.ts | 43 ++++++++++++++++++++++++++++++++----------- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ab85b8..b4abc50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +#### 1.4.7 (2024-08-15) + +Yet another try at the re-connection issue. + #### 1.4.7 (2024-08-13) Check support for HB 2.0. Added more retry if device has not proppely disconnected yet. diff --git a/package.json b/package.json index 244fcdf..4bbcd8e 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "displayName": "Plejd", "name": "homebridge-plejd", "author": "Herlix", - "version": "1.4.7", + "version": "1.4.8", "description": "HomeKit support for the Plejd BLE platform using Homebridge", "license": "Apache-2.0", "type": "module", diff --git a/src/plejdService.ts b/src/plejdService.ts index 20351bb..d0231e9 100644 --- a/src/plejdService.ts +++ b/src/plejdService.ts @@ -98,7 +98,7 @@ export class PlejdService { this.log.debug(`Noble State changed: ${state}`); if (state === "poweredOn") { this.log.info("Scanning for Plejd devices as we started..."); - await noble.startScanningAsync([PlejdCharacteristics.Service], false); + await this.startScanning(); } }); @@ -120,38 +120,56 @@ export class PlejdService { ); await noble.stopScanningAsync(); + if (this.connected()) { + await this.connectedPeripheral?.disconnectAsync(); + } + try { await peripheral.connectAsync(); // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (error: any) { - if (error.toString().includes("Peripheral already connected")) { - await peripheral?.disconnectAsync(); - await this.onDiscover(peripheral); - return; - } - this.log.error( `Connecting failed | ${peripheral.advertisement.localName} | addr: ${peripheral.address}) - err: ${error}`, ); + await peripheral?.disconnectAsync(); + noble.reset(); + await this.startScanning(); return; } this.connectedPeripheral = peripheral.once("disconnect", async () => { this.log.info("Disconnected from mesh"); - noble.reset(); if (noble._state === "poweredOn") { this.log.info( "Scanning for Plejd devices as we are disconnected from mesh...", ); - await noble.startScanningAsync([PlejdCharacteristics.Service], false); + await this.startScanning(); } }); - const characteristics = await this.discoverCaracteristics(peripheral); + let characteristics: noble.Characteristic[] | undefined; + try { + characteristics = await this.discoverCaracteristics(peripheral); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + } catch (e: any) { + this.log.error( + "Failed to discover characteristics, disconnecting. Error:", + e, + ); + if (peripheral.state === "connected") { + await peripheral.disconnectAsync(); + this.startScanning(); + } + return; + } + if (!characteristics) { - this.log.error("Failed to discover characteristics, disconnecting..."); + this.log.error( + "Error: No characteristics found, disconnecting and scanning again...", + ); if (peripheral.state === "connected") { await peripheral.disconnectAsync(); + this.startScanning(); } return; } @@ -379,4 +397,7 @@ export class PlejdService { private connected = () => this.connectedPeripheral && this.connectedPeripheral.state === "connected"; + + private startScanning = async () => + await noble.startScanningAsync([PlejdCharacteristics.Service], false); }