diff --git a/src/integrations/bluetooth-low-energy/bluetooth-low-energy.config.ts b/src/integrations/bluetooth-low-energy/bluetooth-low-energy.config.ts index e7044cd..f49e21d 100644 --- a/src/integrations/bluetooth-low-energy/bluetooth-low-energy.config.ts +++ b/src/integrations/bluetooth-low-energy/bluetooth-low-energy.config.ts @@ -8,6 +8,7 @@ export class BluetoothLowEnergyConfig { tagOverrides: { [key: string]: TagOverride } = {}; timeout = 5; + maxDistance?: number; } class TagOverride { diff --git a/src/integrations/bluetooth-low-energy/bluetooth-low-energy.service.spec.ts b/src/integrations/bluetooth-low-energy/bluetooth-low-energy.service.spec.ts index 8d2fa8f..40c2402 100644 --- a/src/integrations/bluetooth-low-energy/bluetooth-low-energy.service.spec.ts +++ b/src/integrations/bluetooth-low-energy/bluetooth-low-energy.service.spec.ts @@ -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); diff --git a/src/integrations/bluetooth-low-energy/bluetooth-low-energy.service.ts b/src/integrations/bluetooth-low-energy/bluetooth-low-energy.service.ts index 970187c..bf49599 100644 --- a/src/integrations/bluetooth-low-energy/bluetooth-low-energy.service.ts +++ b/src/integrations/bluetooth-low-energy/bluetooth-low-energy.service.ts @@ -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); + } } }