Skip to content

Commit

Permalink
Yet another try at the re-connection issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
Herlix committed Aug 15, 2024
1 parent 7b861a0 commit 954187a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
43 changes: 32 additions & 11 deletions src/plejdService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});

Expand All @@ -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;
}
Expand Down Expand Up @@ -379,4 +397,7 @@ export class PlejdService {

private connected = () =>
this.connectedPeripheral && this.connectedPeripheral.state === "connected";

private startScanning = async () =>
await noble.startScanningAsync([PlejdCharacteristics.Service], false);
}

0 comments on commit 954187a

Please sign in to comment.