Skip to content

Commit

Permalink
Minor refacor of reconnect logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Herlix committed Jul 9, 2024
1 parent 1c91787 commit b43bbd7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 33 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.2 (2024-07-09)

Attempt to fix issue with reconnection to mesh.

#### 1.4.1 (2024-06-30)

Bug fix: Re-scan when unable to connect to Plejd device
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.1",
"version": "1.4.2",
"description": "HomeKit support for the Plejd BLE platform using Homebridge",
"license": "Apache-2.0",
"type": "module",
Expand Down
1 change: 1 addition & 0 deletions src/PlejdHbPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export class PlejdHbPlatform implements DynamicPlatformPlugin {
log,
this.onPlejdUpdates.bind(this),
);
this.plejdService.configureBLE();

this.discoverDevices();
};
Expand Down
74 changes: 42 additions & 32 deletions src/plejdService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { randomBytes } from 'crypto';
import noble from '@abandonware/noble';
import { PLEJD_PING_TIMEOUT, PLEJD_WRITE_TIMEOUT } from './settings.js';
import { delay } from './utils.js';
import EventEmitter from 'events';

const NOBLE_IS_POWER_ON = 'poweredOn';

Expand Down Expand Up @@ -38,10 +39,10 @@ enum PlejdCommand {
}

export class PlejdService {
private connectedPeripheral: noble.Peripheral | null;
private addressBuffer: Buffer | null;
private dataCharacteristic: noble.Characteristic | null;
private sendQueue: Buffer[];
private connectedPeripheral: noble.Peripheral | null = null;
private addressBuffer: Buffer | null = null;
private dataCharacteristic: noble.Characteristic | null = null;
private sendQueue: Buffer[] = [];

constructor(
private readonly config: UserInputConfig,
Expand All @@ -51,25 +52,7 @@ export class PlejdService {
isOn: boolean,
dim?: number,
) => void,
) {
this.addressBuffer = null;
this.dataCharacteristic = null;
this.connectedPeripheral = null;
this.sendQueue = [];

noble.on('stateChange', async (state) => {
await this.startScanning(state);
});

noble.on('warning', (msg: string) => {
this.log.warn('Noble warning: ', msg);
});

noble.on(
'discover',
async (peripheral) => await this.onDiscover(peripheral),
);
}
) {}

/**
*
Expand All @@ -89,7 +72,7 @@ export class PlejdService {
!this.dataCharacteristic ||
!this.addressBuffer
) {
await this.startScanning();
await this.resetBLE();
return;
}

Expand Down Expand Up @@ -118,8 +101,38 @@ export class PlejdService {
this.sendToDeviceQueued(data);
};

configureBLE = () => {
noble.on('stateChange', async (state) => this.connectedPeripheral === null && await this.startScanning(state));
noble.on('warning', (msg: string) =>
this.log.warn('Noble warning: ', msg),
);
noble.on(
'discover',
async (peripheral) => await this.onDiscover(peripheral),
);
};

// -------------- Private -------------- \\

private resetPeripheral = async () => {
if (this.connectedPeripheral?.state === 'connected') {
this.connectedPeripheral.removeAllListeners();
await this.connectedPeripheral.disconnectAsync();
}
this.connectedPeripheral = null;
this.addressBuffer = null;
this.dataCharacteristic?.removeAllListeners();
this.dataCharacteristic = null;
};

private resetBLE = async () => {
await this.resetPeripheral();
await this.startScanning();
};

private onDiscover = async (peripheral: noble.Peripheral) => {
this.resetPeripheral();

this.log.debug(
`Discovered | ${peripheral.advertisement.localName} | addr: ${peripheral.address} | RSSI: ${peripheral.rssi} dB`,
);
Expand All @@ -131,25 +144,21 @@ export class PlejdService {
this.log.error(
`Connecting failed | ${peripheral.advertisement.localName} | addr: ${peripheral.address}) - err: ${error}`,
);
await this.startScanning();
this.resetBLE();
return;
}

this.connectedPeripheral = peripheral;

peripheral.once('disconnect', async () => {
this.connectedPeripheral?.removeAllListeners();
this.connectedPeripheral = null;
this.addressBuffer = null;
this.dataCharacteristic = null;
this.log.info('Disconnected from mesh');
await this.startScanning();
await this.resetBLE();
});

const characteristics = await this.discoverCaracteristics(peripheral);
if (!characteristics) {
this.log.error('Failed to discover characteristics, disconnecting...');
await peripheral.disconnectAsync();
await this.resetBLE();
return;
}
await this.setupDevice(peripheral, characteristics);
Expand Down Expand Up @@ -263,7 +272,7 @@ export class PlejdService {
'Ping failed, device disconnected, will retry to connect to mesh: ',
error,
);
await this.startScanning();
await this.resetBLE();
}
}
};
Expand All @@ -277,6 +286,7 @@ export class PlejdService {
!this.addressBuffer ||
this.addressBuffer?.byteLength === 0
) {
await this.resetBLE();
return;
}

Expand Down

0 comments on commit b43bbd7

Please sign in to comment.