Skip to content

Commit

Permalink
Add hard reset on disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Herlix committed Jul 11, 2024
1 parent 27c0771 commit 8c187f4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 27 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#### 1.4.3 (2024-07-09)
#### 1.4.4 (2024-07-09)

Enable lower level reset on disconnect & add timeout ms.

#### 1.4.3 (2024-07-10)

BLE issue still seems to be there. This update will take another path on reconnecting to the mesh as well as handle any errors when we send commands to the Plejd device, it should now retry on reconnect.

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.4",
"version": "1.4.5",
"description": "HomeKit support for the Plejd BLE platform using Homebridge",
"license": "Apache-2.0",
"type": "module",
Expand Down
36 changes: 12 additions & 24 deletions src/plejdService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ export class PlejdService {
isOn: boolean,
brightness: number | null,
) => {
if (
!this.connectedPeripheral ||
!this.dataCharacteristic ||
!this.addressBuffer
) {
if (!this.connected() || !this.addressBuffer) {
return;
}

Expand Down Expand Up @@ -135,17 +131,9 @@ export class PlejdService {

this.connectedPeripheral = peripheral;

const clearLocals = () => {
this.connectedPeripheral = null;
this.addressBuffer = null;
this.dataCharacteristic?.removeAllListeners();
this.dataCharacteristic = null;
};

peripheral.once('disconnect', async () => {
this.log.info('Disconnected from mesh');
clearLocals();

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);
Expand All @@ -155,7 +143,9 @@ export class PlejdService {
const characteristics = await this.discoverCaracteristics(peripheral);
if (!characteristics) {
this.log.error('Failed to discover characteristics, disconnecting...');
clearLocals();
if (peripheral.state === 'connected') {
await peripheral.disconnectAsync();
}
return;
}

Expand Down Expand Up @@ -239,8 +229,8 @@ export class PlejdService {
private handleQueuedMessages = async () => {
while (
this.sendQueue.length > 0 &&
this.connectedPeripheral &&
this.dataCharacteristic
this.dataCharacteristic &&
this.connected()
) {
const data = this.sendQueue.pop();
if (!data) {
Expand All @@ -261,7 +251,7 @@ export class PlejdService {
};

private startPlejdPing = async (pingChar: noble.Characteristic) => {
while (this.connectedPeripheral && pingChar) {
while (this.connected() && pingChar) {
try {
const ping = randomBytes(1);
pingChar.writeAsync(ping, false);
Expand All @@ -285,11 +275,7 @@ export class PlejdService {
data: Buffer,
isNotification: boolean,
) => {
if (
!this.connectedPeripheral ||
!this.addressBuffer ||
this.addressBuffer?.byteLength === 0
) {
if (!this.connected() || !this.addressBuffer || this.addressBuffer?.byteLength === 0) {
return;
}

Expand Down Expand Up @@ -363,7 +349,7 @@ export class PlejdService {
this.startPlejdPing(pingChar);
await lastDataChar.subscribeAsync();
lastDataChar.on('data', async (data, isNotification) => {
if (!lastDataChar || !this.connectedPeripheral) {
if (!this.connected()) {
await lastDataChar.unsubscribeAsync();
return;
}
Expand All @@ -379,4 +365,6 @@ export class PlejdService {
false,
);
};

private connected = () => this.connectedPeripheral?.state === 'connected';
}
2 changes: 1 addition & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const PLATFORM_NAME = 'Plejd';
*/
export const PLUGIN_NAME = 'homebridge-plejd';

export const PLEJD_WRITE_TIMEOUT = 50;
export const PLEJD_WRITE_TIMEOUT = 200;
export const PLEJD_PING_TIMEOUT = 3000;
/**
* Lights and switches from Plejd
Expand Down

0 comments on commit 8c187f4

Please sign in to comment.