Skip to content

Commit

Permalink
feat(bluetooth-low-energy): add option for reducing discovery log ent…
Browse files Browse the repository at this point in the history
…ries (#853)
  • Loading branch information
PeteBa committed Aug 5, 2021
1 parent e900826 commit e405558
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/integrations/bluetooth-low-energy.md
Expand Up @@ -91,6 +91,7 @@ bluetoothLowEnergy:
| `instanceBeaconEnabled` | Boolean | `true` | Whether this instance should emit iBeacon advertisements via BLE, which can be used by the room-assistant companion app to auto-toggle advertising. |
| `instanceBeaconMajor` | Number | `1` | The major of the advertised iBeacon. |
| `instanceBeaconMinor` | Number | Random | The minor of the advertised iBeacon. |
| `minDiscoveryLogRssi` | Number | -999 | Only log newly discovered beacons if raw RSSI values are greater than this (useful to reduce log spam if on a busy street). |

### Tag Overrides

Expand Down
1 change: 1 addition & 0 deletions src/config/config.service.spec.fail.yml
Expand Up @@ -96,6 +96,7 @@ bluetoothLowEnergy:
timeout: 30
updateFrequence: 55 # FORMAT ERROR: Key Misspelt
# updateFrequency: 55 # FORMAT ERROR: Missing Key
minDiscoveryLogRssi: 50 # FORMAT ERROR: Negative Required
maxDistance: 44.4
rssiFactor: 1.1
tagOverrides:
Expand Down
1 change: 1 addition & 0 deletions src/config/config.service.spec.pass.yml
Expand Up @@ -91,6 +91,7 @@ bluetoothLowEnergy:
instanceBeaconMinor: 555
timeout: 30
updateFrequency: 55
minDiscoveryLogRssi: -60
maxDistance: 44.4
rssiFactor: 1.1
tagOverrides:
Expand Down
1 change: 1 addition & 0 deletions src/config/config.service.spec.ts
Expand Up @@ -87,6 +87,7 @@ describe('ConfigService', () => {
`bluetoothLowEnergy.tagOverrides.ebef1234567890-55555-444.timeout`,
`bluetoothLowEnergy.updateFrequency`,
`bluetoothLowEnergy.updateFrequence`,
`bluetoothLowEnergy.minDiscoveryLogRssi`,
`bluetoothClassic.addresses[1]`,
`bluetoothClassic.addresses[2]`,
`bluetoothClassic.addresses[3]`,
Expand Down
Expand Up @@ -58,6 +58,8 @@ export class BluetoothLowEnergyConfig {
rssiFactor = 1;
@(jf.number().positive().optional())
maxDistance?: number;
@(jf.number().max(0).required())
minDiscoveryLogRssi = -999;
}

function validateTagOverrides(options: {
Expand Down
Expand Up @@ -1005,6 +1005,7 @@ describe('BluetoothLowEnergyService', () => {

it('should log the id of new peripherals that are found', async () => {
mockConfig.processIBeacon = true;
mockConfig.minDiscoveryLogRssi = -99;
jest.spyOn(service, 'isOnAllowlist').mockReturnValue(false);

await service.handleDiscovery({
Expand All @@ -1028,8 +1029,18 @@ describe('BluetoothLowEnergyService', () => {
manufacturerData: iBeaconData,
},
} as Peripheral);
await service.handleDiscovery({
id: 'test-peripheral-99',
rssi: -99,
advertisement: {},
} as Peripheral);
await service.handleDiscovery({
id: 'test-peripheral-100',
rssi: -100,
advertisement: {},
} as Peripheral);

expect(loggerService.log).toHaveBeenCalledTimes(2);
expect(loggerService.log).toHaveBeenCalledTimes(3);
expect(loggerService.log).toHaveBeenCalledWith(
expect.stringContaining('2f234454cf6d4a0fadf2f4911ba9ffa6-1-2'),
BluetoothLowEnergyService.name,
Expand All @@ -1040,6 +1051,11 @@ describe('BluetoothLowEnergyService', () => {
BluetoothLowEnergyService.name,
expect.anything()
);
expect(loggerService.log).toHaveBeenCalledWith(
expect.stringContaining('test-peripheral-99'),
BluetoothLowEnergyService.name,
expect.anything()
);
});

describe('Companion App', () => {
Expand Down
Expand Up @@ -41,7 +41,7 @@ export class BluetoothLowEnergyService
{
private readonly config: BluetoothLowEnergyConfig;
private readonly logger: Logger;
private readonly seenIds = new Set<string>();
private readonly loggedIds = new Set<string>();
private readonly companionAppTags = new Map<string, string>();
private readonly companionAppDenylist = new Set<string>();
private tagUpdaters: {
Expand Down Expand Up @@ -96,11 +96,14 @@ export class BluetoothLowEnergyService

tag = await this.applyCompanionAppOverride(tag);

if (!this.seenIds.has(tag.id)) {
if (
!this.loggedIds.has(tag.id) &&
tag.rssi >= this.config.minDiscoveryLogRssi
) {
this.logger.log(
`Discovered new BLE peripheral ${tag.name} with ID ${tag.id} and RSSI ${tag.rssi}`
`Discovered nearby BLE peripheral ${tag.name} with ID ${tag.id} and RSSI ${tag.rssi}`
);
this.seenIds.add(tag.id);
this.loggedIds.add(tag.id);
}

if (
Expand Down

0 comments on commit e405558

Please sign in to comment.