Skip to content

Commit

Permalink
feat(bluetooth-low-energy): added max distance option
Browse files Browse the repository at this point in the history
Mirrors the feature of room-assistant v1. If peripherals are found with
an estimated distance higher than configured as max, the sighting is
dropped.
  • Loading branch information
mKeRix committed Feb 9, 2020
1 parent a4425ba commit c7098cc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
Expand Up @@ -8,6 +8,7 @@ export class BluetoothLowEnergyConfig {
tagOverrides: { [key: string]: TagOverride } = {};

timeout = 5;
maxDistance?: number;
}

class TagOverride {
Expand Down
Expand Up @@ -407,6 +407,42 @@ describe('BluetoothLowEnergyService', () => {
);
});

it('should report peripherals that are closer than the max distance', () => {
const handleDistanceSpy = jest
.spyOn(service, 'handleNewDistance')
.mockImplementation(() => undefined);
jest.spyOn(service, 'isOnWhitelist').mockReturnValue(true);
mockConfig.maxDistance = 5;

service.handleDiscovery({
id: '12:ab:cd:12:cd',
rssi: -45,
advertisement: {
localName: 'Test BLE Device'
}
} as Peripheral);
expect(handleDistanceSpy).toHaveBeenCalled();
expect(clusterService.publish).toHaveBeenCalled();
});

it('should ignore peripherals that are further away than max distance', () => {
const handleDistanceSpy = jest
.spyOn(service, 'handleNewDistance')
.mockImplementation(() => undefined);
jest.spyOn(service, 'isOnWhitelist').mockReturnValue(true);
mockConfig.maxDistance = 5;

service.handleDiscovery({
id: '12:ab:cd:12:cd',
rssi: -89,
advertisement: {
localName: 'Test BLE Device'
}
} as Peripheral);
expect(handleDistanceSpy).not.toHaveBeenCalled();
expect(clusterService.publish).not.toHaveBeenCalled();
});

it('should reuse existing Kalman filters for the same id', () => {
const sensor = new Sensor('testid', 'Test');
entitiesService.has.mockReturnValue(true);
Expand Down
Expand Up @@ -87,15 +87,20 @@ export class BluetoothLowEnergyService extends KalmanFilterable(Object, 0.8, 15)
tag = this.applyOverrides(tag);
tag.rssi = this.filterRssi(tag.id, tag.rssi);

const globalSettings = this.configService.get('global');
const event = new NewDistanceEvent(
globalSettings.instanceName,
tag.id,
tag.name,
tag.distance
);
this.handleNewDistance(event);
this.clusterService.publish(NEW_DISTANCE_CHANNEL, event);
if (
this.config.maxDistance == undefined ||
tag.distance <= this.config.maxDistance
) {
const globalSettings = this.configService.get('global');
const event = new NewDistanceEvent(
globalSettings.instanceName,
tag.id,
tag.name,
tag.distance
);
this.handleNewDistance(event);
this.clusterService.publish(NEW_DISTANCE_CHANNEL, event);
}
}
}

Expand Down

0 comments on commit c7098cc

Please sign in to comment.