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 40c24028..1e1ba7dc 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,7 +407,7 @@ describe('BluetoothLowEnergyService', () => { ); }); - it('should report peripherals that are closer than the max distance', () => { + it('should report peripherals that are closer than the max distance normally', () => { const handleDistanceSpy = jest .spyOn(service, 'handleNewDistance') .mockImplementation(() => undefined); @@ -421,11 +421,20 @@ describe('BluetoothLowEnergyService', () => { localName: 'Test BLE Device' } } as Peripheral); - expect(handleDistanceSpy).toHaveBeenCalled(); - expect(clusterService.publish).toHaveBeenCalled(); + expect(handleDistanceSpy).toHaveBeenCalledWith( + expect.objectContaining({ + outOfRange: false + }) + ); + expect(clusterService.publish).toHaveBeenCalledWith( + NEW_DISTANCE_CHANNEL, + expect.objectContaining({ + outOfRange: false + }) + ); }); - it('should ignore peripherals that are further away than max distance', () => { + it('should mark peripherals that are further away than max distance as out of range', () => { const handleDistanceSpy = jest .spyOn(service, 'handleNewDistance') .mockImplementation(() => undefined); @@ -439,8 +448,17 @@ describe('BluetoothLowEnergyService', () => { localName: 'Test BLE Device' } } as Peripheral); - expect(handleDistanceSpy).not.toHaveBeenCalled(); - expect(clusterService.publish).not.toHaveBeenCalled(); + expect(handleDistanceSpy).toHaveBeenCalledWith( + expect.objectContaining({ + outOfRange: true + }) + ); + expect(clusterService.publish).toHaveBeenCalledWith( + NEW_DISTANCE_CHANNEL, + expect.objectContaining({ + outOfRange: true + }) + ); }); it('should reuse existing Kalman filters for the same id', () => { @@ -481,7 +499,7 @@ describe('BluetoothLowEnergyService', () => { new NewDistanceEvent('test-instance', 'test', 'Test', 2) ); - expect(sensorHandleSpy).toHaveBeenCalledWith('test-instance', 2); + expect(sensorHandleSpy).toHaveBeenCalledWith('test-instance', 2, false); }); it('should add new room presence sensor if no matching ones exist yet', () => { @@ -505,7 +523,7 @@ describe('BluetoothLowEnergyService', () => { expect.any(Array) ); expect(setInterval).toHaveBeenCalledWith(expect.any(Function), 20 * 1000); - expect(sensorHandleSpy).toHaveBeenCalledWith('test-instance', 1.3); + expect(sensorHandleSpy).toHaveBeenCalledWith('test-instance', 1.3, false); }); it('should log the id of new peripherals that are found', () => { 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 bf495993..5ab469c0 100644 --- a/src/integrations/bluetooth-low-energy/bluetooth-low-energy.service.ts +++ b/src/integrations/bluetooth-low-energy/bluetooth-low-energy.service.ts @@ -87,20 +87,16 @@ export class BluetoothLowEnergyService extends KalmanFilterable(Object, 0.8, 15) tag = this.applyOverrides(tag); tag.rssi = this.filterRssi(tag.id, tag.rssi); - 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); - } + const globalSettings = this.configService.get('global'); + const event = new NewDistanceEvent( + globalSettings.instanceName, + tag.id, + tag.name, + tag.distance, + tag.distance > this.config.maxDistance + ); + this.handleNewDistance(event); + this.clusterService.publish(NEW_DISTANCE_CHANNEL, event); } } @@ -122,7 +118,11 @@ export class BluetoothLowEnergyService extends KalmanFilterable(Object, 0.8, 15) ); } - sensor.handleNewDistance(event.instanceName, event.distance); + sensor.handleNewDistance( + event.instanceName, + event.distance, + event.outOfRange + ); } /** diff --git a/src/integrations/bluetooth-low-energy/new-distance.event.ts b/src/integrations/bluetooth-low-energy/new-distance.event.ts index e822c3d2..59db44ad 100644 --- a/src/integrations/bluetooth-low-energy/new-distance.event.ts +++ b/src/integrations/bluetooth-low-energy/new-distance.event.ts @@ -3,16 +3,19 @@ export class NewDistanceEvent { instanceName: string, tagId: string, tagName: string, - distance: number + distance: number, + outOfRange = false ) { this.instanceName = instanceName; this.tagId = tagId; this.tagName = tagName; this.distance = distance; + this.outOfRange = outOfRange; } instanceName: string; tagId: string; tagName: string; distance: number; + outOfRange: boolean; }