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 a80e517
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 34 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
75 changes: 42 additions & 33 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,12 @@ 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[] = [];
private eventEmitters: Map<string, EventEmitter> = new Map();

constructor(
private readonly config: UserInputConfig,
Expand All @@ -51,25 +54,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 +74,7 @@ export class PlejdService {
!this.dataCharacteristic ||
!this.addressBuffer
) {
await this.startScanning();
await this.resetBLE();
return;
}

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

configureBLE = () => {
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),
);
};

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

private resetPeripheral = async () => {
await this.connectedPeripheral?.disconnectAsync();
this.connectedPeripheral?.removeAllListeners();
this.connectedPeripheral = null;
this.addressBuffer = null;
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 +143,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 +271,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 +285,7 @@ export class PlejdService {
!this.addressBuffer ||
this.addressBuffer?.byteLength === 0
) {
await this.resetBLE();
return;
}

Expand Down Expand Up @@ -347,7 +356,7 @@ export class PlejdService {
if (!state) {
state = noble._state;
}
if (state === NOBLE_IS_POWER_ON) {
if (state === NOBLE_IS_POWER_ON && !this.connectedPeripheral) {
this.log.debug('Scanning for Plejd devices');
await noble.startScanningAsync([PlejdCharacteristics.Service], false);
}
Expand Down

0 comments on commit a80e517

Please sign in to comment.